Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
statemachine.hpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <Bounce2.h>
3
4#include "apps.h"
5#include "can.h"
6#include "debug.h"
7#include "display.h"
8
9#define buzzerPin 4
10
11#define R2D_PIN 32
12#define R2D_TIMEOUT 500
13
14#define APPS_1_PIN 41
15#define APPS_2_PIN 40
16
17#define STARTUP_DELAY_MS 10000
18
19#define APPS_READ_PERIOD_MS 20
20#define BAMOCAR_ATTENUATION_FACTOR 1
21
22#define ASBuzzer 8000
23
24
25extern volatile bool TSOn;
26extern volatile bool R2DOverride;
27extern Bounce r2dButton;
28extern volatile bool ASReady;
29extern elapsedMillis R2DTimer;
30elapsedMillis APPSTimer;
31extern CAN_message_t disable;
32extern volatile bool R2DOverride;
33
34extern FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
35
36
38{
39 IDLE, // waiting for r2d && ATS off
40 DRIVING, // r2d pressed && ATS on
41 ASDRIVING // TSon and ASReady (it doesnt mean that the car is driving; AS State = ASReady || Driving || Emergency)
42};
43
45
47{
48 digitalWrite(buzzerPin, HIGH);
49 delay(1000);
50 digitalWrite(buzzerPin, LOW);
51}
52
54 switch (R2DStatus)
55 {
56 case IDLE:
57 r2dButton.update();
58 #ifdef CAN_DEBUG
59 Serial.print("TSOn:");
60 Serial.println(TSOn);
61 Serial.print("R2D Button Fell:");
62 Serial.println(r2dButton.fell());
63 Serial.print("R2DTimer:");
64 Serial.println(R2DTimer);
65 #endif
66 if ((r2dButton.fell() and TSOn and R2DTimer < R2D_TIMEOUT) or R2DOverride)
67 {
72 break;
73 }
74 else if (TSOn and ASReady)
75 {
76 delay(2000);
80 break;
81 }
82 break;
83
84 case DRIVING:
85 if (not TSOn and not R2DOverride)
86 {
88 can1.write(disable);
89 break;
90 }
91
93 {
94 APPSTimer = 0;
95 int apps_value = readApps();
96
97 if (apps_value >= 0)
98 sendTorqueVal(apps_value);
99 else
100 sendTorqueVal(0);
101 break;
102 }
103
104 break;
105 case ASDRIVING:
106 if (not TSOn)
107 {
108 R2DStatus = IDLE;
109 can1.write(disable);
110 break;
111 }
112 break;
113 default:
114 ERROR("Invalid r2d_status");
115 break;
116 }
117}
int readApps()
Definition apps.cpp:82
void sendTorqueVal(int value_bamo)
Definition can.cpp:218
void request_dataLOG_messages()
Definition can.cpp:152
void initBamocarD3()
Definition can.cpp:244
#define ERROR(...)
Definition debug.h:35
status
@ ASDRIVING
@ DRIVING
@ IDLE
void statemachine()
status R2DStatus
volatile bool ASReady
Definition main.cpp:22
#define buzzerPin
void playR2DSound()
#define APPS_READ_PERIOD_MS
volatile bool TSOn
Definition main.cpp:19
CAN_message_t disable
Definition can.cpp:6
elapsedMillis APPSTimer
#define R2D_TIMEOUT
Bounce r2dButton
Definition main.cpp:42
elapsedMillis R2DTimer
Definition main.cpp:44
volatile bool R2DOverride
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can1
Definition main.cpp:87