-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKomaRoof.ino
440 lines (390 loc) · 13.6 KB
/
KomaRoof.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright (c) 2016 Jari Saukkonen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <DallasTemperature.h>
#include <OneWire.h>
#include <TaskScheduler.h>
#include "DualVNH5019MotorShield.h"
#include "NMEASerial.h"
#include "MessageHandler.h"
#include "PowerConsumptionLog.h"
#include "Roof.h"
#include "Settings.h"
#include "Version.h"
void currentMeasurementTick();
void motorTick();
void temperatureTick();
void voltageTick();
void buttonTick();
void timerTick();
void emergencyStop();
static OneWire oneWire(PIN_ONE_WIRE_BUS);
static DallasTemperature dallasTemperature(&oneWire);
static MessageHandler handler;
static NMEASerial serial(&handler);
static DualVNH5019MotorShield motorShield;
static PowerConsumptionLog powerConsumptionLog;
static Task motorTask(100, TASK_FOREVER, &motorTick);
static Task temperatureTask(1000, TASK_FOREVER, &temperatureTick);
static Task voltageTask(1000, TASK_FOREVER, &voltageTick);
static Task buttonTask(100, TASK_FOREVER, &buttonTick);
static Task currentMeasurementTask(10, TASK_FOREVER, ¤tMeasurementTick);
static Task timerTask(100, TASK_FOREVER, &timerTick);
static Scheduler taskScheduler;
static RoofState roofState = STOPPED;
static Phase phase = IDLE;
static int roofSpeed = FULL_SPEED;
static int targetRoofSpeed = 0;
static int tickCount = 0;
static int direction;
static float temperature = DEVICE_DISCONNECTED_C;
static bool temperatureRequested = false;
static float batteryVoltage = 0.0f;
static volatile bool emergencyStopPressed = false;
static volatile bool limitSwitchOpenActive = false;
static volatile bool limitSwitchCloseActive = false;
static volatile int encoderState = 0;
static volatile int encoderPosition = 0;
static unsigned long moveStartTime = 0;
static void emergencyStopISR();
static void limitSwitchCloseISR();
static void limitSwitchOpenISR();
static void encoderGate1ISR();
static void encoderGate2ISR();
static void stop(const String&);
static void open(const String&);
static void close(const String&);
static void setspeed(const String&);
static void status(const String&);
static void test(const String&);
void setup() {
pinMode(PIN_UNUSED, INPUT);
pinMode(PIN_BUTTON_CLOSE, INPUT);
pinMode(PIN_BUTTON_OPEN, INPUT);
pinMode(PIN_BUTTON_EMERGENCYSTOP, INPUT);
pinMode(PIN_LIMITSWITCH_OPEN, INPUT);
pinMode(PIN_LIMITSWITCH_CLOSE, INPUT);
pinMode(PIN_ENCODER_GATE_1, INPUT);
pinMode(PIN_ENCODER_GATE_2, INPUT);
if (digitalRead(PIN_BUTTON_EMERGENCYSTOP) == LOW && digitalRead(PIN_BUTTON_CLOSE) == LOW) {
encoderPosition = ENCODER_RESET_CLOSED;
roofState = CLOSED;
}
if (digitalRead(PIN_BUTTON_EMERGENCYSTOP) == LOW && digitalRead(PIN_BUTTON_OPEN) == LOW) {
encoderPosition = ENCODER_RESET_OPEN;
roofState = OPEN;
}
attachInterrupt(digitalPinToInterrupt(PIN_BUTTON_EMERGENCYSTOP), emergencyStopISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_LIMITSWITCH_CLOSE), limitSwitchCloseISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_LIMITSWITCH_OPEN), limitSwitchOpenISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_ENCODER_GATE_1), encoderGate1ISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_ENCODER_GATE_2), encoderGate2ISR, CHANGE);
Serial.begin(57600);
handler.registerCommand("TEST", test);
handler.registerCommand("STATUS", status);
handler.registerCommand("OPEN", open);
handler.registerCommand("CLOSE", close);
handler.registerCommand("STOP", stop);
handler.registerCommand("SETSPEED", setspeed);
taskScheduler.init();
taskScheduler.addTask(motorTask);
taskScheduler.addTask(buttonTask);
taskScheduler.addTask(currentMeasurementTask);
taskScheduler.addTask(voltageTask);
taskScheduler.addTask(timerTask);
motorShield.init();
dallasTemperature.begin();
if (dallasTemperature.getDeviceCount() > 0) {
dallasTemperature.setWaitForConversion(false);
dallasTemperature.setResolution(12);
taskScheduler.addTask(temperatureTask);
temperatureTask.enable();
}
motorTask.enable();
buttonTask.enable();
currentMeasurementTask.enable();
voltageTask.enable();
test("");
}
void logger(String msg, unsigned long timeMillis=0) {
if (timeMillis == 0) {
timeMillis = millis();
}
String logMessage = "LOG,TIME=";
logMessage += timeMillis;
logMessage += ",ENCODER=";
logMessage += encoderPosition;
logMessage += ",";
logMessage += msg;
serial.print(logMessage);
}
void loop() {
taskScheduler.execute();
}
void serialEvent() {
serial.onSerialEvent();
}
void emergencyStopISR() {
emergencyStopPressed = (digitalRead(PIN_BUTTON_EMERGENCYSTOP) == HIGH);
}
void limitSwitchOpenISR() {
limitSwitchOpenActive = (digitalRead(PIN_LIMITSWITCH_OPEN) == LOW);
}
void limitSwitchCloseISR() {
limitSwitchCloseActive = (digitalRead(PIN_LIMITSWITCH_CLOSE) == LOW);
}
void updateEncoder(int previous, int current) {
static int grayToBinary[4] = { 0, 1, 3, 2 };
previous = grayToBinary[previous & 0x3];
current = grayToBinary[current & 0x3];
if (current == ((previous+1) & 0x3))
encoderPosition++;
if (current == ((previous-1) & 0x3))
encoderPosition--;
}
void encoderGate1ISR() {
int previousEncoderState = encoderState;
encoderState = (encoderState & 0x1) | (digitalRead(PIN_ENCODER_GATE_1) == LOW ? 0x2 : 0);
updateEncoder(previousEncoderState, encoderState);
}
void encoderGate2ISR() {
int previousEncoderState = encoderState;
encoderState = (encoderState & 0x2) | (digitalRead(PIN_ENCODER_GATE_2) == LOW ? 0x1 : 0);
updateEncoder(previousEncoderState, encoderState);
}
void currentMeasurementTick() {
powerConsumptionLog.measure(motorShield.getM1CurrentMilliamps());
}
void timerTick() {
unsigned long t = millis();
if ((roofState == OPENING || roofState == CLOSING) && (t - moveStartTime > MAX_MOVE_DURATION)) {
motorShield.setM1Speed(0);
motorShield.setM2Speed(0);
roofSpeed = targetRoofSpeed = 0;
roofState = ERROR;
phase = IDLE;
logger("ERROR=DURATION");
}
}
void motorTick() {
// called @ 10hz rate
tickCount++;
powerConsumptionLog.appendCurrentMeasurement();
if (tickCount == 10) {
status("");
powerConsumptionLog.report(serial);
tickCount = 0;
}
unsigned int currentThreshold = (phase == CLOSE_TIGHTLY ? MOTOR_CLOSING_CURRENT_LIMIT_MILLIAMPS : MOTOR_CURRENT_LIMIT_MILLIAMPS);
if (powerConsumptionLog.isOverload(currentThreshold)) {
motorShield.setM1Speed(0);
roofSpeed = targetRoofSpeed = 0;
if (roofState == CLOSING && phase == CLOSE_TIGHTLY) {
roofState = CLOSED;
phase = IDLE;
encoderPosition = 0;
logger(String("STATE=CLOSED,DURATION=") + (millis()-moveStartTime));
} else if (roofState != CLOSED && roofState != OPEN) {
roofState = ERROR;
phase = IDLE;
logger("ERROR=OVERCURRENT");
}
}
if (motorShield.getM1Fault()) {
motorShield.setM1Speed(0);
roofSpeed = targetRoofSpeed = 0;
roofState = ERROR;
phase = IDLE;
logger("ERROR=MOTOR1FAULT");
}
if (emergencyStopPressed) {
motorShield.setM1Speed(0);
motorShield.setM2Speed(0);
roofSpeed = targetRoofSpeed = 0;
if (phase != IDLE) {
logger("CMD=EMERGENCYSTOP");
}
phase = IDLE;
roofState = STOPPED;
return;
}
if (limitSwitchOpenActive) {
if ((phase == RAMP_UP || phase == MOVE_UNTIL_NEAR) && roofState == OPENING) {
logger("LIMIT=SWITCH_OPENING");
phase = RAMP_DOWN;
targetRoofSpeed = 0;
}
limitSwitchOpenActive = false;
}
if (encoderPosition >= ENCODER_MAX_POSITION) {
if ((phase == RAMP_UP || phase == MOVE_UNTIL_NEAR) && roofState == OPENING) {
logger("LIMIT=ENCODER_OPENING");
phase = RAMP_DOWN;
targetRoofSpeed = 0;
}
}
if (limitSwitchCloseActive) {
if ((phase == RAMP_UP || phase == MOVE_UNTIL_NEAR) && roofState == CLOSING) {
logger("LIMIT=SWITCH_CLOSING");
phase = RAMP_DOWN;
targetRoofSpeed = CLOSING_SPEED;
}
limitSwitchCloseActive = false;
}
if (encoderPosition <= ENCODER_MIN_POSITION) {
if ((phase == RAMP_UP || phase == MOVE_UNTIL_NEAR) && roofState == CLOSING) {
phase = RAMP_DOWN;
targetRoofSpeed = CLOSING_SPEED;
}
}
if (targetRoofSpeed != roofSpeed) {
roofSpeed += RAMP_DELTA * (targetRoofSpeed > roofSpeed ? 1 : -1);
if (abs(roofSpeed-targetRoofSpeed) <= (RAMP_DELTA/2)) {
roofSpeed = targetRoofSpeed;
}
}
switch (phase) {
case RAMP_UP: {
motorShield.setM1Speed(roofSpeed * direction);
if (roofSpeed == targetRoofSpeed) {
phase = MOVE_UNTIL_NEAR;
}
break;
}
case RAMP_DOWN: {
motorShield.setM1Speed(roofSpeed * direction);
if (roofSpeed == targetRoofSpeed) {
phase = IDLE;
if (roofState == STOPPING)
{
logger("STATE=STOPPED");
roofState = STOPPED;
}
else if (roofState == OPENING)
{
logger(String("STATE=OPENED,DURATION=") + (millis()-moveStartTime));
roofState = OPEN;
}
else if (roofState == CLOSING)
{
logger("STATE=TIGHTENING");
phase = CLOSE_TIGHTLY;
}
}
break;
}
case CLOSE_TIGHTLY:
// just coast ahead, until we trigger current limit when the roof hits the wall
break;
}
}
void buttonTick() {
if (digitalRead(PIN_BUTTON_OPEN) == LOW && phase == IDLE) {
open("");
}
if (digitalRead(PIN_BUTTON_CLOSE) == LOW && phase == IDLE) {
close("");
}
}
void temperatureTick() {
// called @ 1hz
if (temperatureRequested) {
temperature = dallasTemperature.getTempCByIndex(0);
}
dallasTemperature.requestTemperaturesByIndex(0);
temperatureRequested = true;
}
void voltageTick() {
batteryVoltage = analogRead(PIN_BATTERY_VOLTAGE) * REFERENCE_VOLTAGE / 1024.0 * BATTERY_RESISTOR_DIVISOR;
}
void test(const String&) {
serial.print(String(BOARD_NAME) + String(",VER=") + String(KOMAROOF_VERSION));
}
void open(const String&) {
if (roofState == CLOSED || roofState == STOPPED) {
roofState = OPENING;
phase = RAMP_UP;
targetRoofSpeed = FULL_SPEED;
direction = MOTOR_POLARITY;
moveStartTime = millis();
logger("CMD=OPEN", moveStartTime);
serial.print("OPEN,OK");
} else {
serial.print("OPEN,ERR");
}
}
void close(const String&) {
if (roofState == OPEN || roofState == STOPPED) {
roofState = CLOSING;
phase = RAMP_UP;
targetRoofSpeed = FULL_SPEED;
direction = -MOTOR_POLARITY;
moveStartTime = millis();
logger("CMD=CLOSE", moveStartTime);
serial.print("CLOSE,OK");
} else {
serial.print("CLOSE,ERR");
}
}
void stop(const String&) {
if (phase != IDLE) {
roofState = STOPPING;
phase = RAMP_DOWN;
targetRoofSpeed = 0;
}
if (roofState == ERROR) {
roofState = STOPPED;
phase = IDLE;
}
logger("CMD=STOP");
serial.print("STOP,OK");
}
void status(const String&) {
static const char* roofStateNames[] = { "STOPPED", "OPEN", "CLOSED", "OPENING", "CLOSING", "STOPPING", "ERROR" };
static const char* phaseNames[] = { "IDLE", "RAMP_UP", "MOVE_UNTIL_NEAR", "RAMP_DOWN", "CLOSE_TIGHTLY" };
String message = "STATUS,";
message += "ROOF=";
message += roofStateNames[roofState];
message += ",PHASE=";
message += phaseNames[phase];
message += ",ENCODER=";
message += encoderPosition;
message += ",BATTERYVOLTAGE=";
message += (int)(batteryVoltage);
message += ".";
message += (int)(batteryVoltage*10) % 10;
message += (int)(roundf(batteryVoltage*100)) % 10;
message += ",SPEED=";
message += roofSpeed;
if (temperature != (float)DEVICE_DISCONNECTED_C) {
message += ",TEMP1=";
message += (int)(temperature);
message += ".";
message += (int)(temperature*10) % 10;
message += (int)(roundf(temperature*100)) % 10;
}
serial.print(message);
}
void setspeed(const String& speedAsString) {
targetRoofSpeed = atoi(speedAsString.c_str());
serial.print("SETSPEED,OK");
}