[PYTHON] Try moving the servo motor with RasPi (180 degree version)

Before

A long time ago, I bought a PI HAT that can control servo motors and even soldered it, but I completely forgot about it. When I was checking the inventory of electronic parts, I found it, so I checked the operation. IMG_3896.JPG

parts

  1. Parts, or RasPi3
  2. Servo motor SG-90
  3. Power supply for motor
  4. Motor controller Adafruit 16 channel PWM / servo HAT for Raspberry Pi

After checking, it was still the current product.

environment

  1. python 3.7.3
  2. adafruit-circuitpython-servokit 1.2.1

What I looked up and what I did

It seems that there are two types of servo motors, one that rotates 180 degrees (which can be said to be 90 degrees left and right) and the other that rotates 360 degrees. If you follow the product information for both the 180 degree rotation type and the 360 degree rotation type, Sample Code I was able to reach (-pi / using-the-python-library), but when I copied the code here, it didn't work as expected. First of all, I thought it was a soldering failure, but even if I change the places where the servo motors are connected (there are 16 places), the movement is the same. Hmmm ...

I don't think it's a problem that the soldering iron is lumpy (Is it a transfer of responsibility!), So I tried changing the parameters a little.

So, the following code seemed to work.

# Adafruit 16-Channel PWM/Servo HAT & Bonnet for Raspberry Pi & SG90 sample
#
# see:
# https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi
#
# test system:
# python 3.7.3
# adafruit-circuitpython-servokit 1.2.1

import time
from adafruit_servokit import ServoKit

# Set channels to the number of servo channels on your kit.
# 8 for FeatherWing, 16 for Shield/HAT/Bonnet.
kit = ServoKit(channels=16)

ch = 1

kit.servo[ch].set_pulse_width_range(500, 2400)

# 0..180 step 30 angle
for v in range(0,181,30):
    print( "value=", v )
    kit.servo[ch].angle = v
    time.sleep(1)

# 0, 180 step 90 angle x 3set
for i in range(1,3):
    for v in range(0,181,90):
        kit.servo[ch].angle = v
        time.sleep(1)

# stop(90 angle)
kit.servo[ch].angle = 90

The point is

kit.servo[ch].set_pulse_width_range(500, 2400)

Then, check the data sheet of the servo motor SG-90, and set it because it seems that the bottom is 0.5ms pulse and the top is 2.4ms pulse. Did. Even with this parameter, I feel that it is a little short of 180 degrees, but for the time being it is good.

reference

  1. https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi
  2. https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi/using-the-python-library
  3. http://akizukidenshi.com/catalog/g/gM-08761/

Recommended Posts

Try moving the servo motor with RasPi (180 degree version)
Try moving the servo motor with RasPi (360 degree round and round version)
Servo motor control with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Try blurring the image with opencv2
Try to separate the background and moving object of the video with OpenCV
Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat
Try using the camera with Python's OpenCV
Install by specifying the version with pip
[Python] Try pydash of the Python version of lodash
Power SG-90 servo motor with raspberry pi
Try rewriting the file with the less command
Try to solve the fizzbuzz problem with Keras
Try the python version of emacs-org parser orgparse
Try to solve the man-machine chart with Python
How to try the friends-of-friends algorithm with pyfof
Follow the AR marker with a 2-axis servo
Try the free version of Progate [Python I]
Get the latest Linux kernel version with Arch Linux