-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMB_MPPT_DROOP_RTOS_v2.ino
More file actions
332 lines (249 loc) · 6.8 KB
/
MB_MPPT_DROOP_RTOS_v2.ino
File metadata and controls
332 lines (249 loc) · 6.8 KB
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
/* Control tasks are divided into three tasks.
* MPPT task with the lowest priority
* Droop task with intermediate priority
* Current Control task with hoghest priority
* xQueue is used to send the reference current K_droop from the MPPT task to the Droop task
* xQueue1 is used to send I_ref from the Droop task to the Current Control task
* xQueue2 is to send the activation signal from the CC task to the MPPT task
*/
#include <FreeRTOS_ARM.h>
#include <basic_io_arm.h>
// Task Definition
void TaskMPPT( void *pvParameters );
void TaskDroop( void *pvParameters );
void TaskCC( void *pvParameters );
QueueHandle_t xQueue;
QueueHandle_t xQueue1;
QueueHandle_t xQueue2;
void setup() {
//Serial.begin(115200);
analogReadResolution(12);
analogWriteResolution(12);
xQueue = xQueueCreate(1, sizeof( double ) );
xQueue1 = xQueueCreate(1, sizeof( double ) );
xQueue2 = xQueueCreate(1, sizeof( bool ) );
if(xQueue != NULL){
//create tasks
xTaskCreate(
TaskCC
, (const portCHAR *)"CC" //Human Name
, 200 //Stack Size
, NULL
, 3 //Priority with 3 being the highest
, NULL );
xTaskCreate(
TaskDroop
, (const portCHAR *)"Droop" //Human Name
, 200 //Stack Size
, NULL
, 2 //Priority with 3 being the highest
, NULL );
xTaskCreate(
TaskMPPT
, (const portCHAR *)"MPPT"
, 128
, NULL
,1 //Priority
, NULL );
vTaskStartScheduler();
}
else{
}
}
void loop() {
// empty
}
//*********************************************************//
void TaskCC(void *pvParameters)
{
(void) pvParameters;
//Reads K from MPPT task
TickType_t x_LWT; //Last wake time used to execute thread deterministically
bool EN;
double I_meas;
double I_ref;
double I_PV;
double H = 0.1; //Hyterisis Band
bool S,R; //S R latch
bool DD; //Switch HIGH or LOW
double d;
pinMode(9,OUTPUT); //Switch
pinMode(6,INPUT);
pinMode(10,OUTPUT);
pinMode(2,OUTPUT);
x_LWT = xTaskGetTickCount();
for(;;)
{
digitalWrite(2,HIGH);
while(digitalRead(6)==LOW){ //Simulation Not Running Initialize Values
I_ref = 0;
I_meas = 0;
I_PV = 0;
DD = 0;
digitalWrite(10,HIGH); //LED
}
digitalWrite(10,LOW);
xQueuePeek(xQueue1, &I_ref, 0); //Get I_ref from Droop task
//I_meas = analogRead(A0);
//I_meas = map(I_meas,0,4035,0,35)+1;
//Serial.println(I_meas);
I_PV = analogRead(A4);
I_PV = map(I_PV,0,4070,0,45);
//Serial.print(I_PV);
//Serial.print(" ");
//Serial.println(I_ref);
//S = I_meas <= I_ref-H;
//R = I_meas >= I_ref+H;
S = I_PV <= I_ref-H;
R = I_PV >= I_ref+H;
if(S) DD = HIGH;
if(R) DD = LOW;
// d = DD*100;
// d = map(d,0,100,0,4095);
// analogWrite(9,d);
digitalWrite(9,DD);
//I_meas = analogRead(A0);
//I_meas = map(I_meas,0,4070,0,35);
EN = abs(I_ref-I_meas)<=0.5;
xQueueOverwrite(xQueue2, &EN);
//digitalWrite(2,LOW);
digitalWrite(2,LOW);
vTaskDelayUntil( &x_LWT,50);
}
}
//*****************************************//
//****************************************//
//***************************************//
void TaskDroop(void *pvParameters)
{
double V_DG;
double V_out;
double V_ref = 400; //No Load Voltage
double I_ref;
double AOIREF;
double I_PV;
double K;
bool A;
TickType_t x_LWT;
x_LWT = xTaskGetTickCount();
for(;;)
{
digitalWrite(3,HIGH);
while(digitalRead(6)==LOW){
digitalWrite(10,HIGH);
V_out = 0;
V_DG = 0;
I_ref = 0;
K = 0.2;
}
V_out = analogRead(A2);
V_out = map(V_out,0,4070,0,460);
V_DG = analogRead(A1);
V_DG = map(V_DG,0,4070,0.1,50);
I_PV = analogRead(A4);
I_PV = map(I_PV,0,4070,0,45);
xQueuePeek(xQueue, &K, 0); //Get K from MPPT task
//*********A*********//
if(V_out >= V_ref-20) A = false;
if(V_out < 250) A = true;
//digitalWrite(2,A);
//***************************//
//*******************//
//*******I_ref*******//
//******************//
if(V_out<5){
I_ref = 2;
}
else{
I_ref = K*(V_ref-V_out)*K*V_out/V_DG; //Calculate I_ref
if(I_ref >= I_PV+2) I_ref = I_PV+2;
if(A && I_ref>4) I_ref = 4;
}
// I_ref = 5;//**************************
xQueueOverwrite(xQueue1, &I_ref);
AOIREF = map(I_ref,0,25,0,4090);
analogWrite(DAC0,AOIREF);
//Serial.println(I_ref);
digitalWrite(3,LOW);
vTaskDelayUntil( &x_LWT,200 );
}
}
//****************************************************//
//****************************************************//
//****************************************************//
void TaskMPPT(void *pvParameters)
{
(void) pvParameters;
double K;
double dK = 0.0002;
double I_PV;
double V_PV;
double d_I_PV;
double d_P_PV;
double ipv[2];
double ppv[2];
int sP, sI; //Signs +/-
bool EN;
double AOK;
pinMode(3,OUTPUT);
pinMode(10,OUTPUT);
pinMode(6,INPUT);
for(;;)
{
digitalWrite(4,HIGH);
while(digitalRead(6)==LOW){ //Simulation Not Running Initialize Values
ipv[0] = ipv[1] = 0;
ppv[0] = ppv[1] = 0;
d_I_PV = 0;
d_P_PV = 0;
K = 0.2;
digitalWrite(10,HIGH); //Observe
}
xQueuePeek(xQueue2, &EN, 0); //Read Enable Signal From Queue1
digitalWrite(10,LOW); //Observe
if(EN){
// digitalWrite(3,HIGH);
I_PV = analogRead(A4);
I_PV = map(I_PV,0,4070,0,45);
//Serial.println(I_PV);
V_PV = analogRead(A5);
V_PV = map(V_PV,0,4070,0,60);
//Serial.println(V_PV);
ipv[0] = ipv[1];
ipv[1] = I_PV;
ppv[0] = ppv[1];
ppv[1] = I_PV*V_PV;
d_I_PV = ipv[1]-ipv[0];
d_P_PV = ppv[1]-ppv[0];
//**Calculate Signs of d_I_PV and d_P_PV
if(d_I_PV<0){
sI = -1;
}
if(d_I_PV>0){
sI = 1;
}
if(d_P_PV<0){
sP = -1;
}
if(d_P_PV>0){
sP = 1;
}
//**************************************//
// Serial.println(sP);
K = K+dK*sP*sI;
if(K>=4) K=4;
if(K<0) K=0;
AOK = K*100;
AOK = map(AOK,0,400,0,4090);
// Serial.println(K);
analogWrite(DAC1,AOK);
//Serial.println(K);
xQueueOverwrite( xQueue, &K);
}
else{
//Do Nothing if EN == False
//digitalWrite(3,LOW);
}
digitalWrite(4,LOW);
}
}