diff --git a/Core/Inc/dti.h b/Core/Inc/dti.h index fb0aeb8..775a339 100644 --- a/Core/Inc/dti.h +++ b/Core/Inc/dti.h @@ -78,7 +78,7 @@ void dti_set_torque(int16_t torque); * * @param current_target The desired AC current to do regenerative braking at. Must be positive. */ -void dti_set_regen(uint16_t current_target); +void dti_set_regen(float current_target); /** * @brief Send a CAN message containing the AC current target for regenerative braking. diff --git a/Core/Src/dti.c b/Core/Src/dti.c index 2ba26e9..c49634a 100644 --- a/Core/Src/dti.c +++ b/Core/Src/dti.c @@ -68,12 +68,12 @@ void dti_set_torque(int16_t torque) dti_set_current(ac_current); } -void dti_set_regen(uint16_t current_target) +void dti_set_regen(float current_target) { /* Simple moving average to smooth change in braking target */ // Static variables for the buffer and index - static int16_t buffer[SAMPLES] = { 0 }; + static float buffer[SAMPLES] = { 0 }; static int index = 0; // Add the new value to the buffer @@ -89,10 +89,6 @@ void dti_set_regen(uint16_t current_target) } float average = sum / SAMPLES; - if (current_target == 0) { - average = 0; - } - dti_send_brake_current(average); } @@ -119,7 +115,7 @@ void dti_send_brake_current(float brake_current) .len = 8, .data = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } }; /* Motor controller expects int value * 10 */ - int16_t adjusted_brake_current = brake_current * 10; + uint16_t adjusted_brake_current = (uint16_t)(brake_current * 10); /* convert to big endian */ endian_swap(&adjusted_brake_current, sizeof(adjusted_brake_current)); /* Send CAN message */ diff --git a/Core/Src/monitor.c b/Core/Src/monitor.c index bde8198..5f13181 100644 --- a/Core/Src/monitor.c +++ b/Core/Src/monitor.c @@ -255,7 +255,8 @@ void vPedalsMonitor(void *pv_params) uint16_t accel_val = (uint16_t)(accel_val1 + accel_val2) / 2; - is_braking = (adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) / 2; + is_braking = ((adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) / + 2) > PEDAL_BRAKE_THRESH; brake_state = is_braking; osMessageQueuePut(brakelight_signal, &is_braking, 0U, 0U); @@ -400,8 +401,6 @@ void vTsmsMonitor(void *pv_params) for (;;) { /* If we got a reliable TSMS reading, handle transition to and out of ACTIVE*/ if (!read_tsms_sense(pdu, &tsms_status)) { - printf("Checking pdu"); - // Timer has not been started, and there is a change in TSMS, so start the timer if (tsms != tsms_status && !is_timer_active(&tsms_debounce_timer)) { diff --git a/Core/Src/torque.c b/Core/Src/torque.c index 989a8f1..9b413eb 100644 --- a/Core/Src/torque.c +++ b/Core/Src/torque.c @@ -152,12 +152,11 @@ void handle_endurance(dti_t *mc, float mph, float accel_val, float brake_val) } dti_set_torque(torque); - } else if (mph * mph_to_kmh > 2 && accel_val < regen_thresh) { + } else if (mph * mph_to_kmh > 2 && accel_val <= regen_thresh) { float regen_current = (max_curr / regen_thresh) * (regen_thresh - accel_val); - /* Send regen current to motor controller */ - dti_set_regen((uint16_t)regen_current); + dti_set_regen(regen_current); } else { /* Pedal travel is between thresholds, so there should not be acceleration or braking */ dti_set_torque(0);