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();
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 50'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 _output_coordinator_->refresh_r2d_vars();
91 _output_coordinator_->refresh_emergency_vars();
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();
108 break;
109
111 _output_coordinator_->blink_driving_led();
113 _output_coordinator_->enter_finish_state();
115 break;
116
119 DEBUG_PRINT("Entering EMERGENCY state from FINISHED");
120
121 _output_coordinator_->enter_emergency_state();
124 break;
125 }
127
128 DEBUG_PRINT("Entering OFF state from FINISHED");
129 _output_coordinator_->enter_off_state();
132 break;
133
135 _output_coordinator_->blink_emergency_led();
136
138 DEBUG_PRINT("Entering OFF state from EMERGENCY");
139 _output_coordinator_->enter_off_state();
142 break;
143 }
144 break;
145 default:
146 break;
147 }
148}
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.
void reset_mission_finished()
Resets the mission finished state.
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.
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:12
SystemData system_data
Definition main.cpp:11
OutputCoordinator output_coordinator
Definition main.cpp:15
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