Skip to content

Commit

Permalink
fixed typo & calc. battery level (normalized)
Browse files Browse the repository at this point in the history
  • Loading branch information
kianwasabi committed Nov 17, 2023
1 parent 094dd22 commit a6ab50e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
8 changes: 4 additions & 4 deletions car_info/src/CarInfoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ int main(){
[&](const v0::commonapi::CommonTypes::batteryStruct& val) {
std::cout << " ---------------------------" << std::endl
<< "Received change message for battery" << std::endl
<< "\tlevel : " << val.getLevel() << std::endl
<< "\tvoltage : " << val.getVoltage() << std::endl
<< "\tcurrent : " << val.getCurrent() << std::endl
<< "\tconsuption: " << val.getConsumption() << std::endl
<< "\tlevel : " << val.getLevel() << std::endl
<< "\tvoltage : " << val.getVoltage() << std::endl
<< "\tcurrent : " << val.getCurrent() << std::endl
<< "\tconsumption: " << val.getConsumption() << std::endl
<< " ---------------------------" << std::endl;
},
[&](const CommonAPI::CallStatus &status) {
Expand Down
22 changes: 14 additions & 8 deletions car_info/src/PiRacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ bool PiRacer::deleteInstance()
}

void PiRacer::readBatteryInfo(){

py::object get_battery_voltage = pInstance.attr("get_battery_voltage");
_voltage = py::extract<float>(get_battery_voltage());
py::object get_battery_consumption = pInstance.attr("get_power_consumption");
_consumption = py::extract<float>(get_battery_consumption());
py::object get_battery_current = pInstance.attr("get_battery_current");
_current = py::extract<float>(get_battery_current());
// read absolut voltage from piracer
py::object get_battery_voltage = pInstance.attr("get_battery_voltage");
_voltage = std::abs(py::extract<float>(get_battery_voltage()));
// read absolut current from piracer
py::object get_battery_consumption = pInstance.attr("get_power_consumption");
_consumption = std::abs(py::extract<float>(get_battery_consumption()));
// read absolut current from piracer
py::object get_battery_current = pInstance.attr("get_battery_current");
_current = std::abs(py::extract<float>(get_battery_current()));
// calculate battery level
// This is a 3rd degree polynomial fit of the data from the battery discharge curve (see car_info.md for more details)
// limit to 0.01 - 100.00 & convert to 0.00 - 1.00
double x = _voltage / _numcells;
double y = -691.919 * pow(x,3) + 7991.667 * pow(x,2) - 30541.295 * x + 38661.5;
_level = std::min(std::max(std::round(y*1000) / 1000, 0.0), 1.0); // nomalized value between 0 and 1
y = std::min(std::max(y, 0.01), 100.00);
_level = y / 100.00;
}

const float PiRacer::getBatteryLevel()
Expand Down

0 comments on commit a6ab50e

Please sign in to comment.