SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
FrontValues.cpp
1#include "Front.h"
2#include "MotorControl.h"
3#include "Util.h"
4#include "map"
5
6namespace Front {
7
8static Canbus::Buffer MC0_VOLT_Buffer(ADD_MC0_VOLT);
9static Canbus::Buffer MC1_VOLT_Buffer(ADD_MC1_VOLT);
10static Canbus::Buffer MC0_CURR_Buffer(ADD_MC0_CURR);
11static Canbus::Buffer MC1_CURR_Buffer(ADD_MC1_CURR);
12static Canbus::Buffer MC0_TEMP2_Buffer(ADD_MC0_TEMP2);
13static Canbus::Buffer MC1_TEMP2_Buffer(ADD_MC1_TEMP2);
14static Canbus::Buffer MC0_TEMP3_Buffer(ADD_MC0_TEMP3);
15static Canbus::Buffer MC1_TEMP3_Buffer(ADD_MC1_TEMP3);
16
17static Canbus::Buffer BMS_DATA_Buffer(ADD_BMS_DATA);
18static Canbus::Buffer BMS_BATT_TEMP_Buffer(ADD_BMS_BATT_TEMP);
19static Canbus::Buffer BMS_CURR_LIMIT_Buffer(ADD_BMS_CURR_LIMIT);
20
21static uint8_t BMSSOC() { // Percent
22 return BMS_DATA_Buffer.getUByte(4) / 2; // Byte 4: BMS State of charge buffer
23}
24
25static uint16_t BMSVOLT() {
26 return BMS_DATA_Buffer.getShort(2) / 10; // Byte 2-3: BMS Immediate voltage
27}
28
29static uint16_t BMSAMP() {
30 return BMS_DATA_Buffer.getShort(0); // Byte 0-1: BMS Immediate amperage
31}
32
33static uint8_t BMSTempHigh() {
34 return BMS_BATT_TEMP_Buffer.getByte(4); // Byte 4: BMS Highest Battery Temp
35}
36
37static uint8_t BMSTempLow() {
38 return BMS_BATT_TEMP_Buffer.getByte(5); // Byte 5: BMS Lowest Battery Temp
39}
40
41static uint16_t BMSDischargeCurrentLimit() {
42 return BMS_CURR_LIMIT_Buffer.getShort(0); // Byte 0-1: BMS Discharge Current Limit
43}
44
45static uint16_t BMSChargeCurrentLimit() {
46 return BMS_CURR_LIMIT_Buffer.getShort(2); // Byte 2-3: BMS Charge Current Limit
47}
48
49static uint16_t MC0BoardTemp() {
50 return MC0_TEMP2_Buffer.getShort(0) / 10; // Bytes 0-1: Temperature of Control Board
51}
52
53static uint16_t MC1BoardTemp() {
54 return MC1_TEMP2_Buffer.getShort(0) / 10; // Bytes 0-1: Temperature of Control Board
55}
56
57static uint16_t MC0MotorTemp() {
58 return MC0_TEMP3_Buffer.getShort(4) / 10; // Bytes 4-5: Filtered temperature value from the motor temperature sensor
59}
60
61static uint16_t MC1MotorTemp() {
62 return MC1_TEMP3_Buffer.getShort(4) / 10; // Bytes 4-5: Filtered temperature value from the motor temperature sensor
63}
64
65static uint16_t MC0Voltage() {
66 return MC0_VOLT_Buffer.getShort(0) / 10; // Bytes 0-1: DC BUS MC Voltage
67}
68
69static uint16_t MC1Voltage() {
70 return MC1_VOLT_Buffer.getShort(0) / 10; // Bytes 0-1: DC BUS MC Voltage
71}
72
73static uint16_t MC0Current() {
74 return MC0_CURR_Buffer.getShort(6) / 10; // Bytes 6-7: DC BUS MC Current
75}
76
77static uint16_t MC1Current() {
78 return MC0_CURR_Buffer.getShort(6) / 10; // Bytes 6-7: DC BUS MC Current
79}
80
81static uint32_t MCPowerValue() { // IMPROVE: get power value using three phase values, or find a power value address
82 int MC0_PWR = MC0Voltage() * MC0Current();
83 int MC1_PWR = MC1Voltage() * MC1Current();
84 return (MC0_PWR + MC1_PWR) / 1000; // Sending kilowatts
85}
86
87static struct State::State_t *states[] = {
88 &ECUStates::Initialize_State,
89 &ECUStates::PreCharge_State,
90 &ECUStates::Idle_State,
91 &ECUStates::Charging_State,
92 &ECUStates::Button_State,
93 &ECUStates::Driving_Mode_State,
94 &ECUStates::FaultState,
95};
96
97std::map<uint32_t, struct State::State_t *> stateMap;
98struct State::State_t *currentState;
99
101 Log.i(ID, "Loading State Map");
102 for (auto state : states) {
103 Log.d(ID, "New State", TAG2NUM(state->getID()));
104 Log.d(ID, "State Pointer", (uintptr_t)state);
105 stateMap[TAG2NUM(state->getID())] = state;
106 }
107}
108
110 uint32_t currState = Pins::getCanPinValue(PINS_INTERNAL_STATE);
111 Log.p("state", "Current State", currState, INTERVAL_MED_LOW_PRIORITY);
112 currentState = stateMap[currState]; // returns NULL if not found
113}
114
115void updateStartLight(bool hasBeat) {
116 static bool on = false;
117 if (hasBeat && (currentState == &ECUStates::Idle_State)) {
118 on = !on;
119 } else {
120 on = Pins::getCanPinValue(PINS_INTERNAL_START);
121 }
122 Log("start_light", "Start Light", on, true);
123 Pins::setPinValue(PINS_FRONT_START_LIGHT, on);
124}
125
127 Pins::setInternalValue(PINS_INTERNAL_CHARGE_SIGNAL, currentState == &ECUStates::Idle_State);
128}
129
131 // Motor controllers
132 Log.p("mc0_dc_v", "MC0 DC BUS Voltage", MC0Voltage(), INTERVAL_LOW_PRIORITY);
133 Log.p("mc1_dc_v", "MC1 DC BUS Voltage", MC1Voltage(), INTERVAL_LOW_PRIORITY);
134 Log.p("mc0_dc_i", "MC0 DC BUS Current", MC0Current(), INTERVAL_LOW_PRIORITY);
135 Log.p("mc1_dc_i", "MC1 DC BUS Current", MC1Current(), INTERVAL_LOW_PRIORITY);
136 Log.p("mc0_brd_tmp", "MC0 Board Temp", MC0BoardTemp(), INTERVAL_LOW_PRIORITY);
137 Log.p("mc1_brd_tmp", "MC1 Board Temp", MC1BoardTemp(), INTERVAL_LOW_PRIORITY);
138 Log.p("mc0_mtr_tmp", "MC0 Motor Temp", MC0MotorTemp(), INTERVAL_LOW_PRIORITY);
139 Log.p("mc1_mtr_tmp", "MC1 Motor Temp", MC1MotorTemp(), INTERVAL_LOW_PRIORITY);
140 Log.p("mc_curr_pwr", "MC Current Power", MCPowerValue(), INTERVAL_LOW_PRIORITY);
141
142 // BMS
143 Log.p("bms_soc", "BMS State Of Charge", BMSSOC(), INTERVAL_LOW_PRIORITY);
144 Log.p("bms_v", "BMS Immediate Voltage", BMSVOLT(), INTERVAL_LOW_PRIORITY);
145 Log.p("bms_avg_i", "BMS Pack Average Current", BMSAMP(), INTERVAL_LOW_PRIORITY);
146 Log.p("bms_h_tmp", "BMS Pack Highest Temp", BMSTempHigh(), INTERVAL_LOW_PRIORITY);
147 Log.p("bms_l_tmp", "BMS Pack Lowest Temp", BMSTempLow(), INTERVAL_LOW_PRIORITY);
148 Log.p("bms_dis_i_lim", "BMS Discharge current limit", BMSDischargeCurrentLimit(), INTERVAL_LOW_PRIORITY);
149 Log.p("bms_chr_i_lim", "BMS Charge current limit", BMSChargeCurrentLimit(), INTERVAL_LOW_PRIORITY);
150
151 // General
152 Log.p("fault", "Fault State", Pins::getCanPinValue(PINS_INTERNAL_GEN_FAULT), INTERVAL_LOW_PRIORITY);
153}
154
155static double lastBrake = 0.0, lastSteer = 0.0, lastPedal0 = 0.0, lastPedal1 = 0.0;
156
158 Log.p("mtr_spd", "Current Motor Speed", MC::motorSpeed(), true);
159 Log.p("mtr_0_spd", "Motor 0 Speed", MC::motorSpeed(0), true);
160 Log.p("mtr_1_spd", "Motor 1 Speed", MC::motorSpeed(1), true);
161
162 int pedal0, pedal1;
163 Log.p("brake", "Brake", lastBrake = EMAvg(lastBrake, Pins::getPinValue(PINS_FRONT_BRAKE), 4), true);
164 Log.p("steer", "Steering", lastSteer = EMAvg(lastSteer, Pins::getPinValue(PINS_FRONT_STEER), 4), true);
165 Log.p("pdl_0", "Pedal 0", (pedal0 = (lastPedal0 = EMAvg(lastPedal0, Pins::getPinValue(PINS_FRONT_PEDAL0), 4))), true);
166 Log.p("pdl_1", "Pedal 1", (pedal1 = (lastPedal1 = EMAvg(lastPedal1, Pins::getPinValue(PINS_FRONT_PEDAL1), 4))), true);
167 Log.p("pdl_avg", "Pedal AVG", (pedal0 + pedal1) / 2, true);
168}
169
170} // namespace Front
uint32_t TAG2NUM(LOG_TAG tagValue)
Return the final numbervalue of a LOG_TAG.
Logging::Log_t Log
The global logging object.
Motor Controller module.
double EMAvg(double lastVal, double newVal, int memCount)
Exponential moving average.
Definition Util.cpp:14
Various utility functions.
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 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.
int32_t motorSpeed(int motor=-1)
Get the avg motorspeed.
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.
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 d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void i(LOG_TAG TAG, LOG_MSG message)
Log a string using an info tag.
void p(LOG_TAG name, LOG_MSG prettyName, const uint32_t number, int mediate=false)
Post a monitored value.
The parent state structure to extend from to create more states.
Definition State.h:70