Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
r2d.cpp
Go to the documentation of this file.
1#include "r2d.h"
2#include <Arduino.h>
3#include <FlexCAN_T4.h>
4#include "Bounce2.h"
5#include "can.h"
6
7extern CAN_message_t request_bamo;
8extern CAN_message_t bamo_apps;
9// FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
10
11// int pin_brake_sensor = 20;
17
18bool sound = false;
20
21Bounce pushbutton = Bounce(pin_R2Dbutton, 10);
22// 10 ms debounce
23/*
24int check_bamocar() {
25 can1.write(request_bamo);
26
27 CAN_message_t msg;
28 if (can1.read(msg)) {
29 if (msg.buf[0] == 0x8F) {
30 uint32_t data = msg.buf[1] + msg.buf[2] + msg.buf[3] + msg.buf[4];
31 switch (data) { // all possible errors. O que fazer quando são detetados??
32 case 1:
33 // Parameter damaged
34 Serial.print("Parameter damaged");
35 return 1;
36 break;
37 case 2:
38 // Hardware error
39 Serial.print("Hardware error");
40 return 1;
41 break;
42 case 4:
43 // Safety circuit faulty (only active with RUN)
44 Serial.print("Safety circuit faulty (only active with RUN)");
45 return 1;
46 break;
47 case 8:
48 // CAN TimeOut Time exceeded
49 Serial.print("CAN Timeout time exceeded");
50 return 1;
51 break;
52 case 16:
53 // Bad or wrong encoder signal
54 Serial.print("Bad or wrong encoder signal");
55 return 1;
56 break;
57 case 32:
58 // Power voltage missing
59 Serial.print("Power voltage missing");
60 return 1;
61 break;
62 case 64:
63 // Engine temperature too high
64 Serial.print("Engine temperature too high");
65 return 1;
66 break;
67 case 128:
68 // Unit temperature too high
69 Serial.print("Unit temperature too high");
70 return 1;
71 break;
72 case 256:
73 // Overvoltage > 1.8 x UN reached
74 Serial.print("Overvoltage > 1.8 x UN reached");
75 return 1;
76 break;
77 case 512:
78 // Overcurrent or strong oscillating current detected
79 Serial.print("Overcurrent or strong oscillating current detected");
80 return 1;
81 break;
82 case 1024:
83 // Spinning (without setpoint, wrong direction)
84 Serial.print("Spinning (without setpoint, wrong direction)");
85 return 1;
86 break;
87 case 2048:
88 // User - Error selection
89 Serial.print("User - Error selection");
90 return 1;
91 break;
92 case 16384:
93 // Current - Measurement error
94 Serial.print("Current - Measurement error");
95 return 1;
96 break;
97 case 32768:
98 // Ballast circuit overloaded
99 Serial.print("Ballast circuit overloaded");
100 return 1;
101 break;
102 }
103 }
104 }
105 return 0;
106}
107*/
108
109/*
110void send_to_bamocar(int value_bamo) {
111 uint8_t byte1 = (value_bamo >> 8) & 0xFF; // MSB
112 uint8_t byte2 = value_bamo & 0xFF; // LSB
113
114 bamo_apps.buf[1] = byte2;
115 bamo_apps.buf[2] = byte1;
116
117 can1.write(bamo_apps);
118 }
119
120 int check_BMS() {
121 return 0;
122 }
123*/
125 digitalWrite(buzzerPin, HIGH); // Turn off the buzzer for the other half of the period
126 delay(1000);
127 digitalWrite(buzzerPin, LOW);
128 delay(1000);
129}
130
132 return;
133}
134
135void check_val(const CAN_message_t& msg) {
136 if (msg.id == 0x123) {
137 int val = msg.buf[0];
138 if (pushbutton.update() && val > 100) {
139 if (pushbutton.fallingEdge())
141 }
142 } else
143 return;
144}
145
146r2d_mode r2d_state_machine(r2d_mode cur_state, int apps_value) {
147 r2d_mode next_state = cur_state;
148 switch (cur_state) {
149 case R2D_MODE_STARTUP:
150 delay(POWER_ON_DELAY_MS);
152 sound = false;
153 break;
154 case R2D_MODE_IDLE:
155
156 // if(check_bamocar() != 0) next_state = R2D_MODE_ERROR;
157
158 // if(check_BMS()) {}
159
160 // check apps
161 // check modo dash
162 // check modo volante
163
164 // check fim precharge
165 // check r2d button
166 // check brake
167 if (digitalRead(pin_precharge) == LOW && digitalRead(pin_shutdown_circuit) == LOW) {
168 CAN_message_t msg;
169 int val = 0;
170 // can1.onReceive(check_val);
171 }
172 // update display
173 break;
174 case R2D_MODE_DRIVE:
175
176 if (!sound) {
178 sound = true;
179 }
180
181 // if (check_bamocar() != 0){
182 // next_state = R2D_MODE_ERROR;
183 //}
184 // if(check_BMS()) {}
185
186 // check apps
187 // check modo dash
188 // check modo volante
189 // else {
190 // ler seletor do volante referente aos modos (para limitar o bamocar) -> usar código do Bernardo mas enviamos como parâmetros os valores lidos da BMS
191
192 // send_to_bamocar(apps_value);
193
194 // if(o carro desligar temos de atualizar o estado para o básico) next_state = R2D_MODE_STARTUP;
195 //}
196 break;
197 case R2D_MODE_ERROR:
199 sound = false;
200 break;
201
202 default:
204 sound = false;
205 break;
206 }
207 return next_state;
208}
CAN_message_t bamo_apps
bool sound
Definition r2d.cpp:18
r2d_mode r2d_state_machine(r2d_mode cur_state, int apps_value)
Definition r2d.cpp:146
CAN_message_t request_bamo
int pin_selector_2
Definition r2d.cpp:14
int pin_current_sensor
Definition r2d.cpp:12
int pin_precharge
Definition r2d.cpp:16
void play_r2d_sound()
Definition r2d.cpp:124
int pin_selector_1
Definition r2d.cpp:13
Bounce pushbutton
Definition r2d.cpp:21
void r2d_state_update(r2d_mode *state)
Definition r2d.cpp:131
void check_val(const CAN_message_t &msg)
Definition r2d.cpp:135
r2d_mode next_state
Definition r2d.cpp:19
int pin_shutdown_circuit
Definition r2d.cpp:15
@ R2D_MODE_STARTUP
Definition r2d.h:7
@ R2D_MODE_ERROR
Definition r2d.h:10
@ R2D_MODE_IDLE
Definition r2d.h:8
@ R2D_MODE_DRIVE
Definition r2d.h:9
enum _r2d_mode r2d_mode
#define POWER_ON_DELAY_MS
Definition r2d.h:4
#define buzzerPin
CAN_message_t msg