Last active
September 4, 2016 11:44
-
-
Save RyosukeKamei/dcb2401a284bb2dc10951a6d3159d2fd to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使いサーボモータを動かす ref: http://qiita.com/RyosukeKamei/items/9b15007bf1b77d33764f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git clone https://github.com/RyosukeKamei/raspberrypi3.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo python3 servo_motor.py 90 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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