-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
322 lines (250 loc) · 6.46 KB
/
main.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include "can/can.h"
#include "setup.h"
#include "byteworker.h"
#include "timer.h"
#include "adc.h"
#define VERSION_MAJOR (1)
#define VERSION_MINOR (10)
void can_parse_msgs(can_t *msg);
void emergencyOff(void);
void msgSendInputStatus(uint32_t now, uint16_t temp);
void msgSendState(uint8_t state, uint8_t lastState, uint8_t errorCode);
void msgSendVersion(void);
void msgMarker(uint8_t id);
int main(void) {
wdt_disable();
// The idea here is to have simple temperature regulation of the battery.
// There are 2 temperatures to consider. The temperature of the heating
// element and the internal temperature of the battery.
// The target temperature for the battery is 20 degrees however the outer shell
// is not conducting heat very efficiently. However heat transfer can be increased
// by raising the temperature of the heater above the target temperature of the battery.
// Temperatures of 60 degrees on the outer shell are acceptable.
uint32_t now = 0;
uint32_t timeInputStatusMsg = 0;
uint32_t timeStateMsg = 0;
uint32_t timeVersionMsg = 0;
uint32_t timeHeaterStart= 0;
uint32_t timeLedAlertDuty = 0;
uint16_t heaterAdcRaw = 0;
uint16_t tempFiltered = 0;
uint8_t inRange = FALSE;
uint8_t state = eState_init;
uint8_t lastState = eState_init;
uint8_t maxBatteryTemp = 0;
uint8_t minBatteryTemp = 0;
can_t msgRx;
timer_init();
adc_init();
bw_canInit(500 /*kbit*/);
sei();
wdt_enable(WDTO_250MS);
wdt_reset();
bw_ledSet(eBlueLed, 0);
bw_ledSet(eGreenLed, 0);
tempFiltered = adc_read(eIn_heaterTemp); // get an acceptable start value
while( 1 ) {
wdt_reset();
now = timer_getMs();
if(now > 0x80000000) {
// force reboot when timestamps are getting too big
// this prevents overflow and related inconsistent
// behaviour.
emergencyOff();
while(1);
}
// get can messages
while(can_check_message()) {
can_get_message(&msgRx);
switch(msgRx.id) {
case eMsgId_batteryTempMsg: {
// TODO: update min/max temp values
break;
}
case eMsgId_forceState: {
if(eState_cooling == msgRx.data[0]) {
state = eState_cooling;
}
break;
}
case eMsgId_forceReboot: {
emergencyOff();
while(1);
break;
}
default: {
break;
}
} // switch
} // while
heaterAdcRaw = adc_read(eIn_heaterTemp);
// uint16_t heaterAdc = adc_value2Temp(heaterAdcRaw);
tempFiltered = ((tempFiltered * (eTempFilterIterations-1)) + heaterAdcRaw) / eTempFilterIterations;
inRange = adc_tempInRange(tempFiltered);
if(TRUE != inRange) {
state = eState_emergencyOff;
}
// TODO: check for sudden temperature jumps (sensor damaged?)
// select new state
switch(state) {
case eState_init: // fall through
case eState_cooling: {
if(tempFiltered < eTempLowThresh) {
state = eState_heating;
}
break;
}
case eState_heating: {
if(now > (timeHeaterStart + eTimeout_heaterUseage)) {
state = eState_emergencyOff; // heating phase too long
}
if(tempFiltered > eTempHighThresh) {
state = eState_cooling;
}
break;
}
case eState_forceOn: {
// TODO: manual override via CAN
break;
}
case eState_forceOff: {
// manual override via CAN
break;
}
case eState_emergencyOff: // fall through
default: {
state = eState_emergencyOff;
// never regenerate from this state
break;
}
}
if(lastState != state) {
msgSendState(state, lastState, 0); // report state changes
}
// react on state
switch(state) {
default: // fall through
case eState_init: // fall through
case eState_cooling: // fall through
case eState_emergencyOff: {
bw_outputSet(eOut_relais1, eOut_off);
bw_outputSet(eOut_relais2, eOut_off);
bw_ledSet(eBlueLed, 0);
break;
}
case eState_forceOn: // fall through
case eState_heating: {
if(lastState != state) {
timeHeaterStart = now;
}
bw_outputSet(eOut_relais1, eOut_on);
bw_outputSet(eOut_relais2, eOut_on);
bw_ledSet(eBlueLed, 1);
break;
}
}
/*
// calc temp
// 106k : 24
// 100k : 33
// 90k : 40
// 80k : 50
// 70k : 60
// 60k : 73
// 50k : 83
// 40k : 98
// 50k : 83 <-- 0x212(530);
// 60k : 73 <-- 0x1FC(508)
// 80k : 50 <-- 0x1D1(465)
*/
if(timeInputStatusMsg < now) {
timeInputStatusMsg = now + eDelay_inputStatusMsgCycle;
msgSendInputStatus(now, tempFiltered);
}
if(timeVersionMsg < now) {
timeVersionMsg = now + eDelay_versionMsgCycle;
msgSendVersion();
}
if(timeStateMsg < now) {
timeStateMsg = now + eDelay_stateMsgCycle;
msgSendState(state, lastState, 0);
}
if((timeLedAlertDuty < now) && (eState_emergencyOff == state)) {
timeLedAlertDuty = now + eDelay_alertDuty;
bw_ledToggle(eBlueLed);
}
lastState = state;
timer_wait(100);
} // while(1)
}
void can_wakeBus( void ) {
#ifdef AUTO_WAKE_BUS
// 423#03.00.FF.FF.00.E0.00.00
can_t msg = {
.id = eWakeBusMsg,
.flags = { .rtr = 0, .extended = 0 },
.length = 8,
.data = { 0x03, 0x00, 0xFF, 0xFF, 0x00, 0xE0, 0x00, 0x00 }
};
can_send_message( &msg );
#endif
}
void emergencyOff(void) {
// software detected a serious problem
// make sure the heater stays off and
// never turns back on!
bw_outputSet(eOut_relais1, eOut_off);
bw_outputSet(eOut_relais2, eOut_off);
}
void msgSendInputStatus(uint32_t now, uint16_t temp) {
can_t msg = {
.id = eMsgId_reportInputStatus,
.flags = { .rtr = 0, .extended = 1 },
.length = 6,
.data = {
(now>>24) & 0xFF, // time since boot
(now>>16) & 0xFF, // in milliseconds
(now>> 8) & 0xFF, // in big endian
(now) & 0xFF,
((temp>>8) & 0xFF), // state of relais | heater ADC
msg.data[5] = temp & 0xFF,
}
};
can_send_message(&msg);
}
void msgSendState(uint8_t state, uint8_t lastState, uint8_t errorCode) {
can_t msg = {
.id = eMsgId_reportState,
.flags = { .rtr = 0, .extended = 1 },
.length = 3,
.data = {
state,
lastState,
errorCode
}
};
can_send_message(&msg);
}
void msgSendVersion(void) {
can_t msg = {
.id = eMsgId_reportVersion,
.flags = { .rtr = 0, .extended = 1 },
.length = 2,
.data = { VERSION_MAJOR, VERSION_MINOR }
};
can_send_message(&msg);
}
void msgMarker(uint8_t id) {
can_t msg = {
.id = eMsgId_marker,
.flags = { .rtr = 0, .extended = 1 },
.length = 1,
.data = { id }
};
can_send_message(&msg);
}