Added battery module, cal factor refresh bug
This commit is contained in:
@@ -3,12 +3,70 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "battery.h"
|
||||
#include "base/router.h"
|
||||
#include "data/config_store.h"
|
||||
#include "data/general_store.h"
|
||||
|
||||
int battery::calibrate(const Task &task) {
|
||||
double actual_voltage;
|
||||
memcpy(&actual_voltage, &task.data, sizeof(double));
|
||||
int adc_read = analogRead(VBAT_PIN);
|
||||
double cal_factor = actual_voltage / adc_read;
|
||||
uint32_t output_val;
|
||||
memcpy(&output_val, &cal_factor, sizeof(uint32_t));
|
||||
router::send(MOD_CFG, TASK_CONFIG_VBAT_CAL_SET, output_val);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int battery::push(const Task &task) { return _queue.push(task); }
|
||||
|
||||
battery::battery() : _logger(nullptr) {}
|
||||
|
||||
battery::battery(system_logger *logger)
|
||||
: _logger(logger) {}
|
||||
battery::battery(system_logger *logger) : _logger(logger) {}
|
||||
|
||||
battery::~battery() {}
|
||||
battery::~battery() {}
|
||||
|
||||
int battery::init() {
|
||||
pinMode(VBAT_PIN, INPUT);
|
||||
vehicle_config config;
|
||||
config_global_read(config);
|
||||
_cal = config.vbat_calibration;
|
||||
}
|
||||
|
||||
int battery::loop(unsigned long timeout_ms) {
|
||||
if (millis() > _last_read + _update_interval) {
|
||||
int adc_read = analogRead(VBAT_PIN);
|
||||
_vbat = _cal * adc_read;
|
||||
|
||||
vbat_global_write(_vbat);
|
||||
}
|
||||
Task active_task;
|
||||
int res = _queue.pop(active_task);
|
||||
if (res == 0) {
|
||||
if (active_task.target == MOD_BAT) {
|
||||
|
||||
switch (active_task.type) {
|
||||
case TASK_BATTERY_CAL:
|
||||
this->calibrate(active_task);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (active_task.target == MOD_ALL) {
|
||||
switch (active_task.type)
|
||||
{
|
||||
case TASK_ALL_CONFIG_UPDATED: {
|
||||
vehicle_config config;
|
||||
config_global_read(config);
|
||||
_cal = config.vbat_calibration;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -2,8 +2,10 @@
|
||||
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
|
||||
#define VBAT_PIN A3
|
||||
#include "base/module_base.h"
|
||||
#include "base/ring_buffer.h"
|
||||
#include "custom_types.h"
|
||||
#include "modules/logger/system_logger.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
@@ -12,10 +14,17 @@ class battery : public module_base {
|
||||
private:
|
||||
system_logger *_logger;
|
||||
ring_buffer<Task, 16> _queue;
|
||||
double _vbat = 0;
|
||||
double _cal = 0;
|
||||
unsigned long _update_interval = 1000;
|
||||
unsigned long _last_read = 0;
|
||||
int calibrate(const Task& task);
|
||||
|
||||
public:
|
||||
int push(const Task &task) override;
|
||||
battery();
|
||||
battery(system_logger *logger);
|
||||
~battery();
|
||||
int init();
|
||||
int loop(unsigned long timeout_ms = 500);
|
||||
};
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <string.h>
|
||||
#include "data/track_store.h"
|
||||
#include "data/config_store.h"
|
||||
#include "data/general_store.h"
|
||||
#include "base/router.h"
|
||||
|
||||
char *cmd::trim_arg(char *input) {
|
||||
@@ -107,6 +108,14 @@ cmd::command_id cmd::parse_command_name(const char *input) {
|
||||
return CMD_DISPLAY_DRIVER_PRIMARY;
|
||||
}
|
||||
|
||||
if (strcmp(input, "BATTERY_CAL") == 0) {
|
||||
return CMD_BATTERY_CAL;
|
||||
}
|
||||
|
||||
if (strcmp(input, "BATTERY_PRINT_VBAT") == 0) {
|
||||
return CMD_BATTERY_PRINT_VBAT;
|
||||
}
|
||||
|
||||
return CMD_UNKNOWN;
|
||||
}
|
||||
|
||||
@@ -362,6 +371,46 @@ int cmd::handle_display_driver_primary(unsigned short argc) {
|
||||
return router::send(MOD_LCD, TASK_DISPLAY_DRIVER_PRIMARY);
|
||||
}
|
||||
|
||||
int cmd::handle_battery_cal(unsigned short argc, char *argv[]) {
|
||||
if (argc != 2) {
|
||||
#ifdef ERROR
|
||||
if (_logger != nullptr) {
|
||||
_logger->error("BATTERY_CAL expects 1 argument");
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
double vbat = strtod(argv[1], nullptr);
|
||||
uint32_t task_data;
|
||||
memcpy(&task_data, &vbat, sizeof(uint32_t));
|
||||
#ifdef INFO
|
||||
if (_logger != nullptr) {
|
||||
_logger->info("Calibrating VBAT");
|
||||
}
|
||||
#endif
|
||||
router::send(MOD_BAT, TASK_BATTERY_CAL, task_data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd::handle_battery_print_vbat(unsigned short argc) {
|
||||
if (argc != 1) {
|
||||
#ifdef ERROR
|
||||
if (_logger != nullptr) {
|
||||
_logger->error("BATTERY_PRINT_VBAT expects no arguments");
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef INFO
|
||||
double vbat;
|
||||
vbat_global_read(vbat);
|
||||
if (_logger != nullptr) {
|
||||
_logger->info("VBAT: " + String(vbat));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int cmd::handle_unknown_command(unsigned short argc, char *argv[]) {
|
||||
#ifdef ERROR
|
||||
if (_logger != nullptr) {
|
||||
@@ -403,6 +452,12 @@ int cmd::dispatch_command(command_id command, unsigned short argc, char *argv[])
|
||||
|
||||
case CMD_DISPLAY_DRIVER_PRIMARY:
|
||||
return this->handle_display_driver_primary(argc);
|
||||
|
||||
case CMD_BATTERY_CAL:
|
||||
return this->handle_battery_cal(argc, argv);
|
||||
|
||||
case CMD_BATTERY_PRINT_VBAT:
|
||||
return this->handle_battery_print_vbat(argc);
|
||||
|
||||
case CMD_UNKNOWN:
|
||||
default:
|
||||
|
||||
@@ -25,6 +25,8 @@ private:
|
||||
CMD_TRACK_AUTODETECT,
|
||||
CMD_DISPLAY_GPS_DEBUG,
|
||||
CMD_DISPLAY_DRIVER_PRIMARY,
|
||||
CMD_BATTERY_CAL,
|
||||
CMD_BATTERY_PRINT_VBAT,
|
||||
};
|
||||
|
||||
HardwareSerial *_data_stream;
|
||||
@@ -53,6 +55,8 @@ private:
|
||||
int handle_track_autodetect_command(unsigned short argc);
|
||||
int handle_display_gps_debug(unsigned short argc);
|
||||
int handle_display_driver_primary(unsigned short argc);
|
||||
int handle_battery_cal(unsigned short argc, char *argv[]);
|
||||
int handle_battery_print_vbat(unsigned short argc);
|
||||
int handle_unknown_command(unsigned short argc, char *argv[]);
|
||||
|
||||
public:
|
||||
|
||||
@@ -8,9 +8,7 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
int config::push(const Task &task) {
|
||||
return _queue.push(task);
|
||||
}
|
||||
int config::push(const Task &task) { return _queue.push(task); }
|
||||
|
||||
int config::task_complete() {
|
||||
_task_memory_stale = true;
|
||||
@@ -18,32 +16,32 @@ int config::task_complete() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config::write_track(const track_data& in) {
|
||||
int config::write_track(const track_data &in) {
|
||||
track_data copy = in;
|
||||
copy.magic = CONFIG_MAGIC;
|
||||
if (copy.id < 1 || copy.id > 8) {
|
||||
#ifdef ERROR
|
||||
if (_logger != nullptr) {
|
||||
#ifdef ERROR
|
||||
if (_logger != nullptr) {
|
||||
_logger->error("Cannot write track data with out of range id, aborting");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
EEPROM.put(eeprom_layout::track_slot_addr(copy.id), copy);
|
||||
_config.track_slot_occupied[copy.id - 1] = true;
|
||||
this->write_cfg();
|
||||
#ifdef INFO
|
||||
if (_logger !=nullptr) {
|
||||
#ifdef INFO
|
||||
if (_logger != nullptr) {
|
||||
_logger->info("Succesfully wrote new track into slot " + String(copy.id));
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config::write_track_from_temp() {
|
||||
track_data new_track;
|
||||
track_temp_global_read(new_track);
|
||||
return this->write_track(new_track);
|
||||
track_data new_track;
|
||||
track_temp_global_read(new_track);
|
||||
return this->write_track(new_track);
|
||||
}
|
||||
|
||||
int config::delete_track(unsigned short idx) {
|
||||
@@ -101,6 +99,11 @@ int config::reset_cfg() {
|
||||
return this->write_cfg();
|
||||
}
|
||||
|
||||
int config::write_vbat_cal(double val) {
|
||||
_config.vbat_calibration = val;
|
||||
return this->write_cfg();
|
||||
}
|
||||
|
||||
config::config() : _logger(nullptr), _valid_config(true) {}
|
||||
|
||||
config::config(system_logger *logger) : _logger(logger), _valid_config(true) {}
|
||||
@@ -116,11 +119,12 @@ int config::read_cfg() {
|
||||
int config::write_cfg() {
|
||||
EEPROM.put(eeprom_layout::config_addr, _config);
|
||||
config_global_write(_config);
|
||||
#ifdef INFO
|
||||
#ifdef INFO
|
||||
if (_logger != nullptr) {
|
||||
_logger->info("Config updated and saved to EEPROM");
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
router::send(MOD_ALL, TASK_ALL_CONFIG_UPDATED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -215,7 +219,6 @@ int config::handle_active_task(unsigned long timeout_ms) {
|
||||
}
|
||||
this->task_complete();
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
case TASK_CONFIG_WRITE_TEMP_TRACK: {
|
||||
@@ -236,6 +239,13 @@ int config::handle_active_task(unsigned long timeout_ms) {
|
||||
return res;
|
||||
}
|
||||
|
||||
case TASK_CONFIG_VBAT_CAL_SET: {
|
||||
double cal_value;
|
||||
memcpy(&cal_value, &_active_task.data, sizeof(double));
|
||||
int res = this->write_vbat_cal(cal_value);
|
||||
this->task_complete();
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ private:
|
||||
int write_track_from_temp();
|
||||
int delete_track(unsigned short idx);
|
||||
int reset_cfg();
|
||||
int write_vbat_cal(double val);
|
||||
|
||||
public:
|
||||
int push(const Task &task) override;
|
||||
|
||||
Reference in New Issue
Block a user