This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduwbl.ino
223 lines (198 loc) · 5.22 KB
/
arduwbl.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <Arduino.h>
#include <mcp2515.h>
#include <avr/wdt.h>
#include <SPI.h>
#define CAN0_INT 2
#define WBL_PIN 5
#define SERIAL_SPEED 115200
#define INTACTIVE_DELAY_SECONDS 3
#define MINIMUM_ENGINE_RUN_TIME_SECONDS 20
#define PRINT_STATUS_DURATION_S 30
MCP2515 mcp2515(10);
// volatile bool interrupt = false;
unsigned long lastPrint = millis();
struct EngineInfo
{
unsigned long lastActivity;
unsigned long engineStart;
bool engineRunning;
bool wblOn;
};
EngineInfo engInfo = {0, 0, false, false};
// void irqHandler()
//{
// interrupt = true;
// }
void setup()
{
pinMode(CAN0_INT, INPUT);
pinMode(WBL_PIN, OUTPUT);
digitalWrite(WBL_PIN, LOW);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
// mcp2515.setConfigMode();
mcp2515.setFilterMask(MCP2515::MASK0, false, 0x7FF);
mcp2515.setFilterMask(MCP2515::MASK1, false, 0x7FF);
mcp2515.setFilter(MCP2515::RXF0, false, 0x280);
mcp2515.setFilter(MCP2515::RXF1, false, 0x5C0);
mcp2515.setListenOnlyMode();
// Using built in interrupt handler sometimes freezes mcp2515 if ISF is called
// mid SPI transfer it seems..
// attachInterrupt(digitalPinToInterrupt(2), irqHandler, FALLING);
Serial.begin(SERIAL_SPEED);
Serial.println("WBL controller started");
wdt_enable(WDTO_1S);
}
void loop()
{
if (!digitalRead(CAN0_INT))
{
handleInterrupt();
}
if (millis() < engInfo.lastActivity)
{
}
if ((millis() - engInfo.lastActivity) / 1000 >= INTACTIVE_DELAY_SECONDS && (engInfo.engineRunning || engInfo.wblOn))
{
engInfo.engineRunning = false;
engInfo.engineStart = 0;
if (engInfo.wblOn)
{
Serial.println("shutting off WBL due to inactivity");
engInfo.wblOn = false;
digitalWrite(WBL_PIN, LOW);
}
}
if (engInfo.engineRunning && (millis() - engInfo.engineStart) / 1000 > MINIMUM_ENGINE_RUN_TIME_SECONDS)
{
if (!engInfo.wblOn)
{
Serial.println("wbl on");
engInfo.wblOn = true;
digitalWrite(WBL_PIN, HIGH);
}
}
else
{
if (engInfo.wblOn)
{
Serial.println("wbl off");
engInfo.wblOn = false;
digitalWrite(WBL_PIN, LOW);
}
}
if (millis() - lastPrint >= (PRINT_STATUS_DURATION_S * 1000) && engInfo.engineRunning)
{
printStats();
}
wdt_reset();
}
void printStats()
{
lastPrint = millis();
Serial.print("engine running for ");
Serial.print((millis() - engInfo.engineStart) / 1000);
Serial.println(" seconds");
}
void handleInterrupt()
{
// interrupt = false;
uint8_t irq = mcp2515.getInterrupts();
mcp2515.clearInterrupts();
if (irq & MCP2515::CANINTF_RX0IF)
{
struct can_frame frame;
if (mcp2515.readMessage(MCP2515::RXB0, &frame) == MCP2515::ERROR_OK)
{
processFrame(&frame);
}
}
if (irq & MCP2515::CANINTF_RX1IF)
{
struct can_frame frame;
if (mcp2515.readMessage(MCP2515::RXB1, &frame) == MCP2515::ERROR_OK)
{
processFrame(&frame);
}
}
if ((irq & MCP2515::CANINTF_ERRIF) || (irq & MCP2515::CANINTF_MERRF))
{
checkErr();
}
}
void processFrame(can_frame *frame)
{
if (frame->can_id == 0x5C0)
{
int8_t temp = (int8_t)frame->data[1] - 40;
Serial.print("coolant temp: ");
Serial.print(temp);
Serial.println(" C");
}
if (frame->can_id == 0x280)
{
engInfo.lastActivity = millis();
if (frame->data[5] & 0x80)
{
if (!engInfo.engineRunning)
{
engInfo.engineStart = engInfo.lastActivity;
engInfo.engineRunning = true;
}
}
else
{
engInfo.engineRunning = false;
}
}
}
void serialPrint(can_frame *f)
{
Serial.print(millis(), HEX);
Serial.print(" ");
Serial.print(f->can_id, HEX);
Serial.print(":");
for (int i = 0; i < f->can_dlc; i++)
{
char buf[3];
sprintf(buf, "%02X", f->data[i]);
Serial.print(buf);
}
Serial.println();
}
void checkErr()
{
uint8_t u8ErrorFlag = mcp2515.getErrorFlags();
if (u8ErrorFlag & MCP2515::EFLG_RX1OVR)
{
Serial.println("CanShield error RX1OVR: receive buffer 1 overflow");
}
// if (u8ErrorFlag & MCP2515::EFLG_RX0OVR)
//{
// Serial.println("CanShield error RX0OVR: receive buffer 0 overflow");
// }
if (u8ErrorFlag & MCP2515::EFLG_TXBO)
{
Serial.println("CanShield error TXBO: bus off");
}
if (u8ErrorFlag & MCP2515::EFLG_TXEP)
{
Serial.println("CanShield error TXEP: transmit error-passive");
}
if (u8ErrorFlag & MCP2515::EFLG_RXEP)
{
Serial.println("CanShield error RXEP: receive error-passive");
}
if (u8ErrorFlag & MCP2515::EFLG_TXWAR)
{
Serial.println("CanShield error TXWAR: transmit error warning");
}
if (u8ErrorFlag & MCP2515::EFLG_RXWAR)
{
Serial.println("CanShield error RXWAR: receive error warning ");
}
if (u8ErrorFlag & MCP2515::EFLG_EWARN)
{
Serial.println("CanShield error EWARN: error warning ");
}
}