-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathev3kernel.py
More file actions
1141 lines (953 loc) · 41 KB
/
Copy pathev3kernel.py
File metadata and controls
1141 lines (953 loc) · 41 KB
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env pybricks-micropython
# EV3 — Pybricks 4.0 beta (https://beta.pybricks.com/)
# My github account: https://github.com/tiw302, My ig account: @tiw3025k_
# ? (GRIPPER) [[TOP VIEW]]
# ? [Port A]
# ? .----------.
# ? S1 | S2 S3 | S4
# ? (o) | (o) (o) | (o)
# ? | |
# ? [B]--| [Port D] |--[C]
# ? (L) |(MAIN ARM)| (R)
# ? | |
# ? '----------'
"""
* port connections: [ENGLISH]
port a = front lift motor (gripper)
port b = left wheel motor (forward = negative run)
port c = right wheel motor (forward = positive run)
port d = main arm lift motor (lifts entire a assembly)
port s1 = sensor 1 (far left)
port s2 = sensor 2 (mid left)
port s3 = sensor 3 (mid right)
port s4 = sensor 4 (far right)
* algorithms & optimizations used (international-level):
PIDv2 : derivative-on-measurement + ema filter + back-calc anti-windup
TrapProfile : accel -> cruise -> decel (trapezoidal velocity profile)
Deadband Comp : compensates for ev3 medium motor stiction at low speeds
Normalized : maps sensor.reflection() -> [0,100] using real black/white values
Zero-Alloc : method caching to eradicate garbage collection (gc) in hot loops
Memory Opt : uses __slots__ and const() for minimal ram footprint (~200kb)
* pre-match checklist:
1. update wheel_diameter_mm / axle_track_mm in config.py to match the physical robot
2. run debug.py (down button) to calibrate and hardcode black_light / white_light in config.py
3. hardcode center_offset_xx from calibrate_2sensor_offset()
4. run debug.py (center button) to verify sensor values
.___________________________________________________________________________________________.
* การต่อพอร์ต: [THAI]
port A = มอเตอร์ยกของด้านหน้า (gripper/lift front)
port B = มอเตอร์ล้อซ้าย (เดินหน้า = run ค่าลบ)
port C = มอเตอร์ล้อขวา (เดินหน้า = run ค่าบวก)
port D = มอเตอร์ยกแขน A ทั้งชุด
port S1 = เซ็นเซอร์ 1 (ซ้ายสุด)
port S2 = เซ็นเซอร์ 2 (ซ้ายกลาง)
port S3 = เซ็นเซอร์ 3 (ขวากลาง)
port S4 = เซ็นเซอร์ 4 (ขวาสุด)
* algorithms & optimizations ที่ใช้ (international-level):
PIDv2 : derivative-on-measurement + ema filter + back-calc anti-windup
TrapProfile : accel -> cruise -> decel (trapezoidal velocity profile)
Deadband Comp : ชดเชย EV3 medium motor stiction ตอนความเร็วต่ำ
Normalized : map sensor.reflection() -> [0,100] ด้วยค่า black/white จริง
Zero-Alloc : เทคนิค Cache ลบปัญหา Garbage Collection กระตุกตอนหุ่นวิ่ง
Memory Opt : รีด RAM ด้วย __slots__ และ const() ทำงานไว ประหยัดแบต
* ก่อนแข่ง:
1. แก้ WHEEL_DIAMETER_MM / AXLE_TRACK_MM ใน config.py ให้ตรงหุ่น
2. รัน debug.py (ปุ่มล่าง) เพื่อคาลิเบรต แล้วมาแก้ BLACK_LIGHT / WHITE_LIGHT ใน config.py
3. Hardcode CENTER_OFFSET_xx จาก calibrate_2sensor_offset()
4. รัน debug.py (ปุ่มกลาง) เพื่อเช็คค่าแสงและเซ็นเซอร์ให้ชัวร์ก่อนแข่ง
"""
import math
import gc
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor, ColorSensor
from pybricks.parameters import Port, Stop, Button
from pybricks.tools import StopWatch, wait
from micropython import const
from config import *
def clamp(v, lo, hi):
if v < lo:
return lo
if v > hi:
return hi
return v
def apply_deadband(speed, deadband=DEADBAND_SPEED):
"""helper: inject deadband power to overcome stiction. for use in mission scripts."""
if speed > 1:
return speed + deadband
if speed < -1:
return speed - deadband
return 0
class Robot:
# __slots__: eliminates __dict__ on robot instance — largest single RAM saving
__slots__ = (
"hub",
"left_motor",
"right_motor",
"lift_motor_a",
"lift_motor_d",
"sensor_1",
"sensor_2",
"sensor_3",
"sensor_4",
"black_raw",
"white_raw",
"sensor_map",
)
# ██████ ███████ ████████ ██ ██ ██████
# ██ ██ ██ ██ ██ ██ ██
# █████ █████ ██ ██ ██ ██████
# ██ ██ ██ ██ ██ ██
# ██████ ███████ ██ ██████ ██
#
# >> setup core (initialization, port and sensor checking)
# >> setup core (ระบบตั้งค่าเริ่มต้น เช็คพอร์ตและเซ็นเซอร์)
def __init__(self):
self.hub = EV3Brick()
self.hub.system.set_stop_button((Button.CENTER,))
print("[ROBOT] --------------------------------------")
print("[ROBOT] Initializing Ports...")
self.left_motor = self._init_motor(Port.B, "Left Motor")
self.right_motor = self._init_motor(Port.C, "Right Motor")
self.lift_motor_a = self._init_motor(Port.A, "Lift A", required=False)
self.lift_motor_d = self._init_motor(Port.D, "Lift D", required=False)
self.sensor_1 = self._init_sensor(Port.S1, "S1")
self.sensor_2 = self._init_sensor(Port.S2, "S2")
self.sensor_3 = self._init_sensor(Port.S3, "S3")
self.sensor_4 = self._init_sensor(Port.S4, "S4")
self.sensor_map = {
"1": self.sensor_1,
"2": self.sensor_2,
"3": self.sensor_3,
"4": self.sensor_4,
}
print("[ROBOT] All required ports connected!")
self.check_battery()
print("[ROBOT] --------------------------------------")
self.black_raw = BLACK_LIGHT
self.white_raw = WHITE_LIGHT
def _init_motor(self, port, label, required=True):
try:
m = Motor(port)
print(f"[ROBOT] [OK] {label} ({port})")
return m
except Exception:
print(f"[ROBOT] [FAIL] {label} ({port})")
if required:
self.hub.speaker.beep(200, 500)
raise
return None
def _init_sensor(self, port, label):
try:
s = ColorSensor(port)
print(f"[ROBOT] [OK] {label} ({port})")
return s
except Exception:
print(f"[ROBOT] [FAIL] {label} ({port})")
self.hub.speaker.beep(200, 500)
raise
# ███ ███ ██████ ██ ██ ███████ ███ ███ ███████ ███ ██ ████████
# ████ ████ ██ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██
# ██ ████ ██ ██ ██ ██ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██████ ████ ███████ ██ ██ ███████ ██ ████ ██
#
# >> movement core (drive mechanics, straight motion and turning)
# >> movement core (ระบบขับเคลื่อนล้อซ้ายขวา วิ่งตรง และเลี้ยว)
# __ __ ___ _ _ ___ ___ _____ ___ _ ___ ___ _ _ _____
# | \/ |/ _ \| \ / / __| / __|_ _| _ \ /_\ |_ _/ __| || |_ _|
# | |\/| | (_) |\ V /| _| \__ \ | | | / / _ \ | | (_ | __ | | |
# |_| |_|\___/ \_/ |___| |___/ |_| |_|_\/_/ \_\___\___|_||_| |_|
def move_straight(
self,
distance_cm,
max_speed=MOVE_STRAIGHT_CFG["speed_max"],
min_speed=MOVE_STRAIGHT_CFG["speed_start"],
kp=MOVE_STRAIGHT_CFG["kp"],
ki=MOVE_STRAIGHT_CFG["ki"],
kd=MOVE_STRAIGHT_CFG["kd"],
accel_frac=MOVE_STRAIGHT_CFG["accel_frac"],
decel_frac=MOVE_STRAIGHT_CFG["decel_frac"],
):
"""
drives straight by synchronizing left and right wheels using a pid controller.
formula: motor_degrees = (distance_mm / wheel_circumference) * 360 * correction
it compares encoder differences (left - right) to keep the robot moving perfectly straight.
"""
distance_mm = distance_cm * 10
self.log(f"Start: str {distance_cm}cm")
self.reset_encoders()
# pre-compute all loop-invariant values outside the hot loop (zero runtime alloc)
max_spd = max_speed * 10
min_spd = min_speed * 10
spd_range = max_spd - min_spd
target = abs(distance_mm) / WHEEL_CIRC * 360.0 * DISTANCE_CORRECTION
dirn = 1 if distance_mm >= 0 else -1
accel_end = target * accel_frac
decel_st = target * (1.0 - decel_frac)
decel_dist = target - decel_st
pid_ilim = 100.0
pid_maxout = max_spd * 0.4
db = DEADBAND_SPEED
db_thresh = min_spd * 1.5
d_alpha = 0.2
d_alpha_i = 0.8 # 1.0 - d_alpha (pre-computed)
# cache bound method refs — eliminates attribute lookup on every iteration
la_func = self.left_motor.angle
ra_func = self.right_motor.angle
l_run = self.left_motor.run
r_run = self.right_motor.run
lw = StopWatch()
lw_time = lw.time # cache StopWatch methods too
lw_reset = lw.reset
# pid state as plain locals — local var access is fastest in micropython
pid_integral = 0.0
pid_prev = None
pid_d_filt = 0.0
gc.collect()
gc.disable()
while True:
la = -la_func()
ra = ra_func()
progress = (abs(la) + abs(ra)) * 0.5
if progress >= target:
break
# real dt via multiply (faster than divide by 1000)
dt = lw_time() * 0.001
lw_reset()
if dt <= 0.0:
dt = 0.01
# inline trapezoid profile — no function call overhead
if progress <= accel_end:
t = progress / accel_end if accel_end > 0.0 else 1.0
base = (min_spd + spd_range * t) * dirn
elif progress >= decel_st:
t = (target - progress) / decel_dist if decel_dist > 0.0 else 0.0
base = (min_spd + spd_range * t) * dirn
else:
base = max_spd * dirn
# inline pid: derivative-on-measurement + ema + back-calc anti-windup
enc_diff = la - ra
integ = pid_integral + enc_diff * dt
if integ > pid_ilim:
pid_integral = pid_ilim
elif integ < -pid_ilim:
pid_integral = -pid_ilim
else:
pid_integral = integ
if pid_prev is None:
pid_prev = enc_diff
d_raw = enc_diff - pid_prev
pid_d_filt = d_alpha * d_raw + d_alpha_i * pid_d_filt
pid_prev = enc_diff
p_out = kp * enc_diff
i_out = ki * pid_integral
d_out = kd * pid_d_filt
correction = p_out + i_out + d_out
# back-calc anti-windup — inline, no function call
if ki > 0.0:
if correction > pid_maxout:
clamped = pid_maxout
elif correction < -pid_maxout:
clamped = -pid_maxout
else:
clamped = correction
if correction != clamped:
pid_integral = (clamped - p_out - d_out) / ki
# inline speed clamp
l = base - correction
if l > max_spd:
l = max_spd
elif l < -max_spd:
l = -max_spd
r = base + correction
if r > max_spd:
r = max_spd
elif r < -max_spd:
r = -max_spd
# inline deadband compensation — avoids function call overhead
if abs(l) < db_thresh:
if l > 1:
l += db
elif l < -1:
l -= db
else:
l = 0
if abs(r) < db_thresh:
if r > 1:
r += db
elif r < -1:
r -= db
else:
r = 0
l_run(-l)
r_run(r)
wait(10)
gc.enable()
self.stop_drive()
self.log("Done: str")
# _____ _ _ ___ _ _
# |_ _| | | | _ \ \| |
# | | | |_| | / .` |
# |_| \___/|_|_\_|\_|
def turn(
self,
angle_deg,
max_speed=TURN_CFG["speed_max"],
min_speed=TURN_CFG["speed_start"],
kp=TURN_CFG["kp"],
ki=TURN_CFG["ki"],
kd=TURN_CFG["kd"],
accel_frac=TURN_CFG["accel_frac"],
decel_frac=TURN_CFG["decel_frac"],
):
"""
performs a precise point-turn by spinning wheels in opposite directions.
formula: arc_length = (turn_angle / 360) * (pi * axle_track_mm)
target_degrees = (arc_length / wheel_circumference) * 360
syncs wheels via pid so that abs(left) - abs(right) remains 0.
"""
self.log(f"Start: turn {angle_deg}d")
self.reset_encoders()
# pre-compute all loop-invariant values
max_spd = max_speed * 10
min_spd = min_speed * 10
spd_range = max_spd - min_spd
arc_mm = abs(angle_deg) / 360.0 * (math.pi * AXLE_TRACK_MM)
target = arc_mm / WHEEL_CIRC * 360.0 * TURN_CORRECTION
dirn = 1 if angle_deg >= 0 else -1
accel_end = target * accel_frac
decel_st = target * (1.0 - decel_frac)
decel_dist = target - decel_st
pid_ilim = 80.0
d_alpha = 0.2
d_alpha_i = 0.8
# cache bound method refs
la_func = self.left_motor.angle
ra_func = self.right_motor.angle
l_run = self.left_motor.run
r_run = self.right_motor.run
lw = StopWatch()
lw_time = lw.time
lw_reset = lw.reset
# pid state as locals
pid_integral = 0.0
pid_prev = None
pid_d_filt = 0.0
gc.collect()
gc.disable()
while True:
la = -la_func()
ra = ra_func()
progress = (abs(la) + abs(ra)) * 0.5
if progress >= target:
break
dt = lw_time() * 0.001
lw_reset()
if dt <= 0.0:
dt = 0.01
# inline trapezoid profile
if progress <= accel_end:
t = progress / accel_end if accel_end > 0.0 else 1.0
base = min_spd + spd_range * t
elif progress >= decel_st:
t = (target - progress) / decel_dist if decel_dist > 0.0 else 0.0
base = min_spd + spd_range * t
else:
base = max_spd
# inline pid
sync_err = abs(la) - abs(ra)
integ = pid_integral + sync_err * dt
if integ > pid_ilim:
pid_integral = pid_ilim
elif integ < -pid_ilim:
pid_integral = -pid_ilim
else:
pid_integral = integ
if pid_prev is None:
pid_prev = sync_err
d_raw = sync_err - pid_prev
pid_d_filt = d_alpha * d_raw + d_alpha_i * pid_d_filt
pid_prev = sync_err
correction = kp * sync_err + ki * pid_integral + kd * pid_d_filt
# clamp to [0, max]: prevents wheel reversing when correction is large
l = base - correction
if l > max_spd:
l = max_spd
elif l < 0:
l = 0
r = base + correction
if r > max_spd:
r = max_spd
elif r < 0:
r = 0
l_run(-(dirn * l))
r_run(-dirn * r)
wait(10)
gc.enable()
self.stop_drive()
self.log("Done: turn")
def pivot_turn(
self,
angle_deg,
pivot_side="right",
max_speed=TURN_CFG["speed_max"],
min_speed=TURN_CFG["speed_start"],
accel_frac=TURN_CFG["accel_frac"],
decel_frac=TURN_CFG["decel_frac"],
):
"""
turns the robot by moving only one wheel (pivot turn) with trapezoidal profile.
angle_deg: positive for clockwise, negative for counter-clockwise.
pivot_side: 'right' (hold right wheel, move left) or 'left' (hold left wheel, move right).
"""
self.log(f"Start: pivot turn {angle_deg}d on {pivot_side}")
self.reset_encoders()
# distance is exactly 2x that of a normal point turn.
max_spd = max_speed * 10
min_spd = min_speed * 10
spd_range = max_spd - min_spd
arc_mm = abs(angle_deg) / 360.0 * (2.0 * math.pi * AXLE_TRACK_MM)
target = arc_mm / WHEEL_CIRC * 360.0 * TURN_CORRECTION
dirn = 1 if angle_deg >= 0 else -1
accel_end = target * accel_frac
decel_st = target * (1.0 - decel_frac)
decel_dist = target - decel_st
la_func = self.left_motor.angle
ra_func = self.right_motor.angle
if pivot_side == "right":
active_run = self.left_motor.run
active_ang = la_func
self.right_motor.hold()
run_sign = -dirn
else:
active_run = self.right_motor.run
active_ang = ra_func
self.left_motor.hold()
run_sign = -dirn
gc.collect()
gc.disable()
while True:
progress = abs(active_ang())
if progress >= target:
break
# inline trapezoid profile
if progress <= accel_end:
t = progress / accel_end if accel_end > 0.0 else 1.0
base = min_spd + spd_range * t
elif progress >= decel_st:
t = (target - progress) / decel_dist if decel_dist > 0.0 else 0.0
base = min_spd + spd_range * t
else:
base = max_spd
active_run(run_sign * base)
wait(10)
gc.enable()
self.stop_drive()
self.log("Done: pivot turn")
def drive(self, left_speed, right_speed):
self.left_motor.run(-left_speed)
self.right_motor.run(right_speed)
# _ _ ___ ___ _ _ __ __ _ _ _
# /_\ | | |_ _/ __| \| | \ \ / //_\ | | | |
# / _ \| |__ | | (_ | .` | \ \/\/ // _ \| |__| |__
# /_/ \_\____|___\___|_|\_| \_/\_//_/ \_\____|____|
def align_wall(self, power, time_sec, hold=True):
"""
Runs the robot into a wall using raw Duty Cycle (DC) voltage to square up.
No PID sync is used because PID would fight the mechanical squaring.
power: raw duty cycle power -100 to 100 (positive for forward).
time_sec: time in seconds to push against the wall.
hold: if True, applies active hold after stalling.
"""
self.log(f"Start: align wall {power}% for {time_sec}s")
# we must use raw dc so the motors can stall independently.
# if we used pid sync, the wheel that hits the wall first would stop,
# and the pid would stop the other wheel from moving, defeating the squaring
gc.collect()
gc.disable()
watch = StopWatch()
while watch.time() < time_sec * 1000:
# apply raw unregulated voltage
self.left_motor.dc(-power)
self.right_motor.dc(power)
wait(10)
gc.enable()
self.stop_drive(hold)
self.log("Done: align wall")
# ___ ___ _____ __ ___ _ _ _ _ _____ ___ _ _ ___ _ _ ___
# | \| _ \_ _\ \ / /| __| | | | | \| |_ _|_ _| | | | |_ _| \| | __|
# | |) | /| | \ V / | _| | |_| | .` | | | | || |__| |__ | || .` | _|
# |___/|_|_\___| \_/ |___| \___/|_|\_| |_| |___|____|____|___|_|\_|___|
def drive_until_line(
self,
speed=TRACK_LINE_CFG["speed"],
threshold=TRACK_LINE_CFG["threshold"],
left_sensor="2",
right_sensor="3",
align=True,
align_time_sec=ALIGN_LINE_CFG["time_sec"],
align_target=ALIGN_LINE_CFG["target_val"],
align_kp=ALIGN_LINE_CFG["kp"],
):
"""
drives straight until BOTH sensors detect the line (handles steep angles).
speed: speed percentage (0-100).
threshold: light value to consider as black (usually 30 or below).
align: if True, automatically calls align_line() after stopping.
align_time_sec: seconds to spend aligning if align=True.
align_target: target light value for aligning (edge of the line).
align_kp: proportional gain for aligning.
"""
self.log(f"Start: drive until line at speed {speed}%")
ls = self.sensor_map[left_sensor]
rs = self.sensor_map[right_sensor]
ls_ref = ls.reflection
rs_ref = rs.reflection
l_run = self.left_motor.run
r_run = self.right_motor.run
target_spd = speed * 10
l_run(-target_spd)
r_run(target_spd)
l_found = False
r_found = False
gc.collect()
gc.disable()
# wait until both sensors detect the line
while not (l_found and r_found):
# if left hits the line first, hold left motor and wait for right
if not l_found and ls_ref() <= threshold:
self.left_motor.hold()
l_found = True
# if right hits the line first, hold right motor and wait for left
if not r_found and rs_ref() <= threshold:
self.right_motor.hold()
r_found = True
wait(10)
gc.enable()
self.stop_drive(hold=True)
if align:
self.align_line(
speed=0,
target_val=align_target,
kp=align_kp,
time_sec=align_time_sec,
left_sensor=left_sensor,
right_sensor=right_sensor,
)
self.log("Done: drive until line")
# _____ ___ _ ___ _ _ _ ___ _ _ ___
# |_ _| _ \ /_\ / __| |/ / | | |_ _| \| | __|
# | | | / / _ \ (__| ' < | |__ | || .` | _|
# |_| |_|_\/_/ \_\___|_|\_\ |____|___|_|\_|___|
def track_line(
self,
speed=TRACK_LINE_CFG["speed"],
kp=TRACK_LINE_CFG["kp"],
kd=TRACK_LINE_CFG["kd"],
threshold=TRACK_LINE_CFG["threshold"],
left_sensor="2",
right_sensor="3",
edge="center",
):
"""
world-class competition pd line tracking (wro / fll grade).
features:
1. sensor normalization [.0, 1.0] (environment-independent tuning)
2. ema low-pass filtered derivative (eliminates sensor noise jitter)
3. quadratic turn speed scaling (full speed on straights, smooth grip in curves)
"""
self.log(f"Start: track line at speed {speed}%")
ls = self.sensor_map[left_sensor]
rs = self.sensor_map[right_sensor]
ls_ref = ls.reflection
rs_ref = rs.reflection
l_dc_func = self.left_motor.dc
r_dc_func = self.right_motor.dc
b_raw = float(self.black_raw)
w_raw = float(self.white_raw)
span = max(1.0, w_raw - b_raw)
last_error = 0.0
d_filt = 0.0
last_turn = 0.0
gc.collect()
gc.disable()
while True:
l_val = ls_ref()
r_val = rs_ref()
# intersection detection: both sensors see black
if l_val <= threshold and r_val <= threshold:
break
# normalize sensors [.0 = black, 1.0 = white]
l_norm = clamp((l_val - b_raw) / span, 0.0, 1.0)
r_norm = clamp((r_val - b_raw) / span, 0.0, 1.0)
# edge tracking mode
if edge == "left":
error_raw = (l_norm - 0.5) * 200.0
elif edge == "right":
error_raw = (0.5 - r_norm) * 200.0
else:
error_raw = (l_norm - r_norm) * 100.0
error = error_raw
# anti-jerk derivative suppression (ignores thick side-lines when in center mode)
d_raw = clamp(error - last_error, -20.0, 20.0)
d_filt = 0.35 * d_raw + 0.65 * d_filt
turn = (error * kp) + (d_filt * kd)
# quadratic speed compensation based on turn sharpness
turn_ratio = clamp(abs(turn) / 50.0, 0.0, 1.0)
current_base = max(
speed * 0.2, speed * (1.0 - 0.65 * turn_ratio * turn_ratio)
)
l_dc = clamp(current_base + turn, -100, 100)
r_dc = clamp(current_base - turn, -100, 100)
l_dc_func(-l_dc)
r_dc_func(r_dc)
last_error = error
wait(10)
gc.enable()
self.stop_drive(hold=True)
self.log("Done: track line (intersection detected)")
def track_line_distance(
self,
distance_cm,
speed=TRACK_LINE_DISTANCE_CFG["speed"],
kp=TRACK_LINE_DISTANCE_CFG["kp"],
kd=TRACK_LINE_DISTANCE_CFG["kd"],
left_sensor="2",
right_sensor="3",
edge="center",
):
"""
follows a line for a specific distance (in cm) using world-class pd.
distance_cm: distance to travel (cm).
"""
self.log(f"Start: track line dist {distance_cm}cm at {speed}%")
self.reset_encoders()
distance_mm = distance_cm * 10
target = abs(distance_mm) / WHEEL_CIRC * 360.0 * DISTANCE_CORRECTION
ls = self.sensor_map[left_sensor]
rs = self.sensor_map[right_sensor]
ls_ref = ls.reflection
rs_ref = rs.reflection
l_dc_func = self.left_motor.dc
r_dc_func = self.right_motor.dc
avg_ang = self.avg_angle
b_raw = float(self.black_raw)
w_raw = float(self.white_raw)
span = max(1.0, w_raw - b_raw)
last_error = 0.0
d_filt = 0.0
last_turn = 0.0
gc.collect()
gc.disable()
while avg_ang() < target:
l_val = ls_ref()
r_val = rs_ref()
# normalize sensors [.0 = black, 1.0 = white]
l_norm = clamp((l_val - b_raw) / span, 0.0, 1.0)
r_norm = clamp((r_val - b_raw) / span, 0.0, 1.0)
if edge == "left":
error_raw = (l_norm - 0.5) * 200.0
elif edge == "right":
error_raw = (0.5 - r_norm) * 200.0
else:
error_raw = (l_norm - r_norm) * 100.0
error = error_raw
# anti-jerk derivative suppression (ignores thick side-lines when in center mode)
d_raw = clamp(error - last_error, -20.0, 20.0)
d_filt = 0.35 * d_raw + 0.65 * d_filt
turn = (error * kp) + (d_filt * kd)
# quadratic speed compensation
turn_ratio = clamp(abs(turn) / 50.0, 0.0, 1.0)
current_base = max(
speed * 0.2, speed * (1.0 - 0.65 * turn_ratio * turn_ratio)
)
l_dc = clamp(current_base + turn, -100, 100)
r_dc = clamp(current_base - turn, -100, 100)
l_dc_func(-l_dc)
r_dc_func(r_dc)
last_error = error
wait(10)
gc.enable()
self.stop_drive(hold=True)
self.log(f"Done: track line dist {distance_cm}cm")
def track_line_timer(
self,
time_sec,
speed=TRACK_LINE_TIMER_CFG["speed"],
kp=TRACK_LINE_TIMER_CFG["kp"],
kd=TRACK_LINE_TIMER_CFG["kd"],
left_sensor="2",
right_sensor="3",
):
"""
follows a line for a specific amount of time (in milliseconds) using world-class pd.
time_sec: time to travel (s).
"""
self.log(f"Start: track line timer {time_sec}s at {speed}%")
sw = StopWatch()
sw_time = sw.time
ls = self.sensor_map[left_sensor]
rs = self.sensor_map[right_sensor]
ls_ref = ls.reflection
rs_ref = rs.reflection
l_dc_func = self.left_motor.dc
r_dc_func = self.right_motor.dc
b_raw = float(self.black_raw)
w_raw = float(self.white_raw)
span = max(1.0, w_raw - b_raw)
last_error = 0.0
d_filt = 0.0
gc.collect()
gc.disable()
while sw_time() < time_sec * 1000:
l_val = ls_ref()
r_val = rs_ref()
# normalize sensors [.0 = black, 1.0 = white]
l_norm = clamp((l_val - b_raw) / span, 0.0, 1.0)
r_norm = clamp((r_val - b_raw) / span, 0.0, 1.0)
error = (l_norm - r_norm) * 100.0
# anti-jerk derivative suppression
error_jump = error - last_error
if abs(error_jump) > 20.0:
d_raw = 0.0
else:
d_raw = error_jump
d_filt = 0.35 * d_raw + 0.65 * d_filt
raw_turn = (error * kp) + (d_filt * kd)
turn = clamp(raw_turn, last_turn - 15.0, last_turn + 15.0)
last_turn = turn
# quadratic speed compensation
turn_ratio = clamp(abs(turn) / 50.0, 0.0, 1.0)
current_base = max(
speed * 0.2, speed * (1.0 - 0.65 * turn_ratio * turn_ratio)
)
l_dc = clamp(current_base + turn, -100, 100)
r_dc = clamp(current_base - turn, -100, 100)
l_dc_func(-l_dc)
r_dc_func(r_dc)
last_error = error
wait(10)
gc.enable()
self.stop_drive(hold=True)
self.log(f"Done: track line timer {time_sec}s")
# _ _ ___ ___ _ _ _ ___ _ _ ___
# /_\ | | |_ _/ __| \| | | | |_ _| \| | __|
# / _ \| |__ | | (_ | .` | | |__ | || .` | _|
# /_/ \_\____|___\___|_|\_| |____|___|_|\_|___|
def align_line(
self,
speed=ALIGN_LINE_CFG["speed_start"],
target_val=ALIGN_LINE_CFG["target_val"],
kp=ALIGN_LINE_CFG["kp"],
time_sec=ALIGN_LINE_CFG["time_sec"],
left_sensor="2",
right_sensor="3",
hold=True,
):
"""
world-class 3-step line squaring:
1. walks forward at `speed` until the first sensor sees the line.
2. stops that motor and waits for the other sensor to catch up.
3. runs independent proportional controllers to perfectly lock onto the line edge.
speed: speed to walk forward to the line (0 to skip and just do pid).
target_val: target light value (usually 50 for the edge of black/white).
kp: proportional gain for correcting overshoot/undershoot (duty cycle scale).
time_sec: time in seconds to run the final pid alignment loop.
left_sensor/right_sensor: '1', '2', '3', or '4'.
hold: if True, applies active hold after aligning.
"""
self.log(f"Start: align line 3-step (target {target_val}) for {time_sec}s")
# map string to actual sensor objects
ls = self.sensor_map[left_sensor]
rs = self.sensor_map[right_sensor]
# cache methods to eliminate loop lookup overhead (saves RAM/CPU)
ls_ref = ls.reflection
rs_ref = rs.reflection
l_dc_func = self.left_motor.dc
r_dc_func = self.right_motor.dc
gc.collect()
gc.disable()
# STEP 1 & 2: walk to line and catch-up (if speed != 0)
if speed != 0:
l_found = False
r_found = False
# apply raw duty cycle for searching
l_dc_func(-speed)
r_dc_func(speed)
while not (l_found and r_found):
# +10 provides a small buffer so it detects the line slightly early
if not l_found and ls_ref() <= target_val + 10:
self.left_motor.hold()
l_found = True
if not r_found and rs_ref() <= target_val + 10:
self.right_motor.hold()
r_found = True
wait(10)
# STEP 3: pid edge lock
watch = StopWatch()
while watch.time() < time_sec * 1000:
l_val = ls_ref()
r_val = rs_ref()
# calculate error from the edge of the line
l_err = l_val - target_val
r_err = r_val - target_val
l_dc = clamp(l_err * kp, -60, 60)
r_dc = clamp(r_err * kp, -60, 60)
# apply duty cycle (left is inverted)
l_dc_func(-l_dc)
r_dc_func(r_dc)
wait(10)
gc.enable()
self.stop_drive(hold)
self.log("Done: align line")
def stop_drive(self, hold=True):
if hold:
self.left_motor.hold()
self.right_motor.hold()
else:
self.left_motor.brake()
self.right_motor.brake()
# ██ ██ ███████ ████████
# ██ ██ ██ ██
# ██ ██ █████ ██
# ██ ██ ██ ██
# ███████ ██ ██ ██
#
# >> lift core (robotic arm mechanisms for gripping and lifting)
# >> lift core (ระบบแขนกลสำหรับคีบและยกสิ่งของ)
def lift_a(self, angle=90, speed=LIFT_CFG["speed"], power=100, wait=True):
# move arm using direct raw dc voltage. eliminates pid jerking under heavy load.
self.log(f"Start: Lift A (angle={angle})")
if not self.lift_motor_a:
return
current_angle = self.lift_motor_a.angle()
if abs(angle - current_angle) < 3:
return
target_dir = 1 if angle > current_angle else -1
pwr_val = clamp(abs(power if power != 100 else speed), 15, 100)
# when lowering heavy load, limit downward voltage to prevent slamming
pwr = pwr_val * target_dir if target_dir > 0 else -min(pwr_val, 40)
self.lift_motor_a.dc(pwr)
if wait:
from pybricks.tools import wait as pb_wait
sw = StopWatch()
dist = abs(angle - current_angle)
max_time = (dist / max(1, abs(speed * 5))) * 1000 + 2000
while sw.time() < max_time:
curr = self.lift_motor_a.angle()
if target_dir > 0 and curr >= angle: