Skip to content

Commit 0ba31fe

Browse files
TC + Regen On Performance Cleanup (#340)
Co-authored-by: Peyton-McKee <[email protected]>
1 parent 87b4815 commit 0ba31fe

File tree

2 files changed

+106
-79
lines changed

2 files changed

+106
-79
lines changed

Core/Src/pedals.c

Lines changed: 98 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030

3131
#define REGEN_INCREMENT_STEP 10 /* AC Amps */
3232

33+
typedef void (*drive_handle_func)(float mph, float accel_val, float break_val);
34+
static drive_handle_func drive_handles[MAX_FUNC_STATES];
35+
3336
float torque_limit_percentage = 1.0;
34-
uint16_t regen_limit = 100;
37+
uint16_t regen_limits[2] = { 0, 50 }; // [PERFORMANCE, ENDURANCE]
3538
static bool launch_control_enabled = false;
3639

3740
/* Parameters for the pedal monitoring task */
@@ -47,6 +50,8 @@ static bool launch_control_enabled = false;
4750
#define BRAKE_THRESHOLD_BUF 0.25
4851
enum { ACCELPIN_1, ACCELPIN_2, BRAKEPIN_1, BRAKEPIN_2 };
4952

53+
/* Factor for converting MPH to KMH */
54+
static const float MPH_TO_KMH = 1.609;
5055
static bool brake_pressed = false;
5156
static osMutexId_t brake_state_mut;
5257
static osMutexAttr_t brake_mutex_attributes;
@@ -113,30 +118,41 @@ void decrease_torque_limit()
113118

114119
void increase_regen_limit()
115120
{
121+
func_state_t func_state = get_func_state();
122+
if (func_state != F_PERFORMANCE && func_state != F_EFFICIENCY)
123+
return;
124+
uint16_t regen_limit = get_regen_limit();
116125
if (regen_limit + REGEN_INCREMENT_STEP > MAX_REGEN_CURRENT) {
117-
regen_limit = MAX_REGEN_CURRENT;
126+
set_regen_limit(MAX_REGEN_CURRENT);
118127
} else {
119-
regen_limit += REGEN_INCREMENT_STEP;
128+
set_regen_limit(regen_limit += REGEN_INCREMENT_STEP);
120129
}
121130
}
122131

123132
void decrease_regen_limit()
124133
{
134+
func_state_t func_state = get_func_state();
135+
if (func_state != F_PERFORMANCE && func_state != F_EFFICIENCY)
136+
return;
137+
uint16_t regen_limit = get_regen_limit();
125138
if (regen_limit - REGEN_INCREMENT_STEP < 0) {
126-
regen_limit = 0;
139+
set_regen_limit(0);
127140
} else {
128-
regen_limit -= REGEN_INCREMENT_STEP;
141+
set_regen_limit(regen_limit -= REGEN_INCREMENT_STEP);
129142
}
130143
}
131144

132145
void set_regen_limit(uint16_t limit)
133146
{
134-
regen_limit = limit;
135-
136-
if (regen_limit > MAX_REGEN_CURRENT) {
137-
regen_limit = MAX_REGEN_CURRENT;
138-
} else if (regen_limit < 0.0) {
139-
regen_limit = 0.0;
147+
func_state_t func_state = get_func_state();
148+
if (func_state != F_PERFORMANCE && func_state != F_EFFICIENCY)
149+
return;
150+
if (limit > MAX_REGEN_CURRENT) {
151+
regen_limits[func_state - F_PERFORMANCE] = MAX_REGEN_CURRENT;
152+
} else if (limit < 0.0) {
153+
regen_limits[func_state - F_PERFORMANCE] = 0.0;
154+
} else {
155+
regen_limits[func_state - F_PERFORMANCE] = limit;
140156
}
141157
}
142158

@@ -159,7 +175,11 @@ float get_torque_limit_percentage()
159175

160176
uint16_t get_regen_limit()
161177
{
162-
return regen_limit;
178+
func_state_t func_state = get_func_state();
179+
if (func_state != F_PERFORMANCE && func_state != F_EFFICIENCY) {
180+
return 0;
181+
}
182+
return regen_limits[get_func_state() - F_PERFORMANCE];
163183
}
164184

165185
void toggle_launch_control()
@@ -454,9 +474,9 @@ static int16_t derate_torque(float mph, float accel)
454474
* @param mph Current speed of the car.
455475
* @param accel % pedal travel of the accelerator pedal.
456476
*/
457-
static void handle_pit(float mph, float accel)
477+
static void handle_pit(float mph, float accel_val, float brake_val)
458478
{
459-
dti_set_torque(derate_torque(mph, accel));
479+
dti_set_torque(derate_torque(mph, accel_val));
460480
}
461481

462482
/**
@@ -465,9 +485,9 @@ static void handle_pit(float mph, float accel)
465485
* @param mph Current speed of the car.
466486
* @param accel % pedal travel of the accelerator pedal.
467487
*/
468-
static void handle_reverse(float mph, float accel)
488+
static void handle_reverse(float mph, float accel_val, float brake_val)
469489
{
470-
dti_set_torque(-1 * derate_torque(fabs(mph), accel));
490+
dti_set_torque(-1 * derate_torque(fabs(mph), accel_val));
471491
}
472492

473493
/**
@@ -498,6 +518,8 @@ void accel_pedal_regen_torque(float accel_val)
498518
*/
499519
void accel_pedal_regen_braking(float accel_val)
500520
{
521+
uint16_t regen_limit = get_regen_limit();
522+
501523
/* Calculate AC current target for regenerative braking */
502524
float regen_current =
503525
(regen_limit / REGEN_THRESHOLD) * (REGEN_THRESHOLD - accel_val);
@@ -510,41 +532,6 @@ void accel_pedal_regen_braking(float accel_val)
510532
dti_set_regen((uint16_t)(regen_current * 10));
511533
}
512534

513-
/**
514-
* @brief Torque calculations for efficiency mode. If the driver is braking, do regenerative braking.
515-
*
516-
* @param mc pointer to struct containing dti data
517-
* @param mph mph of the car
518-
* @param accel_val adjusted value of the acceleration pedal
519-
* @param brake_val adjusted value of the brake pedal
520-
* @param torque pointer to torque value
521-
*/
522-
void handle_endurance(float mph, float accel_val, float brake_val)
523-
{
524-
#ifdef USE_BRAKE_REGEN
525-
if (brake_val > PEDAL_BRAKE_THRESH && (mph * 1.609) > 5) {
526-
brake_pedal_regen(brake_val);
527-
} else {
528-
// accelerating, limit torque
529-
linear_accel_to_torque(accel_val, torque);
530-
}
531-
#else
532-
/* Factor for converting MPH to KMH */
533-
static const float MPH_TO_KMH = 1.609;
534-
535-
/* Pedal is in acceleration range. Set forward torque target. */
536-
if (accel_val >= ACCELERATION_THRESHOLD) {
537-
accel_pedal_regen_torque(accel_val);
538-
} else if (mph * MPH_TO_KMH > 5 && accel_val <= REGEN_THRESHOLD) {
539-
accel_pedal_regen_braking(accel_val);
540-
} else {
541-
/* Pedal travel is between thresholds, so there should not be acceleration or braking */
542-
dti_set_torque(0);
543-
}
544-
545-
#endif
546-
}
547-
548535
const float deltaMPHPS_max =
549536
22.0f; // Miles per hour per second, based on matlab accel numbers
550537
const float max_limiting_mph = 30;
@@ -578,13 +565,61 @@ void handle_launch_control(float mph, float accel_val)
578565
prev_accel = accel_val;
579566
}
580567

568+
void handle_performance(float mph, float accel_val, float brake_val)
569+
{
570+
#ifndef POWER_REGRESSION_PEDAL_TORQUE_TRANSFER
571+
uint16_t regen_limit = get_regen_limit();
572+
if (regen_limit <= 0.01) {
573+
linear_accel_to_torque(accel_val);
574+
return;
575+
}
576+
577+
if (accel_val >= ACCELERATION_THRESHOLD) {
578+
if (launch_control_enabled) {
579+
handle_launch_control(mph, (accel_val - 0.25) / 0.75);
580+
} else {
581+
accel_pedal_regen_torque(accel_val);
582+
}
583+
} else if (mph * MPH_TO_KMH > 5 && accel_val <= REGEN_THRESHOLD) {
584+
accel_pedal_regen_braking(accel_val);
585+
} else {
586+
/* Pedal travel is between thresholds, so there should not be acceleration or braking */
587+
dti_set_torque(0);
588+
}
589+
#else
590+
power_regression_accel_to_torque(accel_val);
591+
#endif
592+
}
593+
581594
osThreadId_t process_pedals_thread;
582595
const osThreadAttr_t process_pedals_attributes = {
583596
.name = "PedalMonitor",
584597
.stack_size = 128 * 8,
585598
.priority = (osPriority_t)osPriorityRealtime,
586599
};
587600

601+
/**
602+
* @brief Torque calculations for efficiency mode. If the driver is braking, do regenerative braking.
603+
*
604+
* @param mc pointer to struct containing dti data
605+
* @param mph mph of the car
606+
* @param accel_val adjusted value of the acceleration pedal
607+
* @param brake_val adjusted value of the brake pedal
608+
* @param torque pointer to torque value
609+
*/
610+
void handle_endurance(float mph, float accel_val, float brake_val)
611+
{
612+
/* Pedal is in acceleration range. Set forward torque target. */
613+
if (accel_val >= ACCELERATION_THRESHOLD) {
614+
accel_pedal_regen_torque(accel_val);
615+
} else if (mph * MPH_TO_KMH > 5 && accel_val <= REGEN_THRESHOLD) {
616+
accel_pedal_regen_braking(accel_val);
617+
} else {
618+
/* Pedal travel is between thresholds, so there should not be acceleration or braking */
619+
dti_set_torque(0);
620+
}
621+
}
622+
588623
void vProcessPedals(void *pv_params)
589624
{
590625
pedals_args_t *args = (pedals_args_t *)pv_params;
@@ -612,6 +647,13 @@ void vProcessPedals(void *pv_params)
612647

613648
brake_state_mut = osMutexNew(&brake_mutex_attributes);
614649

650+
drive_handles[READY] = NULL;
651+
drive_handles[FAULTED] = NULL;
652+
drive_handles[F_PIT] = handle_pit;
653+
drive_handles[F_REVERSE] = handle_reverse;
654+
drive_handles[F_PERFORMANCE] = handle_performance;
655+
drive_handles[F_EFFICIENCY] = handle_endurance;
656+
615657
for (;;) {
616658
read_pedals(mpu, adc_data);
617659

@@ -675,32 +717,13 @@ void vProcessPedals(void *pv_params)
675717
float mph = dti_get_mph(mc);
676718
func_state_t func_state = get_func_state();
677719

678-
switch (func_state) {
679-
case F_EFFICIENCY:
680-
handle_endurance(mph, accel_value, brake_value);
681-
break;
682-
case F_PERFORMANCE:
683-
if (launch_control_enabled) {
684-
handle_launch_control(mph, accel_value);
685-
} else {
686-
#ifndef POWER_REGRESSION_PEDAL_TORQUE_TRANSFER
687-
linear_accel_to_torque(accel_value);
688-
#else
689-
power_regression_accel_to_torque(accel_value);
690-
#endif
691-
}
692-
break;
693-
case F_PIT:
694-
handle_pit(mph, accel_value);
695-
break;
696-
case REVERSE:
697-
// EV.3.1.1 no reverse at FSAE
698-
handle_reverse(mph, accel_value);
699-
break;
700-
default:
720+
if (drive_handles[func_state] == NULL) {
701721
dti_set_torque(0);
702-
break;
722+
} else {
723+
drive_handles[func_state](mph, accel_value,
724+
brake_value);
703725
}
726+
704727
osDelay(delay_time);
705728
}
706729
}

Core/Src/steeringio.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,32 @@ void buttons_update(can_msg_t msg, dti_t *mc)
3535
break;
3636
case BUTTON_ESC:
3737
printf("Esc button pressed \n");
38-
if (fabs(dti_get_mph(mc)) < 1) {
38+
if (fabs(dti_get_mph(mc)) <= 0.1) {
3939
set_home_mode();
4040
}
4141
break;
4242
case BUTTON_UP:
4343
printf("Up button pressed \n");
44-
if (get_func_state() == F_EFFICIENCY) {
44+
if (get_func_state() == F_EFFICIENCY ||
45+
(fabs(dti_get_mph(mc)) <= 0.1 && get_brake_state() &&
46+
get_func_state() == F_PERFORMANCE)) {
4547
increase_regen_limit();
4648
}
4749
decrement_nero_index();
4850
break;
4951
case BUTTON_DOWN:
5052
printf("Down button pressed \n");
51-
if (get_func_state() == F_EFFICIENCY) {
53+
if (get_func_state() == F_EFFICIENCY ||
54+
(fabs(dti_get_mph(mc)) <= 0.1 && get_brake_state() &&
55+
get_func_state() == F_PERFORMANCE)) {
5256
decrease_regen_limit();
5357
}
5458
increment_nero_index();
5559
break;
5660
case BUTTON_ENTER:
5761
printf("Enter button pressed \n");
5862
if (get_func_state() == F_PERFORMANCE &&
59-
fabs(dti_get_mph(mc)) < 1) {
63+
fabs(dti_get_mph(mc)) <= 0.1) {
6064
toggle_launch_control();
6165
}
6266
select_nero_index();

0 commit comments

Comments
 (0)