1#include "ECUStates.hpp"
13static bool FaultCheck() {
21static void updateFaultLights() {
22 static int bms, imd, bms_l, imd_l = 0;
25 Log.
d(
"FaultLights",
"Updating Lights");
37 Log.
i(ID,
"Teensy 3.6 SAE BACK ECU Initalizing");
38 Log.
i(ID,
"Setup canbus");
40 Log.
i(ID,
"Initialize pins");
42 Log.
i(ID,
"Waiting for sync");
44 Canbus::sendData(ADD_MC0_CTRL);
45 Canbus::sendData(ADD_MC1_CTRL);
58 Log.
d(ID,
"Checking for Inital fault");
62 Log.
e(ID,
"Inital fault check tripped");
63 return &ECUStates::FaultState;
67 Log.
i(ID,
"Waiting for shutdown signal");
69 elapsedMillis shutdownBounce;
73 if (shutdownBounce > 50)
80 Log.
i(ID,
"Shutdown signal received");
82 Log.
d(ID,
"Finshed Setup");
83 return &ECUStates::PreCharge_State;
87 Log.
w(ID,
"Opening Air1, Air2 and Precharge Relay");
91 Log.
e(ID,
"Precharge Faulted during precharge");
92 return &ECUStates::FaultState;
95bool ECUStates::PreCharge_State::voltageCheck() {
96 int16_t BMSVolt = BMS_DATA_Buffer.getShort(2) / 10;
97 int16_t MC0Volt = MC0_VOLT_Buffer.getShort(0) / 10;
98 int16_t MC1Volt = MC1_VOLT_Buffer.getShort(0) / 10;
100 return 0.65 * BMSVolt <= (MC0Volt + MC1Volt) / 2;
104 Log.
i(ID,
"Precharge running");
107 Log.
e(ID,
"Precharge Faulted before precharge");
108 return &ECUStates::FaultState;
112 Log.
w(ID,
"Closing Air2 and Precharge Relay and opening Air1");
118 return PreCharFault();
121 elapsedMillis timeElapsed;
123 Log.
d(ID,
"Running precharge loop");
125 while (timeElapsed <= 10000) {
126 if (timeElapsed >= 1000 && voltageCheck()) {
127 Log.
w(ID,
"Opening precharge relay");
129 Log.
i(ID,
"Precharge Finished, closing Air1");
131 return &ECUStates::Idle_State;
135 Log.
e(ID,
"Precharge timed out");
136 return PreCharFault();
140 Log.
i(ID,
"Waiting for Button not to be pressed");
144 Log.
i(ID,
"Waiting for Button or Charging Press");
148 elapsedMillis waiting;
152 Log.
i(ID,
"Button Pressed");
154 return &ECUStates::Button_State;
156 Log.
i(ID,
"Charging Pressed");
158 return &ECUStates::Charging_State;
159 }
else if (FaultCheck()) {
160 Log.
w(ID,
"Fault in idle state");
164 return &ECUStates::FaultState;
169 Log.
i(ID,
"Charging on");
171 elapsedMillis voltLogNotify;
177 Log.
e(ID,
"Charging faulted, turning off");
178 return &ECUStates::FaultState;
183 Log.
i(ID,
"Charging turning off");
185 return &ECUStates::Idle_State;
189 Log.
i(ID,
"Waiting for Button not to be pressed");
192 Log.
i(ID,
"Playing sound");
196 elapsedMillis soundTimer;
198 while (soundTimer < 3000) {
200 Log.
e(ID,
"Failed to play sound");
202 return &ECUStates::FaultState;
207 Log.
i(ID,
"Playing sound finished");
209 return &ECUStates::Driving_Mode_State;
212void ECUStates::Driving_Mode_State::carCooling(
bool enable) {
222State::State_t *ECUStates::Driving_Mode_State::DrivingModeFault(
void) {
223 Log.
i(ID,
"Fault happened in driving state");
226 Log.
i(ID,
"Starting MC heartbeat");
228 return &ECUStates::FaultState;
233 Log.
i(ID,
"Driving mode on");
234 Log.
i(ID,
"Cooling on");
237 elapsedMillis controlDelay;
239 Log.
i(ID,
"Stopping MC heartbeat");
242 Log.
d(ID,
"Sending Fault reset to MCs complete");
247 Log.
d(ID,
"Entering drive loop");
249 if (controlDelay > 20) {
252 static bool runReverse =
false;
260 return DrivingModeFault();
263#if ECU_TESTING != BACK_ECU
264 if (((MC0_VOLT_Buffer.getShort(0) / 10) + (MC1_VOLT_Buffer.getShort(0) / 10)) / 2 < 90) {
265 Log.
e(ID,
"'HVD Fault' MC voltage < 90");
266 return DrivingModeFault();
276 int pAVG = (pedal0 + pedal1) / 2;
280 Log.
e(ID,
"Pedal value offset > 10%");
281 return DrivingModeFault();
305 Log.
w(ID,
"Going back to Idle state");
312 Log.
i(ID,
"Starting MC heartbeat");
317 Log.
i(ID,
"Driving mode off");
318 return &ECUStates::Idle_State;
323 Canbus::enableInterrupts(
false);
325 Log.
w(ID,
"Opening Air1, Air2 and Precharge Relay");
330 Log.
w(ID,
"Resetting pins");
333 if (getLastState() == &ECUStates::PreCharge_State) {
335 Log.
e(ID,
"Precharge fault");
342 Log.
e(ID,
"FAULT STATE");
347 return &ECUStates::Initialize_State;
352 static elapsedMillis timeElapsed;
354 if (timeElapsed >= 2000) {
355 timeElapsed = timeElapsed - 2000;
357 Log(
"FAKE ID",
"A7 Pin Value:");
359 Log(ID,
"", 0xDEADBEEF);
360 Log(ID,
"Notify code: ", getNotify());
363 return &ECUStates::Bounce;
368 Log.
i(ID,
"Bounce!");
371 return getLastState();
Interpretation of Aero subteam code.
Configure global build properties.
Echo can messages with a delay.
Checks for defined faults from canbus addresses or pins.
Make one ECU tell the other it is alive.
Special logging functionality.
Logging::Log_t Log
The global logging object.
This module allows for the monitoring and modification of each GPIO pin on an ECU.
#define CONF_BRAKE_MIN
The brake input range to map as doubles.
#define PINS_ANALOG_HIGH
The high analog value, will not force an analog output to lock onto a high state, as PINS_ANALOG_MAX ...
#define PINS_ANALOG_MAX
The maximum analog value, given the current PINS_ANALOG_RES.
Various utility functions.
void run(int breakPressure, int steeringAngle)
Run Aero servo logic, given raw values.
void setup(void)
Attach analog pins to their servo objects.
int getServoValue()
Get the current servo position value.
void setup()
Initialize on receiving ECU to echo messages.
bool softFault(void)
Check if any non-serious fault has occurred.
bool hardFault(void)
Checks if any serious fault has occurred.
bool anyFault(void)
Checks both hardFault and softFault.
void logFault(void)
Interprets and logs the last fault that was checked using the Log library.
void addCallback(beatFunc func)
Add a callback to be run at each haertbeat.
void beginBeating()
begin sending a beat signal at a set interval
void setTorque(int pedal, int brake, int steer)
Calculate and set the torque of both MCs.
void setDirection(bool runForward)
Set the direction of the motors.
int getLastTorqueValue(bool mc0)
Get the last torque percent value sent to a MC.
void enableMotorBeating(bool enable)
Start MC heartbeat function, establishing a connection with the MCs.
void clearFaults(void)
Clear MC faults by sending a clear fault command.
void setup(void)
Initialize MC heartbeat function and clear faults, if any.
void setup(void)
Setup a listener for going into mirror mode, dependent on which ECU is compiled.
void setPinValue(uint8_t GPIO_Pin, int value)
Set the pin value of a predefined pin.
int getCanPinValue(uint8_t CAN_GPIO_Pin)
Get the pin value of a predefined canbus pin.
void initialize(void)
Initialize all predefined pins.
int getPinValue(uint8_t GPIO_Pin)
Get the pin value of a predefined pin.
void setInternalValue(uint8_t Internal_Pin, int value)
Set the value of an internal pin.
void resetPhysicalPins()
Resets physical pins to their inital state.
void d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.
void e(LOG_TAG TAG, LOG_MSG message)
Log a string using an error tag.
void i(LOG_TAG TAG, LOG_MSG message)
Log a string using an info tag.
The parent state structure to extend from to create more states.