[PYTHON] Turn the E-MAX mini servo ES08A II with hardware PWM on RaspberryPi

TL;DR

--Procedure for using E-MAX Mini Servo ES08A II with RaspberryPi Model 3B --Use hardware PWM for stabilization. With software PWM, it trembles.

environment

Connection to GPIO

--It seems that only GPIO 18, Physical 12 can be used for hardware PWM (yellow line).

However, according to the WiringPi manual, there is a condition that only 18 PWM can be used and no analog audio output is used.

Source: https://blog.boochow.com/article/456203751.html

--Connect the black wire to GND and the orange wire to 5V. (There is also an article that it seems better to have a different power supply if possible)

Environmental setting

PIP update


sudo pip install -U pip --proxy="http://<proxy>:<port>/"

wiringpi


sudo pip install wiringpi --proxy="http://<proxy>:<port>/"

Source

Calculation formula for hardware PWM https://hira-hide.hatenablog.com/entry/20170610/1497100926 Most servo motors have 1.5ms instead of 1.4ms in the center http://gomisai.blog75.fc2.com/blog-entry-290.html

** There are individual differences **, so if the servo becomes jerky or suddenly heats up at the maximum angle, it means that the maximum operating angle of the servo has been exceeded, so immediately return it to the neutral position and adjust the angle. Please give me.

servoutil.py


#!/usr/bin/python
# -*- coding: utf-8 -*-

def get_write_value(degree):
    """
Get the value to write from the angle

    Parameters
    ----------
    degree : int
        -90 -> 90,Middle 0

    Returns
    ----------
    write_value : int
        wiringpi.Value to write with pwmWrite
PWM frequency = Base clock frequency of Raspberry Pi PWM / (clock x range)
"How many times do you move around 0 degrees?" Is calculated.
7 when 0 degrees.25%, 90 degrees is 12%、-90 degrees is 2.5%A value that linearly calculates
    """

    #Value check
    if degree > 90:
        degree = 90
    if degree < -90:
        degree = -90

    print(degree)

    #0 for many servo motors such as ES08A II.5ms~2.0 degree is 1 in 4ms.It should be 45ms,
    #Actually 0 degree is 1.It is often 5ms.
    #Adjust based on this point
    edit_degree = degree + 10

    return int((4.75 * edit_degree / 90 + 7.25) * (1024 / 100))

testservo.py


#!/usr/bin/python
# -*- coding: utf-8 -*-

import wiringpi
import sys
import servoutil
 
servo_pin = 18    #Specify pin 18

param = sys.argv
set_degree = int(param[1])
print(set_degree)

wiringpi.wiringPiSetupGpio()    #Above figure pin(BOARD)Mode to pin by number
wiringpi.pinMode(servo_pin, 2)  #Designated as output pin
wiringpi.pwmSetMode(0)          #Specify 0V
wiringpi.pwmSetRange(1024)      #Specify range from 0 to 1024
wiringpi.pwmSetClock(375)       

wiringpi.pwmWrite(servo_pin, servoutil.get_write_value(set_degree))

sudo required to set pinMode

Example


sudo python2 testservo.py 90

Recommended Posts

Turn the E-MAX mini servo ES08A II with hardware PWM on RaspberryPi
Easy installation of OpenCV on RaspberryPi 3+
Turn off Python 2.7 on Debian Stretch
Turn the E-MAX mini servo ES08A II with hardware PWM on RaspberryPi