由照片來看.該產品的DEMO有使用樹莓派加上一組電機控制器,但一口氣吃掉26Pin.
還是使用一組L298N. 只要花掉4Pin就好.只要規劃先將 初始,前進,後退,轉彎,停止的規劃定義好
如對應藍芽手把的類比頭可以先設定為
Fordward : Y+ > 0
Backward: Y- < 0
TurnLeft: X- < 0
TurnRight: X+ > 0
如針對L298N定義的話
ENA | IN1 | IN2 | OUT1 | OUT2 | 馬達狀態 |
( ENB ) | ( IN3 ) | ( IN4 ) | ( OUT3 ) | ( OUT4 ) | |
0 | X | X | X | X | 無動力停止 |
1 | 0 | 0 | 快速煞車 | ||
1 | 0 | 1 | 0 | 1 | 正轉 |
1 | 1 | 0 | 1 | 0 | 反轉 |
1 | 1 | 1 | 快速煞車 |
初始,前進,後退,轉彎,停止的定義大致搞定如下
# OUT1/OUT2 for Right Wheel
# OUT3/OUT4 for Left Wheel
Initial:
ENA = 1;ENB = 1;IN1 = 0;IN2 = 0;IN3 = 0;IN4 = 0
Fordward: # Y+ > 0
ENA = 1;ENB = 1;IN1 = 0;IN2 = 1;IN3 = 0;IN4 = 1
Backward: # Y- < 0
ENA = 1;ENB = 1;IN1 = 0;IN2 = 1;IN3 = 0;IN4 = 1
TurnLeft: # X- < 0
ENA = 1;ENB = 1;IN1 = 0;IN2 = 1;IN3 = 1;IN4 = 0
TurnRight: # X+ > 0
ENA = 1;ENB = 1;IN1 = 1;IN2 = 0;IN3 = 0;IN4 = 1
StopMoving: # Y+ = 0 ,Y- = 0 ,X- = 0 ,X+ = 0
ENA = 1;ENB = 1;IN1 = 0;IN2 = 0;IN3 = 0;IN4 = 0
現在可以開始規劃藍芽手把的類比搖桿去控制GPIO了,以下假設使用PS3藍芽手把連線上RPi2
但實際上的行動方向 應該會有出問題,因為接線的方式可能會造成左右 前後顛倒.
#!/usr/bin/python import RPi.GPIO as GPIO import time import subprocess import pygame # Use physical pin numbers GPIO.setmode(GPIO.BOARD) # Set up header 6 GPIO pin as an input #PinENA = 36 # GPIO16 as ENA #PinENB = 38 # GPIO20 as ENB PinIN1 = 31 # GPIO6 as IN1 PinIN2 = 33 # GPIO13 as IN2 PinIN3 = 35 # GPIO19 as IN3 PinIN4 = 37 # GPIO26 as IN4 #GPIO.setup(PinENA, GPIO.OUT) #GPIO.setup(PinENB, GPIO.OUT) GPIO.setup(PinIN1, GPIO.OUT) GPIO.setup(PinIN2, GPIO.OUT) GPIO.setup(PinIN3, GPIO.OUT) GPIO.setup(PinIN4, GPIO.OUT) def InitialMoto(): # GPIO.output(PinENA, True) # GPIO.output(PinENB, True) GPIO.output(PinIN1, False) GPIO.output(PinIN2, False) GPIO.output(PinIN3, False) GPIO.output(PinIN4, False) def Fordward(): # GPIO.output(PinENA, True) # GPIO.output(PinENB, True) GPIO.output(PinIN1, False) GPIO.output(PinIN2, True) GPIO.output(PinIN3, False) GPIO.output(PinIN4, True) def Backward(): # GPIO.output(PinENA, True) # GPIO.output(PinENB, True) GPIO.output(PinIN1, True) GPIO.output(PinIN2, False) GPIO.output(PinIN3, True) GPIO.output(PinIN4, False) def TurnLeft(): # GPIO.output(PinENA, True) # GPIO.output(PinENB, True) GPIO.output(PinIN1, False) GPIO.output(PinIN2, True) GPIO.output(PinIN3, True) GPIO.output(PinIN4, False) def TurnRight(): # GPIO.output(PinENA, True) # GPIO.output(PinENB, True) GPIO.output(PinIN1, True) GPIO.output(PinIN2, False) GPIO.output(PinIN3, False) GPIO.output(PinIN4, True) def StopMoving(): # GPIO.output(PinENA, True) # GPIO.output(PinENB, True) GPIO.output(PinIN1, False) GPIO.output(PinIN2, False) GPIO.output(PinIN3, False) GPIO.output(PinIN4, False) pygame.init() ps3 = pygame.joystick.Joystick(0) ps3.init()0 print(“Start Moto") InitialMoto() while True: pygame.event.pump() # buttons = [ps3.get_button(0),ps3.get_button(1),ps3.get_button(2),ps3.get_button(3),ps3.get_button(4),ps3.get_button(5),ps3.get_button(6),ps3.get_button(7),ps3.get_button(8),ps3.get_button(9),ps3.get_button(10),ps3.get_button(11),ps3.get_button(12),ps3.get_button(13),ps3.get_button(14),ps3.get_button(15),ps3.get_button(16)] # axis = [ps3.get_axis(0),ps3.get_axis(1),ps3.get_axis(2),ps3.get_axis(3)] # print(buttons) # print(axis) if ps3.get_axis(0) < 0 # TurnLeft TurnLeft() if ps3.get_axis(0) > 0 # TurnRight TurnRight() if ps3.get_axis(1) > 0 # Fordward Fordward() if ps3.get_axis(1) < 0 # Backward Backward() if ps3.get_axis(0) = 0 & ps3.get_axis(1) == 0 # StopMoving StopMoving() subprocess.call("clear")
沒有留言:
張貼留言