OpenChargeMicro/Software/src/charger.cpp

101 lines
1.9 KiB
C++
Raw Normal View History

2018-11-21 23:17:31 +01:00
/*
* Charger.cpp
*
* Created on: 19.11.2018
* Author: Hendrik Schutter
*/
#include "openChargeMicro.h"
2018-11-22 20:43:21 +01:00
charger::charger(const struct s_charger pCharger) {
2018-11-21 23:17:31 +01:00
charger_settings = pCharger;
2018-11-26 22:04:00 +01:00
status.connected = false;
status.active = false;
//active = false;
2018-11-22 23:27:20 +01:00
capacity = 0;
2018-11-21 23:17:31 +01:00
}
charger::~charger() {
}
2018-11-22 22:32:02 +01:00
/* returns current charge-current in mA */
2018-11-23 13:03:13 +01:00
int charger::getCurrent() {
2018-11-21 23:17:31 +01:00
mux.setChannel(charger_settings.chI);
2018-11-26 21:42:12 +01:00
int tmp = io.readAdc(0);
2018-11-23 21:23:40 +01:00
int ret = (int) ((tmp * 1.4286) + 34.857); //TODO calibration and testing
2018-11-21 23:17:31 +01:00
return ret;
}
2018-11-22 22:32:02 +01:00
/* returns current battery voltage in V */
2018-11-21 23:17:31 +01:00
double charger::getVoltage() {
mux.setChannel(charger_settings.chU);
2018-11-26 21:42:12 +01:00
int tmp = io.readAdc(0);
2018-11-26 22:44:52 +01:00
if (tmp == 0)
return 0;
2018-11-23 13:03:13 +01:00
double ret = ((double) tmp) * 0.00615;
2018-11-21 23:17:31 +01:00
return ret;
}
2018-11-23 21:23:40 +01:00
/* returns summed up charged capacity since charge start in µAh */
2018-11-22 23:27:20 +01:00
unsigned int charger::getCapacity() {
return capacity;
2018-11-21 23:17:31 +01:00
}
2018-11-22 23:27:20 +01:00
void charger::setStartTime(struct time_t pTime) {
2018-11-22 22:40:46 +01:00
startTime = pTime;
}
2018-11-22 23:27:20 +01:00
struct time_t charger::getStartTime() {
return startTime;
}
2018-11-21 23:17:31 +01:00
void charger::getInfo() {
2018-11-25 22:04:09 +01:00
#ifdef DEBUG
2018-11-21 23:17:31 +01:00
char buffer[50];
sprintf(buffer, "Nr: %i - Uch %i - Ich %i\r\n", charger_settings.nr,
charger_settings.chU, charger_settings.chI);
serialSend(buffer);
2018-11-25 22:04:09 +01:00
#endif
2018-11-21 23:17:31 +01:00
}
2018-11-26 22:04:00 +01:00
void charger::setStatus(struct s_charger_status pStatus) {
status = pStatus;
2018-11-22 20:43:21 +01:00
}
2018-11-26 22:04:00 +01:00
struct s_charger_status charger::getStatus() {
return status;
2018-11-22 20:43:21 +01:00
}
2018-11-21 23:17:31 +01:00
2018-11-22 23:27:20 +01:00
/* resets the capacity */
void charger::reset() {
2018-11-23 21:23:40 +01:00
capacity = 0;
2018-11-22 23:27:20 +01:00
}
/* updates the capacity */
void charger::update() {
2018-11-26 22:44:52 +01:00
static int full;
if (getCurrent() < 36) {
//not charging --> maybe battery full
full++;
2018-11-28 15:16:59 +01:00
if (full == 5) {
2018-11-26 22:44:52 +01:00
//not charging --> battery full
status.active = false;
full = 0;
#ifdef DEBUG
serialSend("battery full\r\n");
#endif
}
} else {
//not full --> charging
// 1sec / 3600
const double div = 0.000277778;
capacity = capacity + ((unsigned long int) (div * getCurrent() * 1000));
//serialSend("update\r\n");
2018-11-28 15:16:59 +01:00
full = 0;
2018-11-26 22:44:52 +01:00
}
2018-11-22 23:27:20 +01:00
}