-
Notifications
You must be signed in to change notification settings - Fork 0
/
wRCsolver_4x4x4_64.c
558 lines (425 loc) · 14.8 KB
/
wRCsolver_4x4x4_64.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
#pragma config(Sensor, S2, sensorLightLSRotate, sensorLightActive)
#pragma config(Sensor, S3, sensorLightRotate, sensorLightActive)
#pragma config(Sensor, S4, sensorCubeAttatch, sensorTouch)
#pragma config(Motor, motorA, motorRotate, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorB, motorPlatform, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorC, motorLS, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
//#include "encoderMotor_improved.h"
const float tRatioRT = (float)56 / 23;
const float tRatioLS = (float)40 / 8;
const int rotateLimitLightThreshold = 76;
const int LSRotateLightThreshold = 65;
const int ENCODERvAL_LS_BASE = -1830;
const int ENCODERvAL_LS_ARRAY[] = {
ENCODERvAL_LS_BASE,
// Below is the increasement value of LS
0, // LS1
-380, // LS2
-730, // LS3
-1110 // LS4
};
void showSensorVal() {
//while (1) {
eraseDisplay();
//nxtDisplayString(2, "%d", nMotorEncoder[motorPlatform]);
nxtDisplayString(2, "%d", SensorValue[sensorLightLSRotate]);
//wait1Msec(20);
//}
}
void setAngleRelative(int motorName, int relativAngle, int moveSpeed)
{
moveSpeed = abs(moveSpeed);
nMotorEncoder[motorName] = 0; //clear the LEGO motor encoders
nMotorEncoderTarget[motorName] = relativAngle; //set the target stoping position
if (relativAngle < 0) {
moveSpeed = -moveSpeed;
}
motor[motorName] = moveSpeed; //turn both motors on at 30 percent power
while (nMotorRunState[motorName] != runStateIdle) //while the encoder wheel turns one revolution
{
showSensorVal();
// This condition waits for motors B + C to come to an idle position. Both motors stop
// and then jumps out of the loop
}
motor[motorName] = 0; //turn both motors off
//wait1Msec(3000); // wait 3 seconds to see feedback on the debugger screens
// open the "NXT Devices" window to see the distance the encoder spins.
// the robot will come to a stop at the nMotorEncoderTarget position.
}
void action_PT_goUp() {
motor[motorPlatform] = -100;
}
void action_PT_goDown() {
motor[motorPlatform] = 100;
}
void action_PT_stop() {
motor[motorPlatform] = 0;
}
bool isRTAtInitPositon () {
return (SensorValue[sensorLightRotate] >= rotateLimitLightThreshold);
}
void RTrotateToPosition(int motorName) {
while ( SensorValue[sensorLightRotate] < rotateLimitLightThreshold) {
motor[motorName] = 15;
}
motor[motorName] = 0;
}
const int BTN_Gray_Rectangle = 0;
const int BTN_Right_Arrow = 1;
const int BTN_Left_Arrow = 2;
const int BTN_Orange_Square = 3;
const int STATE_ROTATE_AT_INIT = 0;
const int STATE_ROTATE_AT_BEHIND = 1;
const int STATE_ROTATE_AT_ELSE = 2;
const char STATE_PLATFORM_AT_BOTTOM_LIMIT = 0;
const char STATE_PLATFORM_AT_LS1 = 101;
const char STATE_PLATFORM_AT_LS2 = 102;
const char STATE_PLATFORM_AT_LS3 = 103;
const char STATE_PLATFORM_AT_LS4 = 104;
int state_currentRotatePosition;
int state_currentPlatformPosition;
void action_rotateToBehind() {
if (state_currentRotatePosition == STATE_ROTATE_AT_BEHIND)
return;
setAngleRelative(motorRotate, -93*tRatioRT, -30);
state_currentRotatePosition = STATE_ROTATE_AT_BEHIND;
}
void action_rotateToFront() {
if (state_currentRotatePosition == STATE_ROTATE_AT_INIT)
return;
setAngleRelative(motorRotate, 70*tRatioRT, 30);
RTrotateToPosition(motorRotate);
state_currentRotatePosition = STATE_ROTATE_AT_INIT;
}
/// Whether the cube is attached to the platform
bool isCubeAttached() {
return SensorValue[sensorCubeAttatch] == 1;
}
void waitUntilCubeDettached_filtered() {
while(isCubeAttached()) {
while (isCubeAttached()) {}
wait1Msec(90);
}
}
bool action_PT_toLSn_fromBottom(int n) {
if (state_currentPlatformPosition != STATE_PLATFORM_AT_BOTTOM_LIMIT)
return false;
int destVal, finalStateVal;
destVal = ENCODERvAL_LS_BASE + ENCODERvAL_LS_ARRAY[n];
switch(n) {
case 1:
finalStateVal = STATE_PLATFORM_AT_LS1;
break;
case 2:
finalStateVal = STATE_PLATFORM_AT_LS2;
break;
case 3:
finalStateVal = STATE_PLATFORM_AT_LS3;
break;
case 4:
finalStateVal = STATE_PLATFORM_AT_LS4;
break;
defaut: return false;
}
if (! isCubeAttached())
{
// we assume the platform is at the below
nMotorEncoder[motorPlatform] = 0;
action_PT_goUp();
int encoderVal;
while ( ! isCubeAttached()) {
encoderVal = nMotorEncoder[motorPlatform];
// debug display
eraseDisplay();
nxtDisplayString(2, "encoder=%d", encoderVal);
wait1Msec(20);
if (encoderVal < -800) {
// still no cube? bad situation.
break;
}
}
action_PT_stop();
}
if (! isCubeAttached())
{
// nothing hit? I assume there's no cube on the platform!
PlaySound(soundException);
wait1Msec(600);
// move the platform to the origin position
setAngleRelative(motorPlatform, -nMotorEncoder[motorPlatform], 95);
return false;
}
else
{
// the platform now just hit the cube
setAngleRelative(motorPlatform, destVal, 95);
/*
while(1) {
eraseDisplay();
nxtDisplayString(2, "encoder=%d", nMotorEncoder[motorPlatform]);
wait1Msec(20);
}
*/
}
state_currentPlatformPosition = finalStateVal;
return true;
}
void swingTheLS() {
for(int i=0; i< 5; i++) {
setAngleRelative(motorLS, 10*tRatioLS, 50);
//wait1Msec(50);
setAngleRelative(motorLS, -10*tRatioLS, 50);
//wait1Msec(50);
}
//LSRotateToPosition(motorLS, -1);
}
void action_PT_toBottomLimit(bool ignoreCurrentPos=false) {
if ( !ignoreCurrentPos )
if (state_currentPlatformPosition < STATE_PLATFORM_AT_LS1)
return;
//if(! isCubeAttached())
// return;
const int confidentialMovedDownVal = 800;
int fromEncoderVal = nMotorEncoder[motorPlatform];
do {
action_PT_goDown();
// this is a filtered waiting,
// in case of vibration making the wrong state.
waitUntilCubeDettached_filtered();
action_PT_stop();
// too early for the platform to stop move down,
// assuming that the cube dettached accidentally
if ( nMotorEncoder[motorPlatform] - fromEncoderVal
< confidentialMovedDownVal )
{
setAngleRelative(motorPlatform, 360, 80);
bool isFirst = true;
while (! isCubeAttached())
{
PlaySound(soundException);
if (isFirst) {
isFirst = false;
for(int i=0; i< 5; i++) {
setAngleRelative(motorLS, 10*tRatioLS, 50);
//wait1Msec(50);
if ( isCubeAttached()) break;
setAngleRelative(motorLS, -10*tRatioLS, 50);
//wait1Msec(50);
if ( isCubeAttached()) break;
}
}
wait1Msec(500);
}
}
else {
break ;
}
} while(1);
// the cube is landed on the RT shelf,
// hide(move down) the platform;
setAngleRelative(motorPlatform, 350, 80);
state_currentPlatformPosition = STATE_PLATFORM_AT_BOTTOM_LIMIT;
}
void action_PT_fromLS_toLS(int LSfrom, int LSto) {
if (state_currentPlatformPosition < STATE_PLATFORM_AT_LS1)
return;
//if(! isCubeAttached())
// return;
int encoderVal = ENCODERvAL_LS_ARRAY[LSto] - ENCODERvAL_LS_ARRAY[LSfrom];
setAngleRelative(motorPlatform, encoderVal, 70);
bool isFirst = true;
while (! isCubeAttached())
{
PlaySound(soundException);
if (isFirst) {
isFirst = false;
for(int i=0; i< 5; i++) {
setAngleRelative(motorLS, 10*tRatioLS, 50);
//wait1Msec(50);
if ( isCubeAttached()) break;
setAngleRelative(motorLS, -10*tRatioLS, 50);
//wait1Msec(50);
if ( isCubeAttached()) break;
}
}
wait1Msec(500);
}
switch(LSto) {
case 1:
state_currentPlatformPosition = STATE_PLATFORM_AT_LS1;
break;
case 2:
state_currentPlatformPosition = STATE_PLATFORM_AT_LS2;
break;
case 3:
state_currentPlatformPosition = STATE_PLATFORM_AT_LS3;
break;
case 4:
state_currentPlatformPosition = STATE_PLATFORM_AT_LS4;
break;
defaut: return;
}
}
void LSRotateToPosition(int motorName, int direction, int maxFindAngle=40*tRatioLS) {
if (direction == 0 ) return;
direction = direction>0 ? 1 : -1;
const int minSearchSpeed = 25;
int speed = 55;
int _maxAngle, maxAngle = abs(maxFindAngle);
bool isFirstTime=true;
while (1) {
// reset the encoderVal to 0
nMotorEncoder[motorLS] = 0;
motor[motorName] = direction * speed;
while ( SensorValue[sensorLightLSRotate] < LSRotateLightThreshold) {
showSensorVal();
if (isFirstTime) {
_maxAngle = maxAngle / 2;
isFirstTime = false;
}else{
_maxAngle = maxAngle;
}
if (abs(nMotorEncoder[motorLS]) > _maxAngle) {
// rotate too much?
break;
}
}
motor[motorName] = 0;
wait1Msec(200);
if (SensorValue[sensorLightLSRotate] < LSRotateLightThreshold) {
// I think the rotation is just too much... rotate back
direction *= -1;
if (speed <= minSearchSpeed) {
// still cannot find it. dont speed too much time on this.
break;
}
// gradually decrease the speed.
speed = speed <= minSearchSpeed ? minSearchSpeed : speed-7;
}else{
break;
}
}
// TODO: continue to turn(small), find the largest val
}
/*
void action_LS_turn_90_v2() {
bool hasLoad =
(state_currentPlatformPosition >= STATE_PLATFORM_AT_LS1);
int speed=100;
int currentVal, destVal = 70*tRatioLS;
setAngleRelative(motorLS, destVal, speed);
currentVal = nMotorEncoder[motorLS];
LSRotateToPosition(motorLS, 90*tRatioLS - currentVal);
// turn an extra angle because of gap
setAngleRelative(motorLS, 10*tRatioLS, speed);
LSRotateToPosition(motorLS, -1);
}*/
void action_LS_turn_90() {
bool hasLoad =
(state_currentPlatformPosition >= STATE_PLATFORM_AT_LS1);
int speed=100;
int currentVal, destVal = 70*tRatioLS;
setAngleRelative(motorLS, destVal, speed);
// wait a short time for the twist force back
wait1Msec(200);
showSensorVal();
currentVal = nMotorEncoder[motorLS];
LSRotateToPosition(motorLS, 90*tRatioLS - currentVal, ((90+10)*tRatioLS-currentVal)*2 );
// turn an extra angle because of gap
wait1Msec(200);
setAngleRelative(motorLS, 15*tRatioLS, 70);
//setAngleRelative(motorLS, -10*tRatioLS, speed);
wait1Msec(200);
LSRotateToPosition(motorLS, -1);
}
void action_LS_turn_90i() {
bool hasLoad =
(state_currentPlatformPosition >= STATE_PLATFORM_AT_LS1);
int speed=100;
int currentVal, destVal = -70*tRatioLS;
setAngleRelative(motorLS, destVal, speed);
// wait a short time for the twist force back
wait1Msec(200);
currentVal = nMotorEncoder[motorLS];
int deltaVal = (-(90+10)*tRatioLS-currentVal);
LSRotateToPosition(motorLS, -90*tRatioLS - currentVal, deltaVal*2 );
// turn an extra angle because of gap
wait1Msec(200);
setAngleRelative(motorLS, -15*tRatioLS, 70);
//setAngleRelative(motorLS, 10*tRatioLS, speed);
wait1Msec(200);
LSRotateToPosition(motorLS, 1);
}
task main()
{
// warning if the rotatePart is not at the correct position
while ( ! isRTAtInitPositon ()) {
//PlaySound(soundException);
nxtDisplayString(2, "Rotation block is not");
nxtDisplayString(3, " at the init position");
nxtDisplayString(4, " (%d) ", SensorValue[sensorLightRotate]);
wait1Msec(100);
}
PlaySound(soundBeepBeep);
eraseDisplay();
if ( isCubeAttached() ) {
// assuming the platform is at LS
state_currentPlatformPosition = STATE_PLATFORM_AT_LS1;
action_PT_toBottomLimit();
}
// do some initiation
state_currentRotatePosition = STATE_ROTATE_AT_INIT;
state_currentPlatformPosition = STATE_PLATFORM_AT_BOTTOM_LIMIT;
//nMotorPIDSpeedCtrl[motorLS] = mtrSpeedReg;
while(1) {
showSensorVal();
if(nNxtButtonPressed == BTN_Right_Arrow) {
//action_PT_goDown();
//wait1Msec(100);
//action_PT_stop();
//action_PT_toBottomLimit();
action_PT_toLSn_fromBottom(4);
}
if(nNxtButtonPressed == BTN_Left_Arrow) {
//action_PT_goUp();
//wait1Msec(100);
//action_PT_stop();
while(1) {
bool succ =
action_PT_toLSn_fromBottom(2);
if (!succ) break;
action_LS_turn_90();
action_PT_fromLS_toLS(2, 1);
action_LS_turn_90i();
//action_rotateToBehind();
action_PT_toBottomLimit();
//action_rotateToFront();
}
}
if(nNxtButtonPressed == BTN_Orange_Square) {
PlaySound(soundShortBlip);
wait1Msec(500);
while(1) {
nxtDisplayString(1, "rotate to:");
nxtDisplayString(2, "<-Behind Front->");
if(nNxtButtonPressed == BTN_Right_Arrow) {
//action_rotateToFront();
action_LS_turn_90();
break;
}
if(nNxtButtonPressed == BTN_Left_Arrow) {
//action_rotateToBehind();
action_LS_turn_90i();
break;
}
if(nNxtButtonPressed == BTN_Orange_Square) {
swingTheLS();
break;
}
}
PlaySound(soundUpwardTones);
wait1Msec(500);
}
}
}