Skip to content

Instantly share code, notes, and snippets.

@takanakahiko
Last active October 2, 2017 09:47
Show Gist options
  • Save takanakahiko/61e0564d2ef711764432a7f73d1bfceb to your computer and use it in GitHub Desktop.
Save takanakahiko/61e0564d2ef711764432a7f73d1bfceb to your computer and use it in GitHub Desktop.
ラズパイ走らせるアレ

これは何

pythonでモータドライバ経由でモータ制御するやつ

クラスTa7291について

コンストラクタ

Ta7291をつないでいるピンを指定する

drive

引数の速さで回転。 逆転時は負数を指定。

brake

ブレーキ、急停止したいときつかう

ピンの配置について

以下画像を参照されたし

http://lumenbolk.com/wp-content/uploads/2014/08/61.jpg

#!/usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import sys
class Ta7291:
'''
Motor driver TA7291 class
'''
def __init__(self, pwm_pin, input_pin1, input_pin2):
GPIO.setmode(GPIO.BCM)
GPIO.setup(pwm_pin, GPIO.OUT)
GPIO.setup(input_pin1, GPIO.OUT)
GPIO.setup(input_pin2, GPIO.OUT)
self.in1 = input_pin1
self.in2 = input_pin2
self.pwm = GPIO.PWM(18, 50)
def drive(self, speed):
'''
driving motor
speed : from -100 to 100
'''
isForward = speed > 0
output1 = 1 if isForward else 0
output2 = 0 if isForward else 1
GPIO.output(self.in1, output1)
GPIO.output(self.in2, output2)
self.pwm.start(abs(speed))
def brake(self):
'''
set motor to init position
'''
GPIO.output(self.in1, 1)
GPIO.output(self.in2, 1)
time.sleep(0.5)
def cleanup(self):
'''
cleanning GPIO
'''
self.brake()
GPIO.cleanup()
if __name__ == "__main__":
pass
#!/usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import sys
import Ta7291
if __name__ == "__main__":
d = Ta7291.Ta7291(18, 24, 25)
print "Speed up"
for power in range(0, 100, 10):
d.drive(power)
time.sleep(0.3)
time.sleep(1)
print "Speed down"
for power in range(100, 0, -10):
d.drive(power)
time.sleep(0.3)
time.sleep(1)
print "Stop"
d.drive(0)
time.sleep(1)
print "Drive, and brake..."
d.drive(100)
time.sleep(3)
d.brake()
time.sleep(1)
print "Cleanup in end of execution"
d.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment