LoginSignup
1
1

More than 3 years have passed since last update.

RasPiでサーボモーターを動かしてみる(360度ぐるぐる版)

Last updated at Posted at 2020-05-17

まえおき

サーボモーターSG-90を使うときにも書きましたがずいぶん昔に購入してほったらかしだったPI HATを使ってみたら、どうも思った動きにならなかったので、試行した顛末です。
IMG_3894.JPG

パーツ

  1. RasPi3 / RasPi2
  2. サーボモーター FS90R
  3. モーター用の電源
  4. モーターコントローラ Adafruit 16チャンネル PWM/サーボ HAT for Raspberry Pi

環境

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

調べたこと、やったこと

ひとまず、最初にすることは、製品のページからサンプルコードを確認しました。

説明を超訳すると、1で正回転、-1で逆回転、0.5で半分のパワーで回転できると。
回転を止めるのは0でいいらしい。

ひとまず、全力での回転がどれぐらいかわからないけど、まずは1で回してみる。

kit.continuous_servo[1].throttle = 1

逆回転は、

kit.continuous_servo[1].throttle = -1

ん?とりあえず、止めよう。

kit.continuous_servo[1].throttle = 0

止まらん、、、止まって~~
ひとまず、モーター用の電源を抜こう。

で、以下のコードでいけそうな感じ。

# Adafruit 16-Channel PWM/Servo HAT & Bonnet for Raspberry Pi & FS90R 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

def fs90r_convert(throttle):
    """
    Parameters
    ----------
    throttle : float
      require -1.0 .. 1.0, but no check

    Returns : float
      -0.4(right rotation) <= 0.1(stop) <= 0.6(left rotation)
    """
    return 0.1 + throttle / 2

# left rotation (0.1 .. 1.0(max))
for i in range(1,11):
    v = fs90r_convert(i/10)
    print( "i=%f v=%f" % (i/10, v) )
    kit.continuous_servo[ch].throttle = v
    time.sleep(1)

# right rotation (-0.1 .. -1.0(max))
for i in range(-1,-11,-1):
    v = fs90r_convert(i/10)
    print( "i=%f v=%f" % (i/10, v) )
    kit.continuous_servo[ch].throttle = v
    time.sleep(1)

# stop rotation
kit.continuous_servo[ch].throttle = fs90r_convert(0)
time.sleep(1)

# left rotation, max throttle
kit.continuous_servo[ch].throttle = fs90r_convert(1)
time.sleep(1)

# right rotation, half throttle
kit.continuous_servo[ch].throttle = fs90r_convert(-0.5)
time.sleep(1)

# stop rotation
kit.continuous_servo[ch].throttle = fs90r_convert(0)

ポイントは

def fs90r_convert(throttle):
    return 0.1 + throttle / 2

で、FS90R用の換算を挟みました。どうも、0では停止せず、0.1で停止するようです。
引数は、サンプルプログラムで解説されている1から-1の範囲を与えて、正回転、逆回転、0で停止できます。

参考

  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-13206/
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1