-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsatsuma.html
862 lines (573 loc) · 32 KB
/
satsuma.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="melis">
<link href="https://fonts.googleapis.com/css?family=Poppins:100,200,300,400,500,600,700,800,900" rel="stylesheet">
<title>Lemon Kit- Satsuma</title>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Additional CSS Files -->
<link rel="stylesheet" href="assets/css/fontawesome.css">
<link rel="stylesheet" href="assets/css/templatemo-edu-meeting.css">
<link rel="stylesheet" href="assets/css/owl.css">
<link rel="stylesheet" href="assets/css/lightbox.css">
<!--
TemplateMo 569 Edu Meeting
https://templatemo.com/tm-569-edu-meeting
-->
</head>
<body>
<!-- Sub Header -->
<div class="sub-header">
<div class="container">
<div class="row">
<div class="col-lg-8 col-sm-8">
<div class="left-content">
<p>HisarCS<em> IdeaLab</em></p>
</div>
</div>
<div class="col-lg-4 col-sm-4">
<div class="right-icons">
<ul>
<li><a href="#"><i class="fa fa-facebook"></i></a></li>
<li><a href="#"><i class="fa fa-twitter"></i></a></li>
<li><a href="#"><i class="fa fa-behance"></i></a></li>
<li><a href="#"><i class="fa fa-linkedin"></i></a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!-- ***** Header Area Start ***** -->
<header class="header-area header-sticky">
<div class="container">
<div class="row">
<div class="col-12">
<nav class="main-nav">
<!-- ***** Logo Start ***** -->
<a href="index.html" class="logo">
Lemon Robotics Kit
</a>
<!-- ***** Logo End ***** -->
<!-- ***** Menu Start ***** -->
<ul class="nav">
<! Home Button -->
<li><a href="index.html">Home</a></li>
<! Prereqs? -->
<li>
<a href="prereq.html">PreReqs</a>
</li>
<! Lime Button -->
<li>
<a href="lime.html">Lime</a>
</li>
<! Satsuma Button -->
<li class="scroll-to-section"><a href="javascript:void(0)" href="#top" class="active">Satsuma </a></li>
<!-- Lemon Button
<li ><a href="lemon.html">Lemon </a></li>
-->
<li class="scroll-to-section"><a href="#contact">Contact</a></li>
</ul>
<a class='menu-trigger'>
<span>Menu</span>
</a>
<!-- ***** Menu End ***** -->
</nav>
</div>
</div>
</div>
</header>
<!-- ***** Header Area End ***** -->
<section class="section main-banner" id="top" data-section="section1">
<img src="assets/images/satsuma/turtlegercek.HEIC.jpg" height="800">
<div class="video-overlay header-text">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="caption">
<h6 style="font-size: 30px;">Here is how to put together your next model!</h6>
<h2>Welcome to Satsuma!</h2>
<p>Satsuma is the level available in the Lemon Robotics Kit that focuses mostly on hardware and electronics. To build it, make sure you have all the elements required (learn more about these in the prerequisites segment) and follow the steps below!</p>
</section>
<section class="upcoming-meetings" id="meetings">
<div class="container">
<div class="top">
<div class="col-lg-12">
<h1 style=" color: whitesmoke; text-align: center; padding-top: 0px; padding-bottom: 10px; font-display: bold;">Start Now: Build Lime</h1>
<!-- KRAFTPLEX INSTRUCTIONS --------------------- -->
<div class="row ">
<div class="col-lg-12 ">
<h3 style=" color: orange; text-align: left; padding-top: 30px;" class="featurette-heading ">
Assemble the Kraftplex Body & Learn About 3D Design
</h3>
<a> <h3 style=" color: deepskyblue; text-align: left; padding-top: 30px;" class="featurette-heading ">
<span class="tab"></span> 1) The Main Body</a>
</h3>
<p style="color: whitesmoke;"></a> In this document you will learn how to put together the Turtle robot: Satsuma. You will need glue and this guide to complete this step.
<br>
Pro tip: make sure to check the video above to see how everything is done!
<br>
<br>
<p style="color: whitesmoke;"></a>
Take the two leg pieces with the large hole on one and the circle on the other. Combine the 3 pieces after carefully inserting the black servo piece in. To guarantee that it will stick, insert a screw into the little hole. Repeat four times through this stage. Insert the servo into a rectangular hole. After adding the rectangular frame around the servo, secure it by screwing from both sides. After screwing combine the leg and servo. Last but not least, attach the small screw to the black servo component to keep it stationary. Repeat four times through this stage. <br><br>
The same procedure will be done for the hole in the front, but this time the leg parts' opposite corner will be square-shaped rather than round; also the servos' direction will be reversed. Once finished, glue the other corner to the head. There is a darkish area where you can receive assistance while gluing your piece.
<br>
</p>
<br>
<br>
<div class="row">
<div class="col-lg-12">
<center>
<video width="700" height="490" controls>
<source src="assets/images//servos.mp4" type="video/mp4">
</video>
</center>
<div class="row">
<div class="col-lg-12">
<center>
<video width="700" height="490" controls>
<source src="assets/images//mainbod.mp4" type="video/mp4">
</video>
</center>
<br>
<br>
</p>
<div class="row">
<a > <h3 style=" color: deepskyblue; text-align: left; padding-top: 30px;" class="featurette-heading ">
<span class="tab"></span> 2) The Head</a>
</h3>
<br>
<p style="color: whitesmoke;"></a> Take the piece with the capacity to bend and two face-shaped pieces. Start building and gently glue them symmetrically. The small rectangle piece that will make up the nose is then glued in front place. With the two bottom pieces assembled and glued, the head is now finished. Attach the head to the body as shown below.
<br>
</p>
<br>
<br>
<div class="row">
<div class="col-lg-12">
<center>
<video width="700" height="490" controls>
<source src="assets/images//head.mp4" type="video/mp4">
</video>
</center>
<br>
</a>
</a>
</a>
</div>
</div>
</div>
</div>
<a> <h3 style=" color: deepskyblue; text-align: left; padding-top: 30px;" class="featurette-heading ">
<span class="tab"></span> 3) The shell </a>
<p style="color: whitesmoke;"></a>Put the pieces you see in the video in a suitable pattern around the shell. Glue the parts after arranging. This is the shell. Now attach it to the main body you have assembled so far.
<br>
The final robot should look like as shown below. Congratulations! Now that the turtle assembly is complete, let's move on to the electronics.
<br>
</p>
<div class="row">
<div class="col-lg-12">
<center>
<br>
<video width="700" height="490" controls>
<source src="assets/images//shell.mp4" type="video/mp4">
</video>
</center>
</div>
</div>
<div class="row ">
<div class="col-lg-12 ">
<h3 style=" color: orange; text-align: left; padding-top: 30px;" class="featurette-heading ">
And now, the electronics!
</h3>
<a> <h3 style=" color: deepskyblue; text-align: left; padding-top: 30px;" class="featurette-heading ">
<span class="tab"></span> 3)Mastering Electronics With Satsuma</a>
<br>
</h3>
<p style="color: whitesmoke;"> <span class="tab"></span> When compared to the previous project lime, Satsuma has a lot more involved when it comes to electronics. It utilizes many more tools that require even more conceptual knowledge. So going by a level to level basis this project will be the hardest challenge when it comes to electronics. First lets familiarize ourselves with all the components we will be using. Here is a list of materials:
<br><br>
1 x HC-SR04 Ultrasonic Sensor <br>
1 x Breadboard <br>
1 x 1kΩ resistor <br>
1 x 2kΩ resistor <br>
1 x pca9685 16 channel pwm driver <br>
2 x16850 Li-Ion batteries <br>
1 x 5v voltage regulator <br>
Multiple MF, MM, and FF jumper cables <br>
<br>
</p>
<center>
<div class="col-md-7">
<br>
<img src="assets/images/satsuma/satsuma_acik.png" style="border-radius: 30px;" >
<br>
</div>
</center>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
<br>
Amongst all of these components we will first start the assembly with the pca9685 as it will be the center of attention for this kit. However before we get into how this vital component works and how we will use it, we need to understand why we have chosen to use it in the first place. As you might’ve remembered the pico has only two ports that has a continuous electrical output: The 3v3 OUT and the Vbus. Outside of these pins the pico cannot power any components. Therefore it becomes exponentially more difficult to add servos after the two we had initially used in lime. Additionally if we were to connect more servos to the pico directly there would be an increase in the delay for each component we try to communicate with, meaning that the simultaneous rotation of servos would be almost impossible. To circumvent this problem we use a servo driver, the pca9685. The pca acts as a middle-man that can house up to 16 servos at once on a single board. The way that his middleman process works is that the pca uses an external power supply to power all of the servos connected to it while also using it’s connection to the pico to power itself up and use I2C to communicate back and forth with the servos and pico.
</p>
</div>
<br>
<center>
<div class="col-md-5">
<br>
<img src="assets/images/pico.png" style="border-radius: 30px; display: inline-block;" height= "300"; >
</div>
</center>
<br>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
<br>As you can see from the picture above as well as the one that should be next to you the pca9685 has pins to the bottom and the sides. For our purposes since we only need one board for the amount of servos we will be controlling we will be using only one of the duplicate pin sets to the sides. From those pins we will be connecting tot he raspberry pi pico’s GND, 3v3 OUT and I2C pins. Specifically the locations for these connections are: <br><br>
GND to pin 3<br>
SCL to pin 2<br>
SDA to pin 1<br>
VCC to pin 36 <br>
<br>
Additionally there is the top and bottom of the board that require our attention. The top of the board is where we will be connecting our external power supply and the bottom has all the channels that we can connect our servos to. Let’s start by connecting the servos on Satsuma to these channels. Here is the list of how we have connected these servos: <br><br>
Head to channel 0 <br>
Front right leg to channel 1 <br>
Front left leg to channel 2 <br>
Back right leg to channel 3 <br>
Back left leg to channel 4 <br>
<br>
Next we need to add power to the board through the power input to the top of the board. These can be located next to the power light and have V+ and GND written next to them. The pca9685 requires a 5V input from an external power source to be able to power all channels simultaneously. If there too little power the board cannot control multiple channels and if there is too much power, the board will send so much power that the servos circuits will burn and cease functioning. Therefore to get a nearly 5V output from our power source we will be utilizing a voltage regulator.
<br>
</p>
</div>
<br>
<center>
<div class="col-md-5">
<img src="assets/images/pil.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<br>
<p style="color: whitesmoke;"> <span class="tab"></span>
Without getting into the specifics a voltage regulator is a circuit that creates and maintains a fixed output voltage, irrespective of changes to the input voltage or load conditions. We will be using a 5V voltage regulator to keep our voltage at a nearly constant 5 volts. As our external power source we will be using 18650 Lithium-Ion batteries. The benefit of this type of battery is that they are rechargeable so if they ever run out you don’t need to worry about buying or getting a replacement. However normally each battery produces around 4.2V of charge, and with the battery case holder we will be using each battery will produce approximately 3.7V of charge, with the two batteries adding up to a nearly 7.4V output. Since this voltage exceeds the amount we need for our board we need to find a way to connect the batteries and voltage regulator together. To do this we will be using a breadboard.
<br>
<br>
</p>
<center>
<div class="col-md-5">
<img src="assets/images/bread1.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
A breadboard (pictured above) is a type of device that we will be using to build our circuits. The breadboard simplifies building electronic circuits by having a fixed direction that current travels through. When placing the breadboard so that the line in the middle is horizontal (like the picture above) the direction that the current travels in is vertical through each hole. The current travels until it reaches the dead space in the middle which seperates the two parts of the breadboard. So for example if we were to connect our voltage regulator to the breadboard the current travelling would look like the green lines.
<br>
<br>
</p>
<center>
<div class="col-md-5">
<img src="assets/images/bread2.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
The voltage regulators has 3 pins and starting from the left these pins are VIN, GND, and VOUT respectively. VIN is the input of power that we will be using, meaning the batteries, and the VOUT is what we will be connecting to the pca9685. So to connect our batteries for example we will be using the columns of the breadboard where the voltage regulator has the left and the middle pin connected. This way the current from the batteries will travel through the breadboard and reach the voltage regulator.
<br>
<br>
</p>
<center>
<div class="col-md-5">
<img src="assets/images/bread3.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
TNow that we have connected the voltage regulator and the battery together next lets test if our regulator works. To do this we will be using a multimeter, set the multimeter to double digit voltage control. Afterwards connect the black adapter to the GND column and the red adapter to the VOUT column. If your regulator works correctly you will see a reading of approximately 5.00 volts on your multimeter. If it doesn’t work you may have done your wiring incorrectly or you may have faulty equipment. Make sure that the multimeter has the correct reading before proceeding. Next we will be connecting the PCA9685 to our circuit. To draw the current of 5V we will be placing a jumper cable again to the GND column and one to the VOUT column. Then place those to the PCA9685’s top pins that have the V+ and GND mark. It’s important to remember that if you accidentally place these parts in reverse you will short circuit the board making it completely unusable for any future projects including this one. When you have done so the circuit should look like this.
<br>
<br>
</p>
<center>
<div class="col-md-5">
<img src="assets/images/bread4.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<br><br>
<center>
<div class="col-md-5">
<img src="assets/images/bread5.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
<br>
There is one last part remaining for connecting all the electrical circuits, which is connecting the ultrasonic sensor HC-SR04. <br>
But before we begin we need to understand how an ultrasonic sensor works. We will be using the ultrasonic sensor for calculating the distance between our robot and objects in front of it. Ultrasonic sensors work by emitting sound waves at a frequency too high for humans to hear. They then wait for the sound to be reflected back, calculating distance based on the time required. This the fundamental concept of ultrasonic sensors, however we will delving a little further into how these calculations are made so that we can understand our code better. The ultrasonic sensor we will be using, the hc-sr04, features two ultrasonic transducers: a ultrasonic transmitter and receiver. The transmitter converts electrical energy into ultrasound and the receiver converts the ultrasound to electrical signals. Specifically the transmitter converts the electrical signal into 40 KHz ultrasonic sound pulses. When the receiver receives these pulses, it produces an output pulse whose width is proportional to the distance of the object in front. This sensor provides excellent non-contact range detection between 2 cm to 400 cm (~13 feet) with an accuracy of 3 mm. Let’s look at the pinout of the HC-SR04:
<br><br>
VCC: Supplies power to the HC-SR04.<br>
Trig (trigger): The pin is used to trigger ultrasonic sound pulses. By setting this pin to OUT/HIGH (Meaning sending it an electrical current) the sensor initiates an ultrasonic burst.<br>
Echo: This pin goes high when the ultrasonic burst is transmitted and remains high until the sensor receives an echo, after which it goes low. By measuring the time the Echo pin stays high, the distance can be calculated.<br>
GND: This pin is used to ground the sensor.<br>
</p>
<center>
<div class="col-md-5">
<img src="assets/images/ultrasonic.png" style="border-radius: 30px; display: inline-block;" >
</div>
</div>
</center>
<br>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
Now let’s properly look at how distance is calculated with this sensor. It all starts when the trigger pin is set OUT/HIGH. In response, the sensor transmits an ultrasonic burst of eight pulses at 40 kHz. This 8-pulse pattern is specially designed so that the receiver can distinguish the transmitted pulses from ambient ultrasonic noise. These eight ultrasonic pulses travel through the air away from the transmitter. Meanwhile the echo pin goes high to initiate the echo-back signal. If those pulses are not reflected back, the echo signal times out and goes low after 38ms (38 milliseconds). Thus a pulse of 38ms indicates no obstruction within the range of the sensor.
</p>
<!-- andyden gif -->
<p style="color: whitesmoke;"> <span class="tab"></span>
If those pulses are reflected back, the echo pin goes low as soon as the signal is received. This generates a pulse on the echo pin whose width varies from 150 µs to 25 ms depending on the time taken to receive the signal.
<br>
</p>
<!-- andyden gif -->
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
Although we could use the pulse received from the pin, most algorithms (ours included) will utilize the time it took for the echo pin to go low instead. Knowing this let’s convert our data to distance. This can be worked out using the simple distance-speed-time equation.
<br><br>
</p>
<br>
<center>
<p style="color: white; font-size: xx-large;"><strong><b>Distance = Speed x Time</b>
</strong>
</center>
</p>
<br>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
Let us take an example to make it more clear. Suppose we have an object in front of the sensor at an unknown distance and we receive a pulse after 0.001 seconds on the echo pin. Now let’s calculate how far the object is from the sensor. We know the time it took so we can use that in our equation, and we also know the speed since the emitted pulse travels at the speed of sound meaning 343 m/s. Because our ultrasonic sensor is most effective at a range on 2-400 cm’s we will be converting the speed of sound to cm, giving us nearly 34300 cm/s.
<br><br>
</p>
<br>
<center>
<p style="color: white; font-size: xx-large;"><strong><b>Distance = 34300 cm/s x 0.001 s</b>
</strong>
</center>
</p>
<br>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
But we’re not done yet! Remember that the echo pulse indicates the time it takes for the signal to be sent and reflected back. So to get the distance, you have to divide your result by two.
</p>
<br>
<center>
<p style="color: white; font-size: x-large;"><strong><b>Distance = (34300 cm/s x 0.001 s) / 2 <br><br>
Distance = 17.15 cm
</b>
</strong>
</center>
</p>
<br>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
The result of this equation gives us the distance recorded by the ultrasonic sensor which in this case would be approximately 17.15 cm. So all in all we could simplify the equation of calculating distance from an ultrasonic sensor to this formula:
<br><br>
</p>
<center>
<p style="color: white; font-size: x-large;"><strong><b>Distance = (34300 cm/s x Time Elapsed Between Echo States) / 2<br><br>
Distance = 17.15 cm
</b>
</strong>
</center>
</p>
<p style="color: whitesmoke;"> <span class="tab"></span>
<br>
Finally after learning all there is to know about ultrasonic sensors we can start working in a physical space and create the circuit for this sensor.
</p>
<p style="color: white; font-size: large;"> <span class="tab"></span>
<br>
Note: When connecting the ultrasonic sensor keep in mind that we will be working on the opposite side of the breadboard (The one facing away from the voltage regulator). So whenever the connection is represented at the top that is actually the opposite side to the voltage regulator circuit.
<br><br>
</p>
<center>
<div class="col-md-5">
<img src="assets/images/ultra1.png" style="border-radius: 30px; display: inline-block;" >
</div>
</center>
<p style="color: white; font-size: large;"> <span class="tab"></span>
<br>
With this final addition the whole electronic circuit for Satsuma is finished. So now we can move on to coding Satsuma with the pico.
<br>
</p>
<!-- -->
<a > <h3 style=" color: deepskyblue; text-align: left; padding-top: 30px;" class="featurette-heading ">
<span class="tab"></span>
<br>
4)Coding “Satsuma”</a>
</h3>
<p style="color: whitesmoke;">
<br>
First we’ll start by importing our libraries: <br>
<br>
</p>
<div style="width:auto;height:60;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
<b>from</b> adafruit_servokit <b>import</b> ServoKit
<b>import</b> board
<b>import</b> busio
<b>import</b> adafruit_pca9685 <b>import</b> PCA9685
<b>import</b> time
<b>import</b> adafruit_hcsr04
</pre>
</div>
<p style="color: whitesmoke;">
<br>
There are some new libraries here that we need to get familiar with. Do you remember all the code you had to write to move your servos and the calculations to get distance from an ultrasonic sensor, well now you won’t have to write those long pieces of code everytime thanks to these libraries: adafruit_servokit, and adafruit_hcsr04. These libraries have methods that we will be using to more conveniently access features such as moving servos through certain degrees and getting distance from our ultrasonic sensors. Additionally we will also be using the busio library to get the I2C input/output from our pico and communicate with the pca9685, which also has a library that we can use. Next we will set get I2C bus to be able communicate with our connected I2C devive, the pca9685.
<br>
<br>
</p>
<div style="width:auto;height:auto;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
i2c_bus = busio.I2C(scl=board.GP1, sda=board.GP0)
</code>
</pre>
</div>
<br>
<p style="color: whitesmoke;">
Here we have created a reference to an I2C address, specifically the one we have connected our servo board to, which we will later on use to communicate with the device connected at that address. By utilizing this address we will create a pca object from the pca9685 library and set the frequencies of output to match our servos.
<br><br>
</p>
<div style="width:auto;height:auto;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
pca = PCA9685(i2c_bus)
pca.frequency = <a style="color: darkred">50</a>
</code>
</pre>
</div>
<br><br>
<p style="color: whitesmoke;">
Here we have created a reference to an I2C address, specifically the one we have connected our servo board to, which we will later on use to communicate with the device connected at that address. By utilizing this address we will create a pca object from the pca9685 library and set the frequencies of output to match our servos.
<br><br>
</p>
<div style="width:auto;height:auto;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
kit = ServoKit(channels=<a style="color: darkred">16</a>, i2c=i2c_bus)
</code>
</pre>
</div>
<br>
<p style="color: whitesmoke;">
This library has been configured to work with many servo drivers, including the pca9685! But to be able to use the methods of this library we need to create its object with the amount of channels the servo driver has and the I2C address. Next we need to create our ultrasonic sensor object to gauge the distance with this libraries code.
<br><br>
</p>
<div style="width:auto;height:auto;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.GP17, echo_pin=board.GP16)
</code>
</pre>
</div>
<br>
<p style="color: whitesmoke;">
If you have placed the trig and echo pins at different pins please specify accordingly in your own code. Also remember that when referring to GP pins while coding, we always use the digital address instead of the physical placement. Now we will create the main loop of our algorithm:
<br>
<br>
</p>
<div style="width:auto;height:auto;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
<b>while</b> True:
<b>if</b> sonar.distance() >= <a style="color: darkred">50</a>
print(sonar.distance())
</code>
</pre>
</div>
<p style="color: whitesmoke;">
Here we have used the distance method to acquire the distance (in centimeters) calculated by our ultrasonic sensors, which utilizes the same equation we did previously but shortens all of that calculation to a single line! We can also view the output of the sensor thanks to the print statement we have used. With the information in hand when the turtle is away from an object at a safe distance we will start moving the servos with the servokit library. Continuing the code from the last if statement:
<br><br>
</p>
<div style="width:auto;height:auto;background-color: lightgray;">
<pre>
<code style="color: black; padding-left: initial;">
kit.servo[<a style="color: darkred">0</a>.angle = <a style="color: darkred">40</a><a style="color: dimgray;"> #head</a>
kit.servo[<a style="color: darkred">1</a>.angle = <a style="color: darkred">90</a><a style="color: dimgray;"> #left front</a>
kit.servo[<a style="color: darkred">2</a>.angle = <a style="color: darkred">40</a><a style="color: dimgray;"> #right front</a>
kit.servo[<a style="color: darkred">3</a>.angle = <a style="color: darkred">50</a><a style="color: dimgray;"> #right back</a>
kit.servo[<a style="color: darkred">4</a>.angle = <a style="color: darkred">90</a><a style="color: dimgray;"> #left back</a>
time.sleep(<a style="color: darkred">0.5</a>)
kit.servo[<a style="color: darkred">0</a>.angle = <a style="color: darkred">20</a><a style="color: dimgray;"> #head</a>
kit.servo[<a style="color: darkred">1</a>.angle = <a style="color: darkred">110</a><a style="color: dimgray;"> #left front</a>
kit.servo[<a style="color: darkred">2</a>.angle = <a style="color: darkred">20</a><a style="color: dimgray;"> #right front</a>
kit.servo[<a style="color: darkred">3</a>.angle = <a style="color: darkred">90</a><a style="color: dimgray;"> #right back</a>
kit.servo[<a style="color: darkred">4</a>.angle = <a style="color: darkred">50</a><a style="color: dimgray;"> #left back</a>
kit.servo[<a style="color: darkred">0</a>.angle = <a style="color: darkred">60</a><a style="color: dimgray;"> #head</a>
time.sleep(<a style="color: darkred">1</a>)
</code>
</pre>
</div>
<!----- Footer ---- -->
<div class="footer">
<p>Lemon Robotics Kit
<br>Design: <a style="color: orangered;" >HisarCS - İdeaLab .)</a></p>
<p> idealab@hisarschool.k12.tr </p>
<p> 2022 </p>
<!---- footer - -->
</div>
</section>
<!-- Scripts -->
<!-- Bootstrap core JavaScript -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/isotope.min.js"></script>
<script src="assets/js/owl-carousel.js"></script>
<script src="assets/js/lightbox.js"></script>
<script src="assets/js/tabs.js"></script>
<script src="assets/js/isotope.js"></script>
<script src="assets/js/video.js"></script>
<script src="assets/js/slick-slider.js"></script>
<script src="assets/js/custom.js"></script>
<script>
//according to loftblog tut
$('.nav li:first').addClass('active');
var showSection = function showSection(section, isAnimate) {
var
direction = section.replace(/#/, ''),
reqSection = $('.section').filter('[data-section="' + direction + '"]'),
reqSectionPos = reqSection.offset().top - 0;
if (isAnimate) {
$('body, html').animate({
scrollTop: reqSectionPos },
800);
} else {
$('body, html').scrollTop(reqSectionPos);
}
};
var checkSection = function checkSection() {
$('.section').each(function () {
var
$this = $(this),
topEdge = $this.offset().top - 80,
bottomEdge = topEdge + $this.height(),
wScroll = $(window).scrollTop();
if (topEdge < wScroll && bottomEdge > wScroll) {
var
currentId = $this.data('section'),
reqLink = $('a').filter('[href*=\\#' + currentId + ']');
reqLink.closest('li').addClass('active').
siblings().removeClass('active');
}
});
};
$('.main-menu, .responsive-menu, .scroll-to-section').on('click', 'a', function (e) {
e.preventDefault();
showSection($(this).attr('href'), true);
});
$(window).scroll(function () {
checkSection();
});
</script>
</body>
</body>
</html>