-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.c
122 lines (111 loc) · 2.07 KB
/
controller.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
#include "controller.h"
#include "connections.h"
#include "motorSubsystem.h"
#include "endStopSubsystem.h"
#undef READY
#include "sam.h"
#include <pinAccess.h>
#include "button.h"
static enum { START, ZERO, MANUAL, AUTO, MOVE } mode = START;
const char *modeName[] = {
"START ", "ZERO ", "MANUAL", "AUTO ", "MOVE "
};
const char * currentMode()
{
return modeName[mode];
}
static int32_t gSlot = 0;
static int32_t gTarget = 20000;
int32_t slot()
{
return gSlot;
}
uint8_t allowedToMove()
{
if (direction() == grayDir && grayEndStop() == 0) {
return 0;
}
else if (direction() == redDir && redEndStop() == 0) {
return 0;
}
else {
return 1;
}
}
TASK(controllerTask)
{
button_state moveButton = readButton(&button1);
button_state directionButton = readButton(&button2);
button_state goButton = readButton(&button3);
button_state modeButton = readButton(&sw0);
switch (mode) {
case START:
if (redEndStop() == 1) {
setDir(redDir);
startMotor();
}
mode = ZERO;
break;
case ZERO:
if (redEndStop() == 0) {
stopMotor();
resetPosition();
mode = MANUAL;
}
break;
case MANUAL:
if (allowedToMove()) {
if (moveButton == ON) {
startMotor();
}
else {
stopMotor();
}
}
else {
stopMotor();
}
if (directionButton == PRESS) {
setDir(! direction());
}
if (modeButton == PRESS) {
mode = AUTO;
}
break;
case AUTO:
if (directionButton == PRESS) {
gSlot++;
if (gSlot == 3) gSlot = 0;
}
if (goButton == PRESS) {
gTarget = (gSlot + 1) * 20000;
if (gTarget > position()) {
setDir(grayDir);
}
else {
setDir(redDir);
}
startMotor();
mode = MOVE;
}
if (modeButton == PRESS) {
mode = MANUAL;
}
break;
case MOVE:
if (direction() == grayDir) {
if (position() >= gTarget) {
stopMotor();
mode = AUTO;
}
}
else {
if (position() <= gTarget) {
stopMotor();
mode = AUTO;
}
}
break;
}
TerminateTask();
}