Skip to Content
Learn Robotics Programming
book

Learn Robotics Programming

by Danny Staple
November 2018
Beginner
472 pages
13h 5m
English
Packt Publishing
Content preview from Learn Robotics Programming

Code for turning a servo

We'll write some test code that lets the user type a value in degrees and make the servo move to it. The library we are already using for the robot has a PWM module. We start our test code by importing the PWM module and creating the object to work with it. Note we must specify the address here—it's the same I2C device as we are using for the motors, and has the same address. This code is located in the servo_type_position.py file:

from Raspi_MotorHAT.Raspi_PWM_Servo_Driver import PWMimport atexitpwm = PWM(0x6f)# This sets the timebase for it allpwm_frequency = 60pwm.setPWMFreq(pwm_frequency)

Observe that we have atexit again, so we can ensure the controller stops signalling the motor. The servo works in cycles at ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn Robotics Programming - Second Edition

Learn Robotics Programming - Second Edition

Danny Staple

Publisher Resources

ISBN: 9781789340747Supplemental Content