-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1402 lines (1299 loc) · 82.9 KB
/
Copy pathindex.html
File metadata and controls
1402 lines (1299 loc) · 82.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mecanum 4WD Robot Controller - Documentation</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #e0e0e0;
min-height: 100vh;
line-height: 1.7;
}
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
/* Header */
header {
text-align: center; padding: 40px 20px;
background: linear-gradient(135deg, #0f3460 0%, #1a1a2e 100%);
border-radius: 20px; margin-bottom: 30px;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}
header h1 {
font-size: 2.5em; color: #00d9ff; margin-bottom: 10px;
text-shadow: 0 0 20px rgba(0,217,255,0.5);
}
header .subtitle { font-size: 1.2em; color: #a0a0a0; margin-bottom: 20px; }
header .specs {
display: flex; justify-content: center; gap: 15px; flex-wrap: wrap;
}
header .spec {
background: rgba(0,217,255,0.1); border: 1px solid rgba(0,217,255,0.3);
border-radius: 8px; padding: 6px 14px; font-size: 0.85em;
}
header .spec strong { color: #00d9ff; }
/* Navigation */
nav {
display: flex; justify-content: center; gap: 8px; flex-wrap: wrap;
margin-bottom: 30px; position: sticky; top: 10px; z-index: 100;
background: rgba(26,26,46,0.95); padding: 12px; border-radius: 15px;
backdrop-filter: blur(10px); box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
nav a {
padding: 8px 14px; background: #0f3460; color: #00d9ff;
text-decoration: none; border-radius: 20px; transition: all 0.3s;
border: 1px solid rgba(0,217,255,0.4); font-size: 0.82em;
}
nav a:hover, nav a.active { background: #00d9ff; color: #1a1a2e; }
/* Sections */
section {
background: rgba(15, 52, 96, 0.4); border-radius: 20px;
padding: 35px; margin-bottom: 30px;
box-shadow: 0 5px 20px rgba(0,0,0,0.2);
}
section h2 {
color: #00d9ff; font-size: 1.8em; margin-bottom: 8px;
padding-bottom: 10px; border-bottom: 2px solid rgba(0,217,255,0.4);
}
.section-intro {
color: #b0c0d0; font-size: 1.05em; margin-bottom: 20px;
padding: 12px 0; line-height: 1.8;
}
section h3 { color: #4ade80; font-size: 1.25em; margin: 30px 0 12px 0; }
section h4 { color: #fbbf24; font-size: 1.05em; margin: 18px 0 8px 0; }
p { margin-bottom: 10px; }
/* Feature Grid */
.feature-grid {
display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 18px; margin-top: 15px;
}
.feature-card {
background: linear-gradient(135deg, #1a1a2e 0%, #0f3460 100%);
border-radius: 15px; padding: 22px; border-left: 4px solid #00d9ff;
transition: transform 0.3s;
}
.feature-card:hover { transform: translateY(-3px); }
.feature-card h4 { color: #00d9ff; margin-bottom: 8px; font-size: 1.05em; }
.feature-card .icon { font-size: 1.8em; margin-bottom: 8px; }
/* Explanation boxes */
.explain {
background: rgba(0,217,255,0.08); border: 1px solid rgba(0,217,255,0.3);
border-radius: 12px; padding: 18px 22px; margin: 15px 0;
line-height: 1.8;
}
.explain.warn {
background: rgba(251,191,36,0.08); border-color: rgba(251,191,36,0.4);
}
.explain.good {
background: rgba(74,222,128,0.08); border-color: rgba(74,222,128,0.4);
}
.explain.danger {
background: rgba(239,68,68,0.08); border-color: rgba(239,68,68,0.4);
}
.explain strong { color: #00d9ff; }
.explain.warn strong { color: #fbbf24; }
.explain.good strong { color: #4ade80; }
.explain.danger strong { color: #ef4444; }
/* Diagrams (monospace ASCII art) */
.diagram {
background: #0d1b2a; border-radius: 12px; padding: 22px;
margin: 18px 0; font-family: 'Courier New', monospace;
overflow-x: auto; white-space: pre; line-height: 1.45;
font-size: 0.84em; color: #b0c4de; border: 1px solid #1e3a5f;
}
.diagram .cb { color: #00d9ff; }
.diagram .cg { color: #4ade80; }
.diagram .cy { color: #fbbf24; }
.diagram .cr { color: #ef4444; }
.diagram .cp { color: #c084fc; }
.diagram .cd { color: #5a6a7a; }
/* Layer stacks */
.layer {
background: linear-gradient(90deg, #0f3460, #1a1a2e);
border: 2px solid #00d9ff; border-radius: 10px;
padding: 14px 20px; margin: 8px 0; text-align: center;
}
.layer-title { color: #00d9ff; font-weight: bold; }
.layer-desc { color: #a0a0a0; font-size: 0.92em; margin-top: 4px; }
.arrow-down { text-align: center; color: #4ade80; font-size: 1.6em; margin: 3px 0; }
/* Tables */
.tbl {
width: 100%; border-collapse: collapse; margin: 15px 0; font-size: 0.93em;
}
.tbl th, .tbl td {
padding: 10px 14px; text-align: center; border: 1px solid #0f3460;
}
.tbl th { background: #0f3460; color: #00d9ff; font-size: 0.95em; }
.tbl tr:nth-child(even) { background: rgba(15,52,96,0.3); }
.tbl td { vertical-align: middle; }
.tbl td.left { text-align: left; }
.pos { color: #4ade80; font-weight: bold; }
.neg { color: #ef4444; font-weight: bold; }
.dim { color: #666; }
.pwm-c { color: #fbbf24; font-weight: bold; }
/* Robot body diagram */
.robot-box {
max-width: 460px; margin: 20px auto; position: relative;
}
.robot-label-top {
text-align: center; color: #4ade80; font-weight: bold;
font-size: 0.85em; margin-bottom: 6px;
}
.robot-label-bot {
text-align: center; color: #5a6a7a; font-size: 0.8em; margin-top: 6px;
}
.robot-grid {
background: #2a2a4e; border: 3px solid #4ade80; border-radius: 18px;
padding: 24px; display: grid; grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr; gap: 16px;
}
.whl {
background: linear-gradient(135deg, #0f3460, #1a1a2e);
border: 2px solid #00d9ff; border-radius: 10px;
padding: 12px 8px; text-align: center; font-size: 0.82em;
}
.whl .name { color: #00d9ff; font-weight: bold; font-size: 1.15em; }
.whl .sub { color: #a0a0a0; font-size: 0.85em; margin-top: 3px; }
.whl .sub2 { color: #fbbf24; font-size: 0.8em; }
/* Command cards */
.cmd {
background: #151530; border-radius: 10px; padding: 16px;
margin: 10px 0; border-left: 4px solid #fbbf24;
}
.cmd code {
background: #0f3460; padding: 3px 10px; border-radius: 5px;
font-size: 1.05em; color: #4ade80; display: inline-block; margin-bottom: 6px;
}
.cmd .d { color: #b0b0b0; margin-bottom: 6px; }
.cmd .ex {
background: #0a0a1a; padding: 6px 10px; border-radius: 5px;
margin-top: 6px; font-family: monospace; color: #fbbf24; font-size: 0.88em;
}
.cmd .resp {
background: #0a0a1a; padding: 6px 10px; border-radius: 5px;
margin-top: 4px; font-family: monospace; color: #00d9ff; font-size: 0.88em;
}
/* Steps */
.step {
display: flex; align-items: flex-start; gap: 14px; padding: 12px;
background: rgba(15,52,96,0.4); border-radius: 10px;
border-left: 4px solid #00d9ff; margin: 8px 0;
}
.step .num {
background: #00d9ff; color: #1a1a2e; min-width: 32px; height: 32px;
border-radius: 50%; display: flex; align-items: center;
justify-content: center; font-weight: bold; flex-shrink: 0;
}
.step h4 { color: #4ade80; margin-bottom: 2px; font-size: 1em; }
/* Code */
code {
background: rgba(0,217,255,0.12); padding: 2px 7px; border-radius: 4px;
font-family: 'Courier New', monospace; font-size: 0.92em;
color: #4ade80; border: 1px solid rgba(0,217,255,0.2);
}
pre {
background: #0a0a1a; border-radius: 10px; padding: 16px;
overflow-x: auto; font-family: 'Courier New', monospace;
color: #4ade80; border-left: 4px solid #00d9ff;
font-size: 0.88em; line-height: 1.5; margin: 12px 0;
}
pre code {
background: none; padding: 0; border: none; font-size: inherit; color: inherit;
}
.tbl code {
background: rgba(0,217,255,0.15); color: #4ade80; font-size: 0.9em;
padding: 2px 8px; border: 1px solid rgba(74,222,128,0.25);
}
/* Keyboard */
.kb-grid {
display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
gap: 10px; margin: 15px 0;
}
.kb {
background: #1a1a2e; border: 2px solid #4ade80; border-radius: 10px;
padding: 10px; text-align: center;
}
.kb .k {
background: linear-gradient(180deg, #3a3a5e, #2a2a4e);
border: 2px solid #5a5a7e; border-radius: 8px; padding: 6px 14px;
display: inline-block; font-size: 1.3em; font-weight: bold;
color: #fff; margin-bottom: 6px; box-shadow: 0 3px 0 #1a1a2e;
}
.kb .act { color: #a0a0a0; font-size: 0.82em; }
/* Flow */
.flow {
display: flex; align-items: center; justify-content: center;
gap: 8px; flex-wrap: wrap; margin: 15px 0;
}
.fbox {
background: linear-gradient(135deg, #0f3460, #1a1a2e);
border: 2px solid #4ade80; border-radius: 10px;
padding: 10px 16px; text-align: center; min-width: 100px;
}
.fbox.y { border-color: #fbbf24; }
.fbox.r { border-color: #ef4444; }
.fbox.b { border-color: #00d9ff; box-shadow: 0 0 12px rgba(0,217,255,0.25); }
.fbox h4 { margin-bottom: 2px; font-size: 0.95em; }
.farr { color: #00d9ff; font-size: 1.4em; flex-shrink: 0; }
/* Motor states */
.mstates {
display: flex; justify-content: center; gap: 20px; flex-wrap: wrap; margin: 15px 0;
}
.mst {
background: linear-gradient(135deg, #0f3460, #1a1a2e);
border: 3px solid #4ade80; border-radius: 14px;
padding: 16px 22px; text-align: center;
}
.mst.moving { border-color: #fbbf24; animation: pulse 2s infinite; }
.mst.brake { border-color: #ef4444; }
@keyframes pulse {
0%, 100% { box-shadow: 0 0 8px rgba(251,191,36,0.3); }
50% { box-shadow: 0 0 25px rgba(251,191,36,0.5); }
}
.pins { display: flex; gap: 10px; justify-content: center; margin-top: 8px; }
.pin { padding: 4px 12px; border-radius: 6px; font-weight: bold; font-size: 0.82em; }
.pin.hi { background: #4ade80; color: #1a1a2e; }
.pin.lo { background: #ef4444; color: white; }
.pin.pw { background: linear-gradient(90deg, #fbbf24, #f59e0b); color: #1a1a2e; }
/* EEPROM boxes */
.eeprom { display: flex; gap: 0; justify-content: center; margin: 15px 0; flex-wrap: wrap; }
.ebyte {
border: 2px solid #00d9ff; padding: 10px 14px; text-align: center; min-width: 75px;
}
.ebyte:first-child { border-radius: 10px 0 0 10px; }
.ebyte:last-child { border-radius: 0 10px 10px 0; }
.ebyte .ea { color: #666; font-size: 0.7em; }
.ebyte .ev { color: #00d9ff; font-weight: bold; font-size: 1.1em; }
.ebyte .el { color: #a0a0a0; font-size: 0.75em; }
/* File tree */
.ftree {
background: #0d1b2a; border-radius: 10px; padding: 18px;
font-family: 'Courier New', monospace; font-size: 0.83em;
line-height: 1.6; color: #b0c4de; border: 1px solid #1e3a5f;
white-space: pre; overflow-x: auto;
}
.ftree .fd { color: #00d9ff; font-weight: bold; }
.ftree .ff { color: #4ade80; }
.ftree .fc { color: #5a6a7a; }
/* Footer */
footer {
text-align: center; padding: 25px; color: #666;
border-top: 1px solid #0f3460; margin-top: 40px; font-size: 0.9em;
}
@media (max-width: 768px) {
header h1 { font-size: 1.8em; }
.diagram { font-size: 0.7em; }
nav a { padding: 6px 10px; font-size: 0.72em; }
section { padding: 20px; }
}
</style>
</head>
<body>
<div class="container">
<!-- ================================================================ -->
<!-- HEADER -->
<!-- ================================================================ -->
<header>
<h1>Mecanum 4WD Robot Controller</h1>
<p class="subtitle">A small 4-wheeled robot that can move in any direction, controlled by an Arduino</p>
<div class="specs">
<div class="spec"><strong>Brain:</strong> Arduino Uno</div>
<div class="spec"><strong>Motors:</strong> 4 DC with encoders</div>
<div class="spec"><strong>Wheels:</strong> Mecanum (omnidirectional)</div>
<div class="spec"><strong>Control:</strong> USB Serial or PS2 gamepad</div>
</div>
</header>
<div class="explain danger" style="margin: 20px 0; padding: 18px 24px; font-size: 1.05em;">
<strong style="color: #ef4444; font-size: 1.15em;">⚠ Under Active Development</strong><br>
This project has <strong style="color: #ef4444;">not been deployed to production hardware yet</strong>.
Motor signs, encoder directions, PID tuning, and kinematics formulas may require adjustment
for your specific hardware setup. Use at your own risk and always test with wheels off the ground first.
</div>
<!-- ================================================================ -->
<!-- NAVIGATION -->
<!-- ================================================================ -->
<nav>
<a href="#what-is-this" class="active">What Is This?</a>
<a href="#how-it-works">How It Works</a>
<a href="#hardware">Hardware</a>
<a href="#brain">The Brain</a>
<a href="#motors">Motors</a>
<a href="#speed-control">Speed Control</a>
<a href="#mecanum">Mecanum Wheels</a>
<a href="#commands">Commands</a>
<a href="#encoders">Encoders</a>
<a href="#calibration">Calibration</a>
<a href="#hwtest">HW Test</a>
<a href="#gui">GUI</a>
</nav>
<!-- ================================================================ -->
<!-- 1. WHAT IS THIS? -->
<!-- ================================================================ -->
<section id="what-is-this">
<h2>1. What Is This Robot?</h2>
<p class="section-intro">
This is a small robot with <strong>4 wheels</strong> that can move in <strong>any direction</strong> —
forward, backward, sideways, diagonally, or spin in place. It's controlled by sending
text commands over a USB cable from your computer (or wirelessly with a PS2 gamepad).
</p>
<div class="explain good">
<strong>Think of it like this:</strong> Imagine a remote-controlled car, but instead of only going
forward and turning, it can also slide sideways like a crab. This is possible because of special
wheels called <em>Mecanum wheels</em> that have small rollers at 45-degree angles.
</div>
<h3>What Can It Do?</h3>
<div class="feature-grid">
<div class="feature-card">
<div class="icon">↺</div>
<h4>Move In Any Direction</h4>
<p>10 movement directions: forward, backward, left, right, 4 diagonals, rotate clockwise,
and rotate counter-clockwise.</p>
</div>
<div class="feature-card">
<div class="icon">🎯</div>
<h4>Move Exact Distances</h4>
<p>Tell it "go forward 10 cm" and it measures its wheel rotations to stop at exactly the right spot.
Each wheel has a sensor (encoder) that counts 4,320 ticks per revolution.</p>
</div>
<div class="feature-card">
<div class="icon">⚡</div>
<h4>Self-Correcting Speed</h4>
<p>A built-in speed controller (PID) checks each wheel's speed 50 times per second and adjusts
power automatically. If one wheel is slower, it gets more power.</p>
</div>
<div class="feature-card">
<div class="icon">💻</div>
<h4>Computer or Gamepad Control</h4>
<p>Send text commands from your PC ("FWD,100,2000"), use a Python GUI with keyboard controls,
or plug in a PS2 wireless gamepad.</p>
</div>
</div>
<h3>Project Files</h3>
<div class="ftree">
<span class="fd">rzv2n-arduino-motor-controller/</span>
├── <span class="fd">src/</span> <span class="fc">← Arduino firmware (C++ code)</span>
│ ├── <span class="fd">app/</span> <span class="fc">← Robot logic (uses the drivers)</span>
│ │ ├── <span class="ff">main.cpp</span> <span class="fc">← Program entry point (setup + loop)</span>
│ │ ├── <span class="ff">robot.cpp/h</span> <span class="fc">← Main state machine (idle/moving/etc)</span>
│ │ ├── <span class="ff">motion.cpp/h</span> <span class="fc">← Speed controller (PID) + motion targets</span>
│ │ ├── <span class="ff">mecanum.cpp/h</span> <span class="fc">← Wheel math (which wheels go which way)</span>
│ │ └── <span class="ff">serial_cmd.cpp/h</span> <span class="fc">← Parse text commands from USB</span>
│ ├── <span class="fd">hal/</span> <span class="fc">← Hardware drivers (talk to the chips)</span>
│ │ ├── <span class="ff">motor.cpp/h</span> <span class="fc">← Send power to motors via PCA9685</span>
│ │ ├── <span class="ff">encoder.cpp/h</span> <span class="fc">← Read wheel rotation sensors</span>
│ │ ├── <span class="ff">timer.cpp/h</span> <span class="fc">← 50Hz timer tick (heartbeat)</span>
│ │ └── <span class="ff">pca9685.cpp/h</span> <span class="fc">← PWM chip I2C communication</span>
│ ├── <span class="fd">core/</span> <span class="fc">← Settings & shared definitions</span>
│ │ ├── <span class="ff">config.h</span> <span class="fc">← ALL pin numbers, speeds, timing values</span>
│ │ └── <span class="ff">types.h</span> <span class="fc">← Data types (directions, states, commands)</span>
│ └── <span class="fd">test/</span> <span class="fc">← Separate test firmware</span>
│ └── <span class="ff">main.cpp</span> <span class="fc">← Test individual motors/encoders</span>
├── <span class="fd">lib/</span> <span class="fc">← Local libraries</span>
│ ├── <span class="ff">Adafruit_PWMServo/</span> <span class="fc">← PCA9685 I2C driver</span>
│ ├── <span class="ff">QGPMaker/</span> <span class="fc">← Encoder library (pin-change interrupts)</span>
│ ├── <span class="ff">PinChangeInterrupt/</span> <span class="fc">← Pin change interrupt support</span>
│ └── <span class="ff">PS2X/</span> <span class="fc">← PS2 controller library</span>
├── <span class="fd">tools/</span> <span class="fc">← Python scripts for PC</span>
│ ├── <span class="ff">robot_test.py</span> <span class="fc">← GUI with keyboard controls</span>
│ ├── <span class="ff">hw_test.py</span> <span class="fc">← Hardware test command line tool</span>
│ ├── <span class="ff">run.py</span> <span class="fc">← Easy launcher</span>
│ ├── <span class="ff">install.sh</span> <span class="fc">← One-command Python setup</span>
│ └── <span class="ff">requirements.txt</span> <span class="fc">← Python dependencies</span>
├── <span class="fd">docs/</span> <span class="fc">← Documentation</span>
│ ├── <span class="ff">index.html</span> <span class="fc">← This page (interactive visual docs)</span>
│ ├── <span class="ff">architecture.md</span> <span class="fc">← Software layers, state machine, timing</span>
│ ├── <span class="ff">protocol.md</span> <span class="fc">← Serial commands, velocity mode, responses</span>
│ ├── <span class="ff">hardware.md</span> <span class="fc">← Wiring, PCA9685, H-bridges, encoders</span>
│ ├── <span class="ff">motion-control.md</span> <span class="fc">← Kinematics, PID, calibration, speed ramping</span>
│ ├── <span class="ff">tools.md</span> <span class="fc">← GUI, CLI, test firmware, PS2 controller</span>
│ ├── <span class="ff">configuration.md</span> <span class="fc">← All config.h constants, safety, troubleshooting</span>
│ ├── <span class="ff">Motor-Shield-V5.3SCH.pdf</span> <span class="fc">← Motor shield schematic</span>
│ └── <span class="fd">images/</span> <span class="fc">← Reference photos and diagrams</span>
└── <span class="ff">platformio.ini</span> <span class="fc">← Build settings (env:uno + env:hwtest)</span>
</div>
</section>
<!-- ================================================================ -->
<!-- 2. HOW IT WORKS (Big Picture) -->
<!-- ================================================================ -->
<section id="how-it-works">
<h2>2. How It All Works Together</h2>
<p class="section-intro">
Here is the journey of a command from your computer all the way to the wheels spinning.
Understanding this flow makes everything else in this document make sense.
</p>
<h3>The Full Journey of a Command</h3>
<p>Let's say you type <code>FWD,100,2000</code> (meaning "go forward at speed 100 for 2000 encoder ticks").
Here's what happens step by step:</p>
<div class="step"><div class="num">1</div><div>
<h4>You Send a Command</h4>
<p>Your PC sends the text <code>FWD,100,2000\n</code> over USB at 115,200 baud (characters per second).
The Arduino receives it one character at a time.</p>
</div></div>
<div class="step"><div class="num">2</div><div>
<h4>The Command Parser Reads It</h4>
<p><strong>serial_cmd.cpp</strong> collects characters until it sees a newline (<code>\n</code>),
then splits the text by commas to understand: command = FWD, speed = 100, ticks = 2000.</p>
</div></div>
<div class="step"><div class="num">3</div><div>
<h4>The State Machine Accepts It</h4>
<p><strong>robot.cpp</strong> checks: "Am I idle? Yes." So it accepts the command, resets the encoder
counters to zero, and switches from IDLE state to MOVING state. It sends back <code>OK</code>.</p>
</div></div>
<div class="step"><div class="num">4</div><div>
<h4>Mecanum Math Calculates Wheel Directions</h4>
<p><strong>mecanum.cpp</strong> figures out that "forward" means all 4 wheels spin forward at the same speed.
(For "strafe left" it would be a different combination — see the Mecanum section.)</p>
</div></div>
<div class="step"><div class="num">5</div><div>
<h4>The Speed Controller (PID) Runs 50 Times Per Second</h4>
<p>Every 20 milliseconds, <strong>motion.cpp</strong> checks each wheel's encoder to see how fast it's actually
spinning, compares that to how fast it <em>should</em> be spinning, and adjusts the motor power up or down.
This is like cruise control in a car — if you hit a hill, it gives more gas.</p>
</div></div>
<div class="step"><div class="num">6</div><div>
<h4>Motor Driver Sends Power</h4>
<p><strong>motor.cpp</strong> sends the calculated power level to the <strong>PCA9685</strong> chip over I2C.
This chip generates PWM signals (rapid on/off pulses) that control how much power each motor gets.</p>
</div></div>
<div class="step"><div class="num">7</div><div>
<h4>H-Bridges Deliver Power to Motors</h4>
<p>The <strong>TB67H450</strong> H-bridge chips receive the PWM and switch battery power to the motors.
Two signals per motor (IN1/IN2) control direction: one high = forward, the other high = backward.</p>
</div></div>
<div class="step"><div class="num">8</div><div>
<h4>Target Reached → Stop</h4>
<p>When the average encoder count reaches 2000 ticks, the robot brakes briefly (40ms) to stop quickly,
then releases the motors (coast mode = no power), and sends <code>DONE</code> back to your PC.</p>
</div></div>
<div class="explain">
<strong>Key insight:</strong> The Arduino does NOT just "turn the motor on and wait."
It checks and adjusts each wheel's speed 50 times per second using encoder feedback.
This closed-loop control is what makes the robot move straight and stop accurately.
</div>
<h3>Software Layers</h3>
<p>The code is organized in layers. Each layer only talks to the layer directly below it:</p>
<div style="max-width: 650px; margin: 0 auto;">
<div class="layer">
<div class="layer-title">APPLICATION LAYER (src/app/)</div>
<div class="layer-desc">The "brain" — decides what to do: robot.cpp (state machine), motion.cpp (speed controller),
mecanum.cpp (wheel math), serial_cmd.cpp (command parser)</div>
</div>
<div class="arrow-down">↓</div>
<div class="layer">
<div class="layer-title">HARDWARE ABSTRACTION LAYER (src/hal/)</div>
<div class="layer-desc">Talks to the physical chips: motor.cpp (send PWM), encoder.cpp (read sensors),
timer.cpp (50Hz clock), pca9685.h (I2C communication)</div>
</div>
<div class="arrow-down">↓</div>
<div class="layer">
<div class="layer-title">CORE (src/core/)</div>
<div class="layer-desc">Shared settings: config.h (all pin numbers & constants),
types.h (enums like Direction, State, Command)</div>
</div>
<div class="arrow-down">↓</div>
<div class="layer" style="border-color: #fbbf24;">
<div class="layer-title" style="color: #fbbf24;">PHYSICAL HARDWARE</div>
<div class="layer-desc">Arduino Uno ↔ PCA9685 PWM chip ↔ TB67H450 H-bridges ↔ DC Motors + Encoders</div>
</div>
</div>
</section>
<!-- ================================================================ -->
<!-- 3. HARDWARE -->
<!-- ================================================================ -->
<section id="hardware">
<h2>3. Hardware Overview</h2>
<p class="section-intro">
The robot is built from an Arduino Uno connected to several electronic chips. Here's what each part does
and how they connect to each other.
</p>
<h3>Block Diagram — How Everything Connects</h3>
<div class="diagram"><span class="cd"> YOUR COMPUTER ARDUINO MOTOR DRIVER WHEELS</span>
<span class="cd"> ───────────── ─────── ──────────── ──────</span>
<span class="cg">┌──────────┐</span> USB cable <span class="cb">┌──────────────┐</span> I2C wire <span class="cy">┌──────────────┐</span> PWM signals <span class="cr">┌───────────┐</span>
<span class="cg">│ PC / GUI │</span>──(115200)──►<span class="cb">│ Arduino Uno │</span>──(400kHz)─►<span class="cy">│ PCA9685 │</span>──(8 wires)──►<span class="cr">│ 4x H-Bridge│</span>
<span class="cg">│ │</span> baud rate <span class="cb">│ ATmega328P │</span> SDA=A4 <span class="cy">│ PWM Driver │</span> 2 per motor <span class="cr">│ TB67H450 │</span>
<span class="cg">│ Sends: │</span> <span class="cb">│ │</span> SCL=A5 <span class="cy">│ 16 channels │</span> <span class="cr">│ │</span>
<span class="cg">│ FWD,100 │</span> <span class="cb">│ The brain │</span> <span class="cy">│ at 0x60 │</span> <span class="cr">│ Switches │</span>
<span class="cg">│ STOP │</span> <span class="cb">│ of the │</span> <span class="cy">│ │</span> <span class="cr">│ battery │</span>
<span class="cg">│ etc. │</span> <span class="cb">│ robot │</span> <span class="cy">│ Generates │</span> <span class="cr">│ power to │</span>
<span class="cg">└──────────┘</span> <span class="cb">└──────┬───────┘</span> <span class="cy">│ fast pulses │</span> <span class="cr">│ motors │</span>
│ <span class="cy">└──────────────┘</span> <span class="cr">└──────┬────┘</span>
│ │
<span class="cp">Pin-change</span> │
<span class="cp">interrupts</span> ┌────┴─────┐
│ │ 4x Motors│
<span class="cp">┌──────┴───────┐</span> │ with │
<span class="cp">│ 4x Encoders │</span>◄──── physically attached to ──────────│ gears │
<span class="cp">│ (rotation │</span> the motor shafts │ (90:1) │
<span class="cp">│ sensors) │</span> └────┬─────┘
<span class="cp">│ 4320 ticks │</span> │
<span class="cp">│ per wheel │</span> ┌────┴─────┐
<span class="cp">│ revolution │</span> │ 4 Mecanum│
<span class="cp">└──────────────┘</span> │ Wheels │
│ (80mm) │
└──────────┘</div>
<div class="explain">
<strong>Why so many chips?</strong> The Arduino Uno only has 6 PWM pins, but we need 8 (2 per motor × 4 motors).
So we use the <strong>PCA9685</strong> chip which has 16 PWM channels and talks to the Arduino over just 2 wires (I2C).
The <strong>TB67H450</strong> H-bridges are needed because the Arduino can't provide enough current to spin motors directly —
the H-bridges switch the battery's high-current power using the Arduino's low-power signals.
</div>
<h3>Component Details</h3>
<table class="tbl">
<thead><tr><th>Part</th><th>What It Is</th><th>What It Does</th><th>Key Specs</th></tr></thead>
<tbody>
<tr><td><strong>Arduino Uno</strong></td><td>Microcontroller board</td>
<td class="left">The "brain" — runs all the code, reads sensors, makes decisions, sends commands to the PWM chip</td>
<td>16MHz, 2KB RAM, 32KB Flash</td></tr>
<tr><td><strong>PCA9685</strong></td><td>PWM driver chip</td>
<td class="left">Generates 16 independent PWM signals for motor speed control. Connected via I2C (just 2 wires)</td>
<td>I2C addr 0x60, 1600Hz, 12-bit</td></tr>
<tr><td><strong>TB67H450</strong> (x4)</td><td>H-bridge motor driver</td>
<td class="left">Switches battery power to a motor. 2 input pins (IN1/IN2) control direction and speed</td>
<td>One per motor, 2 PWM channels each</td></tr>
<tr><td><strong>DC Motors</strong> (x4)</td><td>Geared motors</td>
<td class="left">Spin the wheels. Internal 90:1 gearbox reduces speed but increases torque</td>
<td>9600 RPM shaft, ~107 RPM output</td></tr>
<tr><td><strong>Encoders</strong> (x4)</td><td>Rotation sensors</td>
<td class="left">Attached to each motor shaft. Count pulses as the wheel turns — like a car's odometer but much more precise</td>
<td>4320 counts per revolution</td></tr>
<tr><td><strong>Mecanum Wheels</strong> (x4)</td><td>Special omnidirectional wheels</td>
<td class="left">Have angled rollers that allow the robot to move sideways by spinning wheels in certain combinations</td>
<td>80mm diameter</td></tr>
</tbody>
</table>
</section>
<!-- ================================================================ -->
<!-- 4. THE BRAIN (State Machine) -->
<!-- ================================================================ -->
<section id="brain">
<h2>4. The Brain — Robot States</h2>
<p class="section-intro">
The Arduino runs a <strong>state machine</strong> — at any moment, the robot is in one of 5 "states"
(like modes). What commands it accepts and what it does depends on which state it's in.
</p>
<div class="explain">
<strong>What's a state machine?</strong> Think of a traffic light: it's either RED, YELLOW, or GREEN.
It follows rules about when to switch (RED → GREEN → YELLOW → RED).
Our robot is similar — it's either IDLE, MOVING, CALIBRATING, TESTING, or ERROR,
and specific events cause it to switch between these modes.
</div>
<h3>The 5 States</h3>
<div class="diagram"><span class="cb"> ROBOT STATE MACHINE</span>
<span class="cb"> (the robot is always in one of these states)</span>
<span class="cg">┌──────────┐</span>
┌───────────<span class="cg">│ IDLE │</span>───────────┐
│ <span class="cg">│ (waiting)│</span> │
│ <span class="cg">└─────┬────┘</span> │
│ │ │
You send You send You send
<span class="cy">CALIB</span> <span class="cy">FWD/BWD/VEL/</span> <span class="cy">TMOTOR/TENC</span>
│ <span class="cy">LEFT/RIGHT/TURN</span> │
│ │ │
▼ ▼ ▼
<span class="cp">┌──────────────┐</span> <span class="cy">┌──────────────┐</span> <span class="cb">┌──────────────┐</span>
<span class="cp">│ CALIBRATING │</span> <span class="cy">│ MOVING │</span> <span class="cb">│ TESTING │</span>
<span class="cp">│ │</span> <span class="cy">│ │</span> <span class="cb">│ │</span>
<span class="cp">│ Measuring │</span> <span class="cy">│ PID running │</span> <span class="cb">│ Raw motor or │</span>
<span class="cp">│ motor speeds │</span> <span class="cy">│ at 50Hz │</span> <span class="cb">│ encoder test │</span>
<span class="cp">│ (~10 seconds)│</span> <span class="cy">│ │</span> <span class="cb">│ │</span>
<span class="cp">└──────┬───────┘</span> <span class="cy">└────┬────┬────┘</span> <span class="cb">└──────┬───────┘</span>
│ │ │ │
<span class="cd">Finished</span> <span class="cd">Target Something</span> <span class="cd">You send</span>
<span class="cd">or STOP</span> <span class="cd">reached went wrong</span> <span class="cd">STOP</span>
│ │ │ │
└───────────────┴────┴────────────┘
│
▼
<span class="cg">Back to IDLE</span>
<span class="cg">(sends DONE or ERROR)</span>
<span class="cr">┌──────────┐</span>
<span class="cr">│ ERROR │</span>◄──── The I2C communication to
<span class="cr">│ │</span> the motor chip failed 5 times
<span class="cr">│Recovers │</span>────► Back to IDLE after 5 seconds
<span class="cr">│ by itself│</span>
<span class="cr">└──────────┘</span></div>
<h3>What Each State Means</h3>
<table class="tbl">
<thead><tr><th>State</th><th>When?</th><th>What Happens</th><th>How It Ends</th></tr></thead>
<tbody>
<tr>
<td><strong style="color:#4ade80;">IDLE</strong></td>
<td class="left">Power on, or after any command finishes</td>
<td class="left">Motors are off (no current flowing). Prints encoder values once per second. Ready for any command.</td>
<td class="left">You send a movement, calibration, or test command</td>
</tr>
<tr>
<td><strong style="color:#fbbf24;">MOVING</strong></td>
<td class="left">You sent FWD, BWD, LEFT, RIGHT, TURN, DIAG*, or VEL</td>
<td class="left">The speed controller (PID) runs 50 times per second, adjusting motor power based on encoder readings.
Prints encoder values 20 times per second.</td>
<td class="left">Target reached (DONE), stalled (ERROR), timed out (ERROR), or you send STOP</td>
</tr>
<tr>
<td><strong style="color:#c084fc;">CALIBRATING</strong></td>
<td class="left">You sent CALIB</td>
<td class="left">Runs all motors at a set power, measures how fast each one spins,
saves the results. Takes about 10 seconds.</td>
<td class="left">Finishes and saves data (DONE), or you send STOP</td>
</tr>
<tr>
<td><strong style="color:#00d9ff;">TESTING</strong></td>
<td class="left">You sent TMOTOR or TENC</td>
<td class="left">TMOTOR: applies raw power to one motor (no speed control) and shows all encoder readings.
TENC: just shows all encoder values while you spin wheels by hand.</td>
<td class="left">Only ends when you send STOP</td>
</tr>
<tr>
<td><strong style="color:#ef4444;">ERROR</strong></td>
<td class="left">The PCA9685 chip stopped responding (I2C failure) 5 times in a row</td>
<td class="left">Motors are turned off for safety. Waits 5 seconds, then automatically recovers to IDLE.</td>
<td class="left">Auto-recovers, or you send STOP</td>
</tr>
</tbody>
</table>
<h3>Safety Features</h3>
<p>The robot has several automatic protections to prevent damage:</p>
<div class="feature-grid">
<div class="feature-card" style="border-left-color: #ef4444;">
<h4>Stall Detection</h4>
<p><strong>Problem:</strong> A wheel is stuck (encoder not moving).<br>
<strong>Response:</strong> If any motor makes less than 50 ticks progress in 5 seconds, the robot stops and reports "Stalled".</p>
</div>
<div class="feature-card" style="border-left-color: #fbbf24;">
<h4>Movement Timeout</h4>
<p><strong>Problem:</strong> A move is taking way too long (something is wrong).<br>
<strong>Response:</strong> Any single movement automatically stops after 30 seconds.</p>
</div>
<div class="feature-card" style="border-left-color: #c084fc;">
<h4>Watchdog Timer</h4>
<p><strong>Problem:</strong> In velocity mode, the PC crashed or disconnected.<br>
<strong>Response:</strong> If no VEL command arrives within 200ms, the robot stops. The PC must keep sending commands to keep moving.</p>
</div>
<div class="feature-card" style="border-left-color: #00d9ff;">
<h4>I2C Health Check</h4>
<p><strong>Problem:</strong> The wire to the PCA9685 came loose.<br>
<strong>Response:</strong> Checked every 2 seconds. After 5 failures, enters ERROR state and turns off all motors.</p>
</div>
</div>
</section>
<!-- ================================================================ -->
<!-- 5. MOTORS -->
<!-- ================================================================ -->
<section id="motors">
<h2>5. Motors & Wiring</h2>
<p class="section-intro">
The robot has 4 DC motors, each connected to an H-bridge driver and an encoder sensor.
This section explains which motor is connected to which chip pins.
</p>
<div class="explain">
<strong>What's an H-bridge?</strong> A motor spins one way when current flows through it in one direction,
and the opposite way when current flows in reverse. An H-bridge is a circuit that can switch the current
direction electronically. Each of our TB67H450 H-bridges has two control pins:
<ul style="margin: 8px 0 0 20px;">
<li><strong>IN1 gets power</strong> (IN2 off) → motor spins forward</li>
<li><strong>IN2 gets power</strong> (IN1 off) → motor spins backward</li>
<li><strong>Both on</strong> → electric brake (motor stops quickly)</li>
<li><strong>Both off</strong> → coast (motor freewheels, no current)</li>
</ul>
</div>
<h3>Motor Positions on the Robot</h3>
<div class="robot-box">
<div class="robot-label-top">↑ FRONT OF ROBOT ↑</div>
<div class="robot-grid">
<div class="whl">
<div class="name">FL</div>
<div class="sub2">Index 0</div>
<div class="sub">PWM: CH8 / CH9</div>
<div class="sub">Encoder: Port 1</div>
</div>
<div class="whl">
<div class="name">FR</div>
<div class="sub2">Index 3</div>
<div class="sub">PWM: CH13 / CH12</div>
<div class="sub">Encoder: Port 4</div>
</div>
<div class="whl">
<div class="name">RL</div>
<div class="sub2">Index 1</div>
<div class="sub">PWM: CH10 / CH11</div>
<div class="sub">Encoder: Port 2</div>
</div>
<div class="whl">
<div class="name">RR</div>
<div class="sub2">Index 2</div>
<div class="sub">PWM: CH15 / CH14</div>
<div class="sub">Encoder: Port 3</div>
</div>
</div>
<div class="robot-label-bot">REAR OF ROBOT</div>
</div>
<div class="explain warn">
<strong>Watch out:</strong> The motor indices are NOT in the order you'd expect!
FL=0, RL=1, RR=2, FR=3. This matches the physical wiring.
Every array in the code (motor channels, encoder values, calibration data) follows this same order.
</div>
<h3>Complete Wiring Table</h3>
<table class="tbl">
<thead><tr><th>Motor</th><th>Index</th><th>IN1 Channel</th><th>IN2 Channel</th><th>Encoder Port</th><th>Encoder Direction</th></tr></thead>
<tbody>
<tr><td><strong>FL</strong> (Front-Left)</td><td>0</td><td>CH 8</td><td>CH 9</td><td>Port 1</td><td class="pos">+1 (normal)</td></tr>
<tr><td><strong>RL</strong> (Rear-Left)</td><td>1</td><td>CH 10</td><td>CH 11</td><td>Port 2</td><td class="pos">+1 (normal)</td></tr>
<tr><td><strong>RR</strong> (Rear-Right)</td><td>2</td><td>CH 15</td><td>CH 14</td><td>Port 3</td><td class="neg">-1 (inverted)</td></tr>
<tr><td><strong>FR</strong> (Front-Right)</td><td>3</td><td>CH 13</td><td>CH 12</td><td>Port 4</td><td class="neg">-1 (inverted)</td></tr>
</tbody>
</table>
<div class="explain">
<strong>Why is the encoder direction inverted for right-side motors?</strong>
The right-side motors are physically mirrored — when the robot goes forward,
the left wheels spin clockwise and the right wheels spin counter-clockwise.
The <code>-1</code> direction multiplier flips the encoder count so that "forward" is always positive for all wheels.
</div>
<h3>H-Bridge States Explained</h3>
<table class="tbl">
<thead><tr><th>What You Want</th><th>IN1 Pin</th><th>IN2 Pin</th><th>What Happens</th></tr></thead>
<tbody>
<tr><td><strong>Spin Forward</strong></td><td class="pwm-c">PWM (pulsing)</td><td class="neg">OFF (0V)</td>
<td class="left">Motor spins forward. Speed depends on pulse width (0-255)</td></tr>
<tr><td><strong>Spin Backward</strong></td><td class="neg">OFF (0V)</td><td class="pwm-c">PWM (pulsing)</td>
<td class="left">Motor spins backward. Same speed range</td></tr>
<tr><td><strong>Brake (quick stop)</strong></td><td class="pos">ON (5V)</td><td class="pos">ON (5V)</td>
<td class="left">Both windings short-circuited. Motor stops fast. Used for 40ms after reaching target</td></tr>
<tr><td><strong>Coast (release)</strong></td><td class="neg">OFF (0V)</td><td class="neg">OFF (0V)</td>
<td class="left">Motor disconnected, spins freely. <strong>No current flows</strong>. This is the IDLE state</td></tr>
</tbody>
</table>
<h3>What Happens When a Move Finishes</h3>
<div class="flow">
<div class="fbox y"><h4 style="color:#fbbf24;">MOVING</h4><p style="font-size:0.85em;">PID adjusting speed</p></div>
<div class="farr">→</div>
<div class="fbox"><h4>Target Reached</h4><p style="font-size:0.85em;">Encoders hit 2000</p></div>
<div class="farr">→</div>
<div class="fbox r"><h4 style="color:#ef4444;">BRAKE</h4><p style="font-size:0.85em;">40ms (quick stop)</p></div>
<div class="farr">→</div>
<div class="fbox b"><h4 style="color:#00d9ff;">COAST</h4><p style="font-size:0.85em;">No power</p></div>
<div class="farr">→</div>
<div class="fbox"><h4 style="color:#4ade80;">DONE sent</h4><p style="font-size:0.85em;">Back to IDLE</p></div>
</div>
</section>
<!-- ================================================================ -->
<!-- 6. SPEED CONTROL (PID) -->
<!-- ================================================================ -->
<section id="speed-control">
<h2>6. Speed Control (PID)</h2>
<p class="section-intro">
If you just set a motor to a fixed power level, it might spin faster or slower depending on battery charge,
floor friction, or load. The PID controller automatically adjusts power to maintain the exact speed you want.
</p>
<div class="explain good">
<strong>Analogy: Cruise control in a car.</strong> You set the speed to 60 km/h. Going uphill, the car gives
more gas. Going downhill, it gives less. It constantly checks the speedometer and adjusts.
Our PID does the same thing, but for each wheel, 50 times per second.
</div>
<h3>How the PID Controller Works</h3>
<p>Each of the 4 motors has its own independent speed controller. Every 20ms (50 times per second), it does this:</p>
<div class="step"><div class="num">1</div><div>
<h4>Read the encoder</h4>
<p>Count how many ticks the wheel moved since the last check (20ms ago).
This is the <strong>actual speed</strong> (ticks per 20ms period).</p>
</div></div>
<div class="step"><div class="num">2</div><div>
<h4>Calculate the error</h4>
<p><code>error = desired speed - actual speed</code><br>
If error is positive, the wheel is too slow. If negative, too fast.</p>
</div></div>
<div class="step"><div class="num">3</div><div>
<h4>Calculate Feed-Forward (initial guess)</h4>
<p>Based on calibration data, estimate how much power (PWM) is needed for this speed.<br>
<code>FF = desired_ticks_per_period × 255 / max_ticks_per_period_for_this_motor</code><br>
This gets us close, but not perfect (that's what the next step fixes).</p>
</div></div>
<div class="step"><div class="num">4</div><div>
<h4>Calculate PI Correction</h4>
<p><strong>P (Proportional):</strong> <code>1.5 × error</code> — big correction if the error is big.<br>
<strong>I (Integral):</strong> <code>0.3 × accumulated_error</code> — slowly builds up if there's a persistent small error
(like a slightly weak motor). Capped at ±150 to prevent runaway.</p>
</div></div>
<div class="step"><div class="num">5</div><div>
<h4>Set the motor power</h4>
<p><code>PWM = Feed-Forward + P correction + I correction</code><br>
Clamped to 0-255, then sent to the PCA9685 chip.</p>
</div></div>
<h3>PID Parameters (in config.h)</h3>
<table class="tbl">
<thead><tr><th>Parameter</th><th>Value</th><th>What It Means</th></tr></thead>
<tbody>
<tr><td>Kp (Proportional)</td><td><strong>1.5</strong></td>
<td class="left">For every 1 tick/period the motor is too slow, add 1.5 PWM. Higher = more aggressive correction.</td></tr>
<tr><td>Ki (Integral)</td><td><strong>0.3</strong></td>
<td class="left">Slowly accumulates correction for persistent errors. Lower = less overshoot but slower to respond.</td></tr>
<tr><td>IMAX (integral limit)</td><td><strong>150</strong></td>
<td class="left">Maximum value the integral can reach. Prevents "windup" (overcorrection after a stall).</td></tr>
<tr><td>Control frequency</td><td><strong>50 Hz</strong></td>
<td class="left">PID runs every 20 milliseconds. Fast enough for smooth control, slow enough for the Arduino to handle.</td></tr>
</tbody>
</table>
<h3>Speed Ramping (Smooth Start & Stop)</h3>
<div class="explain">
<strong>Why not just go full speed immediately?</strong> If you suddenly apply full power, the wheels can slip
on the floor (like flooring the gas pedal). And if you stop instantly, the robot overshoots its target.
So we ramp the speed up at the start and down near the end.
</div>
<table class="tbl">
<thead><tr><th>Phase</th><th>When</th><th>What Happens</th><th>Parameters</th></tr></thead>
<tbody>
<tr><td><strong style="color:#4ade80;">Acceleration</strong></td>
<td>First 300 ticks (~1.7cm)</td>
<td class="left">Speed ramps up linearly from 40 to the target speed. Prevents wheel slip on startup.</td>
<td>ACCEL_RAMP_TICKS=300, ACCEL_MIN_SPEED=40</td></tr>
<tr><td><strong style="color:#fbbf24;">Deceleration</strong></td>
<td>Last 400 ticks (~2.3cm)</td>
<td class="left">Speed reduces as the target approaches. Minimum speed of 60 (enough to keep moving). Prevents overshoot.</td>
<td>SLOWDOWN_TICKS=400, SLOWDOWN_MIN_SPEED=60</td></tr>
</tbody>
</table>
</section>
<!-- ================================================================ -->
<!-- 7. MECANUM WHEELS -->
<!-- ================================================================ -->
<section id="mecanum">
<h2>7. Mecanum Wheels — How Omnidirectional Movement Works</h2>
<p class="section-intro">
Normal wheels can only go forward and backward. Mecanum wheels have angled rollers that create
sideways forces. By spinning different wheels in different directions, the robot can move any way you want.
</p>
<div class="explain good">
<strong>How it works:</strong> Each Mecanum wheel has small rollers at 45 degrees. When the wheel spins,
it pushes both forward AND sideways. By choosing which wheels go forward and which go backward,
you can combine these forces to move in any direction.
</div>
<h3>Wheel Roller Layout (looking down at the robot)</h3>
<div class="diagram"><span class="cb"> FRONT OF ROBOT</span>
<span class="cg">FL</span> ╲╲╲╲╲ ╱╱╱╱╱ <span class="cg">FR</span>
╲╲╲╲╲ ╱╱╱╱╱
┌──────────────────────────────────┐
│ │
│ <span class="cy">ROBOT BODY</span> │
│ │
└──────────────────────────────────┘
<span class="cg">RL</span> ╱╱╱╱╱ ╲╲╲╲╲ <span class="cg">RR</span>
╱╱╱╱╱ ╲╲╲╲╲
<span class="cd">REAR OF ROBOT</span>
<span class="cd">The lines (╲╲ and ╱╱) show the roller angle on each wheel.</span>
<span class="cd">FL and RR have "╲" rollers (same diagonal).</span>
<span class="cd">FR and RL have "╱" rollers (same diagonal).</span></div>
<h3>Which Wheels Spin Which Way for Each Direction</h3>
<p>This table shows the motor direction for each movement type.
<span class="pos">+</span> means the motor drives forward,
<span class="neg">−</span> means backward,
<span class="dim">0</span> means that motor is off.</p>
<table class="tbl">
<thead>
<tr><th>Direction</th><th>FL<br><span style="font-weight:normal;font-size:0.8em;">(idx 0)</span></th>
<th>RL<br><span style="font-weight:normal;font-size:0.8em;">(idx 1)</span></th>
<th>RR<br><span style="font-weight:normal;font-size:0.8em;">(idx 2)</span></th>
<th>FR<br><span style="font-weight:normal;font-size:0.8em;">(idx 3)</span></th>
<th>Which Motors Are Active</th></tr>
</thead>
<tbody>
<tr><td><strong>FORWARD</strong></td><td class="pos">+</td><td class="pos">+</td><td class="pos">+</td><td class="pos">+</td><td>All 4</td></tr>
<tr><td><strong>BACKWARD</strong></td><td class="neg">−</td><td class="neg">−</td><td class="neg">−</td><td class="neg">−</td><td>All 4</td></tr>
<tr><td><strong>LEFT (strafe)</strong></td><td class="neg">−</td><td class="neg">−</td><td class="pos">+</td><td class="pos">+</td><td>All 4</td></tr>
<tr><td><strong>RIGHT (strafe)</strong></td><td class="pos">+</td><td class="pos">+</td><td class="neg">−</td><td class="neg">−</td><td>All 4</td></tr>
<tr><td><strong>ROTATE CCW</strong> (turn left)</td><td class="neg">−</td><td class="pos">+</td><td class="neg">−</td><td class="pos">+</td><td>All 4</td></tr>
<tr><td><strong>ROTATE CW</strong> (turn right)</td><td class="pos">+</td><td class="neg">−</td><td class="pos">+</td><td class="neg">−</td><td>All 4</td></tr>
<tr><td><strong>DIAG FL</strong> (forward-left)</td><td class="dim">0</td><td class="pos">+</td><td class="dim">0</td><td class="pos">+</td><td>RL + FR only</td></tr>
<tr><td><strong>DIAG FR</strong> (forward-right)</td><td class="pos">+</td><td class="dim">0</td><td class="pos">+</td><td class="dim">0</td><td>FL + RR only</td></tr>
<tr><td><strong>DIAG BL</strong> (back-left)</td><td class="dim">0</td><td class="neg">−</td><td class="dim">0</td><td class="neg">−</td><td>RL + FR only</td></tr>
<tr><td><strong>DIAG BR</strong> (back-right)</td><td class="neg">−</td><td class="dim">0</td><td class="neg">−</td><td class="dim">0</td><td>FL + RR only</td></tr>
</tbody>
</table>
<div class="explain warn">
<strong>Diagonal moves only use 2 motors!</strong> The other 2 are off. The firmware tracks which motors
are active with a bitmask, so it only checks those motors' encoders when measuring progress.
Without this, the inactive motors (reading 0) would confuse the "have we reached the target?" check.
</div>
<h3>Velocity Mode (VEL Command) — Inverse Kinematics</h3>
<p>Instead of picking a named direction, you can directly specify velocity components:</p>
<ul style="margin: 10px 0 10px 25px;">
<li><strong>vx</strong> = forward/backward speed (-255 to 255)</li>
<li><strong>vy</strong> = left/right speed (-255 to 255, positive = left)</li>
<li><strong>wz</strong> = rotation speed (-255 to 255, positive = counter-clockwise)</li>
</ul>
<p>The firmware converts these into per-motor speeds using this formula:</p>
<pre>FL = vx - vy - wz
FR = vx + vy + wz
RL = vx + vy - wz
RR = vx - vy + wz
// If any value exceeds 255, all are scaled down proportionally</pre>
<div class="explain">