Maker.io main logo

Autonomous Robotics Platform for Pico - Using the Servo Connections

2022-05-12 | By Kitronik Maker

License: See Original Project

Courtesy of Kitronik

Guide by Kitronik Maker

The ARP has 4 servo connections. These can be used to control standard hobby ‎servos. ‎

The servo connectors are located 2 towards the rear of the ARP, just outside the ‎motors, and 2 towards the front, by the ultrasonic connector. Power is fed, via the ‎power switch, directly from the battery voltage. ‎

The connectors are standard 3 pin, Signal, Voltage, and GND connections for ‎usual hobby servos. A brief guide to servos gives an overview and more ‎information about what servos are and how they are controlled.‎

PIO and Servo Control

On the ARP the servo control signal is driven using the Pico's Programmable Input ‎Output (PIO) functionality. ‎

PIO is a facility that allows the Pico's processor to control things using a very ‎simple state machine processor.‎

When the PicoAutonomousRobotics class is instantiated, it sets up the PIO state ‎machines ready for use.‎

The PicoAutonomousRobotics class defines 2 methods that control the connection ‎of the servos - registerServo(...), and deregisterServo(...)‎

registerServo(...) is called automatically for all servos when the class is ‎instantiated, but if you wish to use the servo pins for another purpose you can call ‎deregisterServo(...), which will stop the state machine running so the pin can be ‎driven as normal GPIO.‎

It is not necessary to understand PIO to use the servos on the ARP, but if you wish ‎to then start with this explainer from Raspberry Pi. ‎

Make Some Movement

Plug a servo into the SV0 connector (near the On/Off switch) and create the ‎following code in Thonny:

Copy Code
from PicoAutonomousRobotics import KitronikPicoRobotBuggy

buggy = KitronikPicoRobotBuggy()

while True:
if buggy.button.value():
buggy.goToPosition(0,180)
else:
buggy.goToPosition(0,0)

Save and Run the Code

When you press the button on the Pico the servo should move from one end of its ‎travel to the other. When you release the button, the servo will move back again. ‎

Sensor Control

You can also control the servos using the ARP sensors.‎

Ensure the front ultrasonic sensor is mounted and create the following code in ‎Thonny:‎

Copy Code
from PicoAutonomousRobotics import KitronikPicoRobotBuggy

buggy = KitronikPicoRobotBuggy()

while True:
#multiply distance by 2 so 180 degrees range occurs over roughly a meter
Dist = buggy.getDistance("f") * 2
#check to make sure we are not driving past the end of a servo range
if(Dist>180):
Dist = 180
#drive the servo
buggy.goToPosition(0, Dist)

Run the Code

This code will move the servo arm in proportion to the distance sensed by the ‎ultrasonic sensor. As the sensor detects an object getting closer the servo will ‎move towards its 0 degree end, and as it gets further away the servo will move ‎towards the 180 degree end. The code multiplies the distance detected by 2 so that ‎the full range of servo movement only takes 90 cm of detection distance.‎

Multiple Servos

The ARP can drive up to 4 servos at once.

‎This variation of the first piece of code moves each servo in turn as the button is ‎pressed and released:‎

Copy Code
from PicoAutonomousRobotics import KitronikPicoRobotBuggy
from utime import sleep

buggy = KitronikPicoRobotBuggy()

while True:
if buggy.button.value():
buggy.goToPosition(0,180)
buggy.goToPosition(1,180)
buggy.goToPosition(2,180)
buggy.goToPosition(3,180)
else:
buggy.goToPosition(0,0)
buggy.goToPosition(1,0)
buggy.goToPosition(2,0)
buggy.goToPosition(3,0)

Controlling multiple servos allows the ARP to be used for more complex projects.‎

Can you build a Robot arm?‎‎ ‎

Coding Resources for Pico-ARP:‎

Online Tutorials - Pico ARP

©Kitronik Ltd – You may print this page & link to it but must not copy the page or part thereof ‎without Kitronik's prior written consent.‎

制造商零件编号 5338
KITRONIK AUTONOMOUS ROBOTICS PLA
Kitronik Ltd.
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum