SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
src/front.cpp
1#include "Front.h"
2#include "ECUGlobalConfig.h"
3#include "Echo.h"
4#include "Heartbeat.h"
5#include "Mirror.h"
6#include "SerialCommand.h"
7#include "SerialVar.h"
8#include "unordered_map"
9#if ECU_TESTING == FRONT_ECU
10#include "Test.h"
11#endif
12
13namespace Front {
14
15LOG_TAG ID = "Front Teensy";
16
17static elapsedMillis timeElapsedHigh;
18static elapsedMillis timeElapsedMidHigh;
19static elapsedMillis timeElapsedMidLow;
20static elapsedMillis timeElapsedLow;
21
23
24static void pushCanMessage() {
25 uint32_t address;
26 char buffer[8];
27 size_t c = 0;
28 c += Serial.readBytes((char *)&address, 4);
29 c += Serial.readBytes(buffer, 8);
30 if (c != 12) {
31 Log.w(ID, "Did not read correct number of Bytes, not pushing message", 12);
32 return;
33 }
34 Log.d(ID, "Pushing Message", address);
35 Canbus::sendData(address, (uint8_t *)buffer);
36}
37
38static void toggleCanbusSniffer() {
39 static bool enabled = false;
40 Canbus::enableCanbusSniffer((enabled = !enabled));
41}
42
43static void sendEchoMessage() {
44 uint32_t delay;
45 uint32_t address;
46 char buffer[8];
47 size_t c = 0;
48 c += Serial.readBytes((char *)&delay, 4);
49 c += Serial.readBytes((char *)&address, 4);
50 c += Serial.readBytes(buffer, 8);
51 if (c != 20) {
52 Log.w(ID, "Did not read correct number of Bytes, not pushing message", 20);
53 return;
54 }
55 Log.d(ID, "Pushing Message", address);
56 Echo::echo(delay, address, (uint8_t *)buffer);
57}
58
59static void toggleMotorDirection() {
60 static bool reverse = false;
61 reverse = !reverse;
62 Pins::setInternalValue(PINS_INTERNAL_REVERSE, reverse);
63}
64
65void blinkStart() {
66 Pins::setPinValue(PINS_FRONT_START_LIGHT, 1);
67 delay(500);
68 Pins::setPinValue(PINS_FRONT_START_LIGHT, 0);
69 delay(500);
70}
71
72void run() {
73 Log.i(ID, "Teensy 3.6 SAE FRONT ECU Initalizing");
74
75 Log.i(ID, "Setting up Canbus");
76 Canbus::setup(); // allocate and organize addresses
77 Log.i(ID, "Initalizing Pins");
78 Pins::initialize(); // setup predefined pins
79#ifndef CONF_LOGGING_ASCII_DEBUG
80 Log.i(ID, "Enabling Logging relay");
81 Logging::enableCanbusRelay(); // Allow logging though canbus
82#endif
84
85 Log.i(ID, "Setting commands");
88 Cmd::setCommand(COMMAND_TOGGLE_CANBUS_SNIFF, toggleCanbusSniffer);
89 Cmd::setCommand(COMMAND_SEND_ECHO, sendEchoMessage);
90 Cmd::setCommand(COMMAND_TOGGLE_REVERSE, toggleMotorDirection);
94
95#ifdef CONF_ECU_DEBUG
97#endif
98
99 TVAggression = 1.8f;
100 static bool hasBeat = false;
101
102 Log.d(ID, "Delaying 2 sec");
103 Serial.flush();
104 blinkStart();
105 Pins::setInternalValue(PINS_INTERNAL_SYNC, 1);
106 blinkStart();
107 Serial.flush();
108
109#if ECU_TESTING == FRONT_ECU
110 full_front_test();
111#endif
112
113 while (!Heartbeat::checkBeat()) {
114 delay(500);
116 }
117
118 while (true) {
119 if (timeElapsedHigh >= INTERVAL_HIGH_PRIORITY) { // High priority updates
120 timeElapsedHigh = 0;
122
124
126 hasBeat = Heartbeat::checkBeat();
127 }
128 if (timeElapsedLow >= INTERVAL_MED_LOW_PRIORITY) { // Low priority updates
129 timeElapsedLow = 0;
130
131 updateStartLight(hasBeat);
132
133 if (Fault::anyFault()) {
135 }
136
137 Pins::setInternalValue(PINS_INTERNAL_TVAGG, TVAggression * 10000);
138
139 Pins::setPinValue(PINS_FRONT_BMS_LIGHT, Pins::getCanPinValue(PINS_INTERNAL_BMS_FAULT));
140 Pins::setPinValue(PINS_FRONT_IMD_LIGHT, Pins::getCanPinValue(PINS_INTERNAL_IMD_FAULT));
141
143 }
144 }
145}
146
147} // namespace Front
Configure global build properties.
Echo can messages with a delay.
Make one ECU tell the other it is alive.
const char * LOG_TAG
Type definition of logging tags This typedef is necessary to allow for easier manipulation of code by...
Definition Log.h:48
Logging::Log_t Log
The global logging object.
This module allows for the monitoring and modification of each GPIO pin on an ECU.
#define COMMAND_SEND_ECHO
Command to listen for a can message to echo, for Echo.h.
#define COMMAND_UPDATE_SERIALVAR
Command to update a serial variable.
#define COMMAND_TOGGLE_REVERSE
Command to enable reverse motors, for MotorControl.h.
#define COMMAND_TOGGLE_CANBUS_SNIFF
Command used to enable the ASCII canbus sniffer.
#define COMMAND_SEND_CANBUS_MESSAGE
Command used to listen for a canbus message to send.
#define COMMAND_PRINT_LOOKUP
Command to send the compressed lookup table over serial.
#define COMMAND_ENABLE_CHARGING
Command used to enable charging.
SerialCommand functionality.
Serial Variable Module.
#define SERIALVAR_TORQUE_VECTORING_AGGRESSION
ID for Torque vectoring aggression serial var.
Basic functions used for testing.
Template of a Serial Variable.
Definition SerialVar.h:48
int receiveCommand(void)
receive any command from serial by matching incoming bytes to a callback
void setCommand(uint8_t command, CommandCallback callback)
attach a single callback to a byte value that will be received over serial
void echo(uint32_t delay, const uint32_t address, uint8_t buf[8])
Send a can message to the back ECU from the front to then echo the same message at a set delay.
bool anyFault(void)
Checks both hardFault and softFault.
void logFault(void)
Interprets and logs the last fault that was checked using the Log library.
Name space used solely for front ECU logic.
Definition src/front.cpp:13
void setChargeSignal()
Enables the charging signal dependent on the current state.
void run()
Runs front ECU code.
Definition src/front.cpp:72
void lowPriorityValues()
Outputs values that are of lower priority.
void highPriorityValues()
Outputs values that are of higher priority.
void updateStartLight(bool hasBeat)
Update the startlight.
void loadStateMap()
Load values that ID each unique state.
void updateCurrentState()
Update and outputs the current state.
void beginReceiving()
Set callback to receive beats over CAN.
int checkBeat()
Poll if a beat has been received.
void enableCanbusRelay()
If a set address is received through canbus, the data will be pushed to a buffer to be printed.
void printLookup()
Print the ZLib compressed string of the current lookup table to serial.
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.
void setInternalValue(uint8_t Internal_Pin, int value)
Set the value of an internal pin.
void receiveSerialVar()
Receive a variable over serial.
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 i(LOG_TAG TAG, LOG_MSG message)
Log a string using an info tag.