-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispatch_Controller.cpp
241 lines (197 loc) · 5.95 KB
/
Dispatch_Controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "Dispatch_Controller.h"
//Magic timing library stuff
#include <SoftTimer.h>
#include <DelayRun.h>
#include <Debouncer.h>
#include "Can_Controller.h"
#include "Rtd_Controller.h"
#include "Store_Controller.h"
#include "Logger.h"
const int BLINK_LIGHT = 0;
const int ENABLE_LIGHT = 1;
const int SHUTDOWN_LIGHT = 2;
const uint16_t PRECHARGE_DELAY = 15000;
Dispatch_Controller::Dispatch_Controller()
: rtd_handler(Rtd_Handler()),
can_node_handler(Can_Node_Handler()),
bms_handler(Bms_Handler()),
motor_handler(Motor_Handler()),
current_sense_handler(Current_Sense_Handler()),
begun(false),
enabled(false)
{
}
// Handle messages as fast as possible
void dispatchPointer(Task*) {
Dispatcher().dispatch();
}
Task stepTask(0, dispatchPointer);
// Check for faults at 10Hz
void checkFaults(Task*) {
Dispatcher().handleFaultPins();
}
Task checkFaultsTask(100, checkFaults);
// Request message from both motors
void requestMotorHeartbeat(Task*) {
Dispatcher().requestMotorHeartbeat();
}
Task heartbeatTask(100, requestMotorHeartbeat);
bool requestPermanentUpdatesLeft(Task*) {
Dispatcher().requestLeftMotorUpdates();
return false;
}
DelayRun requestLeftMotorUpdatesTask(50, requestPermanentUpdatesLeft);
bool requestPermanentUpdatesRight(Task*) {
Dispatcher().requestRightMotorUpdates();
return false;
}
DelayRun requestRightMotorUpdatesTask(100, requestPermanentUpdatesRight);
bool blinkDashLight(Task*) {
if (!Dispatcher().isEnabled()) {
// Only send light if not enabled
Frame dashMessage = { .id=VCU_ID, .body={BLINK_LIGHT}, .len=1};
CAN().write(dashMessage);
}
return false;
}
// Wait to blink light until precharge has finished
DelayRun requestBlinkDashLight(PRECHARGE_DELAY, blinkDashLight);
void Dispatch_Controller::requestMotorHeartbeat() {
bool bothMcOn =
Store().readMotorResponse(Store().RightMotor) &&
Store().readMotorResponse(Store().LeftMotor);
if (!bothMcOn) {
motor_handler.requestHeartbeat();
}
else {
SoftTimer.remove(&heartbeatTask);
SoftTimer.add(&requestLeftMotorUpdatesTask);
SoftTimer.add(&requestRightMotorUpdatesTask);
}
}
void Dispatch_Controller::requestLeftMotorUpdates() {
motor_handler.requestPermanentUpdates(LEFT_MOTOR_REQUEST_ID);
}
void Dispatch_Controller::requestRightMotorUpdates() {
motor_handler.requestPermanentUpdates(RIGHT_MOTOR_REQUEST_ID);
}
void Dispatch_Controller::begin() {
//Make idempotent
if(begun) {
return;
}
begun = true;
// Initialize controllers
CAN().begin();
RTD().begin();
// Initialize handlers
rtd_handler.begin();
can_node_handler.begin();
bms_handler.begin();
motor_handler.begin();
initializeFaultPins();
// Start event loop
SoftTimer.add(&stepTask);
SoftTimer.add(&checkFaultsTask);
// Start MC requests
SoftTimer.add(&heartbeatTask);
Computer().logOne("vehicle_power_on");
Onboard().logOne("vehicle_power_on");
Xbee().logOne("vehicle_power_on");
}
// Must define instance prior to use
Dispatch_Controller* Dispatch_Controller::instance = NULL;
Dispatch_Controller& Dispatch_Controller::getInstance() {
if (!instance) {
instance = new Dispatch_Controller();
instance->begin();
}
return *instance;
}
Dispatch_Controller& Dispatcher() {
return Dispatch_Controller::getInstance();
}
bool Dispatch_Controller::isEnabled() {
return enabled;
}
void Dispatch_Controller::disable() {
// Force idempotency
if(!enabled) {
return;
}
enabled = false;
// Actually disable
RTD().disable();
Frame disableMessage = { .id=VCU_ID, .body={BLINK_LIGHT}, .len=1};
CAN().write(disableMessage);
Computer().logOne("vehicle_disabled_or_shutdown");
Onboard().logOne("vehicle_disabled_or_shutdown");
}
void Dispatch_Controller::enable() {
// Force idempotency
if(enabled) {
return;
}
enabled = true;
// Actually enable
RTD().enable();
// Notify listeners of enable
Frame enableMessage = { .id=VCU_ID, .body={ENABLE_LIGHT}, .len=1};
CAN().write(enableMessage);
Computer().logOne("vehicle_enabled");
Onboard().logOne("vehicle_enabled");
}
void Dispatch_Controller::dispatch() {
// If no message, break early
while(CAN().msgAvailable()) {
Frame frame = CAN().read();
// Send message to each handler
rtd_handler.handleMessage(frame);
bms_handler.handleMessage(frame);
can_node_handler.handleMessage(frame);
motor_handler.handleMessage(frame);
current_sense_handler.handleMessage(frame);
}
}
void Dispatch_Controller::handleFaultPins() {
bool bmsFault = handleSingleFaultPin(BMS_IN, "BMS");
bool imdFault = handleSingleFaultPin(IMD_IN, "IMD");
bool tempFault = handleSingleFaultPin(TEMP_SENSE_IN, "TEMP_SENSE");
bool stopFault = handleSingleFaultPin(STOP_BUTTON_IN, "STOP_BUTTON");
bool bmsPowerFault = handleSingleFaultPin(BMS_POWERED_IN, "BMS_NOT_POWERED");
bool hasFault = bmsFault || imdFault || tempFault || stopFault || bmsPowerFault;
bool prevHasFault = Store().readHasFault();
if (hasFault && !prevHasFault) {
// Car has shutdown :(
// First logically disable
Dispatcher().disable();
// Then turn light off
Frame dashMessage = { .id=VCU_ID, .body={SHUTDOWN_LIGHT}, .len=1};
CAN().write(dashMessage);
// Cancel precharge blink just in case
SoftTimer.remove(&requestBlinkDashLight);
} else if (!hasFault && prevHasFault) {
// Tractive voltage is now live!
// Set a timer to blink light after precharge
SoftTimer.remove(&requestBlinkDashLight);
requestBlinkDashLight.startDelayed();
SoftTimer.add(&requestBlinkDashLight);
}
Store().logHasFault(hasFault);
}
bool Dispatch_Controller::handleSingleFaultPin(int pin, String pinName) {
int fault = digitalRead(pin);
if (fault == LOW) {
Onboard().logTwo("LATCHED_FAULT", pinName);
return true;
}
return false;
}
void Dispatch_Controller::initializeFaultPins() {
pinMode(BMS_IN, INPUT);
pinMode(IMD_IN, INPUT);
pinMode(VCU_IN, INPUT);
pinMode(TEMP_SENSE_IN, INPUT);
pinMode(STOP_BUTTON_IN, INPUT);
pinMode(BMS_POWERED_IN, INPUT);
}