Christmas Information: Kitronik will be closing 24th Dec December 2024, reopening on 2nd of January 2025. See our delivery schedule for more information.

Online Tutorial – Autonomous Robotics Platform for Pico - Using the Servo connections

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 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:


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:


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:


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.
Using the Motors.
Using the Buzzer, Button and Lights.
Using the Line Following Sensors.
Using the Ultrasonic Sensor.
Using the Servo connections.

 

Leave a comment

All comments are moderated before being published