以下のpythonプログラムでサーボを動かそうとしたのですが、動きませんでした。webiopiでたてたサーバーにスマホをアクセスしてスマホの傾き角度の値をもとにサーボを動かすプログラムなのですが、サーボが動くようにするにはどう修正を加えればいいのでしょうか?webiopiはしっかりと動いているのが確認できるのであとはpythonプログラムだけだと考えられます。 よろしくお願いします
python
1import webiopi 2import time, os, sys, re, datetime 3 4webiopi.setDebug() 5GPIO = webiopi.GPIO 6PIN = {'R': {'1': 6, '2': 5, 'P': 12}, 'L': {'1': 13, '2': 19, 'P': 18}, 'servo': {'x': 38, 'y': 40}} 7for key, value in PIN.items() : 8 for key2, value2 in value.items() : 9 GPIO.setFunction(value2, GPIO.PWM) 10 if key2 == 'P' : 11 GPIO.pwmWrite(value2, 1.0) 12past, now = {'x': 0, 'y': 0}, {'x': 0, 'y': 0} 13 14def ServoAngle(pin, angle): 15 up = int(1500 + (angle * 350) / 45) 16 down = 20000 - up 17 GPIO.pulseMicro(pin, up, down) 18 19def MotorDrive(in1, in2, state) : 20 GPIO.pwmWrite(in1, state[0]*1.0) 21 GPIO.pwmWrite(in2, state[1]*1.0) 22 23@webiopi.macro 24def changeDriveMode(mode) : 25 webiopi.debug("ChangeDriveMode : %s" % (mode)) 26 if mode == 'Stop' : 27 MotorDrive(PIN['L']['1'], PIN['L']['2'], [0, 0]) 28 MotorDrive(PIN['R']['1'], PIN['R']['2'], [0, 0]) 29 elif mode == 'Forward' : 30 MotorDrive(PIN['L']['1'], PIN['L']['2'], [1, 0]) 31 MotorDrive(PIN['R']['1'], PIN['R']['2'], [1, 0]) 32 elif mode == 'Backward' : 33 MotorDrive(PIN['L']['1'], PIN['L']['2'], [0, 1]) 34 MotorDrive(PIN['R']['1'], PIN['R']['2'], [0, 1]) 35 elif mode == 'CW' : 36 MotorDrive(PIN['L']['1'], PIN['L']['2'], [1, 0]) 37 MotorDrive(PIN['R']['1'], PIN['R']['2'], [0, 1]) 38 elif mode == 'CWW' : 39 MotorDrive(PIN['L']['1'], PIN['L']['2'], [0, 1]) 40 MotorDrive(PIN['R']['1'], PIN['R']['2'], [1, 0]) 41@webiopi.macro 42def moveServo(xAngle, yAngle): 43 global past # モジュール変数に対して、再代入を行っているためglobalに 44 webiopi.debug("moveServoAngle : %s, %s" % (xAngle, yAngle)) 45 xAngle = int(xAngle) 46 yAngle = int(yAngle) 47 now['x'] = (xAngle - past['x'])%360 # 48 now['y'] = (yAngle - past['y'])%180 # xAngle→yAngleに 49 past = {'x': xAngle, 'y': yAngle} 50 ServoAngle(PIN['servo']['x'], now['x']) 51 ServoAngle(PIN['servo']['y'], now['y']) 52@webiopi.macro 53def resetServo() : 54 webiopi.debug("Servo Reset...") 55
途中joyconでモーターを動かすプログラムが入っていますが必要とあらば消すつもりです
作業・修正依頼は質問として非推奨です。 https://teratail.com/help/avoid-asking また、前の質問でも言われていますが、作成したという友人に直接言った方が早いと思われます。 あと、「友人が作った」という割には今まで流れで同じ系統の質問をしている気がしますが。。。?
あなたの回答
tips
プレビュー