Pi – サーボモーター

昨年、デジタル・マイクロサーボ SG90 と2軸カメラマウントで試した時の記録
ここを参考に、一部変更してます。

重心の関係で、カメラの取付向きを逆に取付。
( 仰角が制限されますが自分の場合は問題ないので )

wiringpi は使用せず webiopi で制御
GPIO は、12,13 を使用

servo.py
上下左右、初期位置は環境に合わせて要変更

import webiopi
GPIO = webiopi.GPIO
SERVO_PAN  = 12
SERVO_TILT = 13
DEFAULT_PAN  = 0
DEFAULT_TILT = -30
CURRENT_PAN  = 0
CURRENT_TILT = -30
PAN_MAX  = 70
PAN_MIN  = -70
TILT_MAX = 10
TILT_MIN = -70
PAN_UNIT  = 10
TILT_UNIT = 10
def setup():
  GPIO.setFunction(SERVO_PAN,  GPIO.PWM)
  GPIO.setFunction(SERVO_TILT, GPIO.PWM)
  GPIO.pulseAngle(SERVO_PAN,  DEFAULT_PAN)
  GPIO.pulseAngle(SERVO_TILT, DEFAULT_TILT)
  webiopi.sleep(0.5)
  GPIO.pulseRatio(SERVO_PAN,  0)
  GPIO.pulseRatio(SERVO_TILT, 0)
def loop():
  webiopi.sleep(0.5)
@webiopi.macro
def defaultPosition():
  global CURRENT_PAN
  global CURRENT_TILT
  CURRENT_PAN  = DEFAULT_PAN
  CURRENT_TILT = DEFAULT_TILT
  GPIO.pulseAngle(SERVO_PAN,  DEFAULT_PAN)
  GPIO.pulseAngle(SERVO_TILT, DEFAULT_TILT)
  webiopi.sleep(0.5)
  GPIO.pulseRatio(SERVO_PAN,  0)
  GPIO.pulseRatio(SERVO_TILT, 0)
@webiopi.macro
def right():
  global CURRENT_PAN
  if CURRENT_PAN > PAN_MIN:
    CURRENT_PAN = CURRENT_PAN - PAN_UNIT
    GPIO.pulseAngle(SERVO_PAN, CURRENT_PAN)
    webiopi.sleep(0.5)
    GPIO.pulseRatio(SERVO_PAN, 0)
@webiopi.macro
def left():
  global CURRENT_PAN
  if CURRENT_PAN < PAN_MAX:
    CURRENT_PAN = CURRENT_PAN + PAN_UNIT
    GPIO.pulseAngle(SERVO_PAN, CURRENT_PAN)
    webiopi.sleep(0.5)
    GPIO.pulseRatio(SERVO_PAN, 0)
@webiopi.macro
def up():
  global CURRENT_TILT
  if CURRENT_TILT < TILT_MAX:
    CURRENT_TILT = CURRENT_TILT + TILT_UNIT
    GPIO.pulseAngle(SERVO_TILT, CURRENT_TILT)
    webiopi.sleep(0.5)
    GPIO.pulseRatio(SERVO_TILT, 0)
@webiopi.macro
def down():
  global CURRENT_TILT
  if CURRENT_TILT > TILT_MIN:
    CURRENT_TILT = CURRENT_TILT - TILT_UNIT
    GPIO.pulseAngle(SERVO_TILT, CURRENT_TILT)
    webiopi.sleep(0.5)
    GPIO.pulseRatio(SERVO_TILT, 0)
def destroy():
  GPIO.setup(SERVO_PAN,  GPIO.IN)
  GPIO.setup(SERVO_TILT, GPIO.IN)

index.html
mjpg-streamer は、事前に設定済

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Servo Control</title>
  <script type="text/javascript" src="/webiopi.js"></script>
  <script type="text/javascript">
    webiopi().ready(function(){
      var callBack = function(macro, args, response){
      }
      var defaultBtn = webiopi().createButton("defaultBtn", "戻", function(){
        console.log("JS:Default")
        webiopi().callMacro("defaultPosition",[], callBack);
      });
      $("#control-def").append(defaultBtn);
      var rightBtn = webiopi().createButton("rightBtn", "右", function(){
        console.log("JS:Right")
        webiopi().callMacro("right",[], callBack);
      });
      $("#control-r").append(rightBtn);
      var leftBtn = webiopi().createButton("leftBtn", "左", function(){
        console.log("JS:Left")
        webiopi().callMacro("left",[], callBack);
      });
      $("#control-l").append(leftBtn);
      var upBtn = webiopi().createButton("upBtn", "上", function(){
        console.log("JS:Up")
        webiopi().callMacro("up",[], callBack);
      });
      $("#control-u").append(upBtn);
      var downBtn = webiopi().createButton("downBtn", "下", function(){
        console.log("JS:Down")
        webiopi().callMacro("down",[],callBack);
      });
      $("#control-d").append(downBtn);
      webiopi().refreshGPIO(true);
    });
  </script>
</head>
<body>
<div align="left">
<table border=0 cellspacing="10" cellpadding="0">
  <tbody>
  <tr>
    <td><img width="640" height="480" src="http://[Raspberry Pi-IP]:8080/?action=stream" /></td>
    <td>
	<table border=0 cellspacing="5" cellpadding="0">
	  <tbody>
	  <tr>
	    <td>&nbsp;</td>
	    <td><div id="control-u"></div></td>
	    <td>&nbsp;</td>
	  </tr>
	  <tr>
	    <td><div id="control-l"></div></td>
	    <td><div id="control-def"></div></td>
	    <td><div id="control-r"></div></td>
	  </tr>
	  <tr>
	    <td>&nbsp;</td>
	    <td><div id="control-d"></div></td>
	    <td>&nbsp;</td>
	  </tr>
	  </tbody>
	</table>
    </td>
  </tr>
  </tbody>
</table>
</div>
</body>
</html>

/etc/webiopi/config を適宜変更後、webiopi.service を再起動

ブラウザでの制御画面

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です