Skip to content

Commit

Permalink
remove bad comments, change func signature to uint16, edit documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabramz committed Jul 31, 2024
1 parent 1a7a458 commit 88a53af
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Core/Inc/dti.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(int16_t current_target);
void dti_set_regen(uint16_t current_target);

/**
* @brief Send a CAN message containing the AC current target for regenerative braking.
Expand Down
3 changes: 1 addition & 2 deletions Core/Src/dti.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void dti_set_torque(int16_t torque)
dti_set_current(ac_current);
}

void dti_set_regen(int16_t current_target)
void dti_set_regen(uint16_t current_target)
{
/* Simple moving average to smooth change in braking target */

Expand All @@ -93,7 +93,6 @@ void dti_set_regen(int16_t current_target)
average = 0;
}

/* DTI CAN manual page 18, scale = 10 */
dti_send_brake_current(average);
}

Expand Down
18 changes: 1 addition & 17 deletions Core/Src/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void vLVMonitor(void *pv_params)
// get final voltage
v_int = (uint32_t)(v_dec * 10.0);

//endian_swap(&v_int, sizeof(v_int));
memcpy(msg.data, &v_int, msg.len);
if (queue_can_msg(msg)) {
fault_data.diag =
Expand Down Expand Up @@ -247,33 +246,19 @@ void vPedalsMonitor(void *pv_params)
eval_pedal_fault(adc_data[ACCELPIN_1], adc_data[ACCELPIN_2],
&diff_timer_accelerator, &sc_timer_accelerator,
&oc_timer_accelerator, &fault_data);
//eval_pedal_fault(adc_data[BRAKEPIN_1], adc_data[BRAKEPIN_1], &diff_timer_brake, &sc_timer_brake, &oc_timer_brake, &fault_data);

/* Offset adjusted per pedal sensor, clamp to be above 0 */
uint16_t accel_val1 = adjust_pedal_val(
adc_data[ACCELPIN_1], ACCEL1_OFFSET, ACCEL1_MAX_VAL);
//printf("Accel 1: %d\r\n", max_pedal1);
uint16_t accel_val2 = adjust_pedal_val(
adc_data[ACCELPIN_2], ACCEL2_OFFSET,
ACCEL2_MAX_VAL); //printf("Accel 2: %d\r\n",max_pedal2);
adc_data[ACCELPIN_2], ACCEL2_OFFSET, ACCEL2_MAX_VAL);

uint16_t accel_val = (uint16_t)(accel_val1 + accel_val2) / 2;
//printf("Avg Pedal Val: %d\r\n\n", accel_val);

/* Raw ADC for tuning */
//printf("Accel 1: %ld\r\n", adc_data[ACCELPIN_1]);
//printf("Accel 2: %ld\r\n", adc_data[ACCELPIN_2]);

/* Brakelight Control */
// printf("Brake 1: %ld\r\n", adc_data[BRAKEPIN_1]);
// printf("Brake 2: %ld\r\n", adc_data[BRAKEPIN_2]);

is_braking = (adc_data[BRAKEPIN_1] + adc_data[BRAKEPIN_2]) / 2;
brake_state = is_braking;

osMessageQueuePut(brakelight_signal, &is_braking, 0U, 0U);
//osMessageQueueReset(break_state_queue);
//osMessageQueuePut(break_state_queue, &is_braking, 0U, 0U);

/* Low Pass Filter */
sensor_data.accelerator_value =
Expand All @@ -284,7 +269,6 @@ void vPedalsMonitor(void *pv_params)
num_samples;

/* Publish to Onboard Pedals Queue */
//printf("Accel pedal queue %d", sensor_data.accelerator_value);
osStatus_t check = osMessageQueuePut(pedal_data_queue,
&sensor_data, 0U, 0U);

Expand Down
11 changes: 5 additions & 6 deletions Core/Src/torque.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void decrease_torque_limit()
}
}

// comment out to use single pedal mode
/* Comment out to use single pedal mode */
//#define USE_BRAKE_REGEN 1

/**
Expand All @@ -112,7 +112,6 @@ void decrease_torque_limit()
*/
void handle_endurance(dti_t *mc, float mph, float accel_val, float brake_val)
{
// max ac current to brake with
/* Maximum AC braking current */
static const int8_t max_curr = 20;
#ifdef USE_BRAKE_REGEN
Expand Down Expand Up @@ -140,23 +139,25 @@ void handle_endurance(dti_t *mc, float mph, float accel_val, float brake_val)
static const float regen_thresh = 0.01;
static const float accel_thresh = 0.05;
static const float mph_to_kmh = 1.609;
// if the rescaled accel is positive then convert it to torque, and full send
/* Pedal is in acceleration range. Set forward torque target. */
if (accel_val >= accel_thresh) {
/* Coefficient to map accel pedal travel % to the max torque */
const float coeff = MAX_TORQUE / (1 - accel_thresh);
/* Makes acceleration pedal more sensitive since domain is compressed but range is the same */
uint16_t torque =
coeff * accel_val - (accel_val * accel_thresh);

if (torque > MAX_TORQUE) {
torque = MAX_TORQUE;
}

dti_set_torque(torque);
} 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((int16_t)regen_current);
dti_set_regen((uint16_t)regen_current);
} else {
/* Pedal travel is between thresholds, so there should not be acceleration or braking */
dti_set_torque(0);
Expand Down Expand Up @@ -194,9 +195,7 @@ void vCalcTorque(void *pv_params)
/* If we receive a new message within the time frame, calc new torque */
if (stat == osOK) {
int32_t rpm = dti_get_rpm(mc);
//printf("rpm %ld", rpm);
mph = rpm_to_mph(rpm);
//printf("mph %d", (int8_t) mph);
set_mph(mph);

func_state_t func_state = get_func_state();
Expand Down

0 comments on commit 88a53af

Please sign in to comment.