Skip to content

Commit

Permalink
games/reverse + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Feb 2, 2025
1 parent 622df3b commit 71e2de1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 62 deletions.
5 changes: 3 additions & 2 deletions Core/Inc/state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef enum {
F_PIT,
F_PERFORMANCE,
F_EFFICIENCY,
REVERSE,
F_REVERSE,
FAULTED,
MAX_FUNC_STATES
} func_state_t;
Expand All @@ -29,9 +29,10 @@ typedef enum {
PIT, //SPEED_LIMITIED
PERFORMANCE, //AUTOCROSS
EFFICIENCY, //ENDURANCE
REVERSE,
DEBUG,
CONFIGURATION,
FLAPPY_BIRD,
GAMES,
EXIT,
MAX_NERO_STATES
} nero_menu_t;
Expand Down
84 changes: 24 additions & 60 deletions Core/Src/state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define SEND_NERO_TIMEOUT 500 /*in millis*/

//#define DISABLE_REVERSE
// #define DISABLE_REVERSE

/* Internal State of Vehicle */
static state_t cerberus_state;
Expand Down Expand Up @@ -45,14 +45,6 @@ static void send_nero_msg()
uint8_t torque_lim_percentage;
} nero_data;

/* Since the screen on NERO relies on the NERO index, and reverse and pit have the same index,
* reverse gets a special index */
if (get_func_state() == REVERSE) {
nero_data.nero_index = 255;
} else {
nero_data.nero_index = (uint8_t)get_nero_state().nero_index;
}

nero_data.home_mode = (uint8_t)get_nero_state().home_mode;
nero_data.mph = get_mph();
nero_data.tsms = (uint8_t)get_tsms();
Expand All @@ -78,7 +70,7 @@ bool get_active()
return cerberus_state.functional == F_EFFICIENCY ||
cerberus_state.functional == F_PERFORMANCE ||
cerberus_state.functional == F_PIT ||
cerberus_state.functional == REVERSE;
cerberus_state.functional == F_REVERSE;
}

nero_state_t get_nero_state()
Expand All @@ -89,13 +81,14 @@ nero_state_t get_nero_state()
static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
dti_t *mc, mpu_t *mpu)
{
/* Make sure wheels are not spinning before changing modes */
if (dti_get_mph(mc) > 1)
return 1;
bool brake_state;

/* Catching state transitions */
switch (new_state) {
case READY:
/* Make sure wheels are not spinning before changing modes */
if (dti_get_mph(mc) > 1)
return 1;

/* Turn off high power peripherals */
// write_fan_battbox(pdu, false);
write_pump(pdu, false);
Expand All @@ -105,48 +98,41 @@ static int transition_functional_state(func_state_t new_state, pdu_t *pdu,
case F_PIT:
case F_PERFORMANCE:
case F_EFFICIENCY:
/* Entering active state from home mode */
if (cerberus_state.functional != REVERSE) {
/* Check that motor is not spinning before changing modes */
if (dti_get_mph(mc) > 1) {
return 2;
}
/* Only turn on motor if brakes engaged and tsms is on */
bool brake_state;
if (read_brake_state(pdu, &brake_state)) {
return 3;
}
if (read_brake_state(pdu, &brake_state)) {
return 3;
}
#ifdef TSMS_OVERRIDE
if (!brake_state) {
return 3;
}
if (!brake_state) {
return 3;
}
#else
if (!brake_state || !get_tsms()) {
return 3;
}
#endif
osThreadFlagsSet(rtds_thread, SOUND_RTDS_FLAG);
/* Only turn on motor if brakes engaged and tsms is on */
if (!brake_state || !get_tsms()) {
return 3;
}
#endif
osThreadFlagsSet(rtds_thread, SOUND_RTDS_FLAG);

/* Turn on high power peripherals */
// write_fan_battbox(pdu, true);
write_pump(pdu, true);
write_fault(mpu, false);
printf("ACTIVE STATE\r\n");
break;
case REVERSE:

case F_REVERSE:
#ifndef DISABLE_REVERSE
/* Can only enter reverse mode if already in pit mode */
if (cerberus_state.functional != F_PIT)
return 4;
#endif
break;
case FAULTED:
/* Turn off high power peripherals */
// write_fan_battbox(pdu, true);
write_pump(pdu, false);
cerberus_state.nero =
(nero_state_t){ .nero_index = OFF, .home_mode = false };

osDelay(1000); /* Delay for 1 sec before faulting car */
write_fault(mpu, true);
printf("FAULTED\r\n");
break;
Expand Down Expand Up @@ -175,24 +161,6 @@ static int transition_nero_state(nero_state_t new_state, pdu_t *pdu, dti_t *mc,
if (new_state.nero_index >= MAX_NERO_STATES)
new_state.nero_index = MAX_NERO_STATES - 1;

#ifndef DISABLE_REVERSE
// Wasn't in home mode and still are not in home mode (Infer a Select Request)
if (!current_nero_state.home_mode && !new_state.home_mode) {
// Only Check if we are in pit mode to toggle direction
if (current_nero_state.nero_index == PIT) {
if (get_func_state() == REVERSE) {
if (transition_functional_state(F_PIT, pdu, mc,
mpu))
return 1;
} else if (get_func_state() == F_PIT) {
if (transition_functional_state(REVERSE, pdu,
mc, mpu))
return 1;
}
}
}
#endif

// Selecting a mode on NERO
if (current_nero_state.home_mode && !new_state.home_mode) {
if (new_state.nero_index < DEBUG &&
Expand Down Expand Up @@ -332,12 +300,8 @@ void vStateMachineDirector(void *pv_params)
for (;;) {
if (osMessageQueueGet(state_trans_queue, &new_state_req, NULL,
pdMS_TO_TICKS(SEND_NERO_TIMEOUT)) ==
osOK) {
// transition state only if state was changed
if (!check_state_change(new_state_req)) {
continue;
}

osOK &&
check_state_change(new_state_req)) {
if (new_state_req.id == NERO)
transition_nero_state(new_state_req.state.nero,
pdu, mc, mpu);
Expand Down

0 comments on commit 71e2de1

Please sign in to comment.