forked from Damme/LandLord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powermgmt.c
executable file
·252 lines (211 loc) · 8.63 KB
/
powermgmt.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
#include <stdio.h>
#include "powermgmt.h"
#include "timers.h"
#include "global.h"
#include "keypad.h"
#define BATTERY_MAX_TEMP 400
//#define BATTERY_MAX_VOLT 28700
#define BATTERY_MAX_VOLT 29000 // test with higher volt
#define BATTERY_MIN_VOLT 24000
#define BATTERY_DELTA_VOLT 1500
#define BATTERY_MAX_AMP 4000
/*
7 x 18650
3.4 x 7 = 23.8v
4.1 x 7 = 28.7v
4.2 x 7 = 29.4v
2sec until red
4sec red
*/
typedef enum {
Startup,
Idle,
CheckChargerInit,
CheckCharger,
StartCharging,
Charging,
BatteryCooldown,
ChargingFinished,
ChargingFailed,
Shutdown
} powerStates;
const char *powerStateStrings[] = {
"Startup",
"Idle",
"CheckChargerInit",
"CheckCharger",
"StartCharging",
"Charging",
"BatteryCooldown",
"ChargingFinished",
"ChargingFailed",
"Shutdown"
};
void powerMgmt_Task(void *pvParameters) {
powerMgmt_Init();
// pulse charger mosfet incase we already is in charger.
GPIO_SET_PIN(CHARGER_ENABLE);
vTaskDelay(xDelay10); // 10ms is enough
GPIO_CLR_PIN(CHARGER_ENABLE);
TickType_t delay = xDelay50;
powerStates powerState = StartCharging; // Ugly fix for forcing charger active when restarting aldready connected to charger.
powerStates lastState = 0;
xSensorMsgType sensor;
xScreenMsgType screenMsg;
uint16_t count = 5;
for (;;) {
if (powerState != lastState) {
lastState = powerState;
xJSONMessageType JSONMsg = {"powerState", {0}};
strcpy(JSONMsg.value, powerStateStrings[powerState]);
xQueueSend(xJSONMessageQueue, &JSONMsg, xDelay100);
}
xQueuePeek(xSensorQueue, &sensor, TicksPerMS*10);
switch(powerState) {
case Startup:
// if checking for charger all the time we dont need this forced charging function.
if (keypad_GetKey() == KEYHOME) {
powerState = CheckChargerInit;
screenMsg.time=50;
sprintf(screenMsg.text, "Forced charging");
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
}
count--;
if (count == 0) powerState = Idle;
break;
case Idle:
// Why not check for charger all the time? Once every 1s
// Todo dim display if idle > x minutes, idle is if lid is closed!
delay = xDelay500;
count = 50;
powerState = CheckChargerInit;
break;
case CheckChargerInit:
// TODO only start charging if volt below xx?
delay = xDelay100;
GPIO_SET_PIN(CHARGER_CHECK);
powerState = CheckCharger;
break;
case CheckCharger:
if (GPIO_CHK_PIN(CHARGER_CONNECTED)) {
// TODO: send signal "in charger"??
// TODO: All stop!
// TODO: Somehow block forward motion
GPIO_CLR_PIN(CHARGER_CHECK);
//GPIO_SET_PIN(MOTOR_MOSFET);
sensor.incharger = 1;
xQueueOverwrite(xSensorQueue, &sensor);
powerState = StartCharging;
count = 50;
screenMsg.time=15;
sprintf(screenMsg.text, "In charger!");
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
}
if (count < 1) {
GPIO_CLR_PIN(CHARGER_CHECK);
//GPIO_SET_PIN(MOTOR_MOSFET);
powerState = Idle;
/*
screenMsg.time=50;
sprintf(screenMsg.text, "No charger found!");
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);*/
}
count--;
break;
// Catch state connected to charger but charger not ready (For example, start mower with charger already connected)
// CheckCharger Yes -> StartCharging
// StartCharging -> Charging
// 4s elapses -> Charger disconnected! -> ChargingFailed
// -> CheckChargerInit -> CheckChargerInit
case StartCharging:
GPIO_SET_PIN(CHARGER_ENABLE);
delay = xDelay100;
powerState = Charging;
count = 50;
break;
case Charging:
if (sensor.batteryChargeCurrent > 5) {
count = 50;
// TODO Tempeature checks
if (sensor.batteryTemp > 430) powerState = BatteryCooldown;
// TODO Count mAh/Wh charged
// TODO Start time and elapsed charging time
if (!sensor.incharger) {
sensor.incharger = 1;
xQueueOverwrite(xSensorQueue, &sensor);
}
}
if (sensor.batteryVolt > BATTERY_MAX_VOLT) {
GPIO_SET_PIN(CHARGER_CHECK);
powerState = ChargingFinished;
screenMsg.time=15;
sprintf(screenMsg.text, "Charge finished!");
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
}
count--;
if (count == 0) {
powerState = ChargingFailed;
screenMsg.time=15;
sprintf(screenMsg.text, "Charger disconnected!");
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
}
break;
case BatteryCooldown:
delay = xDelay1000;
GPIO_SET_PIN(CHARGER_ENABLE);
vTaskDelay(xDelay10); // 10ms is enough
GPIO_CLR_PIN(CHARGER_ENABLE);
if (sensor.batteryTemp < 420) powerState = StartCharging;
break;
case ChargingFinished:
// TODO Signal ROS charge finished
delay = xDelay100;
vTaskDelay(xDelay500);
GPIO_SET_PIN(CHARGER_ENABLE);
vTaskDelay(xDelay10); // 10ms is enough
GPIO_CLR_PIN(CHARGER_ENABLE);
// TODO Seems analog charge current is peak value, so even if we only have the charge enabled for 0.025s per
// second we still get a value of > 300mA. maybe we should "correct" this in software?
// check if we get ADC value to see if charger is still alive? (how long time charge period does this require?)
if (!sensor.incharger) powerState = Idle;
// TODO TODO Noone clears incharger state as of now, Either ROS comms when backing out of charger or sensor when
// forward motion is detected.
if (sensor.batteryVolt < ( BATTERY_MAX_VOLT - BATTERY_DELTA_VOLT) ) powerState = StartCharging;
break;
case ChargingFailed:
delay = xDelay1000;
// mains power loss ? retry start charging over and over again until success?
powerState = CheckChargerInit;
break;
case Shutdown:
vTaskDelay(xDelay250);
GPIO_CLR_PIN(MOTOR_MOSFET);
GPIO_CLR_PIN(CHARGER_ENABLE);
GPIO_CLR_PIN(LCD_BACKLIGHT);
GPIO_CLR_PIN(CHARGER_CHECK);
GPIO_CLR_PIN(POWER);
HALT;
break;
}
if (sensor.batteryVolt < (BATTERY_MIN_VOLT + BATTERY_DELTA_VOLT)) {
// Signal ROS need charge!
} else if (sensor.batteryVolt < (BATTERY_MIN_VOLT - BATTERY_DELTA_VOLT)) {
// Signal ROS to shutdown - also enter shutdown state!
}
// TODO Check high temp -> shut down
// TODO Pulse charger mosfet if high temp / current? ('slow' Software PWM ?? )
if (keypad_GetKey() == KEYPWR) {
screenMsg.time=4;
sprintf(screenMsg.text, "Shutting down! %i", keypad_GetTime());
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
if (keypad_GetTime() > 15) {
powerState = Shutdown;
}
}
vTaskDelay(delay);
#if LOWSTACKWARNING
int stack = uxTaskGetStackHighWaterMark(NULL);
if (stack < 50) printf("Task powerMgmt_Task has %u words left in stack.\r\n", stack);
#endif
}
}