-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtcchargercontroller.cpp
More file actions
84 lines (69 loc) · 2.52 KB
/
tcchargercontroller.cpp
File metadata and controls
84 lines (69 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "tcchargercontroller.h"
#include "canbusnodedetector.h"
#include "leafhvbattery.h"
#include "leafobcharger.h"
#include "tccharger.h"
#include "openinverter/params.h"
#include <QDebug>
#include <QTimer>
TcChargerController::TcChargerController(QObject* parent)
: QObject(parent),
m_timer(new QTimer(this))
{
connect(CanBusNodeDetector::instance(), &CanBusNodeDetector::canBusNodeCreated, this, &TcChargerController::canBusNodeCreated);
connect(m_timer, &QTimer::timeout, this, &TcChargerController::controlChargers);
}
TcChargerController::~TcChargerController()
{
shutdown();
}
void TcChargerController::canBusNodeCreated(CanBusNode* node)
{
connect(node, &QObject::destroyed, this, [this,node]{ canBusNodeDestroyed(node); });
if (auto battery = dynamic_cast<LeafHVBattery*>(node))
m_battery = battery;
else if (auto charger = dynamic_cast<LeafOBCharger*>(node))
m_stockCharger = charger;
else if (auto charger = dynamic_cast<TcCharger*>(node))
m_tcChargers.push_back(charger);
if (!m_tcChargers.isEmpty() && m_stockCharger && m_battery)
m_timer->start(100);
}
void TcChargerController::canBusNodeDestroyed(CanBusNode* node)
{
if (node == m_battery)
m_battery = nullptr;
else if (node == m_stockCharger)
m_stockCharger = nullptr;
m_tcChargers.removeAll(reinterpret_cast<TcCharger*>(node));
if (m_tcChargers.isEmpty() || !m_stockCharger || !m_battery)
shutdown();
}
void TcChargerController::shutdown()
{
m_timer->stop();
for (auto charger : m_tcChargers)
{
charger->setMaxOutputVoltage(0);
charger->setMaxOutputCurrent(0);
}
}
void TcChargerController::controlChargers()
{
if (m_tcChargers.isEmpty() || !m_stockCharger || !m_battery)
{
shutdown();
return;
}
const quint32 maxPower = m_battery->maxPowerForCharger();
const quint32 onboardChargerPower = m_stockCharger->outputPower();
const quint32 totalTcChargerPower = onboardChargerPower > maxPower ? 0 : maxPower - onboardChargerPower;
const quint32 tcChargerPower = std::min(static_cast<quint32>(onboardChargerPower * 1.05), totalTcChargerPower / m_tcChargers.count());
for (auto charger : m_tcChargers)
{
if (charger->status() != TcCharger::Normal)
qDebug() << "Charger state not normal:" << charger << charger->status();
charger->setMaxOutputVoltage(Param::GetInt(Param::Voltspnt));
charger->setMaxOutputCurrent(tcChargerPower / m_battery->voltage());
}
}