Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
checkupManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
4
5#include "debugUtils.hpp"
9
10// Also known as Orchestrator
15private:
16 SystemData *_system_data_;
17 TeensyTimerTool::PeriodicTimer watchdog_timer_;
19 Metro _watchdog_toggle_timer_{WATCHDOG_TOGGLE_DURATION};
20 Metro _watchdog_test_timer_{WATCHDOG_TEST_DURATION};
21
26 bool check_pressure_high() const;
27
32 bool check_pressure_low() const;
33
38 bool failed_to_build_pneumatic_pressure_in_engage_time() const;
43 bool failed_to_build_pneumatic_pressure_in_release_time() const;
49 bool failed_to_build_hydraulic_pressure_in_time() const;
54 bool failed_to_reduce_hydraulic_pressure_in_time() const;
55
59 void handle_ebs_check();
60
61public:
63
86
96
108
111
113
118 explicit CheckupManager(SystemData *system_data) : _system_data_(system_data) {};
119
123 void reset_checkup_state();
124
128 [[nodiscard]] bool should_stay_manual_driving() const;
129
133 bool should_stay_off();
134
139
143 [[nodiscard]] bool should_go_ready_from_off() const;
144
148 [[nodiscard]] bool should_stay_ready() const;
149
153 [[nodiscard]] bool should_enter_emergency(State current_state) const;
154
157
158 [[nodiscard]] bool should_stay_driving() const;
159
163 [[nodiscard]] bool should_stay_mission_finished() const;
164
169 [[nodiscard]] bool emergency_sequence_complete() const;
170
177 [[nodiscard]] bool res_triggered() const;
178
179};
180
185
187 if (_system_data_->mission_ != Mission::MANUAL ||
188 _system_data_->hardware_data_.pneumatic_line_pressure_ != 0 ||
189 _system_data_->hardware_data_.asms_on_) {
190 return false;
191 }
192
193 return true;
194}
195
197 CheckupError init_sequence_state = initial_checkup_sequence();
198
199 if (init_sequence_state != CheckupError::SUCCESS) {
200 return true;
201 }
202 return false;
203}
204
206 switch (checkup_state_) {
208 if (_system_data_->hardware_data_.asms_on_) {
210 DEBUG_PRINT("ASMS activated, starting watchdog check");
211 }
212 break;
214 if (_system_data_->hardware_data_.wd_ready_) {
216 _watchdog_toggle_timer_.reset();
217 DEBUG_PRINT("Watchdog ready, starting toggle sequence");
218 break;
219 }
221 break;
222
224 // Fail immediately if WD_READY goes low during toggling
225 if (!_system_data_->hardware_data_.wd_ready_) {
226 DEBUG_PRINT("Watchdog error: WD_READY went low during toggling phase");
228 }
229
231
232 // Toggle for the specified duration
233 if (_watchdog_toggle_timer_.checkWithoutReset()) {
235 _watchdog_test_timer_.reset();
236 DEBUG_PRINT("Watchdog toggle complete, stopping watchdog");
237 }
238 break;
239
241 _watchdog_test_timer_.reset();
243 DEBUG_PRINT("Stopping watchdog toggle, beginning verification");
244 break;
245
247 if (!_system_data_->hardware_data_.wd_ready_) {
249
251 DEBUG_PRINT("Watchdog no longer ready - verification successful");
252 break;
253 }
254 if (_watchdog_test_timer_.checkWithoutReset()) {
255 DEBUG_PRINT("Watchdog test failed - WD_READY did not go low during verification time");
257 }
258 break;
260 watchdog_timer_.begin([] { DigitalSender::toggle_watchdog(); }, 50'000);
262 break;
264 if (_system_data_->hardware_data_.pneumatic_line_pressure_) {
266 }
267 break;
269 if (_system_data_->hardware_data_._hydraulic_line_pressure >= HYDRAULIC_BRAKE_THRESHOLD) {
271 }
272 break;
274
275 if (_system_data_->hardware_data_.asats_pressed_) {
276 _system_data_->failure_detection_.emergency_signal_ = false;
278 }
279 break;
283
284 break;
286 if (_system_data_->failure_detection_.ts_on_) {
287 DEBUG_PRINT("TS activated");
288
290 }
291 break;
293 handle_ebs_check();
294 break;
296 if (_system_data_->failure_detection_.has_any_component_timed_out() ||
297 _system_data_->failure_detection_.emergency_signal_) {
299 }
301 DEBUG_PRINT("Checkup complete and returning success");
303 }
304 default:
305 break;
306 }
308}
309
310inline void CheckupManager::handle_ebs_check() {
311 switch (pressure_test_phase_) {
313 // Step 10: Disable EBS actuator 1
314 DEBUG_PRINT("Disabling EBS actuator 1");
317 break;
318
320 if (check_pressure_high()) {//pressure should be high even with only one actuator
321 DEBUG_PRINT("Pressure high confirmed with only actuator 2");
323 }
324 break;
325
327 // Step 12: Enable EBS actuator 1 again
328 DEBUG_PRINT("Re-enabling EBS actuator 1");
331 break;
332
334 // Step 13: Disable EBS actuator 2
335 DEBUG_PRINT("Disabling EBS actuator 2");
338 break;
339
341 // Step 14: Check that the brake pressure is still built up correctly
342 if (check_pressure_high()) {
343 DEBUG_PRINT("Pressure high confirmed with only actuator 1");
345 }
346 break;
347
349 // Step 15: Enable EBS actuator 2 again
350 DEBUG_PRINT("Re-enabling EBS actuator 2");
353 break;
354
356 // Step 16: Transition to ready state (isto n fazemos já mas vamos checkar as timestamps e
357 // entramos ready dps)
358 DEBUG_PRINT("EBS check complete, transitioning to next state");
361 break;
362 }
363}
364
366 if (!_system_data_->hardware_data_.asms_on_ || !_system_data_->failure_detection_.ts_on_ ||
367 !_system_data_->hardware_data_.asats_pressed_ ||
368 _system_data_->hardware_data_.bspd_sdc_open_) {
369 return false;
370 }
371 _system_data_->r2d_logics_.enter_ready_state();
372 return true;
373}
374
376 if (!_system_data_->r2d_logics_.r2d) {
377 return true;
378 }
379 _system_data_->r2d_logics_.reset_ebs_timestamp();
380 return false;
381}
382
383// ----------------------------------------------------------
384// this is ugly as fuck but I don't care >:(
385// ----------------------------------------------------------
386inline bool CheckupManager::should_enter_emergency(State current_state) const {
387 if (current_state == State::AS_READY) {
389 } else if (current_state == State::AS_DRIVING) {
391 }
392 return false;
393}
394
396 return _system_data_->failure_detection_.emergency_signal_ ||
397 failed_to_build_pneumatic_pressure_in_engage_time() ||
399 !_system_data_->hardware_data_.asms_on_ || !_system_data_->failure_detection_.ts_on_ ||
400 failed_to_build_hydraulic_pressure_in_time() ||
401 _system_data_->hardware_data_.bspd_sdc_open_;
402}
403
405 return _system_data_->failure_detection_.has_any_component_timed_out() ||
406 _system_data_->failure_detection_.emergency_signal_ ||
407 _system_data_->hardware_data_.bspd_sdc_open_ ||
408 failed_to_build_pneumatic_pressure_in_release_time() ||
409 failed_to_reduce_hydraulic_pressure_in_time() || !_system_data_->hardware_data_.asms_on_ ||
410 !_system_data_->failure_detection_.ts_on_;
411}
412
413inline bool CheckupManager::check_pressure_high() const {
414 return _system_data_->hardware_data_._hydraulic_line_pressure >= HYDRAULIC_BRAKE_THRESHOLD &&
416}
417
418inline bool CheckupManager::check_pressure_low() const {
419 return _system_data_->hardware_data_._hydraulic_line_pressure < HYDRAULIC_BRAKE_THRESHOLD &&
421}
422
423bool CheckupManager::failed_to_build_pneumatic_pressure_in_engage_time() const {
424 return _system_data_->hardware_data_.pneumatic_line_pressure_ == 0 &&
426}
427bool CheckupManager::failed_to_build_pneumatic_pressure_in_release_time() const {
428 return _system_data_->hardware_data_.pneumatic_line_pressure_ == 0 &&
430}
431bool CheckupManager::failed_to_build_hydraulic_pressure_in_time() const {
432 return _system_data_->hardware_data_._hydraulic_line_pressure < HYDRAULIC_BRAKE_THRESHOLD &&
434}
435
436bool CheckupManager::failed_to_reduce_hydraulic_pressure_in_time() const {
437 return _system_data_->hardware_data_._hydraulic_line_pressure >= HYDRAULIC_BRAKE_THRESHOLD &&
439}
440
442 if (abs(_system_data_->hardware_data_._left_wheel_rpm) < 0.1 &&
443 abs(_system_data_->hardware_data_._right_wheel_rpm) < 0.1 &&
444 _system_data_->mission_finished_) {
445 return false;
446 }
447 return true;
448}
449
451 if (_system_data_->hardware_data_.asms_on_) {
452 return true;
453 }
454 return false;
455}
456
459 return true;
460 }
461 return false;
462}
463
464inline bool CheckupManager::res_triggered() const {
465 if (_system_data_->failure_detection_.emergency_signal_) {
466 return true;
467 }
468 return false;
469}
470
The CheckupManager class handles various checkup operations.
bool should_stay_manual_driving() const
Performs a manual driving checkup.
bool should_enter_emergency_in_ready_state() const
EbsPressureTestPhase pressure_test_phase_
bool should_enter_emergency(State current_state) const
Performs an emergency checkup.
Metro _ebs_sound_timestamp_
Timer for the EBS buzzer sound check.
CheckupState checkup_state_
Current state of the checkup process.
bool should_enter_emergency_in_driving_state() const
bool emergency_sequence_complete() const
Checks if the emergency sequence is complete and the vehicle can transition to AS_OFF.
bool should_stay_ready() const
Performs a ready to drive checkup.
CheckupState
The CheckupState enum represents the different states of the initial checkup process....
bool should_go_ready_from_off() const
Performs a last re-check for off to ready transition.
void reset_checkup_state()
Resets the checkup state to the initial state.
bool should_stay_off()
Performs an off checkup.
bool should_stay_mission_finished() const
Performs a mission finished checkup.
bool res_triggered() const
Checks if the RES has been triggered.
bool should_stay_driving() const
CheckupManager(SystemData *system_data)
Constructor for the CheckupManager class.
CheckupError initial_checkup_sequence()
Performs an initial checkup.
static void disable_ebs_actuator_2()
Disables EBS actuator 2.
static void enable_ebs_actuator_2()
Enables EBS actuator 2.
static void enable_ebs_actuator_1()
Enables EBS actuator 1.
static void close_watchdog_sdc()
Closes the watchdog signal for SDC.
static void disable_ebs_actuator_1()
Disables EBS actuator 1.
static void toggle_watchdog()
Toggles the watchdog signal.
static void close_sdc()
Closes the SDC in Master and SDC Logic.
Our own implementation of Metro class.
Definition metro.h:13
void reset()
Resets the timer to the current time.
Definition metro.h:124
bool checkWithoutReset() const
Checks if the interval has passed without resetting the timer.
Definition metro.h:115
#define DEBUG_PRINT(str)
constexpr int WATCHDOG_TEST_DURATION
constexpr int WATCHDOG_TOGGLE_DURATION
constexpr int EBS_BUZZER_TIMEOUT
SystemData system_data
Definition main.cpp:11
int _hydraulic_line_pressure
double _right_wheel_rpm
bool pneumatic_line_pressure_
double _left_wheel_rpm
void reset_ebs_timestamp()
resets timestamps for driving
void enter_ready_state()
resets timestamps for ready
Metro releaseEbsTimestamp
The whole model of the system: holds all the data necessary.
Mission mission_
R2DLogics r2d_logics_
FailureDetection failure_detection_
HardwareData hardware_data_
bool mission_finished_
State
Definition structure.hpp:8
@ AS_DRIVING
@ AS_READY