單按中間滾輪 前進
左右件同鍵 後退
左鍵 左轉
右鍵 右轉想法是直接使用pygame.mouse 但程式碼在shell mode下跑不出預計的效果.
建議在Xwindow下執行
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
import pygame
#from grovepi import *
# Use physical pin numbers
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
# Set up header 4 GPIO pin as an input
PinIN1 = 31 # GPIO6 as IN1
PinIN2 = 33 # GPIO13 as IN2
PinIN3 = 35 # GPIO19 as IN3
PinIN4 = 37 # GPIO26 as IN4
GPIO.setup(PinIN1, GPIO.OUT)
GPIO.setup(PinIN2, GPIO.OUT)
GPIO.setup(PinIN3, GPIO.OUT)
GPIO.setup(PinIN4, GPIO.OUT)
# Connect the Grove Button to digital port D3
#button = 4
#grovepi.pinMode(button,"INPUT")
def InitialMoto():
GPIO.output(PinIN1, False)
GPIO.output(PinIN2, False)
GPIO.output(PinIN3, False)
GPIO.output(PinIN4, False)
def TurnRight():
GPIO.output(PinIN1, False)
GPIO.output(PinIN2, True)
GPIO.output(PinIN3, False)
GPIO.output(PinIN4, True)
def TurnLeft():
GPIO.output(PinIN1, True)
GPIO.output(PinIN2, False)
GPIO.output(PinIN3, True)
GPIO.output(PinIN4, False)
def Backward():
GPIO.output(PinIN1, False)
GPIO.output(PinIN2, True)
GPIO.output(PinIN3, True)
GPIO.output(PinIN4, False)
def Fordward():
GPIO.output(PinIN1, True)
GPIO.output(PinIN2, False)
GPIO.output(PinIN3, False)
GPIO.output(PinIN4, True)
def StopMoving():
GPIO.output(PinIN1, False)
GPIO.output(PinIN2, False)
GPIO.output(PinIN3, False)
GPIO.output(PinIN4, False)
pygame.init()
pygame.display.set_caption('Mouse Contral Area')
size= [1024, 768]
screen= pygame.display.set_mode(size)
print('Start Moto')
InitialMoto()
while True:
pygame.event.get()
if pygame.mouse.get_pressed() == (0,0,0):# Stop Moving
StopMoving()
if pygame.mouse.get_pressed() == (1,0,1): # Backward
Backward()
if pygame.mouse.get_pressed()[1] == 1: # Fordward
Fordward()
if pygame.mouse.get_pressed() == (0,0,1): # Turn Right
TurnRight()
if pygame.mouse.get_pressed() == (1,0,0): # Turn Left
TurnLeft()
subprocess.call("clear")
沒有留言:
張貼留言