Skip to content

Instantly share code, notes, and snippets.

@RyosukeKamei
Last active September 4, 2016 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyosukeKamei/dcb2401a284bb2dc10951a6d3159d2fd to your computer and use it in GitHub Desktop.
Save RyosukeKamei/dcb2401a284bb2dc10951a6d3159d2fd to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使いサーボモータを動かす ref: http://qiita.com/RyosukeKamei/items/9b15007bf1b77d33764f
$ git clone https://github.com/RyosukeKamei/raspberrypi3.git
$ sudo python3 servo_motor.py 90
# GPIOを制御するライブラリ
import wiringpi
# タイマーのライブラリ
import time
# 引数取得
import sys
# サーボモータに接続したGPIO端子番号を指定
servo_pin = 18
# サーボモータを動かす角度を指定する
# set_degree = 90 デフォルト
# 引数から値を受け取る
param = sys.argv
set_degree = int(param[1])
print(set_degree)
wiringpi.wiringPiSetupGpio()
# ハードウェアPWMで出力する
wiringpi.pinMode( servo_pin, 2 )
# サーボモータに合わせたPWM波形の設定
wiringpi.pwmSetMode(0)
wiringpi.pwmSetRange(1024)
wiringpi.pwmSetClock(375)
# 指定した角度が動作範囲内の場合のみサーボモータを制御
if ( set_degree = -90 ):
# 角度から送り出すPWMのパルス幅を算出する
move_deg = int( 81 + 41 / 90 * set_degree )
# サーボモータにPWMを送り、サーボモータを動かす
wiringpi.pwmWrite( servo_pin, move_deg )
$ sudo python3 servo_motor.py {設定したい角度 -90から90まで}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment