Skip to content

Commit 3b70f80

Browse files
ManasijaBhargavankhoulihan27dchansen06horizon-abgithub-actions[bot]
authored
Comment Finite State Machine & Logical Control Loop (#136)
# Finite State Machine & Logical Control Loop ## Problem and Scope We need the car to work ## Description It makes the car work Adds a comment layout for what the ideal state machine loop looks like, what it calls, etc. ## Gotchas and Limitations Comments, code still needs to be written and validated ## Testing - [x] HOOTL testing - [ ] HITL testing - [x] Human tested ### Testing Details None ## Larger Impact Key to later ECU work ## Additional Context and Ticket Works for #151 and with #158 --------- Signed-off-by: Bailey Say <[email protected]> Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Daniel Hansen <[email protected]> Co-authored-by: Kai <[email protected]> Co-authored-by: ManasijaBhargavan <[email protected]> Co-authored-by: Daniel Hansen <[email protected]> Co-authored-by: Bailey Say <[email protected]> Co-authored-by: Daniel Hansen <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Gabe Millikan <[email protected]> Co-authored-by: Th3will <[email protected]> Co-authored-by: Thomas Xu <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: amysaffer <[email protected]> Co-authored-by: Amy Saffer <[email protected]> Co-authored-by: Casey Zwicker <[email protected]> Co-authored-by: Ng0710 <[email protected]> Co-authored-by: Thomas Xu <[email protected]> Co-authored-by: khoulihan27 <[email protected]>
1 parent 06a1adc commit 3b70f80

File tree

6 files changed

+219
-8
lines changed

6 files changed

+219
-8
lines changed

ECU/Application/Inc/StateData.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,31 @@
1919
* logic to access and modify the ECU's operational data.
2020
*/
2121
typedef struct ECU_StateData {
22+
// DON'T TOUCH YET
2223
GR_OLD_ECU_STATUS_1_MSG ecuStatus1;
2324
GR_OLD_ECU_STATUS_2_MSG ecuStatus2;
2425
GR_OLD_ECU_STATUS_3_MSG ecuStatus3;
26+
2527
int32_t dischargeStartMillis;
2628
uint32_t lastECUStatusMsgTick;
2729
uint32_t lastTSSIFlash;
2830
int32_t last_drive_active_control_ms;
2931
float min_amk_heat_cap_throttle_percent;
3032
uint16_t driving_heat_capacity_1;
3133
uint16_t driving_heat_capacity_2;
34+
uint16_t APPS1_Signal;
35+
uint16_t APPS2_Signal;
36+
uint16_t Brake_R_Signal;
37+
uint16_t Brake_F_Signal;
3238
uint8_t acu_error_warning_bits;
3339
uint8_t inverter_fault_map;
3440
bool bse_apps_violation;
3541
bool ts_active_button_engaged;
3642
bool rtd_button_engaged;
3743
} ECU_StateData; // FIXME Add comments to each data field with descriptions and
3844
// rules (eg -1 = invalid?, etc)
45+
// Will also need to add information from ADC into this struct
46+
// --- such as the APPS and Brake signals after doing smoothing
47+
// and whatnot to get the values sane
3948

4049
#endif

ECU/Application/Inc/StateTicks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "StateData.h"
22
#include "StateMachine.h"
33

4-
#ifndef _STATEMACHINE_H_
5-
#define _STATEMACHINE_H_
4+
#ifndef _STATE_TICKS_H_
5+
#define _STATE_TICKS_H_
66

77
/**
88
* @brief Tick function for the ECU state machine.

ECU/Application/Inc/StateUtils.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdbool.h>
2+
#include <stdint.h>
3+
4+
#include "StateData.h"
5+
6+
#ifndef _STATE_UTILS_H_
7+
#define _STATE_UTILS_H_
8+
9+
// Constants
10+
#define BRAKE_F_MIN 0 // TODO: need to be determined FIXME: Rename better
11+
#define BRAKE_F_MAX 4095 // TODO: need to be determined FIXME: Rename better
12+
#define BRAKE_R_MIN 0 // TODO: need to be determined FIXME: Rename better
13+
#define BRAKE_R_MAX 4095 // TODO: need to be determined FIXME: Rename better
14+
#define THROTTLE_MIN_1 0 // TODO: need to be determined
15+
#define THROTTLE_MAX_1 4095 // TODO: need to be determined
16+
#define THROTTLE_MIN_2 0 // TODO: need to be determined
17+
#define THROTTLE_MAX_2 4095 // TODO: need to be determined
18+
#define BSE_DEADZONE 0.05f
19+
#define APPS_PROPORTION 2.0f // TODO: Need to be experimentally determined
20+
#define APPS_OFFSET 250.0f // TODO: Need to be experimentally determined
21+
22+
// Checks stateData for critical errors
23+
bool CriticalError(const ECU_StateData *stateData);
24+
bool CommunicationError(const ECU_StateData *stateData);
25+
bool APPS_BSE_Violation(const ECU_StateData *stateData);
26+
bool PressingBrake(const ECU_StateData *stateData);
27+
float CalcBrakePercent(const ECU_StateData *stateData);
28+
float CalcPedalTravel(const ECU_StateData *stateData);
29+
30+
#endif

ECU/Application/Src/StateTicks.c

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Logomatic.h"
44
#include "StateData.h"
55
#include "StateMachine.h"
6+
#include "StateUtils.h"
67
#include "Unused.h"
78

89
/**
@@ -17,8 +18,7 @@ ECU_StateData stateLump = {0};
1718

1819
void ECU_State_Tick(void)
1920
{
20-
LOGOMATIC("ECU_State_Tick: Current State: %d\n",
21-
stateLump.ecuStatus1.ecu_status);
21+
LOGOMATIC("ECU Current State: %d\n", stateLump.ecuStatus1.ecu_status);
2222

2323
switch (stateLump.ecuStatus1.ecu_status) {
2424
case GR_GLV_OFF:
@@ -40,46 +40,133 @@ void ECU_State_Tick(void)
4040
ECU_Tractive_System_Discharge(&stateLump);
4141
break;
4242
default:
43-
LOGOMATIC("ECU_State_Tick: Unknown State %d\n",
43+
LOGOMATIC("ECU Current State Unknown: %d\n",
4444
stateLump.ecuStatus1.ecu_status);
45-
LOGOMATIC("ECU_State_Tick: Resetting to GR_GLV_ON\n");
45+
LOGOMATIC("ECU: Resetting to GLV On\n");
4646
stateLump.ecuStatus1.ecu_status = GR_GLV_ON;
4747
break;
4848
}
4949
}
5050

51+
/*
52+
53+
TODO: implement state functionality when loading INTO the state, not just
54+
transitioning state
55+
56+
*/
57+
5158
void ECU_GLV_Off(ECU_StateData *stateData)
5259
{
5360
UNUSED(stateData);
5461
// TODO Implement functionality
62+
// ERROR --> GLV_OFF should never be reached
5563
}
5664

5765
void ECU_GLV_On(ECU_StateData *stateData)
5866
{
5967
UNUSED(stateData);
68+
/*
69+
if(stateData->TractiveSystemVoltage >= 60){ // should never happen but
70+
has to be accounted for stateData->currentState = GR_TS_DISCHARGE;
71+
emit an error
72+
break;
73+
}
74+
*/
75+
6076
// TODO Implement functionality
77+
if (stateData->ts_active_button_engaged) {
78+
stateData->ecuStatus1.ecu_status = GR_PRECHARGE_ENGAGED;
79+
}
6180
}
6281

6382
void ECU_Precharge_Engaged(ECU_StateData *stateData)
6483
{
6584
UNUSED(stateData);
85+
if (stateData->ecuStatus2.ts_voltage > 60) {
86+
// Go to TS discharge
87+
stateData->ecuStatus1.ecu_status = GR_TS_DISCHARGE;
88+
// Emit an error
89+
return;
90+
}
6691
// TODO Implement functionality
92+
/*if(not TS Active || Communication Error (CAN)){
93+
stateData->currentState = GR_TS_DISCHARGE
94+
break;
95+
}*/
96+
/*if(1 Isolation relay close && second isolation relay close){ --> CAN!
97+
stateData->currentState = GR_PRECHARGE_COMPLETE
98+
}*/
6799
}
68100

69101
void ECU_Precharge_Complete(ECU_StateData *stateData)
70102
{
71103
UNUSED(stateData);
72104
// TODO Implement functionality
105+
/*
106+
On but idle
107+
108+
If Tractive System (TS) active/Critical Error --> Tractive
109+
System Discharge If Brake & RTD (Ready to Drive) --> Drive Active
110+
*/
111+
/*
112+
if (TS pressed or critical error) {
113+
stateData->currentState = GR_TS_DISCHARGE
114+
emit error
115+
break;
116+
}
117+
*/
118+
/*
119+
if(PressingBrake(stateData) && stateData->RTD){
120+
stateData->currentState = GR_DRIVE_ACTIVE;
121+
}
122+
*/
123+
124+
// Pseudocode
73125
}
74126

75127
void ECU_Drive_Active(ECU_StateData *stateData)
76128
{
77129
UNUSED(stateData);
78130
// TODO Implement functionality
131+
/*
132+
If APPS/BSE Violation --> Don't drive until resolved (no state
133+
change) If Tractive System (TS) active/Critical Error -->
134+
Tractive System Discharge
135+
--> pressed again
136+
If RTD (Ready to Drive) --> Precharge Complete
137+
*/
138+
139+
// Pseudocode
140+
/*
141+
if (!TSActive || criticalError(stateData)) {
142+
stateData->currentState = GR_TS_DISCHARGE
143+
emit an error
144+
break
145+
}
146+
if (!RTD) {
147+
stateData->currentState = GR_PRECHARGE_COMPLETE
148+
emit a warning if not moving
149+
break
150+
}
151+
and then we drive the car
152+
- calcPedalTravel func :p
153+
- make tuna-ble function
154+
*/
79155
}
80156

81157
void ECU_Tractive_System_Discharge(ECU_StateData *stateData)
82158
{
83159
UNUSED(stateData);
84-
// TODO Implement functionality
160+
// TODO Implement functionality of state itself
161+
/*
162+
Discharge the tractive system to below 60 volts
163+
If TS voltage < 60 --> stateData->GLV_ON
164+
*/
165+
if (stateData->ecuStatus2.ts_voltage < 60) {
166+
stateData->ecuStatus1.ecu_status = GR_GLV_ON;
167+
}
168+
/*
169+
If TS fails to discharge over time then stay and emit a warning,
170+
see #129
171+
*/
85172
}

ECU/Application/Src/StateUtils.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "StateUtils.h"
2+
3+
#include <math.h>
4+
#include <stdbool.h>
5+
6+
#include "StateData.h"
7+
#include "Unused.h"
8+
9+
void setSoftwareLatch(bool close)
10+
{
11+
UNUSED(close);
12+
// TODO Implement functionality
13+
// LOGOMATIC("Setting software latch to %d\n", close);
14+
/*
15+
if (close && !HAL_GPIO_ReadPin(SOFTWARE_OK_CONTROL_GPIO_Port,
16+
SOFTWARE_OK_CONTROL_Pin)) // Avoid writing pins that are already
17+
written too
18+
{
19+
HAL_GPIO_WritePin(SOFTWARE_OK_CONTROL_GPIO_Port,
20+
SOFTWARE_OK_CONTROL_Pin, GPIO_PIN_SET);
21+
}
22+
else if (!close && HAL_GPIO_ReadPin(SOFTWARE_OK_CONTROL_GPIO_Port,
23+
SOFTWARE_OK_CONTROL_Pin))
24+
{
25+
HAL_GPIO_WritePin(SOFTWARE_OK_CONTROL_GPIO_Port,
26+
SOFTWARE_OK_CONTROL_Pin, GPIO_PIN_RESET);
27+
}
28+
*/
29+
}
30+
31+
bool CriticalError(const ECU_StateData *stateData)
32+
{
33+
if (stateData->ecuStatus1.max_cell_temp > 60) {
34+
return true;
35+
}
36+
if (stateData->ecuStatus2.ts_voltage > 600) {
37+
return true;
38+
}
39+
return false;
40+
}
41+
42+
bool CommunicationError(const ECU_StateData *stateData)
43+
{
44+
UNUSED(stateData);
45+
// TODO: implement COMMS errors
46+
return false;
47+
}
48+
49+
bool APPS_BSE_Violation(const ECU_StateData *stateData)
50+
{
51+
// Checks 2 * APPS_1 is within 10% of APPS_2 and break + throttle at the
52+
// same time
53+
return fabs(stateData->APPS2_Signal -
54+
stateData->APPS1_Signal * APPS_PROPORTION - APPS_OFFSET) >
55+
stateData->APPS2_Signal * 0.1f ||
56+
(PressingBrake(stateData) &&
57+
CalcPedalTravel(stateData) >= 0.25f);
58+
}
59+
60+
bool PressingBrake(const ECU_StateData *stateData)
61+
{
62+
return (stateData->Brake_F_Signal - BRAKE_F_MIN >
63+
BSE_DEADZONE * (BRAKE_F_MAX - BRAKE_F_MIN)) &&
64+
(stateData->Brake_R_Signal - BRAKE_R_MIN >
65+
BSE_DEADZONE * (BRAKE_R_MAX - BRAKE_R_MIN));
66+
// Ideally TCM receives values of 0 after this is no longer called xD.
67+
}
68+
69+
float CalcBrakePercent(
70+
const ECU_StateData *stateData) // THIS IS NOT ACTUALLY BRAKE TRAVEL,
71+
// PRESSURE SENSORS CAPTURE BRAKE TRAVEL
72+
{
73+
return (float)(stateData->Brake_F_Signal + stateData->Brake_R_Signal -
74+
BRAKE_R_MIN - BRAKE_F_MIN) /
75+
(BRAKE_F_MAX - BRAKE_F_MIN + BRAKE_R_MAX - BRAKE_R_MIN);
76+
}
77+
78+
float CalcPedalTravel(const ECU_StateData *stateData)
79+
{
80+
return (float)(stateData->APPS1_Signal + stateData->APPS2_Signal -
81+
THROTTLE_MIN_2 - THROTTLE_MIN_1) /
82+
(THROTTLE_MAX_1 + THROTTLE_MAX_2 - THROTTLE_MIN_1 -
83+
THROTTLE_MIN_2);
84+
}

ECU/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ target_sources(
6868
Core/Src/system_stm32g4xx.c
6969
Core/Src/usart.c
7070
# Application
71-
Application/Src/StateTicks.c
7271
Application/Src/CANdler.c
72+
Application/Src/StateTicks.c
73+
Application/Src/StateUtils.c
7374
)
7475

7576
target_link_libraries(

0 commit comments

Comments
 (0)