-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPs3RaspiRobot.py
More file actions
164 lines (148 loc) · 4.93 KB
/
Ps3RaspiRobot.py
File metadata and controls
164 lines (148 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# coding: Latin-1
# Load library functions we want
import time
import pygame
import RPi.GPIO as GPIO
import turtle # to draw the drawing along with Robot
from turtle import *
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set which GPIO pins the drive outputs are connected to
DRIVE_1 = 17
DRIVE_2 = 26
DRIVE_3 = 6
DRIVE_4 = 27
# Set all of the drive pins as output pins
GPIO.setup(DRIVE_1, GPIO.OUT)
GPIO.setup(DRIVE_2, GPIO.OUT)
GPIO.setup(DRIVE_3, GPIO.OUT)
GPIO.setup(DRIVE_4, GPIO.OUT)
# Function to set all drives off
def MotorOff():
GPIO.output(DRIVE_1, GPIO.LOW)
GPIO.output(DRIVE_2, GPIO.LOW)
GPIO.output(DRIVE_3, GPIO.LOW)
GPIO.output(DRIVE_4, GPIO.LOW)
Button = 2
#setup turtle screen
turtle.setup(800,600)
wn = turtle.Screen()
wn.title("Guru's Turtle Robot!")
t = turtle.Turtle('turtle') # choose your own icons have picked turtle
# Settings for JoyBorg
leftDrive = DRIVE_1 # Drive number for left motor
rightDrive = DRIVE_4 # Drive number for right motor
axisUpDown = 1 # Joystick axis to read for up / down position
axisUpDownInverted = False # Set this to True if up and down appear to be swapped
axisLeftRight = 3 # Joystick axis to read for left / right position
axisLeftRightInverted = False # Set this to True if left and right appear to be swapped
interval = 0.1 # Time between keyboard updates in seconds, smaller responds faster but uses more processor time
# Setup pygame and key states
global hadEvent
global moveUp
global moveDown
global moveLeft
global moveRight
global moveQuit
hadEvent = True
moveUp = False
moveDown = False
moveLeft = False
moveRight = False
moveQuit = False
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
# Function to handle pygame events
def PygameHandler(events):
# Variables accessible outside this function
global hadEvent
global moveUp
global moveDown
global moveLeft
global moveRight
global moveQuit
# Handle each event individually
for event in events:
if event.type == pygame.QUIT:
# User exit
hadEvent = True
moveQuit = True
elif event.type == pygame.KEYDOWN:
# A key has been pressed, see if it is one we want
hadEvent = True
if event.key == pygame.K_ESCAPE:
moveQuit = True
elif event.type == pygame.KEYUP:
# A key has been released, see if it is one we want
hadEvent = True
if event.key == pygame.K_ESCAPE:
moveQuit = False
elif event.type == pygame.JOYAXISMOTION:
# A joystick has been moved, read axis positions (-1 to +1)
hadEvent = True
upDown = joystick.get_axis(axisUpDown)
leftRight = joystick.get_axis(axisLeftRight)
# Invert any axes which are incorrect
if axisUpDownInverted:
upDown = -upDown
if axisLeftRightInverted:
leftRight = -leftRight
# Determine Up / Down values
if upDown < -0.1:
moveUp = True
moveDown = False
t.forward(2)
elif upDown > 0.1:
moveUp = False
moveDown = True
t.backward(2)
else:
moveUp = False
moveDown = False
# Determine Left / Right values
if leftRight < -0.1:
moveLeft = True
moveRight = False
t.left(2)
elif leftRight > 0.1:
moveLeft = False
moveRight = True
t.right(2)
else:
moveLeft = False
moveRight = False
try:
print ('Press [ESC] or Press PS3 O button to quit')
# Loop indefinitely
while True:
# Get the currently pressed keys on the keyboard
PygameHandler(pygame.event.get())
if hadEvent:
# Keys have changed, generate the command list based on keys
hadEvent = False
if moveQuit:
break
elif moveLeft:
leftState = GPIO.LOW
rightState = GPIO.HIGH
elif moveRight:
leftState = GPIO.HIGH
rightState = GPIO.LOW
elif moveUp:
leftState = GPIO.HIGH
rightState = GPIO.HIGH
else:
leftState = GPIO.LOW
rightState = GPIO.LOW
GPIO.output(leftDrive, leftState)
GPIO.output(rightDrive, rightState)
# Wait for the interval period
time.sleep(interval)
# Disable all drives
MotorOff()
except KeyboardInterrupt:
# CTRL+C exit, disable all drives
MotorOff()