-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.c
438 lines (374 loc) · 9.35 KB
/
user.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
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
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
#include <xc.h> /* XC8 General Include File */
#include <stdint.h> /* For uint8_t definition */
#include <stdbool.h> /* For true/false definition */
#include <adc.h>
#include <string.h>
#include <timers.h>
#include <usb/usb.h>
#include <usb/usb_device_cdc.h>
#include "user.h"
#include "battery.h"
extern enum State cur_State;
extern tPIParams PI;
extern Battery battery;
extern unsigned short long precondition_Time;
extern unsigned short long charge_Time;
extern unsigned short timeout;
extern long seconds;
/******************************************************************************/
/* User Functions */
/******************************************************************************/
/* Return difference (in minuts) from actual time (retrive it from timer 1)
* to initial time (in seconds) */
unsigned int calc_Time(long initial)
{
unsigned int interval = 0;
if (seconds >= initial)
{
interval = seconds - initial;
interval = (int) interval / 60;
}
return interval;
}
/* App initialisation
* init ADC
* init timer 1
* init interrupts
*/
void InitApp(void)
{
cur_State = LIPO_ALGO_STARTED;
strncpy(battery.battery_type,"LIPO\0",5);
battery.charge.restore_Lowest_Voltage = 5;
battery.number_of_cells = 1;
battery.charge.restore_Charge_Current = 0.5;
seconds = 0;
OpenADC( ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_6_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,
ADC_1ANA );
/* open timers */
OpenTimer1( TIMER_INT_ON &
T1_8BIT_RW &
T1_SOURCE_EXT &
T1_PS_1_1 &
T1_OSC1EN_ON &
T1_SYNC_EXT_OFF );
//ei();
USBDeviceInit(); //usb_device.c
#if defined(USB_INTERRUPT)
USBDeviceAttach(); //usb_device.c
#endif
/* TODO init PI structure */
PI.Ki = 2;
PI.Kp = 35;
/* TODO init analog port */
}
/* Eval voltage on a channel
* result on volt
*/
short V_Eval(unsigned char channel,signed float *voltage)
{
signed float temp = 0;
SelChanConvADC(channel);
timeout = ADC_TIMEOUT;
while(BusyADC() && timeout > 0); //Wait here until conversion is finished
if (timeout == 0)
{
return KO;
}
temp = ReadADC();
*voltage = (temp / 1024.0) * 5.0;
return OK;
}
/* Set I or V by selected channel */
short I_V_Set(unsigned char channel)
{
short res;
short count = 0;
do
{
res = V_Eval(channel, &PI.Feedback);
if (res != OK)
{
return KO;
}
CalcPI(&PI);
/* TODO set duty cycle */
/* TODO add a delay */
count++;
/* Stop if error is in a dedband or a fix number of steps */
} while (((PI.Error > DEADBAND) || (PI.Error < -1*DEADBAND)) && (count < ERROR_STEP));
return OK;
}
/* Proportional Integral regulator algorithm */
void CalcPI(tPIParams *PIdata)
{
PIdata->Error = PIdata->Setpoint - PIdata->Feedback;
// Deadband -- If the magnitude of the error is 2 or less,
// then don't calculate the PI routine at all. This saves
// processor time and avoids oscillation problems.
if ((PIdata->Error > DEADBAND) || (PIdata->Error < -DEADBAND))
{
// If the PI controller is saturated, then don't do any
// accumulation of the integral.
if (PIdata->Saturated == 0)
{
// Do some boundary checking on the integral value
// to avoid overflow. If the integral value is near
// the limits, then we won't do the accumulation.
if (PIdata->Error > 0)
{
if (PIdata->Integral < 32000)
PIdata->Integral += PIdata->Error;
}
else
{
if (PIdata->Integral > -32000)
/* TODO : Verifie Intergal type int */
PIdata->Integral += PIdata->Error;
}
}
// Now, calculate the actual PI function here.
PIdata->Output = (PIdata->Error * PIdata->Kp + PIdata->Integral * PIdata->Ki)/256;
// Perform boundary checks on the PI controller output. If the
// output limits are exceeded, then set output to the limit
// and set flag.
if (PIdata->Output > PIdata->OutMax)
{
PIdata->Saturated = 1;
PIdata->Output = PIdata->OutMax;
}
else if (PIdata->Output < 0)
{
PIdata->Saturated = 1;
PIdata->Output = 0;
}
else
PIdata->Saturated = 0;
}
}
/* */
short initialize(void)
{
return OK;
}
/* */
short list_Battery(void)
{
return OK;
}
/* */
void select_Battery(void)
{
}
/* */
short list_Action(void)
{
return OK;
}
/* */
void select_Action(void)
{
}
/* */
short list_Program(void)
{
return OK;
}
/* */
void select_Program(void)
{
}
/* */
short check_Lipo(void)
{
/* TODO Check Imput voltage */
/* TODO Check cells number */
return OK;
}
/* Check if procondition is necessary */
short check_Precondition(short *precondition)
{
signed float voltage = 0;
short res;
res = V_Eval(ADC_CH0,&voltage);
if (res != OK)
{
return KO;
}
if (voltage < battery.charge.restore_Lowest_Voltage * battery.number_of_cells)
{
*precondition = true;
}
else
{
*precondition = false;
}
usb_send("voltage : %f, precondition : %hd",voltage,precondition);
return OK;
}
/* Start precondition cycle */
short start_Precondition()
{
short res;
PI.Setpoint = battery.charge.restore_Charge_Current;
PI.Saturated = 0;
res = I_V_Set(ADC_CH10);
if (res != OK)
{
return KO;
}
precondition_Time = seconds;
return OK;
}
/* Verifie the ends condition for precondition */
short verifie_Precondition(char *end)
{
signed float voltage = 0;
short res = KO;
res = V_Eval(ADC_CH0,&voltage);
if (res != OK)
{
return KO;
}
if (calc_Time(precondition_Time) >= battery.charge.restore_Charge_Time)
{
strncpy(end,"TIMER",5);
return OK;
}
if (voltage >= battery.charge.restore_Lowest_Voltage * battery.number_of_cells)
{
strncpy(end,"END",3);
return OK;
}
else
{
PI.Setpoint = battery.charge.restore_Charge_Current;
res = I_V_Set(ADC_CH10);
if (res != OK)
{
return KO;
}
strncpy(end,"PROGRESS",8);
return OK;
}
}
/* */
short start_CC()
{
short res;
PI.Setpoint = battery.charge.chg_Current;
PI.Saturated = 0;
res = I_V_Set(ADC_CH10);
if (res != OK)
{
return KO;
}
charge_Time = seconds;
return OK;
}
/* */
short verifie_Vbat(char *end)
{
signed float voltage = 0;
short res = KO;
res = V_Eval(ADC_CH0,&voltage);
if (res != OK)
{
return KO;
}
if (calc_Time(charge_Time) > battery.charge.safety_Timer)
{
strncpy(end,"TIMER",5);
return OK;
}
if (voltage >= battery.charge.chg_Cell_Volt * battery.number_of_cells)
{
strncpy(end,"END",3);
return OK;
}
else
{
PI.Setpoint = battery.charge.chg_Current;
res = I_V_Set(ADC_CH10);
if (res != OK)
{
return KO;
}
strncpy(end,"PROGRESS",8);
return OK;
}
}
/* */
short start_CV()
{
short res;
PI.Setpoint = battery.charge.chg_Cell_Volt * battery.number_of_cells;
PI.Saturated = 0;
res = I_V_Set(ADC_CH0);
if (res != OK)
{
return KO;
}
return OK;
}
/* */
short verifie_Ibat(char *end)
{
signed float current = 0;
short res = KO;
res = V_Eval(ADC_CH10,¤t);
if (res != OK)
{
return KO;
}
if (calc_Time(charge_Time) > battery.charge.safety_Timer)
{
strncpy(end,"TIMER",5);
return OK;
}
if (current >= battery.charge.chg_End_Current)
{
strncpy(end,"END",3);
return OK;
}
else
{
PI.Setpoint = battery.charge.chg_Cell_Volt * battery.number_of_cells;
res = I_V_Set(ADC_CH0);
if (res != OK)
{
return KO;
}
strncpy(end,"PROGRESS",8);
return OK;
}
}
/* */
short stop_CV()
{
signed float voltage = 0;
short res = KO;
float v_bat = battery.charge.chg_Cell_Volt * battery.number_of_cells;
/* TODO set duty cycle to 0 */
res = V_Eval(ADC_CH0,&voltage);
if (res != OK)
{
return KO;
}
if (voltage >= v_bat - (DELTA/1000) && voltage <= v_bat + (DELTA/1000))
{
return OK;
}
else
{
return KO;
}
}
/* */
void end_Charge()
{
}