This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANsimulator.ino
151 lines (129 loc) · 2.3 KB
/
CANsimulator.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdint.h>
#include <FlexCAN.h>
// Vehicle values
uint16_t rpm;
uint16_t oilTemp;
uint16_t waterTemp;
uint16_t brakeTemp;
uint8_t gear;
uint8_t speed;
uint16_t voltage;
bool fanOn;
uint16_t speedCount;
bool reverse;
const uint16_t MAX_RPM = 14000;
static CAN_message_t msg1;
static CAN_message_t msg2;
void demo() {
if (reverse) {
if (rpm != 0) {
rpm -= 25;
}
else {
oilTemp = random(4730, 4830);
waterTemp = random(4030, 4230);
brakeTemp = random(3730, 4230);
voltage = random(11900, 12900);
if (gear > 0) {
gear--;
rpm = 9000;
}
else {
rpm = 0;
speed = 0;
}
}
}
else {
if (rpm != MAX_RPM) {
rpm += 25;
}
else {
oilTemp = random(4730, 4830);
waterTemp = random(4030, 4230);
brakeTemp = random(3730, 4230);
voltage = random(11900, 12900);
if (gear < 6) {
gear++;
rpm = 2000;
}
else {
rpm = 13500;
}
}
}
if (reverse) {
if (speed > 0) {
if (speedCount == 15) {
speed--;
speedCount = 0;
}
else {
speedCount++;
}
}
}
else {
if (speed < 200 && gear != 0) {
if (speedCount == 15) {
speed++;
speedCount = 0;
}
else {
speedCount++;
}
}
}
}
void setup() {
// Initialize the CAN bus
Can0.begin(500000);
Serial.begin(9600);
rpm = 0;
speed = 0;
oilTemp = 130;
waterTemp = 130;
voltage = 12000;
reverse = false;
// CAN message headers
msg1.id = 0x1;
msg1.len = 8;
msg1.flags.extended = 0;
msg1.flags.remote = 0;
msg2.id = 0x2;
msg2.len = 8;
msg2.flags.extended = 0;
msg2.flags.remote = 0;
}
void loop() {
demo();
// CAN message 1 payload
msg1.buf[0] = (uint8_t)rpm;
msg1.buf[1] = rpm >> 8;
msg1.buf[2] = (uint8_t)voltage;
msg1.buf[3] = voltage >> 8;
msg1.buf[4] = (uint8_t)waterTemp;
msg1.buf[5] = waterTemp >> 8;
msg1.buf[6] = (uint8_t)speed;
msg1.buf[7] = 0;
while (!Can0.write(msg1, Can0.getFirstTxBox())) {
}
// CAN message 2 payload
msg2.buf[0] = (uint8_t)oilTemp;
msg2.buf[1] = oilTemp >> 8;
msg2.buf[2] = gear;
msg2.buf[3] = 0;
msg2.buf[4] = (uint8_t)waterTemp;
msg2.buf[5] = waterTemp >> 8;
msg2.buf[6] = speed;
msg2.buf[7] = speed >> 8;
while (!Can0.write(msg2, Can0.getLastTxBox())) {
}
if (gear == 6 && rpm == 14000) {
reverse = true;
}
if (reverse && gear == 0 && rpm == 0) {
reverse = false;
}
delay(5);
}