Skip to content

Commit

Permalink
new flags / eeprom reset option
Browse files Browse the repository at this point in the history
  • Loading branch information
Xnyle committed Sep 18, 2018
1 parent 14c9b39 commit 1fdb365
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 186 deletions.
8 changes: 4 additions & 4 deletions ACAcommons.c
Expand Up @@ -204,11 +204,11 @@ void updatePasStatus(void) {
ui16_time_ticks_between_pas_interrupt = ui16_time_ticks_for_pas_calculation; //save recent cadence
ui16_PAS_High = ui16_PAS_High_Counter;

if ((0 == (ui8_aca_flags & PAS_INVERTED)) && ((float) ui16_time_ticks_between_pas_interrupt / (float) ui16_PAS_High > flt_s_pas_threshold)) {
if ((0 == (ui16_aca_flags & PAS_INVERTED)) && ((float) ui16_time_ticks_between_pas_interrupt / (float) ui16_PAS_High > flt_s_pas_threshold)) {
if (PAS_act < 7) {
PAS_act++;
}
} else if ((PAS_INVERTED == (ui8_aca_flags & PAS_INVERTED)) && ((float) ui16_time_ticks_between_pas_interrupt / (float) ui16_PAS_High < flt_s_pas_threshold)) {
} else if ((PAS_INVERTED == (ui16_aca_flags & PAS_INVERTED)) && ((float) ui16_time_ticks_between_pas_interrupt / (float) ui16_PAS_High < flt_s_pas_threshold)) {
if (PAS_act < 7) {
PAS_act++;
}
Expand Down Expand Up @@ -239,7 +239,7 @@ void updatePasStatus(void) {

void updateOffroadStatus(void) {

if (((ui8_aca_flags & BRAKE_DISABLES_OFFROAD) == BRAKE_DISABLES_OFFROAD) && (ui8_offroad_state > 4)) {
if (((ui16_aca_flags & BRAKE_DISABLES_OFFROAD) == BRAKE_DISABLES_OFFROAD) && (ui8_offroad_state > 4)) {
// if disabling is enabled :)
if (!GPIO_ReadInputPin(BRAKE__PORT, BRAKE__PIN)) {
ui8_offroad_counter++;
Expand All @@ -253,7 +253,7 @@ void updateOffroadStatus(void) {
}

// check if offroad mode is enabled
if (0 == (ui8_aca_flags & OFFROAD_ENABLED)) {
if (0 == (ui16_aca_flags & OFFROAD_ENABLED)) {
return;
}

Expand Down
11 changes: 6 additions & 5 deletions ACAcontrollerState.c
Expand Up @@ -85,7 +85,7 @@ uint16_t ui16_time_ticks_for_speed_calculation = 0; //time tics for speed measur
uint8_t ui8_SPEED_Flag = 0; //flag for SPEED interrupt
uint8_t ui8_offroad_counter = 0; //counter for offroad switching procedure

uint8_t ui8_aca_flags = 0; //if throttle input should be bases on assist level and other flags
uint16_t ui16_aca_flags = 0; //if throttle input should be bases on assist level and other flags

uint8_t ui8_adc_read_throttle_busy = 0;
uint16_t ui16_torque[NUMBER_OF_PAS_MAGS]; //array for torque values of one crank revolution
Expand All @@ -105,7 +105,7 @@ void controllerstate_init(void) {
uint8_t eepromHighVal;

// convert static defines to volatile vars
ui8_aca_flags = ACA;
ui16_aca_flags = ACA;
ui8_speedlimit_kph = limit;
ui8_speedlimit_actual_kph = limit;
ui8_throttle_min_range = ADC_THROTTLE_MIN_VALUE;
Expand All @@ -125,17 +125,18 @@ void controllerstate_init(void) {
eepromVal = eeprom_read(OFFSET_BATTERY_CURRENT_MAX_VALUE);
if (eepromVal > 0 || eepromHighVal > 0) ui16_battery_current_max_value = ((uint16_t) eepromHighVal << 8) + (uint16_t) eepromVal;

eepromHighVal = eeprom_read(OFFSET_ACA_FLAGS_HIGH_BYTE);
eepromVal = eeprom_read(OFFSET_ACA_FLAGS);
if (eepromVal > 0 || eepromHighVal > 0) ui16_aca_flags = ((uint16_t) eepromHighVal << 8) + (uint16_t) eepromVal;

eepromVal = eeprom_read(OFFSET_REGEN_CURRENT_MAX_VALUE);
if (eepromVal > 0) ui16_regen_current_max_value = eepromVal;

eepromVal = eeprom_read(OFFSET_MAX_SPEED_DEFAULT);
if (eepromVal > 0) ui8_speedlimit_kph = eepromVal;
eepromVal = eeprom_read(OFFSET_MAX_SPEED_WITHOUT_PAS);
if (eepromVal > 0) ui8_speedlimit_without_pas_kph = eepromVal;
eepromVal = eeprom_read(OFFSET_MAX_SPEED_WITH_THROTTLE_OVERRIDE);
if (eepromVal > 0) ui8_speedlimit_with_throttle_override_kph = eepromVal;
eepromVal = eeprom_read(OFFSET_ACA_FLAGS);
if (eepromVal > 0) ui8_aca_flags = eepromVal;
eepromVal = eeprom_read(OFFSET_CURRENT_CAL_A);
if (eepromVal > 0) ui8_current_cal_a = eepromVal;
eepromVal = eeprom_read(OFFSET_ASSIST_LEVEL);
Expand Down
24 changes: 13 additions & 11 deletions ACAcontrollerState.h
Expand Up @@ -74,7 +74,7 @@ extern uint8_t ui8_SPEED_Flag; //Flag for PAS Interrupt detected
extern uint16_t ui16_time_ticks_between_speed_interrupt; //Speed duration of one wheel revolution (tics * 64us)
extern uint8_t ui8_offroad_counter;

extern uint8_t ui8_aca_flags;
extern uint16_t ui16_aca_flags;

extern uint8_t ui8_adc_read_throttle_busy;

Expand Down Expand Up @@ -110,16 +110,18 @@ typedef enum {

typedef enum {
// values from 0-31 are allowed as signals are stored in a single uint32_t
ASSIST_LVL_AFFECTS_THROTTLE = ((uint8_t) 1),
OFFROAD_ENABLED = ((uint8_t) 2),
BRAKE_DISABLES_OFFROAD = ((uint8_t) 4),

DIGITAL_REGEN = ((uint8_t) 8),
SPEED_INFLUENCES_REGEN = ((uint8_t) 16),
SPEED_INFLUENCES_TORQUESENSOR = ((uint8_t) 32),
PAS_INVERTED = ((uint8_t) 64),

DUMMY_ALWAYS_ON = ((uint8_t) 128)
ASSIST_LVL_AFFECTS_THROTTLE = ((uint16_t) 1),
OFFROAD_ENABLED = ((uint16_t) 2),
BRAKE_DISABLES_OFFROAD = ((uint16_t) 4),

DIGITAL_REGEN = ((uint16_t) 8),
SPEED_INFLUENCES_REGEN = ((uint16_t) 16),
SPEED_INFLUENCES_TORQUESENSOR = ((uint16_t) 32),
PAS_INVERTED = ((uint16_t) 64),

DUMMY_ALWAYS_ON = ((uint16_t) 128),

BYPASS_LOW_SPEED_REGEN_PI_CONTROL = ((uint16_t) 256)
} ACA_FLAGS;

#endif /* BOCONTROLLERSTATE_H */
Expand Down
3 changes: 2 additions & 1 deletion ACAeeprom.c
Expand Up @@ -26,6 +26,7 @@
uint8_t eeprom_magic_byte = 0;

void eeprom_init(void) {
#ifndef EEPROM_NOINIT
eeprom_magic_byte = (FLASH_ReadByte(EEPROM_BASE_ADDRESS + EEPROM_MAX_INIT_RANGE));
if (eeprom_magic_byte != EEPROM_INIT_MAGIC_BYTE) {
// eeprom needs to be reset after flashing
Expand All @@ -48,7 +49,7 @@ void eeprom_init(void) {
// reread to check everything went well
eeprom_magic_byte = (FLASH_ReadByte(EEPROM_BASE_ADDRESS + EEPROM_MAX_INIT_RANGE));
}

#endif
}

uint8_t eeprom_read(uint8_t address_offset) {
Expand Down
6 changes: 4 additions & 2 deletions ACAeeprom.h
Expand Up @@ -27,7 +27,6 @@ extern uint8_t eeprom_magic_byte;

typedef enum {
OFFSET_MOTOR_ANGLE = ((uint8_t) 0x01),
OFFSET_ACA_FLAGS = ((uint8_t) 0x02),
OFFSET_ASSIST_LEVEL = ((uint8_t) 0x03),
OFFSET_THROTTLE_MIN_RANGE = ((uint8_t) 0x04),
OFFSET_THROTTLE_MAX_RANGE = ((uint8_t) 0x05),
Expand All @@ -44,7 +43,10 @@ typedef enum {
OFFSET_BATTERY_CURRENT_MAX_VALUE_HIGH_BYTE = ((uint8_t) 0x0E),
OFFSET_BATTERY_CURRENT_MAX_VALUE = ((uint8_t) 0x0F),
OFFSET_REGEN_CURRENT_MAX_VALUE = ((uint8_t) 0x10),
OFFSET_CURRENT_CAL_A = ((uint8_t) 0x11)
OFFSET_CURRENT_CAL_A = ((uint8_t) 0x11),

OFFSET_ACA_FLAGS_HIGH_BYTE = ((uint8_t) 0x12),
OFFSET_ACA_FLAGS = ((uint8_t) 0x13)

} BO_EEPROM_OFFSETS;

Expand Down
37 changes: 24 additions & 13 deletions ACAsetPoint.c
Expand Up @@ -114,33 +114,44 @@ uint16_t aca_setpoint(uint16_t ui16_time_ticks_between_speed_interrupt, uint16_t
ui32_dutycycle = 0;
ui8_control_state = 255;

// check for brake --> set regen current
// check for brake --> set regen current
} else if (brake_is_set()) {

ui8_control_state = 255;
//Current target based on regen assist level
if ((ui8_aca_flags & DIGITAL_REGEN) == DIGITAL_REGEN) {
if ((ui16_aca_flags & DIGITAL_REGEN) == DIGITAL_REGEN) {

ui8_temp = i16_assistlevel[ui8_assistlevel_global >> 4];
ui8_control_state -= 1;

//Current target based on linear input on pad X4
//Current target based on linear input on pad X4
} else {
ui8_temp = map(ui16_adc_read_x4_value() >> 2, ui8_throttle_min_range, ui8_throttle_max_range, 0, 100); //map regen throttle to limits
ui8_control_state -= 2;
}
float_temp = (float) ui8_temp * (float) (ui16_regen_current_max_value) / 100.0;

//Current target gets ramped down with speed
if (((ui8_aca_flags & SPEED_INFLUENCES_REGEN) == SPEED_INFLUENCES_REGEN) && (ui16_virtual_erps_speed < ((ui16_speed_kph_to_erps_ratio * ((uint16_t) ui8_speedlimit_kph)) / 100))) {
float_temp *= ((float) ui16_virtual_erps_speed / ((float) (ui16_speed_kph_to_erps_ratio * ((float) ui8_speedlimit_kph)) / 100.0)); // influence of current speed based on base speed limit
ui8_control_state -= 4;
if (((ui16_aca_flags & SPEED_INFLUENCES_REGEN) == SPEED_INFLUENCES_REGEN) && (ui16_virtual_erps_speed < ((ui16_speed_kph_to_erps_ratio * ((uint16_t) ui8_speedlimit_kph)) / 100))) {
if (ui16_virtual_erps_speed < 30) {
// turn of regen at low speeds
// based on erps in order to avoid an additional calculation
float_temp = 0.0;
} else {

float_temp *= ((float) ui16_virtual_erps_speed / ((float) (ui16_speed_kph_to_erps_ratio * ((float) ui8_speedlimit_kph)) / 100.0)); // influence of current speed based on base speed limit
ui8_control_state -= 4;
}
}

uint32_current_target = (uint32_t) ui16_current_cal_b - float_temp;
ui32_dutycycle = PI_control(ui16_BatteryCurrent, uint32_current_target);
if (((ui16_aca_flags & BYPASS_LOW_SPEED_REGEN_PI_CONTROL) == BYPASS_LOW_SPEED_REGEN_PI_CONTROL) && (ui32_dutycycle == 0)) {
//try to get best regen at Low Speeds for BionX IGH
ui32_dutycycle = ui16_virtual_erps_speed * 2;
}

//limit max erps
//limit max erps
} else if (ui32_erps_filtered > ui16_erps_max) {
ui32_dutycycle = PI_control(ui32_erps_filtered, ui16_erps_max); //limit the erps to maximum value to have minimum 30 points of sine table for proper commutation
ui8_control_state = 2;
Expand All @@ -162,7 +173,7 @@ uint16_t aca_setpoint(uint16_t ui16_time_ticks_between_speed_interrupt, uint16_t
uint32_current_target = ((uint16_t) (uint32_current_target)*(uint16_t) (float_temp * 100.0)) / 100 + ui16_current_cal_b;
ui8_control_state += 1;

//in you are pedaling faster than in ramp end defined, desired battery current level is set,
//in you are pedaling faster than in ramp end defined, desired battery current level is set,
} else {
uint32_current_target = (i16_assistlevel[ui8_assistlevel_global & 15]*(ui16_battery_current_max_value) / 100 + ui16_current_cal_b);
ui8_control_state += 2;
Expand All @@ -173,16 +184,16 @@ uint16_t aca_setpoint(uint16_t ui16_time_ticks_between_speed_interrupt, uint16_t
float_temp = (float) sumtorque;

// map curret target to assist level, not to maximum value
if ((ui8_aca_flags & ASSIST_LVL_AFFECTS_THROTTLE) == 1) {
if ((ui16_aca_flags & ASSIST_LVL_AFFECTS_THROTTLE) == 1) {
float_temp *= ((float) i16_assistlevel[ui8_assistlevel_global & 15] / 100.0);
ui8_control_state += 4;
}
// if torque sensor is requested
if (flt_torquesensorCalibration != 0.0) {
// flt_torquesensorCalibration is >fummelfactor * NUMBER_OF_PAS_MAGS * 64< (64 cause of <<6)
float_temp *= flt_torquesensorCalibration / ((uint32_t) ui16_virtual_capped_pas_activity); // influence of cadence
//increase power linear with speed for convenient commuting :-)
if ((ui8_aca_flags & SPEED_INFLUENCES_TORQUESENSOR) == SPEED_INFLUENCES_TORQUESENSOR) {
//increase power linear with speed for convenient commuting :-)
if ((ui16_aca_flags & SPEED_INFLUENCES_TORQUESENSOR) == SPEED_INFLUENCES_TORQUESENSOR) {
float_temp *= ((float) ui16_virtual_erps_speed / ((float) (ui16_speed_kph_to_erps_ratio * ((float) ui8_speedlimit_kph)) / 100.0)); // influence of current speed based on base speed limit
}
ui8_control_state += 8;
Expand Down Expand Up @@ -215,13 +226,13 @@ uint16_t aca_setpoint(uint16_t ui16_time_ticks_between_speed_interrupt, uint16_t
ui32_dutycycle = PI_control(ui16_BatteryCurrent, uint32_current_target);

//disable PWM if enabled and motor is at standstill and no power is wanted
if (uint_PWM_Enable && ui32_erps_filtered==0 && uint32_current_target==ui16_current_cal_b) {
if (uint_PWM_Enable && ui32_erps_filtered == 0 && uint32_current_target == ui16_current_cal_b) {
TIM1_CtrlPWMOutputs(DISABLE);
uint_PWM_Enable = 0;
}

//enable PWM if disabled and voltage is 2V higher than min, some hysteresis and power is wanted
if (!uint_PWM_Enable && ui8_BatteryVoltage > BATTERY_VOLTAGE_MIN_VALUE + 8 && uint32_current_target!=ui16_current_cal_b) {
if (!uint_PWM_Enable && ui8_BatteryVoltage > BATTERY_VOLTAGE_MIN_VALUE + 8 && uint32_current_target != ui16_current_cal_b) {
TIM1_CtrlPWMOutputs(ENABLE);
uint_PWM_Enable = 1;

Expand Down
27 changes: 15 additions & 12 deletions BOdisplay.c
Expand Up @@ -131,7 +131,8 @@ void addConfigStateInfos(void) {
addPayload(CODE_MAX_SPEED_DEFAULT, ui8_speedlimit_kph);
addPayload(CODE_MAX_SPEED_WITHOUT_PAS, ui8_speedlimit_without_pas_kph);
addPayload(CODE_MAX_SPEED_WITH_THROTTLE_OVERRIDE, ui8_speedlimit_with_throttle_override_kph);
addPayload(CODE_ACA_FLAGS, ui8_aca_flags);
addPayload(CODE_ACA_FLAGS_HIGH_BYTE, ui16_aca_flags >> 8);
addPayload(CODE_ACA_FLAGS, ui16_aca_flags);
addPayload(CODE_ASSIST_LEVEL, ui8_assistlevel_global);
addPayload(CODE_THROTTLE_MIN_RANGE, ui8_throttle_min_range);
addPayload(CODE_THROTTLE_MAX_RANGE, ui8_throttle_max_range);
Expand All @@ -140,11 +141,11 @@ void addConfigStateInfos(void) {
addPayload(CODE_PID_GAIN_P, float2int(flt_s_pid_gain_p, 2.0));
addPayload(CODE_PID_GAIN_I, float2int(flt_s_pid_gain_i, 2.0));
addPayload(CODE_RAMP_END, ui16_s_ramp_end >> 5);
addPayload(CODE_RAMP_START, ui16_s_ramp_start >> 5);
addPayload(CODE_RAMP_START, ui16_s_ramp_start >> 6);
addPayload(CODE_MAX_BAT_CURRENT_HIGH_BYTE, ui16_battery_current_max_value >> 8);
addPayload(CODE_MAX_BAT_CURRENT, ui16_battery_current_max_value);
addPayload(CODE_MAX_REGEN_CURRENT, ui16_regen_current_max_value);
// 1 more elements left/avail (max22)
// 0 more elements left/avail (max22)

}

Expand Down Expand Up @@ -240,6 +241,15 @@ void digestConfigRequest(uint8_t configAddress, uint8_t requestedCodeLowByte, ui
ui8_offroad_state = requestedValue;
addPayload(requestedCodeLowByte, ui8_offroad_state);
break;
case CODE_ACA_FLAGS:
ui16_aca_flags = ((uint16_t) requestedValueHighByte << 8)+(uint16_t) requestedValue;
if (configAddress == EEPROM_ADDRESS) {
eeprom_write(OFFSET_ACA_FLAGS_HIGH_BYTE, requestedValueHighByte);
eeprom_write(OFFSET_ACA_FLAGS, requestedValue);
}
addPayload(CODE_ACA_FLAGS_HIGH_BYTE, ui16_aca_flags >> 8);
addPayload(requestedCodeLowByte, ui16_aca_flags);
break;
case CODE_MAX_BAT_CURRENT:
ui16_battery_current_max_value = ((uint16_t) requestedValueHighByte << 8)+(uint16_t) requestedValue;
if (configAddress == EEPROM_ADDRESS) {
Expand Down Expand Up @@ -270,13 +280,6 @@ void digestConfigRequest(uint8_t configAddress, uint8_t requestedCodeLowByte, ui
}
addPayload(requestedCodeLowByte, ui8_assistlevel_global);
break;
case CODE_ACA_FLAGS:
ui8_aca_flags = requestedValue;
if (configAddress == EEPROM_ADDRESS) {
eeprom_write(OFFSET_ACA_FLAGS, requestedValue);
}
addPayload(requestedCodeLowByte, ui8_aca_flags);
break;
case CODE_THROTTLE_MIN_RANGE:
ui8_throttle_min_range = requestedValue;
if (configAddress == EEPROM_ADDRESS) {
Expand Down Expand Up @@ -329,11 +332,11 @@ void digestConfigRequest(uint8_t configAddress, uint8_t requestedCodeLowByte, ui
addPayload(requestedCodeLowByte, ui16_s_ramp_end >> 5);
break;
case CODE_RAMP_START:
ui16_s_ramp_start = requestedValue << 5;
ui16_s_ramp_start = requestedValue << 6;
if (configAddress == EEPROM_ADDRESS) {
eeprom_write(OFFSET_RAMP_START, requestedValue);
}
addPayload(requestedCodeLowByte, ui16_s_ramp_start >> 5);
addPayload(requestedCodeLowByte, ui16_s_ramp_start >> 6);
break;
case CODE_MAX_SPEED_DEFAULT:
ui8_speedlimit_kph = requestedValue;
Expand Down
4 changes: 3 additions & 1 deletion BOdisplay.h
Expand Up @@ -102,12 +102,14 @@ typedef enum {
CODE_ACTUAL_MAX_SPEED = ((uint8_t) 0x90),
CODE_MAX_SPEED_WITHOUT_PAS = ((uint8_t) 0x91),
CODE_MAX_SPEED_WITH_THROTTLE_OVERRIDE = ((uint8_t) 0x92),
CODE_ACA_FLAGS = ((uint8_t) 0x93),

CODE_MAX_BAT_CURRENT_HIGH_BYTE = ((uint8_t) 0x94),
CODE_MAX_BAT_CURRENT = ((uint8_t) 0x95),
CODE_MAX_REGEN_CURRENT = ((uint8_t) 0x96),

CODE_ACA_FLAGS_HIGH_BYTE = ((uint8_t) 0x97),
CODE_ACA_FLAGS = ((uint8_t) 0x98),

CODE_HALL_ORDER_BASE = ((uint8_t) 0x10),
CODE_CURRENT_AT_HALL_POSITION_BASE = ((uint8_t) 0x00)
} BO_VALUE_CODES;
Expand Down
Binary file modified OSEC Parameter Configurator.jar
Binary file not shown.
7 changes: 4 additions & 3 deletions config.h
Expand Up @@ -19,7 +19,7 @@
#define PHASE_CURRENT_MAX_VALUE 300L
#define REGEN_CURRENT_MAX_VALUE 50L
#define MOTOR_ROTOR_DELTA_PHASE_ANGLE_RIGHT 200
#define current_cal_a 10
#define current_cal_a 5
#define TEMP_CAL_A 1.6
#define TEMP_CAL_B -110.4
#define LEVEL_1 12
Expand All @@ -36,12 +36,13 @@
#define GEAR_RATIO 24L
#define BATTERY_LI_ION_CELLS_NUMBER 13
#define PAS_THRESHOLD 3.0
#define RAMP_START 6000
#define RAMP_START 12000
#define PWM_CYCLES_SECOND 15625L
#define SPEEDSENSOR_INTERNAL
#define BLUOSEC
#define TQS_CALIB 0.0
#define ACA 156
#define EEPROM_INIT_MAGIC_BYTE 161 // makes sure (chance of fail 1/255) eeprom is invalidated after flashing new config
#define EEPROM_NOINIT // eeprom will not be cleared
#define EEPROM_INIT_MAGIC_BYTE 146 // makes sure (chance of fail 1/255) eeprom is invalidated after flashing new config

#endif /* CONFIG_H_ */

0 comments on commit 1fdb365

Please sign in to comment.