ラズパイでモーターを制御するプログラムをつくろうとしています。
ラズパイ3 model B モータードライバーはL293Dを使っています。
まず、下のプログラムでは問題なくモーターが動きました。
Python
1import RPi.GPIO as GPIO 2from time import sleep 3 4# Set up pins 5MotorPin1 = 17 6MotorPin2 = 27 7MotorEnable = 22 8 9# ~ GPIO.setmode(GPIO.BOARD) 10GPIO.setmode(GPIO.BCM) 11 12GPIO.setup(MotorPin1, GPIO.OUT) 13GPIO.setup(MotorPin2, GPIO.OUT) 14GPIO.setup(MotorEnable, GPIO.OUT) 15 16pwm = GPIO.PWM(MotorEnable, 100) 17 18#dutycycle 0% 矩形波を出力 19pwm.start(0) 20 21try: 22 while True: 23 dcycle = 100 24 # Set direction Clockwise 25 GPIO.output(MotorPin1, True) 26 GPIO.output(MotorPin2, False) 27 pwm.ChangeDutyCycle(dcycle) 28 GPIO.output(MotorEnable, True) 29 sleep(2) 30 GPIO.output(MotorEnable, False) 31 32 dcycle = 50 33 # Set direction Counterclockwise 34 GPIO.output(MotorPin1, False) 35 GPIO.output(MotorPin2, True) 36 pwm.ChangeDutyCycle(dcycle) 37 GPIO.output(MotorEnable, True) 38 sleep(2) 39 GPIO.output(MotorEnable, False) 40 41 42 pwm.stop() 43 GPIO.cleanup() 44 45except KeyboardInterrupt: 46 pwm.stop() 47 GPIO.cleanup() 48 49 print("KeyboardInterrupt") 50 pass
多分何度も使う事になるだろうからと、このモーター制御部をclassにしようとして失敗しています。下記のようにclass化したのですが、まったくモーターが動きません。
Python
1import RPi.GPIO as GPIO 2from time import sleep 3 4 5class motor_with_L293D: 6 def __init__(self): 7 self.Motor1Pin1 = 17 8 self.Motor1Pin2 = 27 9 self.Motor1Enable = 22 10 11 def setup(self): 12 GPIO.setmode(GPIO.BCM) 13 # Set pins to output 14 GPIO.setup(self.Motor1Pin1, GPIO.OUT) 15 GPIO.setup(self.Motor1Pin2, GPIO.OUT) 16 GPIO.setup(self.Motor1Enable, GPIO.OUT, initial=False) 17 18 19 def motor1(self, direction, dutycycle1=100): 20 21 pwm1 = GPIO.PWM(self.Motor1Enable, 100) 22 pwm1.start(0) 23 24 if direction == 0: 25 GPIO.output(self.Motor1Enable, False) 26 print("Stop") 27 28 elif direction == 1: 29 GPIO.output(self.Motor1Pin1, True) 30 GPIO.output(self.Motor1Pin2, False) 31 pwm1.ChangeDutyCycle(dutycycle1) 32 GPIO.output(self.Motor1Enable, True) 33 print("Clockwise, dutycycle:", dutycycle1) 34 35 elif direction == -1: 36 GPIO.output(self.Motor1Pin1, False) 37 GPIO.output(self.Motor1Pin2, True) 38 pwm1.ChangeDutyCycle(dutycycle1) 39 GPIO.output(self.Motor1Enable, True) 40 print("CounterClockwise, dutycycle:", dutycycle1) 41 42 43if __name__ == '__main__': 44 45 motor = motor_with_L293D() 46 motor.setup() 47 48 try: 49 while True: 50 51 motor.motor1(1, 100) 52 sleep(5) 53 motor.motor1(0) 54 sleep(1) 55 motor.motor1(-1, 100) 56 sleep(5) 57 motor.motor1(0) 58 59 60 except KeyboardInterrupt: 61 GPIO.cleanup() 62
コードを見た目だけで比べると
pwm.start(0)が無限ループ内、外にある、などの違いがあるようです。
このあたりの違いは意図したもの(違っても問題ない)でしょうか。
仕込んであるprintの表示は出るのですか? (多分出ているのだと思うけど)
もとのプログラムでもやっているので「動かない」原因ではありませんが、
PWMに設定したピンに対して
GPIO.output(MotorEnable, True)
なんてやるとただのGPIO(PWMなし)にオーバーライドされてしまいませんか? (いまRaspberryPiをセットアップしてないので確認できない)
気になるのは、motor1()に入るたびの
pwm1 = GPIO.PWM(self.Motor1Enable, 100)
pwm1.start(0)
の「重ねがけ」ですが、それが問題だとしても「最初の一回」は動くはずだし。
気がついたら線が一本外れていた...なんてないですか? (実際時々あるので)
仕込んであるprintは表示されます。時々前のプログラム動かすとちゃんと動くので配線ではないです。
重ねがけのところ、今晩もう少しいじって見てみます。

回答1件
あなたの回答
tips
プレビュー