-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNavigator.m
667 lines (606 loc) · 26.9 KB
/
Navigator.m
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
classdef Navigator < handle
properties
state; % the state of the robot.
previousState;
classifier; % the neural net classifier.
body; % the motor_carrier object for this SLAM instance.
%destinations_visited; % nodes in the track graph.
%forks_visited; % the locations where forks are encountered.
%forks_completely_traversed; % forks marked as being completely visited.
track_graph;
calibration_time;
max_ir_reading;
min_ir_reading;
blinking_rate;
is_calibrated;
currentBranch;
previousBranch;
branchCheck;
branchChoice;
branchFailed;
hasBox;
findingBox;
boxType;
%Facing enum?
MAX_SIZE
MIN_SIZE
MAX_HALL
MIN_HALL
end
properties (Constant = true)
KP_RPM = 0.055;
KD_RPM = 0.005;
KI_RPM = 0.0;
KP_IR = 1.2;
KD_IR = 0.005;
KI_IR = 0.0;
TARGET_RPM = 10;
MAX_RPM = 30;
TARGET_IR_READING = 0;
R_MOTOR_SF = 0.95;
MOTOR_L = 4;
MOTOR_R = 3;
SERVO = 4;
BAD_BLOCK = 0;
GOOD_BLOCK = 1;
EXCELLENT_BLOCK = 2;
HALL_EFFECT = 1;
SERVO_ANALOG = 2;
SF_ULTRASON
end
methods
function obj = Navigator(body)
obj.state = States.StandBy;
obj.previousState = States.StandBy;
%obj.classifier = classifier;
obj.body = body;
obj.max_ir_reading = [nan, nan, nan, nan];
obj.min_ir_reading = [nan, nan, nan, nan];
obj.calibration_time = 5;
obj.blinking_rate = 0.125;
obj.is_calibrated = false;
obj.hasBox = false;
obj.findingBox = true;
obj.currentBranch = 0;
obj.previousBranch = 0;
obj.branchCheck = ones(1,4);
obj.branchChoice = 1;
obj.branchFailed = false;%
obj.boxType = nan;
obj.body.servo(obj.SERVO,0);
end
function [posX, posY] = do_task(obj)
switch(obj.state)
case States.StandBy
obj.previousState = States.StandBy;
obj.body.motor(obj.MOTOR_R, 0);
obj.body.motor(obj.MOTOR_L, 0);
obj.body.servo(obj.SERVO,0);
obj.body.resetEncoder(1);
obj.body.resetEncoder(2);
pause(0.1);
if(obj.is_calibrated)
obj.state = States.FollowLineForward; %CHANGE
else
obj.state = States.Calibration;
obj.body.setRGB(255,0,0);
end
posX = 0;
posY = 0;
return;
case States.Calibration
success = false;
while (not(success))
success = obj.calibrate_IR_Sensor();
end
fprintf("Success! The IR sensor has been calibrated.");
obj.body.setRGB(0,255,0)
obj.state = States.StandBy;
obj.is_calibrated = true;
posX = 0;
posY = 0;
return;
case States.FollowLineForward % PID control for IR sensor and speed
disp("followingLine\n");
ir_reading = obj.body.readReflectance();
pause(0.1)
control_M1 = 0; control_M2 = 0;
error_IR = 0; prev_error_IR = 0;
Threshold = 100;
tic;
while not(obj.isFork(ir_reading))
[error_IR, control_M1, control_M2] = obj.ir_PD_Controller(obj.normalize_IR_reading(ir_reading),prev_error_IR);
[control_M1, control_M2] = obj.setMotorSpeeds(control_M1, control_M2);
obj.body.motor(obj.MOTOR_R, round(control_M1*obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, round(control_M2));
prev_error_IR = error_IR;
ir_reading = obj.body.readReflectance();
if(not(obj.line_within_proximity(ir_reading,[1,2,3,4])))
Threshold = Threshold - 1;
if(Threshold <= 0)
obj.stopMotors();
while(not(obj.line_within_proximity(ir_reading,[2,3])))
ir_reading = obj.body.readReflectance();
pause(0.0001);
end
pause(0.1);
Threshold = 100;
end
else
Threshold = 100;
end
end
% Hit fork
obj.body.motor(obj.MOTOR_R, 0);
obj.body.motor(obj.MOTOR_L, 0);
pause(0.01);
elapsed_time = toc;
obj.state = States.Branches;
;
case States.Branches
% check which branch we are in
% If we are in zero
if(obj.currentBranch == 0)
% Do we have a box?
if(obj.hasBox)
% choose path based on type of box
% obj.MoveForward(2);
% obj.Turn90DegreeLeft();
% pause(2);
% obj.TurnToBranchX(1);
% obj.previousBranch = obj.currentBranch;
% obj.currentBranch = 5;
% obj.state = States.FollowLineForward;
if(obj.boxType == 0)
obj.MoveForward(1.8);
obj.Turn90DegreeLeft();
pause(2);
obj.TurnToBranchX(2);
obj.previousBranch = obj.currentBranch;
obj.currentBranch = 6;
obj.state = States.FollowLineForward;
else
obj.MoveForward(1.8);
obj.Turn90DegreeLeft();
pause(2);
obj.TurnToBranchX(1);
obj.previousBranch = obj.currentBranch;
obj.currentBranch = 5;
obj.state = States.FollowLineForward;
end
elseif(obj.branchFailed)%
obj.branchFailed = false;%
obj.body.servo(obj.SERVO, 0);
obj.state = States.TurnAround;%
% Else
else
% choose one of the branches from 1 to 4
% Move and turn to that branch
obj.MoveForward(1.8);
if(obj.branchCheck(obj.branchChoice) ~= 1)
obj.branchChoice = obj.branchChoice + 1;
end
obj.Turn90DegreeLeft();
pause(2);
obj.TurnToBranchX(obj.branchChoice);
obj.previousBranch = obj.currentBranch;
obj.currentBranch = obj.branchChoice;
obj.branchChoice = obj.branchChoice + 1;
if(obj.branchChoice == 5)
obj.branchChoice = 1;
end
% go to follow line state
obj.state = States.FollowLineForward;
end
% Else if we are in branches 1 to 4
elseif(obj.currentBranch >= 1 && obj.currentBranch <= 4)
% If we are trying to find box
if(obj.findingBox)
if(obj.branchFailed == true)%
obj.ReturnToZero();%
else
% go to find box state
obj.state = States.GraspItem; % graspItem incomplete needs to move forward
% in grasp item we need to set findingBox to
% false
end
% Else
else
obj.ReturnToZero();
end
% Else we are in branches 5 and 6
else
% If we have a box
if(obj.hasBox)
% drop it off
obj.state = States.UnGraspItem;
% else
else
% move back to branch zero
if(obj.currentBranch == 6)
obj.ReturnToZeroFrom6();
else
obj.ReturnToZero();
end
end
end
;
case States.MoveToCenter
disp("moving to center \n");
obj.MoveForward(2.3);
disp(obj.previousState);
obj.Turn90DegreeLeft();
if(obj.previousState == States.Branches) %% we can add more logic here aka if branch one doesnt have object then go to state branch 2 directly
obj.state = States.BranchOne;
else
obj.state = States.TurnAround;
end
;
case States.GraspItem % Pd? control for grasping the object
% disp("I picked a Box");
obj.MoveForward(3);
obj.state = States.GoBack;
% obj.previousState = States.GraspItem;
obj.body.startStream('analog');
success = false;
size = 0;
attempts = 3;%
currentAttempts = 0;%
while(not(success))
obj.body.servo(obj.SERVO,0);
pause(5);
[success, block_features] = obj.pickUpAndAnalyzeBlock();
if(success == false)%
currentAttempts = currentAttempts +1;%
if(currentAttempts == attempts)%
obj.branchCheck(obj.currentBranch) = 0;%
obj.branchFailed = true;%
break;
end%
% distanceDetected = obj.body.ultrasonicPulse() * 0.0172;
% if(distanceDetected > (obj.SF_ULTRASON * 5 * 2.54))
% obj.branchCheck(obj.currentBranch) = 0;
% end
end
end
if(~obj.branchFailed)
predicted_block_type = obj.classifier.predict(block_features');
obj.boxType = predicted_block_type;
obj.hasBox = true;
obj.findingBox = false;
display(obj.boxType);
end
% obj.changeState(predicted_block_type);
pause(0.1);
;
case States.TurnAround % Pd? control for rotating the robot a complete 180
disp("Turning around \n");
tic;
obj.body.motor(obj.MOTOR_R, round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, - round(obj.TARGET_RPM));
pause(1.5);
while(obj.line_within_proximity(obj.body.readReflectance, [1,2,3,4]))
pause(0.0001);
end
reading_ir = obj.body.readReflectance();
while(not(obj.line_within_proximity(reading_ir,[1,2,3])))
reading_ir= obj.body.readReflectance();
pause(0.0001);
end
obj.body.motor(obj.MOTOR_R, 0);
obj.body.motor(obj.MOTOR_L,0);
obj.state = States.FollowLineForward;
;
case States.UnGraspItem % Pd? control for ungrasping an item
obj.hasBox = false;
obj.findingBox = true;
obj.MoveForward(2);
obj.body.servo(4,0);
obj.state = States.GoBack;
;
case States.BeFree % Pd? control for
;
case States.GoBack
% runtime = 3.0;
tic
obj.body.motor(obj.MOTOR_R, -round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, - obj.TARGET_RPM);
while(obj.line_within_proximity(obj.body.readReflectance, [1,2,3,4]))
pause(0.0001);
end
reading_ir = obj.body.readReflectance();
while(not(obj.line_within_proximity(reading_ir,[1,2,3])))
reading_ir= obj.body.readReflectance();
pause(0.0001);
end
obj.body.motor(obj.MOTOR_R, 0);
obj.body.motor(obj.MOTOR_L,0);
obj.state = States.TurnAround;
;
end
end
function ReturnToZero(obj)
obj.previousBranch = obj.currentBranch;
obj.currentBranch = 0;
% Turn back into branch zero and keep moving
obj.MoveForward(2.3);
% forward or re orient the robot to choose a new
% path
obj.Turn90DegreeLeft();
obj.state = States.TurnAround;
end
function ReturnToZeroFrom6(obj)
obj.previousBranch = obj.currentBranch;
obj.currentBranch = 0;
% Turn back into branch zero and keep moving
obj.MoveForward(2.3);
% forward or re orient the robot to choose a new
% path
obj.Turn90AlitleMoreDegreeLeft();
obj.state = States.TurnAround;
end
function MoveForward(obj,runtime)
tic
obj.body.motor(obj.MOTOR_R, round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, obj.TARGET_RPM);
while toc < runtime
end
obj.stopMotors();
end
function Turn90DegreeLeft(obj)
runtime = 2.5;
tic;
obj.body.motor(obj.MOTOR_R, -round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, obj.TARGET_RPM);
while(toc < runtime)
end
obj.stopMotors();
end
function Turn90AlitleMoreDegreeLeft(obj)
runtime = 3.3;
tic;
obj.body.motor(obj.MOTOR_R, -round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, obj.TARGET_RPM);
while(toc < runtime)
end
obj.stopMotors();
end
function TurnToBranchX(obj, count)
disp('Turning');
currentCount = 0;
obj.body.motor(obj.MOTOR_R, round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, round(-obj.TARGET_RPM));
passed_branch = true;
while(currentCount < count)
values = obj.body.readReflectance();
obj.stopMotors();
pause(0.001);
obj.body.motor(obj.MOTOR_R, round(obj.TARGET_RPM * obj.R_MOTOR_SF));
obj.body.motor(obj.MOTOR_L, round(-obj.TARGET_RPM));
if(obj.line_within_proximity(values,[2,3]) && passed_branch) % this will probably need padding
currentCount = currentCount + 1;
if(currentCount == count)
break;
end
passed_branch = false;
pause(0.5);
elseif(not(passed_branch) && obj.line_not_in_sight(values, [2,3]))
passed_branch = true;
end
end
if(count == 4)
obj.body.motor(obj.MOTOR_R, 0);
pause(0.5);
end
obj.stopMotors();
end
function val = normalize_IR_reading(obj, reading)
val = (reading - obj.min_ir_reading)./ (obj.max_ir_reading - obj.min_ir_reading);
end
function [curr_error_M1, curr_error_M2, control_M1, control_M2] = keepTargetSpeed_PD(obj, prev_error_M1, prev_error_M2, prev_control_M1, prev_control_M2)
[Vel_M1, Vel_M2] = obj.body.readEncoderVel();
rpm_M1 = Vel_M1 * (1 / 720) * 60;
rpm_M2 = Vel_M2 * (1 / 720) * 60;
% Motor 1 (right motor PID values)
error_M1 = obj.TARGET_RPM - rpm_M1;
% error_sum_M1 = error_sum_M1 + error_M1;
error_delta_M1 = prev_error_M1 - error_M1;
curr_error_M1 = error_M1;
% Motor 2 (left motor PID values)
error_M2 = obj.TARGET_RPM - rpm_M2;
% error_sum_M2 = error_sum_M2 + error_M2;
error_delta_M2 = prev_error_M2 - error_M2;
curr_error_M2 = error_M2;
% Control calculations
control_M1 = prev_control_M1 + error_M1.*obj.KP_RPM + obj.KD_RPM.*error_delta_M1; %YOU WILL NEED TO EDIT THIS
control_M2 = prev_control_M2 + error_M2.*obj.KP_RPM + obj.KD_RPM.*error_delta_M2;
end
function success = calibrate_IR_Sensor(obj)
obj.body.reflectanceSetup();
pause(.5)
fprintf("Place ir sensor completelely off the track.\n");
obj.body.setRGB(255,0,255);
pause(obj.calibration_time);
[samples,increments] = getSamples(obj);
if(isequal(samples, [nan, nan, nan, nan]) || increments == 0)
success = false;
return;
end
mean_min = mean(samples./increments);
obj.min_ir_reading = [mean_min,mean_min,mean_min,mean_min];
fprintf("Place ir sensor such that the reflective tape covers the entire sensor.\n")
obj.body.setRGB(255,0,255);
pause(obj.calibration_time);
[samples, increments] = getSamples(obj);
if(isequal(samples, [nan, nan, nan, nan]) || increments == 0)
success = false;
return;
end
mean_max = mean(samples./increments);
obj.max_ir_reading = [mean_max,mean_max,mean_max,mean_max];
success = true;
return;
end
function [samples, increments] = getSamples(obj)
tic;
samples = zeros(1,4);
blink_red = false;
increments = 0;
elapsed_time = 0;
while elapsed_time < obj.calibration_time
if(mod(elapsed_time, obj.blinking_rate) < 0.1)
if(blink_red)
obj.body.setRGB(255,69,0);
blink_red = false;
else
obj.body.setRGB(0,69,255);
blink_red = true;
end
end
samples = samples + obj.body.readReflectance();
increments = increments + 1;
elapsed_time = toc;
end
end
function val = isFork(obj, ir_reading)
normalized_ir = obj.normalize_IR_reading(ir_reading);
val = obj.valThresholdG(normalized_ir(1), 1, 0.5) && ...
obj.valThresholdG(normalized_ir(2), 1, 0.5) && ...
obj.valThresholdG(normalized_ir(3), 1, 0.5) && ...
obj.valThresholdG(normalized_ir(4), 1, 0.5);
end
function val = valThresholdG(obj, i, j, padding)
val = i >= (j - padding);
end
function val = valThresholdL(obj, i, j, padding)
val = i <= (j + padding);
end
function ScaleFactor = FindSF(obj,inputVal)
if inputVal >= 0 && inputVal < 10
ScaleFactor = 1.8108;
elseif inputVal >= 10 && inputVal < 15
ScaleFactor = 1.7083;
elseif inputVal >= 15 && inputVal < 20
ScaleFactor = 1.4024;
elseif inputVal >= 20 && inputVal < 25
ScaleFactor = 1.2583;
elseif inputVal >= 25 && inputVal < 30
ScaleFactor = 1.13;
elseif inputVal >= 30 && inputVal < 35
ScaleFactor = 1.3374;
else
ScaleFactor = 1.1556;
end
end
function val = normalize_val(obj,in, max, min)
val = (in - min)./max;
end
function [success, block_features] = pickUpAndAnalyzeBlock(obj)
previousAnalogVal = 0;
currentAnalogVal = 0;
success = true;
block_features = zeros(5,1);
for i = 10:10:180
obj.body.servo(4, i);
currentAnalogVal = obj.body.getAverageData('analog', 5);
pause(0.1);
highTresh = 0;
lowTresh = 0;
if( i < 50)
highTresh = (previousAnalogVal + 0.02);
lowTresh = (previousAnalogVal - 0.02);
else
highTresh = (previousAnalogVal + 10);
lowTresh = (previousAnalogVal - 10);
end
if( (currentAnalogVal(obj.SERVO_ANALOG) <= highTresh ) && (currentAnalogVal(obj.SERVO_ANALOG)>= lowTresh ) )
try
%s 47 i 140, m 35 i 110 , B 23 i 80
if (i <= 80 )
obj.body.servo(obj.SERVO, i - 23); %s 47 m 35 , B 23
size = i - 23;
break;
elseif(i > 80 && i < 115)
obj.body.servo(obj.SERVO, i - 30);
size = i - 30;
break;
else
obj.body.servo(obj.SERVO, i - 25);
size = i - 25;
break;
end
catch
success = false;
return;
end
end
previousAnalogVal = currentAnalogVal(2);
end
if(i == 180)
success = false
return;
end
[r,g,b] = obj.body.rgbRead();
AnalogData = obj.body.getAverageData('analog',5);
pause(0.01);
block_features(5) = obj.normalize_val(size, obj.MAX_SIZE, obj.MIN_SIZE);
block_features(2:4) = obj.normalize_val([r,g,b]',255,0);
block_features(1) = obj.normalize_val(AnalogData(obj.HALL_EFFECT), obj.MAX_HALL, obj.MIN_HALL);
end
function [curr_error_IR, control_M1, control_M2] = ir_PD_Controller(obj, ir_normalized, prev_error_IR)
% Motor 1 (right motor PID values)
error_IR = obj.get_IR_error(ir_normalized);
% error_sum_M1 = error_sum_M1 + error_M1;
error_delta_IR = prev_error_IR - error_IR;
curr_error_IR = error_IR;
% Control calculations
pd_control_result = error_IR.*obj.KP_IR + obj.KD_IR.*error_delta_IR;
control_M1 = obj.TARGET_RPM - pd_control_result;
control_M2 = obj.TARGET_RPM + pd_control_result;
end
function error = get_IR_error(obj, reading_ir)
error = -2*reading_ir(1) - reading_ir(2) + reading_ir(3) + 2*reading_ir(4);
end
function [control_M1, control_M2] = setMotorSpeeds(obj, prev_control_M1, prev_control_M2)
if prev_control_M1 > obj.MAX_RPM
control_M1 = obj.MAX_RPM;
elseif prev_control_M1 < (-1 * obj.MAX_RPM)
control_M1 = -1 * obj.MAX_RPM;
else
control_M1 = prev_control_M1;
end
if prev_control_M2 > obj.MAX_RPM
control_M2 = obj.MAX_RPM;
elseif prev_control_M2 < (-1 * obj.MAX_RPM)
control_M2 = -1 * obj.MAX_RPM;
else
control_M2 = prev_control_M2;
end
end
function bool = line_within_proximity(obj, reading, sensors)
max_avg = mean(obj.max_ir_reading);
bool = false;
for i = length(sensors)
bool = bool || obj.valThresholdG(reading(sensors(i)),max_avg,(1/2).*max_avg);
end
end
function bool = line_in_sight(obj, reading, sensors)
max_avg = mean(obj.max_ir_reading);
bool = true;
for i = length(sensors)
bool = bool && obj.valThresholdG(reading(sensors(i)),max_avg,(1/2).*max_avg);
end
end
function bool = line_not_in_sight(obj, reading, sensors)
min_avg = mean(obj.min_ir_reading);
bool = true;
for i = length(sensors)
bool = bool && obj.valThresholdL(reading(sensors(i)),min_avg,(3/5).*min_avg);
end
end
function stopMotors(obj)
obj.body.motor(obj.MOTOR_L,0);
obj.body.motor(obj.MOTOR_R,0);
end
end
end