Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
outputCoordinator.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "TeensyTimerTool.h"
5#include "debugUtils.hpp"
7#include "enum_utils.hpp"
8#include "metro.h"
10#include "timings.hpp"
11
13private:
14 Metro blink_timer_{LED_BLINK_INTERVAL};
15
16 SystemData* system_data_;
17 Communicator* communicator_;
18 DigitalSender* digital_sender_;
19
20 Metro mission_timer_;
21 Metro state_timer_;
22 Metro process_timer_{PROCESS_INTERVAL};
23
24 uint8_t previous_master_state_;
25 uint8_t previous_checkup_state_;
26 uint8_t previous_mission_;
27
28public:
31 : system_data_(system_data),
32 communicator_(communicator),
33 digital_sender_(digital_sender),
34 mission_timer_(MISSION_PUBLISH_INTERVAL),
35 state_timer_(STATE_PUBLISH_INTERVAL),
36 previous_master_state_(static_cast<uint8_t>(15)),
37 previous_checkup_state_(static_cast<uint8_t>(15)),
38 previous_mission_(static_cast<uint8_t>(15)) {}
39
40 void init() {
41 mission_timer_.reset();
42 state_timer_.reset();
43 DEBUG_PRINT("Output coordinator initialized...");
44 }
45
46 void process(uint8_t current_master_state, uint8_t current_checkup_state) {
47 if (process_timer_.check()) {
48 send_soc();
49 send_asms();
50 send_debug_on_state_change(current_master_state, current_checkup_state);
51 send_mission_update();
52 send_state_update(current_master_state);
53 dash_ats_update(current_master_state);
54 update_physical_outputs();
55 send_rpm();
56 process_timer_.reset();
57 }
58 }
59
61 if (blink_timer_.check()) {
62 digital_sender_->blink_led(ASSI_BLUE_PIN);
63 }
64 }
65
67 if (blink_timer_.check()) {
68 digital_sender_->blink_led(ASSI_YELLOW_PIN);
69 }
70 }
71
76 digital_sender_->turn_off_assi();
77 blink_timer_.reset();
78 digital_sender_->activate_ebs();
79 digital_sender_->open_sdc();
80 }
81
86 digital_sender_->turn_off_assi();
87 digital_sender_->deactivate_ebs();
88 digital_sender_->open_sdc();
89 // digital_sender_->close_sdc(); // close sdc only when ats pressed and in manual mode already
90 DEBUG_PRINT("Entering manual state...");
91 }
92
97 digital_sender_->turn_off_assi();
98 digital_sender_->deactivate_ebs();
99 digital_sender_->open_sdc();
100 }
101
106 digital_sender_->turn_off_assi();
107 digital_sender_->turn_on_yellow();
108 digital_sender_->activate_ebs();
109 digital_sender_->close_sdc();
110 }
111
116 digital_sender_->turn_off_assi();
117 blink_timer_.reset();
118 digital_sender_->deactivate_ebs();
119 digital_sender_->close_sdc();
120 }
121
126 digital_sender_->turn_off_assi();
127 digital_sender_->turn_on_blue();
128 digital_sender_->activate_ebs();
129 digital_sender_->open_sdc();
130 }
131
132private:
133 // Communication functions
134 void send_debug_on_state_change(uint8_t current_master_state, uint8_t current_checkup_state) {
135 Communicator::publish_debug_morning_log(*system_data_, current_master_state,
136 current_checkup_state);
137 }
138
139 void send_soc() { Communicator::publish_soc(system_data_->hardware_data_.soc_); }
140
141 void send_asms() { Communicator::publish_asms_on(system_data_->hardware_data_.asms_on_); }
142
143 void send_mission_update() {
144 if (mission_timer_.check()) {
146 mission_timer_.reset();
147 }
148 }
149
150 void send_state_update(uint8_t current_master_state) {
151 if (state_timer_.check()) {
152 Communicator::publish_state(current_master_state);
153 state_timer_.reset();
154 }
155 }
156
157 void update_physical_outputs() {
158 brake_light_update();
159 bsdp_sdc_update();
160 // digital_sender_->turn_on_blue();
161 // digital_sender_->turn_on_yellow();
162 }
163
164 void brake_light_update() {
165 int brake_val = system_data_->hardware_data_._hydraulic_line_pressure;
166 // DEBUG_PRINT("Brake pressure: " + String(brake_val));
167 //DEBUG_PRINT("Brake pressure lower threshold: " +
168 // String(BRAKE_PRESSURE_LOWER_THRESHOLD));
169 //DEBUG_PRINT("Brake pressure upper threshold: " +
170 // String(BRAKE_PRESSURE_UPPER_THRESHOLD));
171
172 if (brake_val >= BRAKE_PRESSURE_LOWER_THRESHOLD &&
173 brake_val <= BRAKE_PRESSURE_UPPER_THRESHOLD) {
174 digital_sender_->turn_on_brake_light();
175 } else {
176 digital_sender_->turn_off_brake_light();
177 }
178 }
179 void bsdp_sdc_update() {
180 if (system_data_->hardware_data_.bspd_sdc_open_) {
181 digital_sender_->bspd_error();
182 } else {
183 digital_sender_->no_bspd_error();
184 }
185 }
186 void dash_ats_update(uint8_t current_master_state) {
187 if (system_data_->hardware_data_.ats_pressed_ &&
188 current_master_state == to_underlying(State::AS_MANUAL)) {
189 digital_sender_->close_sdc();
190 }
191 else {
192 // !! this was opening sdc as soon as ats was released
193 // digital_sender_->open_sdc();
194 }
195 }
196 void send_rpm() { Communicator::publish_rpm(); }
197};
Class that contains definitions of typical messages to send via CAN It serves only as an example of t...
static int publish_mission(int mission_id)
Publish AS Mission to CAN.
static int publish_rpm()
Publish rl wheel rpm to CAN.
static int publish_debug_morning_log(const SystemData &system_data, uint8_t sate, uint8_t state_checkup)
Publish AS Mission to CAN.
static int publish_soc(uint8_t soc)
Publish SOC to CAN.
static int publish_asms_on(bool asms_on)
Publish ASMS state to CAN.
static int publish_state(int state_id)
Publish AS State to CAN.
Class responsible for controlling digital outputs in the Master Teensy.
void bspd_error()
Turns on the BSPD error signal.
static void turn_off_assi()
Turns off both ASSI LEDs (yellow and blue).
void blink_led(int pin)
Blinks the LED at the given pin.
void turn_on_brake_light()
Turns on the brake light.
void no_bspd_error()
Turns off the BSPD error signal.
void turn_on_blue()
Turns on the blue ASSI LED.
void turn_off_brake_light()
Turns off the brake light.
static void activate_ebs()
Activates the solenoid EBS valves.
static void deactivate_ebs()
Deactivates the solenoid EBS valves.
static void open_sdc()
Opens the SDC in Master and SDC Logic.
static void close_sdc()
Closes the SDC in Master and SDC Logic.
void turn_on_yellow()
Turns on the yellow ASSI LED.
Our own implementation of Metro class.
Definition metro.h:13
void reset()
Resets the timer to the current time.
Definition metro.h:124
bool check()
Checks if the interval has passed and resets the timer if true.
Definition metro.h:90
OutputCoordinator(SystemData *system_data, Communicator *communicator, DigitalSender *digital_sender)
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.
void process(uint8_t current_master_state, uint8_t current_checkup_state)
#define DEBUG_PRINT(str)
constexpr auto to_underlying(Enum e) noexcept
Definition enum_utils.hpp:4
constexpr int PROCESS_INTERVAL
constexpr int LED_BLINK_INTERVAL
constexpr int BRAKE_PRESSURE_UPPER_THRESHOLD
constexpr int BRAKE_PRESSURE_LOWER_THRESHOLD
constexpr int ASSI_BLUE_PIN
constexpr int ASSI_YELLOW_PIN
Communicator communicator
Definition main.cpp:14
SystemData system_data
Definition main.cpp:11
DigitalSender digital_sender
Definition main.cpp:17
int _hydraulic_line_pressure
The whole model of the system: holds all the data necessary.
Mission mission_
HardwareData hardware_data_
@ AS_MANUAL
constexpr auto MISSION_PUBLISH_INTERVAL
Definition timings.hpp:6
constexpr auto STATE_PUBLISH_INTERVAL
Definition timings.hpp:5