Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <FlexCAN_T4.h>
3#include <math.h>
4#include <elapsedMillis.h>
5#include <logging.h>
6#include "rpm.h"
7
8
9#define AVG_SAMPLES 20
10
11#define BRAKE_LIGHT 2
12#define BRAKE_LIGHT_LOWER_THRESH 165 // 165/1023 * 3.3V = 0.532V
13#define BRAKE_LIGHT_UPPER_THRESH 510 // /1023 * 3.3V = 0.78V
14#define BRAKE_LIGHT_BRIGHTNESS 150 // 0-255
15#define BRAKE_LIGHT_MIN_ACTIVE_PERIOD 200 // ms
16
17#define BRAKE_SENSOR_PIN A5
18#define CURRENT_SENSOR_PIN A4
19#define RIGHT_WHEEL_ENCODER_PIN 30
20#define LEFT_WHEEL_ENCODER_PIN 28
21
22#define RPM_PUBLISH_PERIOD 5000 // micro secs
23#define WPS_PULSES_PER_ROTATION 36 // Number of pulses per one rotation of the wheel
24#define LIMIT_RPM_INTERVAL 500000 // Time limit, in micros, after which rpms go to zero
25#define SENSOR_SAMPLE_PERIOD 20 // ms
26
27#define CAN_BAUD_RATE 500000
28#define CAN_TRANSMISSION_PERIOD 100 // ms
29
30#define C1_ID 0x123
31
32#define BRAKE_MSG_1ST_BYTE 0x90
33#define RR_RPM_MSG_1ST_BYTE 0x11
34#define RL_RPM_MSG_1ST_BYTE 0x12
35
36#define LOGGING_PERIOD 10
37
38#define BAMO_SPEED 0x30
39#define BAMO_RPM_MOTOR 0xCE
40#define BAMO_ACTUAL_CURRENT 0x5f
41#define BAMO_MOTOR_TEMP 0x49
42#define BAMO_POWER_STAGE_TEMP 0x4A
43#define BAMO_MOTOR_VOLTAGE 0x8A
44#define BAMO_MOTOR_TORQUE 0xA0
45#define BAMO_DC_VOLTAGE 0xeb
46
47uint16_t brake_val = 0;
48
49elapsedMicros rpm_publisher_timer; // Right Wheel Sensor Timer
50elapsedMillis canTimer;
51elapsedMillis brake_sensor_timer;
53elapsedMillis writeTIMER;
54elapsedMillis CURRENTtimer;
55
57
58
59int current = 0;
60int voltage = 0;
61int mintmp = 0;
62int maxtmp = 0;
63int avgtmp = 0;
64int apps1 = 0;
65int apps2 = 0;
66int brake = 0;
67
68int speed = 0;
69int rpm_max = 0;
70int I_actual = 0;
72int motorTemp = 0;
73int lemos = 0;
74int motorTemp2 = 0;
76int torque = 0;
79
80float rr_rpm;
81float rl_rpm;
82unsigned long last_wheel_pulse_rr;
83unsigned long last_wheel_pulse_rl;
86
87FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
88
89CAN_message_t brake_sensor_c3;
90CAN_message_t rr_rpm_msg;
91CAN_message_t rl_rpm_msg;
92CAN_message_t current_controll;
93
94int8_t current_byte1; // MSB
95int8_t current_byte2; // LSB
97
98bool R2D = false;
99
101
102int average(int *buffer, int n)
103{
104 int sum = 0;
105 for (int i = 0; i < n; i++)
106 {
107 sum += buffer[i];
108 }
109 return sum / n;
110}
111
112void bufferInsert(int *buffer, int n, int value)
113{
114 for (int i = 0; i < n - 1; i++)
115 {
116 buffer[i] = buffer[i + 1];
117 }
118 buffer[n - 1] = value;
119}
120
122{
123 current_controll.id = 0x201;
124 current_controll.len = 3;
125 current_controll.buf[0] = 0xfb;
126
128 brake_sensor_c3.len = 3;
130
131 rr_rpm_msg.id = C1_ID;
132 rr_rpm_msg.len = 5;
134 rl_rpm_msg.id = C1_ID;
135 rl_rpm_msg.len = 5;
137}
138
139void canbusSniffer(const CAN_message_t &msg)
140{
141 #ifdef DEBUG
142 Serial.println("CAN message received");
143 Serial.print("Message ID: ");
144 Serial.println(msg.id, HEX);
145 #endif
146 switch (msg.id)
147 {
148 case BMS_ID:
149 current = ((msg.buf[0] << 8) | msg.buf[1]);
150 voltage = ((msg.buf[5] << 8) | msg.buf[6]) / 10;
151 mintmp = msg.buf[2];
152 maxtmp = msg.buf[3];
153 avgtmp = msg.buf[4];
154 break;
155
156 case 0x111:
157 apps1 = ((msg.buf[1] << 8) | msg.buf[0]);
158 apps2 = ((msg.buf[3] << 8) | msg.buf[2]);
159 break;
160 case BAMO_RESPONSE_ID:
161 if (msg.buf[0] == BAMO_SPEED)
162 {
163 speed = (msg.buf[2] << 8) | msg.buf[1];
164 }
165 if (msg.buf[0] == BAMO_RPM_MOTOR)
166 {
167 rpm_max = (msg.buf[2] << 8) | msg.buf[1];
168 }
169 if (msg.buf[0] == BAMO_ACTUAL_CURRENT)
170 {
171 I_actual = (msg.buf[2] << 8) | msg.buf[1];
172 }
173 if (msg.buf[0] == BAMO_MOTOR_TEMP)
174 {
175 motorTemp = (msg.buf[2] << 8) | msg.buf[1];
176 }
177 if (msg.buf[0] == BAMO_POWER_STAGE_TEMP)
178 {
179 powerStageTemp = (msg.buf[2] << 8) | msg.buf[1];
180 }
181 if (msg.buf[0] == BAMO_MOTOR_VOLTAGE)
182 {
183 motor_voltage = (msg.buf[2] << 8) | msg.buf[1];
184 }
185 if (msg.buf[0] == BAMO_MOTOR_TORQUE)
186 {
187 torque = (msg.buf[2] << 8) | msg.buf[1];
188 }
189 if (msg.buf[0] == BAMO_DC_VOLTAGE)
190 {
191 battery_voltage = (msg.buf[2] << 8) | msg.buf[1];
192 }
193
194 break;
195 }
196}
197
199{
200 can1.begin();
201 can1.setBaudRate(500000);
202 can1.enableFIFO();
203 can1.enableFIFOInterrupt();
204 can1.setFIFOFilter(REJECT_ALL);
205 can1.setFIFOFilter(0, 0x111, STD);
206 can1.setFIFOFilter(1, BMS_ID, STD);
207 can1.setFIFOFilter(2, BAMO_RESPONSE_ID, STD);
208 can1.onReceive(canbusSniffer);
209 initMessages();
210}
211
212void sendBrakeVal(uint16_t brake_value)
213{
214 brake_sensor_c3.buf[2] = (brake_value >> 8) & 0xFF; // MSB
215 brake_sensor_c3.buf[1] = brake_value & 0xFF; // LSB
216
217 can1.write(brake_sensor_c3);
218}
219
221{
223 {
226 return true;
227 }
229 {
230 analogWrite(BRAKE_LIGHT, 0);
231 return false;
232 }
233 return false;
234}
235
236void setup()
237{
239
242 #ifdef DEBUG
243 Serial.begin(9600);
244 #endif
245
246 canbusSetup();
248 pinMode(BRAKE_SENSOR_PIN, INPUT);
249 pinMode(BRAKE_LIGHT, OUTPUT);
250
251 attachInterrupt(digitalPinToInterrupt(RIGHT_WHEEL_ENCODER_PIN), []()
252 {
254 last_wheel_pulse_rr = micros();
255 }, RISING);
256 attachInterrupt(digitalPinToInterrupt(LEFT_WHEEL_ENCODER_PIN), []()
257 {
259 last_wheel_pulse_rl = micros();
260 }, RISING);
261}
262
263void loop()
264{
266 {
268 brake_val = analogRead(BRAKE_SENSOR_PIN);
272 #ifdef DEBUG
273 Serial.print("Reading Brake line:");
274 Serial.println(brake_val);
275 #endif
277 {
278 #ifdef DEBUG
279 Serial.println("Brake Light ON");
280 #endif
281 }
283 {
284 // Serial.println("Message sent");
286 canTimer = 0;
287 }
288 }
289
291 #ifdef DEBUG
292 Serial.print("Publishing RR RPM: ");
293 Serial.println(rr_rpm);
294 Serial.print("Publishing RL RPM: ");
295 Serial.println(rl_rpm);
296 #endif
297 unsigned long time_interval_rr = (last_wheel_pulse_rr - second_to_last_wheel_pulse_rr);
298 unsigned long time_interval_rl = (last_wheel_pulse_rl - second_to_last_wheel_pulse_rl);
299 rr_rpm = micros() - last_wheel_pulse_rr > LIMIT_RPM_INTERVAL ? 0.0 : 1 / (time_interval_rr * 1e-6 * WPS_PULSES_PER_ROTATION ) * 60;
300 rl_rpm = micros() - last_wheel_pulse_rl > LIMIT_RPM_INTERVAL ? 0.0 : 1 / (time_interval_rl * 1e-6 * WPS_PULSES_PER_ROTATION ) * 60;
301 char rr_rpm_byte[4];
302 char rl_rpm_byte[4];
303 rpm_2_byte(rr_rpm, rr_rpm_byte);
304 rpm_2_byte(rl_rpm, rl_rpm_byte);
305 rr_rpm_msg.buf[1] = rr_rpm_byte[0];
306 rr_rpm_msg.buf[2] = rr_rpm_byte[1];
307 rr_rpm_msg.buf[3] = rr_rpm_byte[2];
308 rr_rpm_msg.buf[4] = rr_rpm_byte[3];
309 rl_rpm_msg.buf[1] = rl_rpm_byte[0];
310 rl_rpm_msg.buf[2] = rl_rpm_byte[1];
311 rl_rpm_msg.buf[3] = rl_rpm_byte[2];
312 rl_rpm_msg.buf[4] = rl_rpm_byte[3];
313 can1.write(rr_rpm_msg);
314 can1.write(rl_rpm_msg);
316 }
317
318
320 {
322 // current = 0; voltage = 0; mintmp = 0; maxtmp = 0; avgtmp = 0; apps1 = 0; apps2 = 0; brake = 0;
323 // speed = 0; I_actual = 0; powerStageTemp = 0; motorTemp = 0;
324 writeTIMER = 0;
325 }
326 if (CURRENTtimer > 8)
327 {
328 CURRENTtimer = 0;
329
330 current_byte1 = (I_actual >> 8) & 0xFF; // MSB
331 current_byte2 = I_actual & 0xFF; // LSB
332
333 current_message_bamo.id = 0x201;
334 current_message_bamo.len = 5;
335 current_message_bamo.buf[0] = 0xfb;
338 current_message_bamo.buf[3] = 0x00;
339 current_message_bamo.buf[4] = 0x00;
340
342 }
343}
void setup()
Definition main.cpp:10
void loop()
AMI main loop: Button gets picked and lights up the corresponding LED LAST BUTTON CHECKED WINS.
Definition main.cpp:23
int voltage
Definition main.cpp:60
#define BRAKE_LIGHT_UPPER_THRESH
Definition main.cpp:13
int powerStageTemp2
Definition main.cpp:75
elapsedMillis canTimer
Definition main.cpp:50
int powerStageTemp
Definition main.cpp:71
float rl_rpm
Definition main.cpp:81
int torque
Definition main.cpp:76
#define C1_ID
Definition main.cpp:30
float rr_rpm
Definition main.cpp:80
int motorTemp2
Definition main.cpp:74
#define BAMO_POWER_STAGE_TEMP
Definition main.cpp:42
int I_actual
Definition main.cpp:70
#define BAMO_SPEED
Definition main.cpp:38
int avgtmp
Definition main.cpp:63
int apps2
Definition main.cpp:65
bool brakeLightControl(int brake_val)
Definition main.cpp:220
elapsedMicros rpm_publisher_timer
Definition main.cpp:49
int speed
Definition main.cpp:68
int battery_voltage
Definition main.cpp:78
unsigned long last_wheel_pulse_rl
Definition main.cpp:83
int rpm_max
Definition main.cpp:69
int motorTemp
Definition main.cpp:72
elapsedMillis writeTIMER
Definition main.cpp:53
#define LEFT_WHEEL_ENCODER_PIN
Definition main.cpp:20
#define BRAKE_LIGHT
Definition main.cpp:11
int8_t current_byte2
Definition main.cpp:95
void bufferInsert(int *buffer, int n, int value)
Definition main.cpp:112
unsigned long second_to_last_wheel_pulse_rl
Definition main.cpp:85
#define BAMO_MOTOR_TEMP
Definition main.cpp:41
void canbusSniffer(const CAN_message_t &msg)
Definition main.cpp:139
int motor_voltage
Definition main.cpp:77
#define RL_RPM_MSG_1ST_BYTE
Definition main.cpp:34
CAN_message_t brake_sensor_c3
Definition main.cpp:89
#define WPS_PULSES_PER_ROTATION
Definition main.cpp:23
CAN_message_t current_controll
Definition main.cpp:92
int brake
Definition main.cpp:66
uint16_t brake_val
Definition main.cpp:47
#define BRAKE_LIGHT_MIN_ACTIVE_PERIOD
Definition main.cpp:15
int8_t current_byte1
Definition main.cpp:94
void sendBrakeVal(uint16_t brake_value)
Definition main.cpp:212
#define BAMO_MOTOR_VOLTAGE
Definition main.cpp:43
CAN_message_t current_message_bamo
Definition main.cpp:96
#define BAMO_ACTUAL_CURRENT
Definition main.cpp:40
#define BAMO_RPM_MOTOR
Definition main.cpp:39
elapsedMillis brake_sensor_timer
Definition main.cpp:51
#define BAMO_DC_VOLTAGE
Definition main.cpp:45
unsigned long second_to_last_wheel_pulse_rr
Definition main.cpp:84
CAN_message_t rr_rpm_msg
Definition main.cpp:90
void initMessages()
Definition main.cpp:121
elapsedMillis brake_light_active_timer
Definition main.cpp:52
#define SENSOR_SAMPLE_PERIOD
Definition main.cpp:25
int current
Definition main.cpp:59
#define BRAKE_MSG_1ST_BYTE
Definition main.cpp:32
#define BRAKE_SENSOR_PIN
Definition main.cpp:17
#define CAN_TRANSMISSION_PERIOD
Definition main.cpp:28
#define BAMO_MOTOR_TORQUE
Definition main.cpp:44
int mintmp
Definition main.cpp:61
void canbusSetup()
Definition main.cpp:198
int maxtmp
Definition main.cpp:62
elapsedMillis CURRENTtimer
Definition main.cpp:54
Logging loggingInstance
Definition main.cpp:56
#define LIMIT_RPM_INTERVAL
Definition main.cpp:24
unsigned long last_wheel_pulse_rr
Definition main.cpp:82
int apps1
Definition main.cpp:64
int lemos
Definition main.cpp:73
#define BRAKE_LIGHT_BRIGHTNESS
Definition main.cpp:14
#define AVG_SAMPLES
Definition main.cpp:9
CAN_message_t rl_rpm_msg
Definition main.cpp:91
#define RPM_PUBLISH_PERIOD
Definition main.cpp:22
#define BRAKE_LIGHT_LOWER_THRESH
Definition main.cpp:12
#define RR_RPM_MSG_1ST_BYTE
Definition main.cpp:33
#define RIGHT_WHEEL_ENCODER_PIN
Definition main.cpp:19
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can1
Definition main.cpp:87
#define LOGGING_PERIOD
Definition main.cpp:36
bool R2D
Definition main.cpp:98
int avgBuffer1[AVG_SAMPLES]
Definition main.cpp:100
void write_to_file(int current, int voltage, int mintmp, int maxtmp, int avgtmp, int apps1, int apps2, int brake, int rpm, int I_actual, int powerStageTmp, int motorTmp, int Torque, int motor_voltage, int battery_voltage)
Definition logging.cpp:90
void setup_log()
Definition logging.cpp:56
#define BMS_ID
Definition logging.h:21
#define BAMO_RESPONSE_ID
Definition logging.h:22
long average
Definition message.cpp:14
void rpm_2_byte(float rr_rpm, char *rr_rpm_byte)
Definition rpm.h:12
CAN_message_t msg