2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ラズパイでServo Driver HATを使ってみる。

Last updated at Posted at 2020-03-20

Raspberry Pi 4とubuntu、ROS2の組み合わせで遊んでみようとAmazonでPWM Servo Motor Driverを購入しました。

Servo Driver HAT仕様

仕様

電源 6V~12V(VIN端子)
サーボ電圧 5V
ロジック電圧 3.3V
ドライバー PCA9685
制御 I/F I2C
寸法 65.0mmx36.0mm
取付穴サイズ 3.0mm

PIN仕様

白枠で囲んだところ
hat.jpg

PIN 色
GND(サーボモータの茶色と接続)
5V VCC
PWMの信号線

モータ

茶色の線をGNDに接続
mon1.jpg

こちらは茶色がなくて黒をGNDに接続
mon2.jpg

I2Cを有効にする

ubuntuなのでraspi-configが使えないので、/boot/firmware/config.txtを直接書き換えます。

/dev/firmware/config.txtの末尾に以下を追記。

/boot/firmware/config.txt
dtparam=i2c_arm=on

/etc/modulesの末尾に以下を追記。

i2c-dev
i2c-bcm2708

再起動

$ sudo reboot

再起動後にi2cが有効になっているか確認

$ dmesg | grep i2c
[    2.824095] i2c /dev entries driver
$ lsmod | grep i2c
i2c_bcm2708            20480  0

ツールをインストール

$ sudo apt install i2c-tools
$ sudo apt install python3-smbus

i2cが有効かどうかの確認

$ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --

PythonからPCA9685を使えるようにする。

Adafruit Python PCA9685からライブラリとサンプルを取得

pipがインストールされていなかったので、先にpipをインストール

$ sudo apt install python-pip python3-pip

次にPCA9685用のライブラリをインストール

$ sudo pip install adafruit-pca9685

FS90Rを使用したの場合

今回はFS90Rを0番に繋いで以下のようなサンプルを動作させてみたところ、モーターがon→offした。

sample.py
# !/usr/bin/python

import Adafruit_PCA9685
import time

pwm = Adafruit_PCA9685.PCA9685()

pwm.set_pwm_freq(60)

pwm.set_pwm(0, 0, 1)

time.sleep(1)

pwm.set_pwm(0, 0, 0)

set_pwmの最初に引数は何番目に接続しているかで、2,3番目はもう少し勉強しないと…。

SG92Rを使用する場合

PCA9685は、パルス150~650が角度0~180に対応しているので以下の式を使用してパルスを出す。

pulse = ( 650 - 150 ) / 180 * 角度 + 150
sample.py
# !/usr/bin/python

import Adafruit_PCA9685
import time

pwm = Adafruit_PCA9685.PCA9685()
pwm.set_pwm_freq(60)
# 180度
pwm.set_pwm(0, 0, 650)
time.sleep(1)
# 90度
pwm.set_pwm(0, 0, 400)
time.sleep(1)
# 0度
pwm.set_pwm(0, 0, 150)

FS90R(追記)

360度サーボモータは、角度を90を基準に0~180の間で変化させることで、回転方向やスピードをコントロールするらしいので、以下のようなサンプルを作ってみました。

sample.py
# !/usr/bin/python

import Adafruit_PCA9685
import time

pwm = Adafruit_PCA9685.PCA9685()
pwm.set_pwm_freq(60)
# 180度
pwm.set_pwm(0, 0, 650)
time.sleep(1)
# 0度
pwm.set_pwm(0, 0, 150)
time.sleep(1)
# 90度
pwm.set_pwm(0, 0, 400)

を実行してみたら、0度と180度では、同じ速度で逆回転になりました。
でも、90度だと思っていた400では停止しなかった...。
でも372~382くらいだと止まるんだよなー。

もういっこ買ってきて試したら375では止まらなくて380で止まった。
個体差?
パルスは、130~630の間で380くらいで停止が良い感じかなぁ。

秋月のPCA9685の基板の取説にはパルス150~600になっていた。これだと375はちょうど真ん中の値なんだよなー。
http://akizukidenshi.com/download/ds/akizuki/AE-PCA9685.pdf

FS90Rとmicro:bit

micro:bitのサーボドライバを使って以下のようなサンプルを動作させたところ90度で止まらなかったので、やっぱりずれていそう。

sample.js
input.onButtonPressed(Button.A, function () {
    kitronik_i2c_16_servo.servoWrite(kitronik_i2c_16_servo.Servos.Servo1, 0)
})
input.onButtonPressed(Button.AB, function () {
    kitronik_i2c_16_servo.servoWrite(kitronik_i2c_16_servo.Servos.Servo1, 90)
})
input.onButtonPressed(Button.B, function () {
    kitronik_i2c_16_servo.servoWrite(kitronik_i2c_16_servo.Servos.Servo1, 180)
})
2
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?