StoredMap - Use esp_err_t for errors

This commit is contained in:
Ashcon Mohseninia 2022-12-16 11:26:16 +00:00
parent 955be7768b
commit d11c466938
6 changed files with 40 additions and 49 deletions

1
.gitignore vendored
View File

@ -45,3 +45,4 @@ cmake-build-debug/
html/**
latex/**
__pycache__
__pycache__/*

View File

@ -35,7 +35,7 @@ PressureManager::PressureManager(SensorData* sensor_ptr, uint16_t max_torque) {
key_name = MAP_NAME_PCS_BROWN;
default_data = BROWN_PCS_CURRENT_MAP;
this->pressure_pwm_map = new StoredTcuMap(key_name, PCS_CURRENT_MAP_SIZE, pwm_x_headers, pwm_y_headers, 8, 4, default_data);
if (!this->pressure_pwm_map->init_ok()) {
if (this->pressure_pwm_map->init_status() != ESP_OK) {
delete[] this->pressure_pwm_map;
}
@ -45,7 +45,7 @@ PressureManager::PressureManager(SensorData* sensor_ptr, uint16_t max_torque) {
key_name = MAP_NAME_TCC_PWM;
default_data = TCC_PWM_MAP;
tcc_pwm_map = new StoredTcuMap(key_name, TCC_PWM_MAP_SIZE, pwm_tcc_x_headers, pwm_tcc_y_headers, 7, 5, default_data);
if (!this->tcc_pwm_map->init_ok()) {
if (this->tcc_pwm_map->init_status() != ESP_OK) {
delete[] this->tcc_pwm_map;
}
@ -60,7 +60,7 @@ PressureManager::PressureManager(SensorData* sensor_ptr, uint16_t max_torque) {
default_data = SMALL_NAG_FILL_TIME_MAP;
}
hold2_time_map = new StoredTcuMap(key_name, FILL_TIME_MAP_SIZE, hold2_x_headers, hold2_y_headers, 4, 5, default_data);
if (!this->hold2_time_map->init_ok()) {
if (this->hold2_time_map->init_status() != ESP_OK) {
delete[] this->hold2_time_map;
}
@ -75,7 +75,7 @@ PressureManager::PressureManager(SensorData* sensor_ptr, uint16_t max_torque) {
default_data = SMALL_NAG_FILL_PRESSURE_MAP;
}
hold2_pressure_map = new StoredTcuMap(key_name, FILL_PRESSURE_MAP_SIZE, hold2p_x_headers, hold2p_y_headers, 1, 5, default_data);
if (!this->hold2_pressure_map->init_ok()) {
if (this->hold2_pressure_map->init_status() != ESP_OK) {
delete[] this->hold2_pressure_map;
}
@ -90,7 +90,7 @@ PressureManager::PressureManager(SensorData* sensor_ptr, uint16_t max_torque) {
default_data = SMALL_NAG_WORKING_MAP;
}
this->mpc_working_pressure = new StoredTcuMap(key_name, WORKING_PRESSURE_MAP_SIZE, wp_x_headers, wp_y_headers, 11, 7, default_data);
if (!this->mpc_working_pressure->init_ok()) {
if (this->mpc_working_pressure->init_status() != ESP_OK) {
delete[] this->mpc_working_pressure;
}
}

View File

@ -37,7 +37,7 @@ AbstractProfile::AbstractProfile(bool is_diesel,
default_map = def_upshift_data_petrol;
}
this->upshift_table = new StoredTcuMap(key_name, SHIFT_MAP_SIZE, shift_table_x_header, upshift_y_headers, SHIFT_MAP_X_SIZE, SHIFT_MAP_Y_SIZE, default_map);
if (!this->upshift_table->init_ok()) {
if (this->upshift_table->init_status() != ESP_OK) {
delete[] this->upshift_table;
}
@ -50,7 +50,7 @@ AbstractProfile::AbstractProfile(bool is_diesel,
default_map = def_downshift_data_petrol;
}
this->downshift_table = new StoredTcuMap(key_name, SHIFT_MAP_SIZE, shift_table_x_header, upshift_y_headers, SHIFT_MAP_X_SIZE, SHIFT_MAP_Y_SIZE, default_map);
if (!this->downshift_table->init_ok()) {
if (this->downshift_table->init_status() != ESP_OK) {
delete[] this->downshift_table;
}
@ -59,11 +59,11 @@ AbstractProfile::AbstractProfile(bool is_diesel,
int16_t step_size = (redline-1000) / 4;
int16_t shift_rpm_points[5] = {(int16_t)1000, (int16_t)(1000+(step_size)), (int16_t)(1000+(step_size*2)), (int16_t)(1000+(step_size*3)), redline};
this->upshift_time_map = new StoredTcuMap(upshift_time_map_name, SHIFT_TIME_MAP_SIZE, shift_time_table_x_header, const_cast<int16_t*>(shift_rpm_points), 6, 5, def_upshift_time_data);
if (!this->upshift_time_map->init_ok()) {
if (this->upshift_time_map->init_status() != ESP_OK) {
delete[] this->upshift_time_map;
}
this->downshift_time_map = new StoredTcuMap(downshift_time_map_name, SHIFT_TIME_MAP_SIZE, shift_time_table_x_header, const_cast<int16_t*>(shift_rpm_points), 6, 5, def_downshift_time_data);
if (!this->downshift_time_map->init_ok()) {
if (this->downshift_time_map->init_status() != ESP_OK) {
delete[] this->downshift_time_map;
}
}
@ -90,15 +90,6 @@ ShiftCharacteristics AbstractProfile::get_shift_characteristics(ProfileGearChang
return result;
}
// void AbstractProfile::reload_data() {
// if (this->upshift_table) {
// this->upshift_table->reload_from_eeprom();
// }
// if (this->downshift_table) {
// this->downshift_table->reload_from_eeprom();
// }
// }
AgilityProfile::AgilityProfile(bool is_diesel) : AbstractProfile(
is_diesel,

View File

@ -90,7 +90,6 @@ public:
}
}
virtual uint8_t get_profile_id(void) = 0;
// void reload_data();
protected:
uint8_t profile_id = 0;
StoredTcuMap* upshift_table = nullptr;

View File

@ -1,6 +1,7 @@
#include "stored_map.h"
#include "nvs/eeprom_config.h"
#include "esp_heap_caps.h"
#include "esp_check.h"
StoredTcuMap::StoredTcuMap(const char *eeprom_key_name,
const uint16_t map_element_count,
@ -13,53 +14,51 @@ StoredTcuMap::StoredTcuMap(const char *eeprom_key_name,
x_headers,
y_headers)
{
this->initialized = false;
this->default_map = default_map;
if ((x_size * y_size) == map_element_count)
{
int16_t *dest = static_cast<int16_t *>(heap_caps_malloc(map_element_count * sizeof(int16_t), MALLOC_CAP_SPIRAM));
if (nullptr != dest)
{
if (EEPROM::read_nvs_map_data(eeprom_key_name, dest, default_map, map_element_count))
{
this->init_state = EEPROM::read_nvs_map_data(eeprom_key_name, dest, default_map, map_element_count);
if (this->init_state == ESP_OK) {
if (this->add_data(dest, map_element_count))
{
// Everything OK!
this->map_element_count = map_element_count;
this->map_name = eeprom_key_name;
this->initialized = true;
this->init_state = ESP_OK;
}
else
{
ESP_LOGE("STO_MAP", "Cannot Load Stored map %s! Map add data failed!", eeprom_key_name);
this->init_state = ESP_ERR_INVALID_ARG; // Only if dest is nullptr
}
}
else
{
ESP_LOGE("STO_MAP", "Cannot Load Stored map %s! EEPROM Read NVS map failed!", eeprom_key_name);
}
}
else
{
ESP_LOGE("STO_MAP", "Cannot Load Stored map %s! Internal map allocation failed!", eeprom_key_name);
this->init_state = ESP_ERR_NO_MEM;
}
heap_caps_free(dest);
}
else
{
ESP_LOGE("STO_MAP", "Cannot Load Stored map %s! Map size is, but X and Y headers (%d,%d) make %d elements!", eeprom_key_name, x_size, y_size, x_size * y_size);
this->init_state = ESP_ERR_INVALID_SIZE;
}
}
bool StoredTcuMap::init_ok(void) const
esp_err_t StoredTcuMap::init_status(void) const
{
return this->initialized;
return this->init_state;
}
/**
* @brief Save new map contents to EEPROM (This will mean next TCU load will use the new map)
*/
bool StoredTcuMap::save_to_eeprom(void)
esp_err_t StoredTcuMap::save_to_eeprom(void)
{
return EEPROM::write_nvs_map_data(this->map_name, this->get_current_data(), this->map_element_count);
}
@ -69,16 +68,16 @@ bool StoredTcuMap::save_to_eeprom(void)
* Note. This is a temporary replace. If you power the car down, changes made will be lost unless they
* are written to EEPROM. This also acts as a failsafe in the event of a bad map edit, just reboot the car!
*/
bool StoredTcuMap::replace_map_content(int16_t *new_data, uint16_t content_len)
esp_err_t StoredTcuMap::replace_map_content(int16_t *new_data, uint16_t content_len)
{
bool result = false;
esp_err_t result = ESP_OK;
if (content_len == (this->map_element_count))
{
result = this->add_data(new_data, this->map_element_count);
}
else
{
ESP_LOGE("STO_MAP", "replace_map_content failed! New data has invalid size of %d. Map should have %d entries", content_len, this->map_element_count);
result = ESP_ERR_NVS_INVALID_LENGTH;
}
return result;
}
@ -86,7 +85,7 @@ bool StoredTcuMap::replace_map_content(int16_t *new_data, uint16_t content_len)
/**
* @brief Reloads the previously saved map from EEPROM into the map (Undo function)
*/
bool StoredTcuMap::reload_from_eeprom(void)
esp_err_t StoredTcuMap::reload_from_eeprom(void)
{
return this->read_from_eeprom(this->map_name, this->map_element_count);
}
@ -100,34 +99,34 @@ bool StoredTcuMap::reload_from_eeprom(void)
// return this->add_data(const_cast<int16_t *>(this->default_map), this->map_element_count);
// }
bool StoredTcuMap::read_from_eeprom(const char *key_name, uint16_t expected_size)
esp_err_t StoredTcuMap::read_from_eeprom(const char *key_name, uint16_t expected_size)
{
bool ret = false;
esp_err_t ret;
bool mem_is_allocated = this->allocate_ok();
if (mem_is_allocated)
{
int16_t *dest = static_cast<int16_t *>(heap_caps_malloc(expected_size * sizeof(int16_t), MALLOC_CAP_SPIRAM));
if (dest != nullptr)
{
bool read_map_data_successful = EEPROM::read_nvs_map_data(key_name, dest, this->default_map, expected_size);
if (read_map_data_successful)
ret = EEPROM::read_nvs_map_data(key_name, dest, this->default_map, expected_size);
if (ret != ESP_OK)
{
ret = this->add_data(dest, expected_size);
}
else
{
ESP_LOGE("STO_MAP", "Read from eeprom failed (read_nvs_map_data failed)");
if(!this->add_data(dest, expected_size)) {
ret = ESP_ERR_INVALID_ARG;
}
}
}
else
{
ESP_LOGE("STO_MAP", "Read from eeprom failed (Cannot allocate dest array)");
ret = ESP_ERR_NO_MEM;
}
heap_caps_free(dest);
}
else
{
ESP_LOGE("STO_MAP", "Stored map has not been loaded! Internal map allocation failed!");
ret = ESP_ERR_NO_MEM;
}
return ret;
}

View File

@ -2,6 +2,7 @@
#define STORED_MAP_H
#include "../lib/core/tcumap.h"
#include "esp_err.h"
class StoredTcuMap : public TcuMap {
@ -16,23 +17,23 @@ class StoredTcuMap : public TcuMap {
const int16_t* default_map
);
bool init_ok(void) const;
esp_err_t init_status(void) const;
/**
* @brief Save new map contents to EEPROM (This will mean next TCU load will use the new map)
*/
bool save_to_eeprom(void);
esp_err_t save_to_eeprom(void);
/**
* @brief Replace map contents with new data (Keeping it in memory, call `save_to_eeprom` to write it to the TCU's EEPROM)
* Note. This is a temporary replace. If you power the car down, changes made will be lost unless they
* are written to EEPROM. This also acts as a failsafe in the event of a bad map edit, just reboot the car!
*/
bool replace_map_content(int16_t* new_data, uint16_t content_len);
esp_err_t replace_map_content(int16_t* new_data, uint16_t content_len);
/**
* @brief Reloads the previously saved map from EEPROM into the map (Undo function)
*/
bool reload_from_eeprom(void);
esp_err_t reload_from_eeprom(void);
/**
* @brief Resets the map data to the stock map from the TCU firmware (maps.cpp)
@ -51,8 +52,8 @@ class StoredTcuMap : public TcuMap {
const char* map_name;
uint16_t map_element_count;
const int16_t* default_map;
bool initialized;
bool read_from_eeprom(const char* key_name, uint16_t expected_size);
esp_err_t init_state;
esp_err_t read_from_eeprom(const char* key_name, uint16_t expected_size);
};
#endif // STORED_MAP_H