Skip to content

Commit

Permalink
Simplify module settings and add SolenoidSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
rnd-ash committed Apr 26, 2023
1 parent e607615 commit 1e4664b
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 222 deletions.
Empty file removed src/diag/config_editor.cpp
Empty file.
9 changes: 0 additions & 9 deletions src/diag/config_editor.h

This file was deleted.

39 changes: 5 additions & 34 deletions src/diag/diag_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <tcu_maths.h>
#include "kwp2000.h"
#include "esp_core_dump.h"
#include "../nvs/module_settings.h"

DATA_GEARBOX_SENSORS get_gearbox_sensors(Gearbox* g) {
DATA_GEARBOX_SENSORS ret = {};
Expand Down Expand Up @@ -274,43 +275,13 @@ const esp_app_desc_t* get_image_header(void) {
}

kwp_result_t get_module_settings(uint8_t module_id, uint16_t* buffer_len, uint8_t** buffer) {
if (module_id == CFG_IDX_TCC) {
const TCC_ADV_OPTS* ptr = gearbox->tcc->get_running_opts();
uint8_t* dest = (uint8_t*)heap_caps_malloc(sizeof(TCC_ADV_OPTS)+1, MALLOC_CAP_SPIRAM);
if (nullptr == ptr || nullptr == dest) {
delete[] dest;
return NRC_CONDITIONS_NOT_CORRECT_REQ_SEQ_ERROR;
} else {
// Malloc OK, copy and return
dest[0] = CFG_IDX_TCC;
memcpy(&dest[1], ptr, sizeof(TCC_ADV_OPTS));
*buffer_len = sizeof(TCC_ADV_OPTS)+1;
*buffer = dest;
return NRC_OK;
}
} else {
return NRC_GENERAL_REJECT;
}
return ModuleConfiguration::read_settings(module_id, buffer_len, buffer);
}

kwp_result_t set_module_settings(uint8_t module_id, uint16_t buffer_len, uint8_t* buffer) {
if (module_id == CFG_IDX_TCC) {
if (buffer_len == 1 && buffer[0] == 0x00) {
if (ESP_OK == gearbox->tcc->reset_opts()) {
return NRC_OK;
} else {
return NRC_SUB_FUNC_NOT_SUPPORTED_INVALID_FORMAT;
}
} else if (buffer_len != sizeof(TCC_ADV_OPTS)) {
return NRC_GENERAL_REJECT;
} else {
if (ESP_OK == gearbox->tcc->set_running_opts(*(TCC_ADV_OPTS*)buffer)) {
return NRC_OK;
} else {
return NRC_SUB_FUNC_NOT_SUPPORTED_INVALID_FORMAT;
}
}
if (buffer_len == 1 && buffer[0] == 0x00) {
return ModuleConfiguration::reset_settings(module_id);
} else {
return NRC_GENERAL_REJECT;
return ModuleConfiguration::write_settings(module_id, buffer_len, buffer);
}
}
1 change: 0 additions & 1 deletion src/diag/diag_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <esp_app_format.h>
#include "kwp2000_defines.h"
#include "esp_app_desc.h"
#include "config_editor.h"

// Diagnostic data IDs and data structures
// used by the KWP2000 server on the TCM
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "nvs/eeprom_config.h"
#include "diag/kwp2000.h"
#include "solenoids/constant_current.h"
#include "nvs/module_settings.h"

// CAN LAYERS
#include "canbus/can_egs51.h"
Expand Down Expand Up @@ -55,6 +56,8 @@ SPEAKER_POST_CODE setup_tcm() {
if (EEPROM::init_eeprom() != ESP_OK) {
ret = SPEAKER_POST_CODE::EEPROM_FAIL;
} else {
// Read our configuration (This is allowed to fail as the default opts are always set by default)
ModuleConfiguration::load_all_settings();
switch (VEHICLE_CONFIG.egs_can_type) {
case 1:
egs_can_hal = new Egs51Can("EGS51", 20, 500000); // EGS51 CAN Abstraction layer
Expand Down
82 changes: 82 additions & 0 deletions src/nvs/module_settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "module_settings.h"
#include "eeprom_impl.h"

TCC_MODULE_SETTINGS TCC_CURRENT_SETTINGS = TCC_DEFAULT_SETTINGS;
SOL_MODULE_SETTINGS SOL_CURRENT_SETTINGS = SOL_DEFAULT_SETTINGS;

// These macro will fail should the naming convension of the settings not be correct
// so it enforces the following rule:

// (xxx) denotes the 3 letter prefix of the module settings name
// NVS Key name : xxx_SETTINGS_NVS_KEY
// Current settings name : xxx_CURRENT_SETTINGS
// Default settings name : xxx_DEFAULT_SETTINGS
// Settings variable type: xxx_MODULE_SETTINGS
// Settings SCN KEY IDs : xxx_MODULE_SETINGS_SCN_ID
#define READ_EEPROM_SETTING(pfx) \
EEPROM::read_subsystem_settings<pfx##_MODULE_SETTINGS>(pfx##_SETTINGS_NVS_KEY, &pfx##_CURRENT_SETTINGS, &pfx##_DEFAULT_SETTINGS)

#define RESET_EEPROM_SETINGS(pfx) \
pfx##_CURRENT_SETTINGS = pfx##_DEFAULT_SETTINGS; \
return EEPROM::write_subsystem_settings(pfx##_SETTINGS_NVS_KEY, &pfx##_DEFAULT_SETTINGS); \

// Checks and writes the buffer as the setting
#define CHECK_AND_WRITE_SETTINGS(pfx, buffer_len, buffer) \
if (sizeof(pfx##_MODULE_SETTINGS) != buffer_len) { \
return ESP_ERR_INVALID_SIZE; \
} else { \
pfx##_MODULE_SETTINGS settings = *(pfx##_MODULE_SETTINGS*)buffer; \
pfx##_CURRENT_SETTINGS = settings; \
return EEPROM::write_subsystem_settings(pfx##_SETTINGS_NVS_KEY, &pfx##_CURRENT_SETTINGS); \
} \

#define READ_SETTINGS_TO_BUFFER(pfx, buffer_len_dest, buffer_dest) \
const pfx##_MODULE_SETTINGS* ptr = &pfx##_CURRENT_SETTINGS; \
uint8_t* dest = (uint8_t*)heap_caps_malloc(sizeof(pfx##_MODULE_SETTINGS)+1, MALLOC_CAP_SPIRAM); \
if (nullptr == ptr || nullptr == dest) { \
delete[] dest; \
return ESP_ERR_NO_MEM; \
} else { \
dest[0] = pfx##_MODULE_SETINGS_SCN_ID; \
memcpy(&dest[1], ptr, sizeof(pfx##_MODULE_SETTINGS)); \
*buffer_len = sizeof(pfx##_MODULE_SETTINGS)+1; \
*buffer = dest; \
return ESP_OK; \
} \

esp_err_t ModuleConfiguration::load_all_settings() {
esp_err_t res = ESP_OK;
READ_EEPROM_SETTING(TCC);
READ_EEPROM_SETTING(SOL);
return res;
}

esp_err_t ModuleConfiguration::reset_settings(uint8_t idx) {
switch (idx) {
case TCC_MODULE_SETINGS_SCN_ID:
RESET_EEPROM_SETINGS(TCC)
case SOL_MODULE_SETINGS_SCN_ID:
RESET_EEPROM_SETINGS(SOL)
default:
return ESP_ERR_INVALID_ARG;
}
}

esp_err_t ModuleConfiguration::read_settings(uint8_t module_id, uint16_t* buffer_len, uint8_t** buffer) {
if (module_id == TCC_MODULE_SETINGS_SCN_ID) {
READ_SETTINGS_TO_BUFFER(TCC, buffer_len, buffer);
} else if (module_id == SOL_MODULE_SETINGS_SCN_ID) {
READ_SETTINGS_TO_BUFFER(SOL, buffer_len, buffer);
} else {
return ESP_ERR_INVALID_ARG;
}
}

esp_err_t ModuleConfiguration::write_settings(uint8_t module_id, uint16_t buffer_len, uint8_t* buffer) {
if (module_id == TCC_MODULE_SETINGS_SCN_ID) {
CHECK_AND_WRITE_SETTINGS(TCC, buffer_len, buffer)
} else if (module_id == SOL_MODULE_SETINGS_SCN_ID) {
CHECK_AND_WRITE_SETTINGS(SOL, buffer_len, buffer)
}
return ESP_ERR_INVALID_ARG;
}
120 changes: 120 additions & 0 deletions src/nvs/module_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef __MODULE_SETTINGS_H
#define __MODULE_SETTINGS_H

#include <stdint.h>
#include <tcu_maths.h>
#include <esp_err.h>

// TCC Settings

#define TCC_SETTINGS_NVS_KEY "TCC_A2"
typedef struct {
bool adapt_enable;
bool enable_d1;
bool enable_d2;
bool enable_d3;
bool enable_d4;
bool enable_d5;
uint16_t prefill_pressure;
uint16_t lock_rpm_threshold;
uint16_t min_locking_rpm;
uint16_t adjust_interval_ms;
uint16_t tcc_stall_speed;
uint16_t min_torque_adapt;
uint16_t max_torque_adapt;
uint16_t prefill_min_engine_rpm;
uint16_t base_pressure_offset_start_ramp;
LinearInterpSetting pressure_increase_ramp_settings;
uint8_t adapt_pressure_inc;
uint16_t adapt_lock_detect_time;
uint16_t pulling_slip_rpm_low_threshold;
uint16_t pulling_slip_rpm_high_threhold;
float reaction_torque_multiplier;
uint16_t trq_consider_coasting;
LinearInterpSetting load_dampening;
LinearInterpSetting pressure_multiplier_output_rpm;
uint16_t max_allowed_bite_pressure;
uint16_t max_allowed_pressure_longterm;
} __attribute__ ((packed)) TCC_MODULE_SETTINGS;

const TCC_MODULE_SETTINGS TCC_DEFAULT_SETTINGS = {
.adapt_enable = true,
.enable_d1 = true,
.enable_d2 = true,
.enable_d3 = true,
.enable_d4 = true,
.enable_d5 = true,
.prefill_pressure = 500,
.lock_rpm_threshold = 50,
.min_locking_rpm = 1100,
.adjust_interval_ms = 500,
.tcc_stall_speed = 2500,
.min_torque_adapt = 50,
.max_torque_adapt = 110,
.prefill_min_engine_rpm = 900,
.base_pressure_offset_start_ramp = 300,
.pressure_increase_ramp_settings = {
.new_min = 1,
.new_max = 5,
.raw_min = 100,
.raw_max = 1000,
},
.adapt_pressure_inc = 10,
.adapt_lock_detect_time = 2000,
.pulling_slip_rpm_low_threshold = 20,
.pulling_slip_rpm_high_threhold = 100,
.reaction_torque_multiplier = 1.5,
.trq_consider_coasting = 40,
.load_dampening = {
.new_min = 100,
.new_max = 50,
.raw_min = -40,
.raw_max = 40,
},
.pressure_multiplier_output_rpm = {
.new_min = 1.00,
.new_max = 1.25,
.raw_min = 1500,
.raw_max = 2500,
},
.max_allowed_bite_pressure = 1800,
.max_allowed_pressure_longterm = 7000,
};

#define SOL_SETTINGS_NVS_KEY "SOL_A0"

typedef struct {
uint16_t min_batt_power_on_test;
uint16_t current_threshold_error;
uint16_t cc_vref_solenoid;
float cc_temp_coefficient_wires;
float cc_reference_resistance;
float cc_reference_temp;
float cc_max_adjust_per_step;
} __attribute__ ((packed)) SOL_MODULE_SETTINGS;

const SOL_MODULE_SETTINGS SOL_DEFAULT_SETTINGS = {
.min_batt_power_on_test = 11000,
.current_threshold_error = 500,
.cc_vref_solenoid = 12000,
.cc_temp_coefficient_wires = 0.393,
.cc_reference_resistance = 5.3,
.cc_reference_temp = 25,
.cc_max_adjust_per_step = 2
};

extern TCC_MODULE_SETTINGS TCC_CURRENT_SETTINGS;
extern SOL_MODULE_SETTINGS SOL_CURRENT_SETTINGS;

// Setting IDx
#define TCC_MODULE_SETINGS_SCN_ID 0x01
#define SOL_MODULE_SETINGS_SCN_ID 0x02

namespace ModuleConfiguration {
esp_err_t load_all_settings();
esp_err_t reset_settings(uint8_t idx);
esp_err_t write_settings(uint8_t module_id, uint16_t buffer_len, uint8_t* buffer);
esp_err_t read_settings(uint8_t module_id, uint16_t* buffer_len, uint8_t** buffer);
}

#endif
23 changes: 10 additions & 13 deletions src/solenoids/constant_current.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#include "esp_log.h"
#include "math.h"
#include "gearbox.h"

#define REFERENCE_RESISTANCE 5.3 //
#define REFERENCE_TEMP 25 // Celcius
#include "../nvs/module_settings.h"

ConstantCurrentDriver::ConstantCurrentDriver(Solenoid* target, const char *name) {
this->solenoid = target;
Expand Down Expand Up @@ -42,13 +40,12 @@ void ConstantCurrentDriver::update() {
pwm = 0;
this->last_off_time = now;
} else {

float calc_resistance = REFERENCE_RESISTANCE;
float calc_resistance = SOL_CURRENT_SETTINGS.cc_reference_resistance;
if (gearbox != nullptr) {
calc_resistance = (float)REFERENCE_RESISTANCE+(((float)REFERENCE_RESISTANCE*(gearbox->sensor_data.atf_temp-REFERENCE_TEMP)*0.393)/100.0);
calc_resistance = SOL_CURRENT_SETTINGS.cc_reference_resistance+((SOL_CURRENT_SETTINGS.cc_reference_resistance*(gearbox->sensor_data.atf_temp-SOL_CURRENT_SETTINGS.cc_reference_temp)*SOL_CURRENT_SETTINGS.cc_temp_coefficient_wires)/100.0);
}
// Assume 12.0V and target resistance, errors will be compensated by solenoid API and this error
float req_pwm = (4096.0*((float)this->current_target/((float)12000.0/calc_resistance)))*(1.0+this->pwm_adjustment_percent);
float req_pwm = (4096.0*((float)this->current_target/((float)SOL_CURRENT_SETTINGS.cc_vref_solenoid/calc_resistance)))*(1.0+this->pwm_adjustment_percent);
pwm = req_pwm;
// Calc PWM before we look at previous error

Expand All @@ -59,18 +56,18 @@ void ConstantCurrentDriver::update() {
if (0u == actual_current){ //(now-this->last_change_time) < 50) {
error = 0;
} else {
error = (float)(this->current_target-actual_current)/(12000.0F/calc_resistance);
error *= this->current_target/((float)12000.0/calc_resistance);
error = (float)(this->current_target-actual_current)/((float)SOL_CURRENT_SETTINGS.cc_vref_solenoid/calc_resistance);
error *= this->current_target/((float)SOL_CURRENT_SETTINGS.cc_vref_solenoid/calc_resistance);
}
//ESP_LOGI("CC", "SOL %s, E %.2f, ADJ %.2f", this->name, error, this->pwm_adjustment_percent);
if (abs(this->current_target-actual_current) > 10 && error != 0) {
this->pwm_adjustment_percent += (error);
this->last_change_time = now;
}
if(this->pwm_adjustment_percent > 2) {
this->pwm_adjustment_percent = 2;
} else if (this->pwm_adjustment_percent < -2) {
this->pwm_adjustment_percent = -2;
if(this->pwm_adjustment_percent > SOL_CURRENT_SETTINGS.cc_max_adjust_per_step) {
this->pwm_adjustment_percent = SOL_CURRENT_SETTINGS.cc_max_adjust_per_step;
} else if (this->pwm_adjustment_percent < -SOL_CURRENT_SETTINGS.cc_max_adjust_per_step) {
this->pwm_adjustment_percent = -SOL_CURRENT_SETTINGS.cc_max_adjust_per_step;
}
//ESP_LOGI("CC","%s: ADJ %.2f, REQ: %d, READ %d, PWM %d", this->name, this->pwm_adjustment_percent, this->current_target, actual_current, pwm);
}
Expand Down
1 change: 0 additions & 1 deletion src/solenoids/constant_current.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ConstantCurrentDriver {
uint64_t last_change_time = 0;
};


extern ConstantCurrentDriver* mpc_cc;
extern ConstantCurrentDriver* spc_cc;

Expand Down
Loading

0 comments on commit 1e4664b

Please sign in to comment.