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
|
#include <ch.h> // needs for all ChibiOS programs
#include <hal.h> // hardware abstraction layer header
#include <vex.h> // vex library header
#include "config.h" // motor and digital configs
#include "lift.hpp" // lift system
#include "claw.hpp"
// missing stdlib functions
extern "C" {
void _exit(int code) {
vexLcdPrintf(0, 0, "exit(%d)!", code);
vexLcdPrintf(0, 1, "halting...");
while (1);
}
void _kill(int pid) {
(void)pid; // TODO...?
}
int _getpid(void) {
return 0; // TODO..?
}
}
void reset(void)
{
uint32_t aircr = *((uint32_t *)0xE000ED0C);
aircr = (aircr & 0xFFFF) | (0x5FA << 16) | 5;
*((volatile uint32_t *)0xE000ED0C) = aircr;
asm("DSB");
while (1);
}
// music for autonomous
static char *getiton =
"GetItOn:d=8,0=5,b=125:16d#5,16d5,16c5,16p,2f4.,16d#5,16d5,16c5,16p,2f4.,16d#5,16d5,16c5,16p,2f4.,16d#5,16d5,16a#4,16p,2c5.,1f4";
// called once
void vexUserSetup(void)
{
vexDigitalConfigure(const_cast<vexDigiCfg*>(dConfig), DIG_CONFIG_SIZE(dConfig));
vexMotorConfigure(const_cast<vexMotorCfg*>(mConfig), MOT_CONFIG_SIZE(mConfig));
}
static int autonToUse = 2;
static char *autons[3] = {
"basic (3)",
"fence push (3-5)",
"bag drop (4-6)"
};
void vexUserInit(void)
{
lift::init(); // start the lift system
claw::init(); // start the claw system
// if in disabled autonomous, prompt for a selection
if (vexSpiGetControl() & (kFlagCompetitionSwitch | kFlagAutonomousMode)) {
int timeout = 100, sel = autonToUse;
while (1) {
vexLcdPrintf(0, 0, "select auton: %2d", (timeout / 10));
if (timeout-- <= 0)
break;
vexLcdPrintf(0, 1, autons[sel]);
auto btn = vexLcdButtonGet(0);
if ((btn & kLcdButtonLeft) && sel > 0)
sel--;
else if ((btn & kLcdButtonRight) && sel < 2)
sel++;
else if (btn & kLcdButtonCenter) {
autonToUse = sel;
break;
}
vexSleep(100);
}
vexLcdPrintf(0, 0, "auton selected: ");
vexLcdPrintf(0, 1, autons[autonToUse]);
}
}
msg_t vexAutonomous(void *arg)
{
(void)arg;
vexTaskRegister("auton");
// jam out
vexAudioPlayRtttl(getiton, 100, 0);
// drop the star
claw::unhook(); // 500ms
if (autonToUse == 0) {
// pushy push push
vexMotorSet(mDriveLeft, 127); // drive forward
vexMotorSet(mDriveRight, -127);
vexSleep(2200);
vexMotorSet(mDriveLeft, -127); // back up
vexMotorSet(mDriveRight, 127);
vexSleep(1200);
} else if( autonToUse == 1 ) {
lift::set(60);
vexMotorSet(mDriveLeft, 127); // forward
vexMotorSet(mDriveRight, -127);
vexSleep(2800);
vexMotorSet(mDriveLeft, -40); // back up
vexMotorSet(mDriveRight, 40);
vexSleep(2300);
vexMotorSet(mDriveLeft, -80); // turn
vexMotorSet(mDriveRight, -80);
lift::set(0);
} else if( autonToUse == 2 ) {
lift::set(-10);
vexMotorSet(mDriveLeft, 127); // straight
vexMotorSet(mDriveRight, -127);
vexSleep(1000); // 2200ms
vexMotorSet(mDriveLeft, 0); // stop
vexMotorSet(mDriveRight, 0);
claw::hold(); // 3100ms
lift::set(90);
vexMotorSet(mDriveLeft, -80); // turn
vexMotorSet(mDriveRight, -80);
vexSleep(500); // 3600ms
vexMotorSet(mDriveLeft, 127); // forward
vexMotorSet(mDriveRight, -127);
vexSleep(1400); // 5000ms
vexMotorSet(mDriveLeft, 0); // stop
vexMotorSet(mDriveRight, 0);
claw::open();
vexSleep(500); // 5500ms
vexMotorSet(mDriveLeft, -40); // back
vexMotorSet(mDriveRight, 40);
vexSleep(800); // 6300ms
// DRAFT
vexMotorSet(mDriveLeft, -80); // turn
vexMotorSet(mDriveRight, -80);
vexSleep(1400); // 7700ms
lift::set(0);
vexMotorSet(mDriveLeft, 127); // straight
vexMotorSet(mDriveRight, -127);
vexSleep(800); // 8500ms
vexMotorSet(mDriveLeft, 0); // stop
vexMotorSet(mDriveRight, 0);
lift::set(-10);
claw::hold(); // 9400ms
lift::set(90);
vexSleep(400); // 9800ms
vexMotorSet(mDriveLeft, -80); // turn
vexMotorSet(mDriveRight, -80);
vexSleep(1000); // 10,800ms
}
vexMotorStopAll();
while (1)
vexSleep(25);
return (msg_t)0;
}
static char waVexLcdTask[512];
msg_t vexLcdTask(void *arg)
{
(void)arg;
vexTaskRegister("lcdt");
while (!chThdShouldTerminate()) {
vexLcdPrintf(0, 0, "%3.0f%% / %3.0f%%",
(float)(vexSpiGetMainBattery() - 5000) / 32.0f, (float)(vexSpiGetBackupBattery() - 6000) / 40.0f);
vexSleep(500);
}
return (msg_t)0;
}
msg_t vexOperator(void *arg)
{
(void)arg;
auto joy = reinterpret_cast<jsdata2*>(vexSpiGetJoystickDataPtr(1));
// Must call this
vexTaskRegister("operator");
chThdCreateStatic(waVexLcdTask, 512, NORMALPRIO - 1, vexLcdTask, nullptr);
// Run until asked to terminate
while (!chThdShouldTerminate()) {
if (joy->Btn7R)
reset();
int dy = vexControllerGet(Ch3);
int dx = vexControllerGet(Ch4);
vexMotorSet(mDriveLeft, dy - dx);
vexMotorSet(mDriveRight, -dy - dx);
if (joy->Btn5U)
lift::raise();
else if (joy->Btn5D)
lift::lower();
if (joy->Btn8D)
lift::set(0);
else if (joy->Btn8L)
lift::set(30);
else if (joy->Btn8R)
lift::set(60);
else if (joy->Btn8U)
lift::set(90);
int claw = (joy->Btn6U ? 127 : (joy->Btn6D ? -127 : 0));
vexMotorSet(mClaw, claw);
// Don't hog cpu
vexSleep(100);
}
return (msg_t)0;
}
|