forked from vjval1974/Brew-Machine-MkIV
/
crane.c
710 lines (619 loc) · 19.9 KB
/
crane.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//-------------------------------------------------------------------------
// Author: Brad Goold
// Date: 18 Feb 2012
// Email Address: W0085400@umail.usq.edu.au
//
// Purpose:
// Pre:
// Post:
// RCS $Date$
// RCS $Revision$
// RCS $Source$
// RCS $Log$
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
// Included Libraries
//-------------------------------------------------------------------------
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "stm32f10x.h"
#include "FreeRTOS.h"
#include "lcd.h"
#include "task.h"
#include "adc.h"
#include "leds.h"
#include "semphr.h"
#include "queue.h"
#include "I2C-IO.h"
#include "console.h"
#include "crane.h"
#include "brew.h"
#include "io_util.h"
#include "message.h"
#include "stir.h"
#include "main.h"
volatile int8_t cs = STOPPED;
int8_t iCraneState = STOPPED;
//static const int STEP_COMPLETE = BREW_STEP_COMPLETE;
//static const int STEP_FAILED = BREW_STEP_FAILED;
//static const int STEP_TIMEOUT = BREW_STEP_TIMEOUT;
xQueueHandle xCraneQueue = NULL;
xSemaphoreHandle xAppletRunningSemaphore;
xTaskHandle xCraneTaskHandle = NULL, xCraneAppletDisplayHandle = NULL;
void vTaskCrane(void * pvParameters);
int8_t vGetCraneState(void)
{
return iCraneState;
}
void vCraneInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Set up the input pin configuration for PE4
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Pin = CRANE_UPPER_LIMIT_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
// GPIO_Init( CRANE_UPPER_LIMIT_PORT, &GPIO_InitStructure );
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Pin = CRANE_LOWER_LIMIT_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
// GPIO_Init( CRANE_LIMIT_PORT, &GPIO_InitStructure );
vPCF_ResetBits(CRANE_PIN1, CRANE_PORT); //pull low
vPCF_ResetBits(CRANE_PIN2, CRANE_PORT); //pull low
vSemaphoreCreateBinary(xAppletRunningSemaphore);
xCraneQueue = xQueueCreate( 5, sizeof( struct GenericMessage * ) );
if (xCraneQueue == NULL)
{
vConsolePrint("Crane Queue couldn't be created\r\n");
}
vConsolePrint("Crane Initialised\r\n");
}
void vCraneFunc(int Command)
{
//portENTER_CRITICAL(); // so that we dont get switched out whilst accessing these pins.
switch (Command)
{
case UP:
{
// need to turn relay 2 off which sets up for REV direction
vPCF_ResetBits(CRANE_PIN2, CRANE_PORT); //pull low
vPCF_SetBits(CRANE_PIN1, CRANE_PORT); //pull low
break;
}
case DN:
{
// need to turn relay 1 off which sets up for REV direction
vPCF_ResetBits(CRANE_PIN1, CRANE_PORT); //pull low
vPCF_SetBits(CRANE_PIN2, CRANE_PORT); //pull low
break;
}
default:
{
vPCF_ResetBits(CRANE_PIN1, CRANE_PORT); //pull low
vPCF_ResetBits(CRANE_PIN2, CRANE_PORT); //pull low
break;
}
}
//portEXIT_CRITICAL();
}
struct GenericMessage xMessageA, xLastMessageA, xToSendA;
void vTaskCrane(void * pvParameters)
{
static struct GenericMessage * xMessage, *xLastMessage, * xToSend;
// xLastMessage = (struct GenericMessage *)pvPortMalloc(sizeof(struct GenericMessage));
// xMessage = (struct GenericMessage *)pvPortMalloc(sizeof(struct GenericMessage));
// xToSend = (struct GenericMessage *)pvPortMalloc(sizeof(struct GenericMessage));
xLastMessage = &xLastMessageA;
xMessage = &xMessageA;
xToSend = &xToSendA;
static int iComplete = 0;
static int iStep = 0;
xToSend->ucFromTask = CRANE_TASK;
xToSend->ucToTask = BREW_TASK;
xToSend->pvMessageContent = (void *)&iComplete;
const int iTest = 40;
static unsigned char ucDownIncrements = 0;
uint8_t limit = 0xFF, limit1 = 0xFF; //neither on or off.
static int iC = STOP;
static int iCommandState = 0;
char buf[40];
xLastMessage->pvMessageContent = &iC;
unsigned char failedI2cReads = 0;
// testing vars
char cCount = 0;
char cLimitVal = 255;
for (;;)
{
if(xQueueReceive(xCraneQueue, &xMessage, 50) != pdPASS)
{
xMessage->pvMessageContent = xLastMessage->pvMessageContent;
}
else // received successfully
{
iStep = xMessage->uiStepNumber;
vConsolePrint("Crane Task has received a message\r\n");
xLastMessage->pvMessageContent = xMessage->pvMessageContent;
xLastMessage->ucFromTask = xMessage->ucFromTask;
iC = *((int*)xMessage->pvMessageContent); //casts to int * and then dereferences.
iComplete = 0;
xToSend->uiStepNumber = xMessage->uiStepNumber;
iCommandState = 0;
ucDownIncrements = 0;
//#ifdef TESTING
// iCommandState = 1;
// iComplete = STEP_COMPLETE;
// vTaskDelay(1000);
// {
// vConsolePrint("Crane Command Complete, Sending Message\r\n");
// vTaskDelay(50);
// xToSend->pvMessageContent = (void*)&iTest;
// xQueueSendToBack(xBrewTaskReceiveQueue, &xToSend, 1000);
// //xQueueSendToBack(xBrewTaskReceiveQueue, (void *)40, 100);
// iCommandState = 0;
// iCraneState = STOPPED;
// iC = STOP;
// }
//#endif
}
switch(iCraneState)
{
case TOP:
{
if (iC == DN)
{
vCraneFunc(DN);
iCommandState = 0; // not done yet
iCraneState = DRIVING_DOWN;
}
else if (iC == DN_INC)
{
iCraneState = DRIVING_DOWN_INC;
}
else if (iC == UP)
{
iCommandState = 1;
iComplete = STEP_COMPLETE;
}
//iCommandState = 0;
break;
}
case BOTTOM:
{
if (iC == UP)
{
// vConsolePrint("Command = UP whilst at bottom\r\n");
vCraneFunc(UP);
iCraneState = DRIVING_UP;
iCommandState = 0; // not done yet
}
else if (iC == DN)
{
// vConsolePrint("Command = DN whilst at bottom\r\n");
iCommandState = 1;
iComplete = STEP_COMPLETE;
}
break;
}
case DRIVING_UP:
{
if (iC == STOP)
{
vCraneFunc(STOP);
vConsolePrint("STOP while DU \r\n");
iCraneState = STOPPED;
iCommandState = 1;
iComplete = STEP_COMPLETE;
}
else if (iC == DN)
{
vConsolePrint("Command = DN whilst driving up\r\n");
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
if (limit == 255) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
}
if (limit != 1)
{
vCraneFunc(DN);
iCraneState = DRIVING_DOWN;
}
else
{
iCraneState = BOTTOM;
iCommandState = 1;
iComplete = STEP_COMPLETE;
}
}
else
{
vPCF_SetBits(CRANE_UPPER_LIMIT_PIN, CRANE_UPPER_LIMIT_PORT); // yes the port and pin are reverse from read
vTaskDelay(100);
limit = cI2cGetInput(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
//limit = debounce(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
while (limit == 255) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
}
if (limit == 1)
{
vCraneFunc(STOP);
vConsolePrint("STOP limit DU \r\n");
iCraneState = TOP;
iComplete = STEP_COMPLETE;
iCommandState = 1;
//if (xMessage->ucFromTask == BREW_TASK)
// xQueueSendToBack(xBrewTaskReceiveQueue, &xToSend, 0);
}
}
break;
}
case DRIVING_DOWN:
{
if (iC == STOP)
{
vCraneFunc(STOP);
vConsolePrint("STOP while DDN \r\n");
iCraneState = STOPPED;
iComplete = STEP_COMPLETE;
iCommandState = 1;
}
else if (iC == UP)
{
vConsolePrint("Command = UP whilst driving down\r\n");
//limit = debounce(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
limit = cI2cGetInput(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
if (limit == 255) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
}
if (limit == 1)
{
iCraneState = TOP;
iComplete = STEP_COMPLETE;
iCommandState = 1;
}
else
{
vCraneFunc(UP);
iCraneState = DRIVING_UP;
iCommandState = 0;
}
}
else// if (xMessage->cDirection == DN)
{
vConsolePrint("waiting on lower limit\r\n");
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
if (limit == 255) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
}
if (limit == 1)
{
vTaskDelay(200);
vCraneFunc(STOP);
vConsolePrint("STOP limit DDN \r\n");
iCraneState = BOTTOM;
iCommandState = 1;
iComplete = STEP_COMPLETE;
// if (xMessage->ucFromTask == BREW_TASK)
// xQueueSendToBack(xBrewTaskReceiveQueue, &xToSend, 0);
}
}
break;
}
case DRIVING_DOWN_INC:
{
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
// cLimitVal = limit;
// while( cLimitVal == 1 && cCount < 5)
// {
// limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
// cLimitVal = limit;
// }
if (limit == ERROR) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
}
if(limit == FALSE)
{
vCraneFunc(DN);
vConsolePrint("Crane DN \r\n");
vTaskDelay(200);
vCraneFunc(STOP);
vConsolePrint("Crane STOP \r\n");
vTaskDelay(1500);
ucDownIncrements++;
}
else if (limit == TRUE)
{
// Make sure we are down
vCraneFunc(DN);
vTaskDelay(100);
vConsolePrint("STOP limit DDN \r\n");
vCraneFunc(STOP);
iCraneState = BOTTOM;
iComplete = STEP_COMPLETE;
iCommandState = 1;
ucDownIncrements = 0;
vStir(STIR_STOPPED);
// if (xMessage->ucFromTask == BREW_TASK)
// xQueueSendToBack(xBrewTaskReceiveQueue, &xToSend, 0);
}
if (ucDownIncrements == 11)
vStir(STIR_DRIVING);
break;
}
case STOPPED:
{
if (iC == UP)
{
vConsolePrint("Crane: State = Stopped, Cmd = UP\r\n");
limit = cI2cGetInput(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
if (limit == 255) // read failed
{
vConsolePrint("Crane I2cFail1\r\n");
vTaskDelay(200);
limit = cI2cGetInput(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
}
//limit = debounce(CRANE_UPPER_LIMIT_PORT, CRANE_UPPER_LIMIT_PIN);
if (limit != 1)
{
iCommandState = 0;
vCraneFunc(UP);
iCraneState = DRIVING_UP;
}
else
{
vConsolePrint("Crane: State = Stopped, Cmd = UP, Sensed top\r\n");
iCommandState = 1;
iComplete = STEP_COMPLETE;
iCraneState = TOP;
}
}
else if (iC == DN)
{
vConsolePrint("Crane: State = Stopped, Cmd = DN\r\n");
vPCF_SetBits(CRANE_LOWER_LIMIT_PIN, CRANE_LOWER_LIMIT_PORT); // yes the port and pin are reverse from read
vTaskDelay(100);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
if (limit == 255) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
}
if (limit != 1)
{
iCommandState = 0;
vCraneFunc(DN);
iCraneState = DRIVING_DOWN;
}
else
{
iCommandState =1;
iComplete = STEP_COMPLETE;
iCraneState = BOTTOM;
}
}
else if (iC == DN_INC)
{
vPCF_SetBits(CRANE_LOWER_LIMIT_PIN, CRANE_LOWER_LIMIT_PORT); // yes the port and pin are reverse from read
vTaskDelay(100);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
if (limit == 255) // read failed
{
vTaskDelay(200);
limit = cI2cGetInput(CRANE_LOWER_LIMIT_PORT, CRANE_LOWER_LIMIT_PIN);
}
if (limit != 1)
{
iCommandState = 0;
iCraneState = DRIVING_DOWN_INC;
}
else
{
iCommandState = 1;
iComplete = STEP_COMPLETE;
iCraneState = BOTTOM;
}
}
iC = 14; // goes into undefined command state. shouldnt be needed.
break;
}
default:
{
vConsolePrint("Default Case in Crane Function\r\n");
vCraneFunc(STOP); //stop the crane on an instant.
iCraneState = STOPPED;
break;
}
}// Switch
if (iCommandState == 1 && xMessage->ucFromTask == BREW_TASK)
{
vConsolePrint("Crane Command Complete, Sending Message\r\n");
vTaskDelay(50);
xToSend->pvMessageContent = (void*)&iTest;
xQueueSendToBack(xBrewTaskReceiveQueue, &xToSend, 10000);
//xQueueSendToBack(xBrewTaskReceiveQueue, (void *)40, 100);
iCommandState = 0;
iCraneState = STOPPED;
}
//vTaskDelay(500);
}// Task Loop
}//func
void vCraneAppletDisplay( void *pvParameters){
static char tog = 0; //toggles each loop
for(;;)
{
xSemaphoreTake(xAppletRunningSemaphore, portMAX_DELAY); //take the semaphore so that the key handler wont
//return to the menu system until its returned
//display the state and user info (the state will flash on the screen)
switch (iCraneState)
{
case DRIVING_UP:
{
if(tog)
{
lcd_fill(1,220, 180,29, Black);
lcd_printf(1,13,15,"DRIVING UP");
}
else{
lcd_fill(1,210, 180,17, Black);
}
break;
}
case DRIVING_DOWN:
{
if(tog)
{
lcd_fill(1,220, 180,29, Black);
lcd_printf(1,13,15,"DRIVING DOWN");
}
else{
lcd_fill(1,210, 180,17, Black);
}
break;
}
case TOP:
{
if(tog)
{
lcd_fill(1,210, 180,29, Black);
lcd_printf(1,13,11,"TOP");
}
else
{
lcd_fill(1,210, 180,17, Black);
}
break;
}
case BOTTOM:
{
if(tog)
{
lcd_fill(1,210, 180,29, Black);
lcd_printf(1,13,11,"BOTTOM");
}
else
{
lcd_fill(1,210, 180,17, Black);
}
break;
}
case STOPPED:
{
if(tog)
{
lcd_fill(1,210, 180,29, Black);
lcd_printf(1,13,11,"STOPPED");
}
else
{
lcd_fill(1,210, 180,17, Black);
}
break;
}
default:
{
break;
}
}
tog = tog ^ 1;
xSemaphoreGive(xAppletRunningSemaphore); //give back the semaphore as its safe to return now.
vTaskDelay(500);
}
}
#define UP_X1 0
#define UP_Y1 30
#define UP_X2 150
#define UP_Y2 100
#define UP_W (UP_X2-UP_X1)
#define UP_H (UP_Y2-UP_Y1)
#define DN_X1 0
#define DN_Y1 105
#define DN_X2 150
#define DN_Y2 175
#define DN_W (DN_X2-DN_X1)
#define DN_H (DN_Y2-DN_Y1)
#define ST_X1 155
#define ST_Y1 30
#define ST_X2 300
#define ST_Y2 175
#define ST_W (ST_X2-ST_X1)
#define ST_H (ST_Y2-ST_Y1)
#define BAK_X1 200
#define BAK_Y1 190
#define BAK_X2 315
#define BAK_Y2 235
#define BAK_W (BAK_X2-BAK_X1)
#define BAK_H (BAK_Y2-BAK_Y1)
void vCraneApplet(int init){
if (init)
{
lcd_DrawRect(UP_X1, UP_Y1, UP_X2, UP_Y2, Red);
lcd_fill(UP_X1+1, UP_Y1+1, UP_W, UP_H, Blue);
lcd_DrawRect(DN_X1, DN_Y1, DN_X2, DN_Y2, Red);
lcd_fill(DN_X1+1, DN_Y1+1, DN_W, DN_H, Blue);
lcd_DrawRect(ST_X1, ST_Y1, ST_X2, ST_Y2, Cyan);
lcd_fill(ST_X1+1, ST_Y1+1, ST_W, ST_H, Red);
lcd_DrawRect(BAK_X1, BAK_Y1, BAK_X2, BAK_Y2, Cyan);
lcd_fill(BAK_X1+1, BAK_Y1+1, BAK_W, BAK_H, Magenta);
lcd_printf(10,1,12, "MANUAL CRANE");
lcd_printf(8,4,2, "UP");
lcd_printf(8,8,2, "DN");
lcd_printf(26,6,4, "STOP");
lcd_printf(30, 13, 4, "Back");
xTaskCreate( vCraneAppletDisplay,
( signed portCHAR * ) "Crane_display",
configMINIMAL_STACK_SIZE +500,
NULL,
tskIDLE_PRIORITY + 1,
&xCraneAppletDisplayHandle );
}
}
struct GenericMessage xMessage;
struct GenericMessage * pxMessage;
volatile int iUp= UP, iDn = DN, iStop = STOP;
int iCraneKey(int xx, int yy){
uint16_t window = 0;
static uint8_t w = 5,h = 5;
static uint16_t last_window = 0;
pxMessage = &xMessage;
pxMessage->pvMessageContent = &iStop;
if (xx > UP_X1+1 && xx < UP_X2-1 && yy > UP_Y1+1 && yy < UP_Y2-1)
{
pxMessage->pvMessageContent = &iUp;
xQueueSendToBack( xCraneQueue, &pxMessage, 0 );
}
else if (xx > DN_X1+1 && xx < DN_X2-1 && yy > DN_Y1+1 && yy < DN_Y2-1)
{
pxMessage->pvMessageContent = &iDn;
xQueueSendToBack( xCraneQueue, &pxMessage, 0 );
}
else if (xx > ST_X1+1 && xx < ST_X2-1 && yy > ST_Y1+1 && yy < ST_Y2-1)
{
pxMessage->pvMessageContent = &iStop;
xQueueSendToBack( xCraneQueue, &pxMessage, portMAX_DELAY );
}
else if (xx > BAK_X1 && yy > BAK_Y1 && xx < BAK_X2 && yy < BAK_Y2)
{
//try to take the semaphore from the display applet. wait here if we cant take it.
xSemaphoreTake(xAppletRunningSemaphore, portMAX_DELAY);
//delete the display applet task if its been created.
if (xCraneAppletDisplayHandle != NULL)
{
vTaskDelete(xCraneAppletDisplayHandle);
vTaskDelay(100);
xCraneAppletDisplayHandle = NULL;
}
//return the semaphore for taking by another task.
xSemaphoreGive(xAppletRunningSemaphore);
return 1;
}
vTaskDelay(10);
return 0;
}