update param config
This commit is contained in:
@ -10,20 +10,39 @@ extern "C" {
|
||||
|
||||
#include "util.h"
|
||||
|
||||
CountedEnum(ParamType, size_t, PF_PLIM, PF_TLIM, PF_SLIM, PF_TVEC, PF_PG, PF_REKU);
|
||||
typedef enum {
|
||||
DC_0 = 0,
|
||||
DC_1 = 1,
|
||||
DC_2 = 2,
|
||||
DC_3 = 3,
|
||||
} Discipline;
|
||||
|
||||
typedef struct {
|
||||
unsigned plim; //< Power limit
|
||||
unsigned tlim; //< Torque limit
|
||||
unsigned slim; //< Speed limit
|
||||
unsigned tvec; //< Torque vectoring
|
||||
unsigned pg; //< Power ground
|
||||
unsigned reku; //< Rekuperation
|
||||
} Params;
|
||||
uint32_t value;
|
||||
uint32_t min_value;
|
||||
uint32_t max_value;
|
||||
uint32_t step_value;
|
||||
float display_factor;
|
||||
} ParamConfig;
|
||||
|
||||
extern Params params;
|
||||
// clang-format off
|
||||
CountedEnum(ParamType, size_t,
|
||||
PF_SpeedLimit,
|
||||
PF_TorqueLimit,
|
||||
PF_PowerLimit,
|
||||
PF_Discipline,
|
||||
PF_TorqueVectoringOn,
|
||||
PF_TractionControlOn,
|
||||
PF_Rekuperation,
|
||||
PF_TractionControlP,
|
||||
PF_TractionControlI,
|
||||
PF_TractionControlMuMax,
|
||||
PF_TractionControlSlipRef,
|
||||
);
|
||||
// clang-format on
|
||||
|
||||
extern ParamConfig params[];
|
||||
|
||||
void params_init();
|
||||
void params_inc(ParamType param);
|
||||
void params_dec(ParamType param);
|
||||
void params_broadcast(ParamType param);
|
||||
|
||||
@ -146,7 +146,6 @@ int main(void)
|
||||
if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
params_init();
|
||||
shorttimer_init(htim_us);
|
||||
led_init(&hspi3, &htim1);
|
||||
|
||||
|
||||
@ -2,102 +2,43 @@
|
||||
#include "can-halal.h"
|
||||
#include "vehicle.h"
|
||||
|
||||
/**
|
||||
* Decrements the given value if it is above the minimum allowed value
|
||||
*/
|
||||
// TODO these functions take into account that the parameters are unsigned, it's definitely better to have them
|
||||
// signed but would need to be tested with the autobox
|
||||
#define DEC_IF_ABOVE(param_val, min_val, decr_amt) ((param_val) = (((int)(param_val) - (int)(decr_amt)) > (int)(min_val)) ? ((param_val) - (decr_amt)) : (min_val))
|
||||
#define INC_IF_BELOW(param_val, max_val, incr_amt) ((param_val) = (((param_val) + (incr_amt)) < (max_val)) ? ((param_val) + (incr_amt)) : (max_val))
|
||||
ParamConfig params[] = {
|
||||
[PF_SpeedLimit] = {.value = 70, .min_value = 0, .max_value = 100, .step_value = 1, .display_factor = 1.0},
|
||||
[PF_TorqueLimit] = {.value = 1400, .min_value = 0, .max_value = 1500, .step_value = 50, .display_factor = 1.0},
|
||||
[PF_PowerLimit] = {.value = 20, .min_value = 0, .max_value = 80, .step_value = 1, .display_factor = 1.0},
|
||||
[PF_Discipline] = {.value = 0, .min_value = 0, .max_value = 3, .step_value = 1, .display_factor = 1.0}, // TODO text
|
||||
[PF_TorqueVectoringOn] = {.value = 1, .min_value = 0, .max_value = 1, .step_value = 1, .display_factor = 1.0},
|
||||
[PF_TractionControlOn] = {.value = 1, .min_value = 0, .max_value = 1, .step_value = 1, .display_factor = 1.0},
|
||||
[PF_Rekuperation] = {.value = 1, .min_value = 0, .max_value = 1, .step_value = 1, .display_factor = 1.0},
|
||||
[PF_TractionControlP] = {.value = 20, .min_value = 0, .max_value = 50, .step_value = 1, .display_factor = 0.1},
|
||||
[PF_TractionControlI] = {.value = 20, .min_value = 0, .max_value = 50, .step_value = 1, .display_factor = 0.1},
|
||||
[PF_TractionControlMuMax] = {.value = 16, .min_value = 0, .max_value = 30, .step_value = 1, .display_factor = 0.1},
|
||||
[PF_TractionControlSlipRef] = {.value = 4, .min_value = 0, .max_value = 10, .step_value = 1, .display_factor = 0.1},
|
||||
};
|
||||
|
||||
Params params = {0};
|
||||
void params_inc(ParamType param) {
|
||||
uint32_t inc_value = params[param].value + params[param].step_value;
|
||||
|
||||
void params_init()
|
||||
{
|
||||
// Default values
|
||||
params.plim = 20;
|
||||
params.tlim = 1400;
|
||||
params.slim = 70;
|
||||
params.tvec = 50;
|
||||
params.pg = 0;
|
||||
params.reku = 0;
|
||||
}
|
||||
|
||||
void params_inc(ParamType param)
|
||||
{
|
||||
switch (param)
|
||||
{
|
||||
case PF_PLIM:
|
||||
INC_IF_BELOW(params.plim, 80, 1);
|
||||
break;
|
||||
case PF_TLIM:
|
||||
INC_IF_BELOW(params.tlim, 1500, 100);
|
||||
break;
|
||||
case PF_SLIM:
|
||||
INC_IF_BELOW(params.slim, 100, 1);
|
||||
break;
|
||||
case PF_TVEC:
|
||||
INC_IF_BELOW(params.tvec, 100, 1);
|
||||
break;
|
||||
case PF_PG:
|
||||
INC_IF_BELOW(params.pg, 100, 1);
|
||||
break;
|
||||
case PF_REKU:
|
||||
INC_IF_BELOW(params.reku, 100, 1);
|
||||
break;
|
||||
if (inc_value > params[param].max_value) {
|
||||
params[param].value = params[param].max_value;
|
||||
} else {
|
||||
params[param].value = inc_value;
|
||||
}
|
||||
}
|
||||
|
||||
void params_dec(ParamType param)
|
||||
{
|
||||
switch (param)
|
||||
{
|
||||
case PF_PLIM:
|
||||
DEC_IF_ABOVE(params.plim, 0, 1);
|
||||
break;
|
||||
case PF_TLIM:
|
||||
DEC_IF_ABOVE(params.tlim, 0, 100);
|
||||
break;
|
||||
case PF_SLIM:
|
||||
DEC_IF_ABOVE(params.slim, 0, 1);
|
||||
break;
|
||||
case PF_TVEC:
|
||||
DEC_IF_ABOVE(params.tvec, 0, 1);
|
||||
break;
|
||||
case PF_PG:
|
||||
DEC_IF_ABOVE(params.pg, 0, 1);
|
||||
break;
|
||||
case PF_REKU:
|
||||
DEC_IF_ABOVE(params.reku, 0, 1);
|
||||
break;
|
||||
void params_dec(ParamType param) {
|
||||
uint32_t dec_value = 0;
|
||||
if (params[param].value > params[param].step_value) { // check for potential underflow
|
||||
dec_value = params[param].value - params[param].step_value;
|
||||
}
|
||||
|
||||
if (dec_value < params[param].min_value) {
|
||||
params[param].value = params[param].min_value;
|
||||
} else {
|
||||
params[param].value = dec_value;
|
||||
}
|
||||
}
|
||||
|
||||
void params_broadcast(ParamType param)
|
||||
{
|
||||
int32_t value;
|
||||
switch (param)
|
||||
{
|
||||
case PF_PLIM:
|
||||
value = params.plim;
|
||||
break;
|
||||
case PF_TLIM:
|
||||
value = params.tlim;
|
||||
break;
|
||||
case PF_SLIM:
|
||||
value = params.slim;
|
||||
break;
|
||||
case PF_TVEC:
|
||||
value = params.tvec;
|
||||
break;
|
||||
case PF_PG:
|
||||
value = params.pg;
|
||||
break;
|
||||
case PF_REKU:
|
||||
value = params.reku;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
vehicle_broadcast_param(param, value);
|
||||
void params_broadcast(ParamType param) { //
|
||||
vehicle_broadcast_param(param, params[param].value);
|
||||
}
|
||||
|
||||
@ -163,11 +163,10 @@ void vehicle_select_mission(Mission mission) {
|
||||
}
|
||||
|
||||
void vehicle_broadcast_param(ParamType param, int32_t value) {
|
||||
uint8_t data[5];
|
||||
uint8_t *ptr = data;
|
||||
ptr = ftcan_marshal_unsigned(ptr, param, 1);
|
||||
ptr = ftcan_marshal_signed(ptr, value, 4);
|
||||
ftcan_transmit(CAN_ID_STW_PARAM_SET, data, 5);
|
||||
uint8_t data[6];
|
||||
*((uint16_t *)data) = param;
|
||||
*((uint32_t *)(data + 2)) = value;
|
||||
ftcan_transmit(CAN_ID_STW_PARAM_SET, data, 6);
|
||||
}
|
||||
|
||||
void vehicle_broadcast_buttons(GPIO_PinState *button_states) {
|
||||
|
||||
Reference in New Issue
Block a user