Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
message.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <FlexCAN_T4.h>
3
4FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
5
6CAN_message_t msg2;
7
8long //Parameters
9const int aisPin = 15;
10const int numReadings = 10;
12int readIndex = 0;
13long total = 0;
14long average = 0;
15
16long smooth() { /* function smooth */
18
19 // subtract the last reading:
21 // read the sensor:
22 readings[readIndex] = analogRead(aisPin);
23 // add value to total:
25 // handle index
26 readIndex = readIndex + 1;
27 if (readIndex >= numReadings) {
28 readIndex = 0;
29 }
30 // calculate the average:
32
33 return average;
34}
35
36void setup() {
37 Serial.begin(9600);
38
39 can1.begin();
40 can1.setBaudRate(500000);
41
42 //CAN_message_t msg3;
43 //msg3.id = 0x201;
44 //msg3.len = 3;
45 //msg3.buf[0] = 0x51;
46 //msg3.buf[1] = 0x00;
47 //msg3.buf[2] = 0x00;
48 //can1.write(msg3);
49}
50
51void loop() {
52 long val = smooth();
53
54 uint16_t value_bamo = val * 32767.0 / 1023.0;
55
56 uint8_t byte1 = (value_bamo >> 8) & 0xFF; //MSB
57 uint8_t byte2 = value_bamo & 0xFF; //LSB
58
59 Serial.print("byte1: ");
60 Serial.print(byte1,HEX);
61 Serial.print("\n byte2: ");
62 Serial.print(byte2,HEX);
63 //definir a mensagem de acordo com o que o BAMOCAR pede
64 //speed command value
65
66 Serial.printf("\n Value sent: %d",value_bamo);
67
68
69 CAN_message_t msg;
70 msg.id = 0x201;
71 msg.len=3;
72 msg.buf[0] = 0x31;
73 msg.buf[1] = byte2;
74 msg.buf[2] = byte1;
75
76 Serial.print("Sent message with ID 0x");
77 Serial.print(msg.id, HEX);
78 Serial.print(": ");
79 for (int i = 0; i < msg.len; i++) {
80 Serial.print(msg.buf[i]);
81 }
82
83 can1.write(msg);
84
85 Serial.println("\n Message sent!");
86 delay(10);
87
88 /*
89
90 if (can1.read(msg2)) {
91 Serial.print("Received message with ID 0x");
92 Serial.print(msg2.id, HEX);
93 Serial.print(": ");
94 for (int i = 0; i < msg2.len; i++) {
95 Serial.print(msg2.buf[i]);
96 }
97 Serial.print('\n');
98 }
99 */
100
101}
int readIndex
Definition message.cpp:12
long average
Definition message.cpp:14
long total
Definition message.cpp:13
void setup()
Definition message.cpp:36
const int numReadings
Definition message.cpp:10
long const int aisPin
Definition message.cpp:9
int readings[numReadings]
Definition message.cpp:11
long smooth()
Definition message.cpp:16
CAN_message_t msg2
Definition message.cpp:6
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can1
Definition message.cpp:4
void loop()
Definition message.cpp:51
CAN_message_t msg