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.
After checking, it was still the current product.
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.
Recommended Posts