-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelToM150.ino
81 lines (59 loc) · 1.93 KB
/
accelToM150.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* october or november 2024
* TFR CAN Message Echo Code
* Adafruit feather m4 CAN
* also uses Izze racing 6dof accelerometer
* receives accelerometer messages and transmits them to M150 over different CAN Ids
* because in the old days we couldn't set the ecu to receive messages over CAN IDs that weren't multiples of 16
*
* ari reischer
*/
/*
*/
#include <CANSAME5x.h>
void rx2Tx(int CAN_ID_RX, int CAN_ID_TX);
CANSAME5x CAN;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println("CAN Sender");
pinMode(PIN_CAN_STANDBY, OUTPUT);
digitalWrite(PIN_CAN_STANDBY, false); // turn off STANDBY
pinMode(PIN_CAN_BOOSTEN, OUTPUT);
digitalWrite(PIN_CAN_BOOSTEN, true); // turn on booster
// start the CAN bus at 1 Mbps
if (!CAN.begin(1000000)) {
Serial.println("Starting CAN failed!");
while (1) delay(10);
}
Serial.println("Starting CAN!");
}
void loop() {
// Call function to echo Rotation Rates and Acceleration from the IMU's default CAN ID to one that MoTeC can read
// Rotation Rates
// Roll, Pitch, and Yaw, with 2 bytes for each in the message
rx2Tx(0x4EC, 0x4E0);
// Acceleration
// Longitudinal, Latitudinal, and Vertical, with 2 bytes for each in the message
rx2Tx(0x4ED, 0x4E1);
delay(1);
}
void rx2Tx(int CAN_ID_RX, int CAN_ID_TX) {
// Run checks for CAN message:
// - Check if a packet can be parsed
// - Check if CAN message is not RTR (Remote Request Frame)
// - Check if CAN is avaiable
if (CAN.parsePacket() && !CAN.packetRtr() && CAN.available()) {
// Read only the specified CAN ID
if (CAN.packetId() == CAN_ID_RX) {
// begin a message on the write CAN ID
CAN.beginPacket(CAN_ID_TX);
// echo the message from read ID to write ID
while (CAN.available()) {
CAN.write(CAN.read());
}
// finish and send CAN write packet
CAN.endPacket();
}
}
}