-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #23 - state machine skeleton --------- Co-authored-by: RChandler234 <root@DESKTOP-DEAD2D4>
- Loading branch information
1 parent
7a58b33
commit c537e5e
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef STATE_MACHINE_H | ||
#define STATE_MACHINE_H | ||
|
||
#include "cmsis_os.h" | ||
|
||
// TODO: add actual list of states | ||
typedef enum | ||
{ | ||
IDLE_START, | ||
CHARGE_BOOT_CAP, | ||
OFFSET_CALIB, | ||
CLEAR, | ||
START, | ||
SWITCH_OVER, | ||
START_RUN, | ||
RUN, | ||
ANY_STOP, | ||
STOP_IDLE, | ||
FAULT_NOW, | ||
MAX_FUNC_STATES | ||
} state_t; | ||
|
||
extern osThreadId_t sm_director_handle; | ||
extern const osThreadAttr_t sm_director_attributes; | ||
|
||
void vStateMachineDirector(void *pv_params); | ||
|
||
/* Adds a functional state transition to be processed */ | ||
int queue_state(state_t new_state); | ||
|
||
/* Retrieves the current functional state */ | ||
state_t get_state(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "state_machine.h" | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
|
||
#define STATE_TRANS_QUEUE_SIZE 16 | ||
|
||
/* Internal State of Vehicle */ | ||
static state_t cerberus_state; | ||
|
||
/* State Transition Map */ | ||
// TODO: fill out state transition map | ||
static const bool valid_trans_to_from[MAX_FUNC_STATES][MAX_FUNC_STATES] = { | ||
/*BOOT READY DRIVING FAULTED*/ | ||
{true}}; | ||
|
||
osThreadId_t sm_director_handle; | ||
const osThreadAttr_t sm_director_attributes = { | ||
.name = "State Machine Director", | ||
.stack_size = 128 * 4, | ||
.priority = (osPriority_t)osPriorityAboveNormal3, | ||
}; | ||
|
||
static osMessageQueueId_t state_trans_queue; | ||
|
||
int queue_state(state_t new_state) | ||
{ | ||
if (!state_trans_queue) | ||
return 1; | ||
|
||
return osMessageQueuePut(state_trans_queue, &new_state, 0U, 0U); | ||
} | ||
|
||
state_t get_state() | ||
{ | ||
return cerberus_state; | ||
} | ||
|
||
void vStateMachineDirector(void *pv_params) | ||
{ | ||
cerberus_state = IDLE_START; | ||
|
||
state_trans_queue = osMessageQueueNew(STATE_TRANS_QUEUE_SIZE, sizeof(state_t), NULL); | ||
|
||
state_t new_state; | ||
|
||
for (;;) | ||
{ | ||
if (osOK != osMessageQueueGet(state_trans_queue, &new_state, NULL, 50)) | ||
{ | ||
// TODO queue fault, low criticality | ||
continue; | ||
} | ||
|
||
if (!valid_trans_to_from[new_state][cerberus_state]) | ||
{ | ||
// TODO queue fault, low criticality | ||
continue; | ||
} | ||
|
||
// transition state via LUT ? | ||
cerberus_state = new_state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters