Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
test_integration.cpp
Go to the documentation of this file.
5#include "unity.h"
6
7#define HYDRAULIC_PRESSURE_HIGH 0xf8
8#define HYDRAULIC_PRESSURE_LOW 0x01
9#define BAMOCAR_VDC_HIGH 0x11
10#define RES_GO 0x02
11
16
24
28void to_ready() {
30 // sd.digitalData.watchdog_state = true;
32
33 uint8_t bamo_msg[] = {VDC_BUS, 0x00, BAMOCAR_VDC_HIGH}; // VDC_BUS fill
35
37 uint8_t hydraulic_msg[] = {HYDRAULIC_LINE, HYDRAULIC_PRESSURE_HIGH, 0x00};
38 communicator.c1_callback(hydraulic_msg);
39
40 // Iterate a few times to go to check wd
41 Metro time{1};
42 while (!time.checkWithoutReset()) {
44 }
45
46 // sd.digitalData.watchdog_state = false;
47 // Wait for wd timeout
49 while (!time2.checkWithoutReset()) {
55 }
56}
57
63 reset();
64 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
65
66 to_ready();
67
68 TEST_ASSERT_EQUAL(false, sd.failure_detection_.emergency_signal_);
69
71 as_state._checkup_manager_.checkupState);
72
73 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
74}
75
81 reset();
82
83 bool went_ready = false;
84 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
85
87 // sd.digitalData.watchdog_state = true;
89
90 uint8_t bamo_msg[] = {VDC_BUS, 0x00, BAMOCAR_VDC_HIGH}; // VDC_BUS fill
92
94 uint8_t hydraulic_msg[] = {HYDRAULIC_LINE, HYDRAULIC_PRESSURE_HIGH, 0x00};
95 communicator.c1_callback(hydraulic_msg);
96
97 // // Iterate a few times to go to check wd
98 // Metro time{1};
99 // while (!time.checkWithoutReset()) {
100 // as_state.calculate_state();
101 // }
102
103 // sd.digitalData.watchdog_state = false;
104 sd.hardware_data_.asms_on_ = false; // switch previously checked condition
105 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
106 // Wait for wd timeout
107
109 while (!time2.checkWithoutReset()) {
110 if (as_state.state_ == State::AS_READY) went_ready = true;
111
116 }
117
118 TEST_ASSERT_EQUAL(false, went_ready);
119 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
120}
121
126 bool reverted_to_off = false;
127 reset();
128 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
129 to_ready();
130 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
131
132 // Procedure to go back to AS_OFF state
133 uint8_t bamo_msg2[] = {VDC_BUS, 0x00, 0x00}; // cd voltage set to below threshold
135
136 Metro time3{1};
137 while (!time3.checkWithoutReset()) {
139 if (as_state.state_ == State::AS_OFF) reverted_to_off = true;
140 }
141
142 TEST_ASSERT_EQUAL(false, reverted_to_off);
143 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
144}
145
151 reset();
152 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
153 to_ready();
154 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
155
157 while (!time.checkWithoutReset()) {
159 }
160
161 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
162
163 Metro time2{EBS_BUZZER_TIMEOUT / 2};
164 while (!time2.checkWithoutReset()) {
166 }
167 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
168
169 Metro time3{EBS_BUZZER_TIMEOUT / 2};
170 while (!time3.checkWithoutReset()) {
172 }
173 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
174 sd.hardware_data_.asms_on_ = false;
176 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
177}
178
184 reset();
185 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
186 to_ready();
187 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
188
189 Metro time{READY_TIMEOUT_MS / 2};
190 while (!time.checkWithoutReset()) {
196 }
197
198 uint8_t msg[8] = {RES_GO, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
200
201 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
202
203 Metro time2{READY_TIMEOUT_MS / 2};
204 while (!time2.checkWithoutReset()) {
210 }
211
214 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
215
216 // brake pressure has a threshold to be updated, otherwise emergency
217 Metro time3{RELEASE_EBS_TIMEOUT_MS / 2};
218 while (!time3.checkWithoutReset()) {
224 }
225
226 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_); // still within threshold, okay
227
228 Metro time4{RELEASE_EBS_TIMEOUT_MS + 10};
235 }
236 // threshold over, still with brake pressure, emergency
239 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
240}
241
247 reset();
248 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
249 to_ready();
250 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
251
253 while (!time.checkWithoutReset()) {
259 }
260
261 uint8_t msg[8] = {RES_GO, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
264
265 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
266
267 uint8_t hydraulic_msg[] = {HYDRAULIC_LINE, HYDRAULIC_PRESSURE_LOW,
268 0x00}; // loose brake activation
269 communicator.c1_callback(hydraulic_msg);
271 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
272
275 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
276}
277
283 reset();
284 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
285 to_ready();
286 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
287 sd.r2d_logics_.r2d = true;
289 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
290
291 uint8_t pc_msg[] = {MISSION_FINISHED};
293
294 uint8_t c1_msg[] = {RIGHT_WHEEL_CODE, 0x00, 0x01, 0x00, 0x00}; // value not 0
295 communicator.c1_callback(c1_msg); // right wheel = msg
297
299 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
300
301 uint8_t c1_msg2[] = {RIGHT_WHEEL_CODE, 0x00, 0x00, 0x00, 0x00};
302 communicator.c1_callback(c1_msg2); // right wheel = msg
303
305 TEST_ASSERT_EQUAL(State::AS_FINISHED, as_state.state_);
306
307 sd.hardware_data_.asms_on_ = false;
309 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
310}
311
317 reset();
318 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
319 to_ready();
320 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
321 sd.r2d_logics_.r2d = true;
323 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
326 sd.mission_finished_ = true;
328 TEST_ASSERT_EQUAL(State::AS_FINISHED, as_state.state_);
331 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
332}
333
339 reset();
340 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
341 sd.hardware_data_.asms_on_ = false;
343 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
344
347 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
348
351 TEST_ASSERT_EQUAL(State::AS_MANUAL, as_state.state_);
352
355 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
356}
357
362 reset();
363 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
364 uint8_t hydraulic_msg[] = {HYDRAULIC_LINE, HYDRAULIC_PRESSURE_HIGH, 0x00};
365 communicator.c1_callback(hydraulic_msg);
367
368 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
369 sd.r2d_logics_.r2d = true;
370
372 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
373 to_ready(); // will validate asms is on and ts is active
375 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
376
378 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_); // r2d reset after transition to ready
379
380 sd.r2d_logics_.r2d = true;
382 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
383}
384
389 reset();
390 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
391
393 // sd.digitalData.watchdog_state = true;
395
396 uint8_t bamo_msg[] = {VDC_BUS, 0x00, BAMOCAR_VDC_HIGH};
398
400 uint8_t hydraulic_msg[] = {HYDRAULIC_LINE, HYDRAULIC_PRESSURE_HIGH, 0x00};
401 // uint8_t hydraulic_msg2[] = {HYDRAULIC_LINE, HYDRAULIC_PRESSURE_LOW, 0x00}; // loose brake
402 // activation
403 communicator.c1_callback(hydraulic_msg);
404
405
406
408 while (!time3.checkWithoutReset()) {
409 communicator.c1_callback(hydraulic_msg);
415 }
416
417 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
418}
419
424 reset();
425 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
426 to_ready();
427 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
428 sd.r2d_logics_.r2d = true;
430 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
431
432 sd.hardware_data_.bspd_sdc_open_ = true; // ebs before finished checks
433
435 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
436
437 uint8_t pc_msg[] = {MISSION_FINISHED};
440 uint8_t c1_msg[] = {RIGHT_WHEEL_CODE, 0x00, 0x00, 0x00, 0x00};
441 communicator.c1_callback(c1_msg); // right wheel = msg
443 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
444}
445
450 reset();
451 TEST_ASSERT_EQUAL(State::AS_OFF, as_state.state_);
452 to_ready();
453 TEST_ASSERT_EQUAL(State::AS_READY, as_state.state_);
454 sd.r2d_logics_.r2d = true;
456 TEST_ASSERT_EQUAL(State::AS_DRIVING, as_state.state_);
457
458 uint8_t pc_msg[] = {MISSION_FINISHED};
461 uint8_t c1_msg[] = {RIGHT_WHEEL_CODE, 0x00, 0x00, 0x00, 0x00};
462 communicator.c1_callback(c1_msg); // right wheel = msg
464 TEST_ASSERT_EQUAL(State::AS_FINISHED, as_state.state_);
465
468 TEST_ASSERT_EQUAL(State::AS_FINISHED, as_state.state_);
469
472 TEST_ASSERT_EQUAL(State::AS_EMERGENCY, as_state.state_);
473}
474
475void setUp() {}
476
477int main() {
478 UNITY_BEGIN();
484 RUN_TEST(test_ready_to_emg_to_off);
486 RUN_TEST(test_finished_to_emg);
488 RUN_TEST(test_flow_driving);
489 RUN_TEST(test_flow_ready);
490 RUN_TEST(test_flow_emergency);
491 RUN_TEST(test_flow_finished);
492 return UNITY_END();
493}
The ASState class manages and transitions between different states of the vehicle system.
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.
Class that contains definitions of typical messages to send via CAN It serves only as an example of t...
static void res_state_callback(const uint8_t *buf)
Callback RES default callback.
static void pc_callback(const uint8_t *buf)
Callback for message from AS CU.
static void bamocar_callback(const uint8_t *buf)
Callback from inversor, for alive signal and data.
Class responsible for controlling digital outputs in the Master Teensy.
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
constexpr unsigned long READY_TIMEOUT_MS
constexpr unsigned long RELEASE_EBS_TIMEOUT_MS
constexpr int COMPONENT_TIMESTAMP_TIMEOUT
constexpr int INITIAL_CHECKUP_STEP_TIMEOUT
constexpr int EBS_BUZZER_TIMEOUT
double _right_wheel_rpm
bool pneumatic_line_pressure_
double _left_wheel_rpm
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_
@ AS_DRIVING
@ AS_MANUAL
@ AS_READY
@ AS_FINISHED
@ AS_EMERGENCY
CAN_message_t msg
void test_off_to_ready_wayback_impossible()
Test function to validate AS_READY doesn't revert to AS_Ready.
void test_driving_to_finished_to_off()
Test function to validate AS_DRIVING to AS_FINISHED transition and AS_FINISHED to AS_OFF after.
void test_flow_finished()
Test function to validate flow diagram conditions to go AS_FINISHED.
Communicator communicator
void test_ready_to_emg_to_off()
Test function to validate AS_READY to AS_Emergency and the AS_EMERGENCY to AS_OFF transition.
#define RES_GO
ASState as_state
void test_off_to_ready_success(void)
Test function to validate AS_OFF to AS_Ready transition normal and successful transition.
void test_finished_to_emg()
Test function to validate AS_FINISHED to AS_EMERGENCY transition if RES is activated after mission fi...
void test_flow_emergency()
Test function to validate flow diagram conditions to go AS_EMERGENCY.
void setUp()
void test_ready_to_driving_to_emg2()
Test function to validate AS_READY to AS_Driving transition and AS_DRIVING to AS_EMERGENCY if failure...
DigitalSender digitalSender
#define HYDRAULIC_PRESSURE_HIGH
#define BAMOCAR_VDC_HIGH
void to_ready()
Auxiliary function to set state as ready.
void test_flow_ready()
Test function to validate flow diagram conditions to go AS_READY.
#define HYDRAULIC_PRESSURE_LOW
void reset()
Auxiliary function to reset data values.
SystemData sd
void test_off_to_manual_wayback()
Test function to validate AS_OFF to AS_Manual transition and the other way around.
void test_off_to_ready_recheck()
Test function to validate AS_OFF to AS_Ready transition with checks reverting to invalid mid transiti...
int main()
void test_flow_driving()
Test function to validate flow diagram conditions to go AS_DRIVING.
void test_ready_to_driving_to_emg()
Test function to validate AS_READY to AS_Driving transition and AS_DRIVING to AS_EMERGENCY if brakes ...