Raspberry Piを使ってモーターを動かすコードを書きました。
以下のように、RPi.GPIOを使って制御するときは問題なくモーターが動くのですが、
Python
1import RPi.GPIO as GPIO 2from time import sleep 3 4# Set up pins 5MotorPin1 = 17 6MotorPin2 = 27 7MotorEnable = 22 8 9GPIO.setmode(GPIO.BCM) 10 11GPIO.setup(MotorPin1, GPIO.OUT) 12GPIO.setup(MotorPin2, GPIO.OUT) 13GPIO.setup(MotorEnable, GPIO.OUT) 14 15pwm = GPIO.PWM(MotorEnable, 100) 16 17##dutycycle 0% 矩形波を出力 18pwm.start(0) 19 20try: 21 while True: 22 #forward 23 dcycle = 100 24 GPIO.output(MotorPin1, True) 25 GPIO.output(MotorPin2, False) 26 pwm.ChangeDutyCycle(dcycle) 27 GPIO.output(MotorEnable, True) 28 sleep(2) 29 30 #stop 31 GPIO.output(MotorPin1, False) 32 GPIO.output(MotorPin2, False) 33 sleep(2) 34 35 #reverse 36 dcycle = 50 37 GPIO.output(MotorPin1, False) 38 GPIO.output(MotorPin2, True) 39 pwm.ChangeDutyCycle(dcycle) 40 GPIO.output(MotorEnable, True) 41 sleep(2) 42 43 #stop 44 GPIO.output(MotorPin1, False) 45 GPIO.output(MotorPin2, False) 46 sleep(2) 47 48 pwm.stop() 49 GPIO.cleanup() 50 51except KeyboardInterrupt: 52 pwm.stop() 53 GPIO.cleanup() 54 print("KeyboardInterrupt") 55
これを下記のようにpigpioに変更したところ、モーターが動かなくなりました。
ただ、動きはしないのですが、モーターを前転あるいは後転させる命令部分でモーターから音とかすかな震動がしているので、命令が行っていないという訳では無いようです。
おそらく、下記のコードのうち、GPIO.set_PWM_dutycycle(MotorEnable, dcycle)がモーターを動かすのに適していない命令なのではないかと思うのですが、どう直して良いか分かりません。
よろしくお願いします。
Python
1import pigpio 2from time import sleep 3 4# Set up pins 5MotorPin1 = 17 6MotorPin2 = 27 7MotorEnable = 22 8 9GPIO = pigpio.pi() 10GPIO.set_mode(MotorPin1, pigpio.OUTPUT) 11GPIO.set_mode(MotorPin2, pigpio.OUTPUT) 12GPIO.set_mode(MotorEnable, pigpio.OUTPUT) 13 14try: 15 while True: 16 #forward 17 dcycle = 100 18 GPIO.write(MotorPin1, 1) 19 GPIO.write(MotorPin2, 0) 20 GPIO.set_PWM_dutycycle(MotorEnable, dcycle) 21 22 sleep(2) 23 24 #stop 25 GPIO.write(MotorPin1, 0) 26 GPIO.write(MotorPin2, 0) 27 sleep(2) 28 29 #reverse 30 dcycle = 50 31 GPIO.write(MotorPin1, 0) 32 GPIO.write(MotorPin2, 1) 33 GPIO.set_PWM_dutycycle(MotorEnable, dcycle) 34 sleep(2) 35 36 #stop 37 GPIO.write(MotorPin1, 0) 38 GPIO.write(MotorPin2, 0) 39 sleep(2) 40 41except KeyboardInterrupt: 42 print("KeyboardInterrupt")
回答1件
あなたの回答
tips
プレビュー