-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speed.ino
62 lines (52 loc) · 1.5 KB
/
Speed.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <SPI.h> // SPI is used to talk to the CAN Controller
#include <mcp_can.h>
#define CAN_ID_PID 0x7DF
MCP_CAN CAN(9); // set SPI Chip Select to pin 9
unsigned char len = 0;
unsigned char buf[8];
unsigned int canID;
uint8_t sent_speed_rq = 0; // sent speed request flag
void setup()
{
Serial.begin(115200); // this is for communicating with the serial monitor
//tries to initialize, if failed --> it will loop here forever
START_INIT:
if (CAN_OK == CAN.begin(CAN_500KBPS)) // setting CAN baud rate to 500Kbps
{
Serial.println("Init success!");
}
else
{
Serial.println("Init failed");
delay(1000);
goto START_INIT;
}
}
void loop()
{
// if we just received a message from the CAN and haven't sent another request yet...
if (!sent_speed_rq)
{
requestSpeed();
}
readIncomingCANData();
}
void readIncomingCANData()
{
if (CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buffer
canID = CAN.getCanId(); // getting the ID of the incoming message
if (canID == 0x7E8 && buf[2] == 0x0D) // reading only our 0x7E8 message
{
Serial.println(buf[3]);
sent_speed_rq = 0;
}
}
}
void requestSpeed()
{
unsigned char tmp[8] = {0x02, 0x01, 0x0D, 0, 0, 0, 0, 0};
CAN.sendMsgBuf(0x7DF, 0, 8, tmp);
sent_speed_rq = 1; // set request flag
}