Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
stateLogic.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <model/structure.hpp>
6
7#include "TeensyTimerTool.h"
8using namespace TeensyTimerTool;
9
18class ASState {
19private:
20 PeriodicTimer emergency_timer_;
21 volatile bool timer_has_started = false;
23 _output_coordinator_;
25 *_communicator_;
26
27 inline static ASState *instance = nullptr;
28
29public:
33
43 : _output_coordinator_(output_coordinator),
44 _communicator_(communicator),
46 instance = this;
47 }
48
52 void calculate_state();
53 void timer_started() { timer_has_started = true; }
54};
55
57 switch (state_) {
60 break;
61 }
62
63 DEBUG_PRINT("Entering OFF state from MANUAL");
64 _output_coordinator_->enter_off_state();
65
67 break;
68
69 case State::AS_OFF:
70
71 if (!timer_has_started) {
72 emergency_timer_.begin(
73 [] {
74 instance->timer_started();
75 if (instance->_checkup_manager_.should_enter_emergency(instance->state_)) {
76 instance->_output_coordinator_->enter_emergency_state();
77 instance->_checkup_manager_._ebs_sound_timestamp_.reset();
78 instance->state_ = State::AS_EMERGENCY;
79 }
80 },
81 150'000);
82 }
83 // If manual driving checkup fails, the car can't be in OFF state, so it goes back to MANUAL
85 DEBUG_PRINT("Entering MANUAL state from OFF");
86 _output_coordinator_->enter_manual_state();
88 break;
89 }
90
92 if (!_checkup_manager_.should_go_ready_from_off()) break; // recheck all states
93
94 DEBUG_PRINT("Entering READY state from OFF");
95 _output_coordinator_->enter_ready_state();
97 DEBUG_PRINT("READY state entered...");
98 break;
99
100 case State::AS_READY:
101
103 break;
104 }
105 _output_coordinator_->enter_driving_state();
107 break;
108
110 _output_coordinator_->blink_driving_led();
112 _output_coordinator_->enter_finish_state();
114 break;
115
118 DEBUG_PRINT("Entering EMERGENCY state from FINISHED");
119
120 _output_coordinator_->enter_emergency_state();
123 break;
124 }
126
127 DEBUG_PRINT("Entering OFF state from FINISHED");
128 _output_coordinator_->enter_off_state();
131 break;
132
134 _output_coordinator_->blink_emergency_led();
135
137 DEBUG_PRINT("Entering OFF state from EMERGENCY");
138 _output_coordinator_->enter_off_state();
141 break;
142 }
143 break;
144 default:
145 break;
146 }
147}
The ASState class manages and transitions between different states of the vehicle system.
void timer_started()
void calculate_state()
Calculates the state of the vehicle.
CheckupManager _checkup_manager_
CheckupManager object for handling various checkup operations.
State state_
Current state of the vehicle system, initialized to OFF.
ASState(SystemData *system_data, Communicator *communicator, OutputCoordinator *output_coordinator)
Constructor for the ASState class.
The CheckupManager class handles various checkup operations.
bool should_stay_manual_driving() const
Performs a manual driving checkup.
bool should_enter_emergency(State current_state) const
Performs an emergency checkup.
Metro _ebs_sound_timestamp_
Timer for the EBS buzzer sound check.
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.
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
Class that contains definitions of typical messages to send via CAN It serves only as an example of t...
void reset()
Resets the timer to the current time.
Definition metro.h:124
void enter_finish_state()
ASSI blue LED on, ebs valves activated, sdc open.
void enter_manual_state()
Everything off, sdc closed.
void enter_off_state()
Everything off, sdc open.
void enter_driving_state()
ASSI LEDs yellow flashing, ebs valves deactivated, sdc closed.
void enter_emergency_state()
ASSI LEDs blue flashing, sdc open and buzzer ringing.
void enter_ready_state()
ASSI yellow LED on, ebs valves activated, sdc closed.
#define DEBUG_PRINT(str)
Communicator communicator
Definition main.cpp:14
SystemData system_data
Definition main.cpp:11
OutputCoordinator output_coordinator
Definition main.cpp:18
The whole model of the system: holds all the data necessary.
State
Definition structure.hpp:8
@ AS_DRIVING
@ AS_MANUAL
@ AS_READY
@ AS_FINISHED
@ AS_EMERGENCY