summaryrefslogtreecommitdiffstats
path: root/vexuser.cpp
blob: 7b4ca6953e2e40c983ca1c0a6fa87c1c02135072 (plain)
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
#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

// 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(dConfig, DIG_CONFIG_SIZE(dConfig));
	vexMotorConfigure(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)
{
	// if in disabled autonomous, prompt for a selection
	if (vexSpiGetControl() & (kFlagCompetitionSwitch | kFlagAutonomousMode)) {
		int timeout = 100, sel = 0;
		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
	vexMotorSet(mClaw, 127);
	vexSleep(200);
	vexMotorSet(mClaw, -127);
	vexSleep(500);
	vexMotorSet(mClaw, 0);
	vexSleep(500);

	if (autonToUse == 0) {
	
	// pushy push push
	vexMotorSet(mDriveLeft, 127);    // drive forward
	vexMotorSet(mDriveRight, -127);
	vexSleep(2500);
	vexMotorSet(mDriveLeft, -127);   // back up
	vexMotorSet(mDriveRight, 127);
	vexSleep(1500);
	vexMotorStopAll();

	} else if( autonToUse == 1 ) {

	vexMotorSet(mLiftLowLeft,   60);  // lift up
	vexMotorSet(mLiftHighLeft,  60);
	vexMotorSet(mLiftLowRight,  60);
	vexMotorSet(mLiftHighRight, 60);
	vexSleep(500);
	vexMotorSet(mLiftLowLeft,   10);  // lift steady
	vexMotorSet(mLiftHighLeft,  10);
	vexMotorSet(mLiftLowRight,  10);
	vexMotorSet(mLiftHighRight, 10);

	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);
	vexMotorSet(mLiftLowLeft,   -60); // drop lift 
	vexMotorSet(mLiftHighLeft,  -60);
	vexMotorSet(mLiftLowRight,  -60);
	vexMotorSet(mLiftHighRight, -60);
	vexSleep(500);
	vexMotorStopAll();

	} else if( autonToUse == 2 ) {

	vexMotorSet(mLiftLowLeft,   -40); // lift down
	vexMotorSet(mLiftHighLeft,  -40);
	vexMotorSet(mLiftLowRight,  -40);
	vexMotorSet(mLiftHighRight, -40);
	vexMotorSet(mDriveLeft, 127);     // straight
	vexMotorSet(mDriveRight, -127);
	vexSleep(2000);
	vexMotorSet(mDriveLeft, 0);       // stop
	vexMotorSet(mDriveRight, 0);
	vexMotorSet(mClaw, 127);          // grab
	vexSleep(500);
	vexMotorSet(mClaw, 50);           // steady
	vexMotorSet(mLiftLowLeft,   127); // lift
	vexMotorSet(mLiftHighLeft,  127);
	vexMotorSet(mLiftLowRight,  127);
	vexMotorSet(mLiftHighRight, 127);
	vexSleep(850);
	vexMotorSet(mLiftLowLeft,   10);  // steady
	vexMotorSet(mLiftHighLeft,  10);
	vexMotorSet(mLiftLowRight,  10);
	vexMotorSet(mLiftHighRight, 10);
	vexMotorSet(mDriveLeft, 80);      // turn
	vexMotorSet(mDriveRight, 80);
	vexSleep(500);
	vexMotorSet(mDriveLeft, 127);     // forward
	vexMotorSet(mDriveRight, -127);
	vexSleep(1500);
	vexMotorSet(mDriveLeft, 0);       // stop
	vexMotorSet(mDriveRight, 0);
	vexMotorSet(mClaw, -127);         // drop
	vexSleep(1000);
	vexMotorSet(mDriveLeft, -40);     // back
	vexMotorSet(mDriveRight, 40);
	vexSleep(1000);
	vexMotorStopAll();

	}

	while (1)
		vexSleep(25);

    return (msg_t)0;
}

static char waVexLcdTask[512];
msg_t vexLcdTask(void *arg)
{
	(void)arg;

	vexTaskRegister("lcdtask");

	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;
}

constexpr const int liftMaxSpeed = 60;
static int liftTargetLoc = 0;

static char waVexLiftTask[512];
msg_t vexLiftTask(void *arg)
{
	(void)arg;

	vexTaskRegister("lcdtask");

	//static int timeout = -1;
	while (!chThdShouldTerminate()) {
		int actual = vexEncoderGet(kVexQuadEncoder_1);
		int diff = liftTargetLoc - actual;
		int speed = 0; 
		if (diff != 0) {
			/*if (timeout == -1)
				timeout = 10;
			else if (timeout > 0)
				timeout--;
			else
				diff *= 2;*/

			speed = 2 * diff;
		} else {
			/*timeout = -1,*/ speed = 0;
		}

		//vexLcdPrintf(0, 1, "A%02d T%02d S%d", actual, timeout, speed);
		vexMotorSet(mLiftLowLeft,   speed);
		vexMotorSet(mLiftHighLeft,  speed);
		vexMotorSet(mLiftLowRight,  speed);
		vexMotorSet(mLiftHighRight, speed);

		vexSleep(50);
	}

	return (msg_t)0;
}

msg_t vexOperator(void *arg)
{
	(void)arg;

	// Must call this
	vexTaskRegister("operator");

	chThdCreateStatic(waVexLcdTask, 512, NORMALPRIO - 1, vexLcdTask, nullptr);
	chThdCreateStatic(waVexLiftTask, 512, NORMALPRIO - 1, vexLiftTask, nullptr);

	// Run until asked to terminate
	while (!chThdShouldTerminate()) {
		if (vexControllerGet(Btn7R))
			reset();

		int dy = vexControllerGet(Ch3);
		int dx = -vexControllerGet(Ch4);

		vexMotorSet(mDriveLeft, dy + dx);
		vexMotorSet(mDriveRight, -dy + dx);

		if (vexControllerGet(Btn5U))
			liftTargetLoc += 5;
		else if (vexControllerGet(Btn5D))
			liftTargetLoc -= 5;

		if (vexControllerGet(Btn8D))
			liftTargetLoc = 0;
		else if (vexControllerGet(Btn8L))
			liftTargetLoc = 30;
		else if (vexControllerGet(Btn8R))
			liftTargetLoc = 60;
		else if (vexControllerGet(Btn8U))
			liftTargetLoc = 90;

		int claw = (vexControllerGet(Btn6U) ? 127 : (vexControllerGet(Btn6D) ? -127 : 0));
		vexMotorSet(mClaw, claw);

		// Don't hog cpu
		vexSleep(100);
	}

	return (msg_t)0;
}