forked from Nthn9211/Assistive-Technology-Capstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboardSteering.py
76 lines (62 loc) · 2.54 KB
/
keyboardSteering.py
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
# This file is only usable on a Raspberry Pi - it will not work on the current design which uses a NUC and Arduino to generate PWM signals
import pygame
from pygame.locals import *
import RPi.GPIO as GPIO # Import the GPIO library.
import time # Import time library
GPIO.setmode(GPIO.BOARD) # Set Pi to use pin number when referencing GPIO pins.
# Can use GPIO.setmode(GPIO.BCM) instead to use
# Broadcom SOC channel names.
# MBTechWorks.com 2016
# Pulse Width Modulation (PWM) demo to cycle brightness of an LED
GPIO.setup(16, GPIO.OUT) # Set GPIO pin 16 to output mode. Blue wire
GPIO.setup(18, GPIO.OUT) # Set GPIO pin 18 to output mode. Orange wire
linX = GPIO.setup(11, GPIO.IN) # Set GPIO pin 11 to input mode. Coordinate X white wire
angZ = GPIO.setup(13, GPIO.IN) # Set GPIO pin 11 to input mode. Angular Z black wire
bluePin = GPIO.PWM(18, 100) # Initialize PWM on pwmPin 100Hz frequency
orangePin = GPIO.PWM(16, 100) # Initialize PWM on pwmPin 100Hz frequency
bluedc=80 # set blue dc variable to 0 for 0%
orangedc=80
bluePin.start(bluedc)
orangePin.start(orangedc)
bluePin.ChangeDutyCycle(bluedc)
orangePin.ChangeDutyCycle(orangedc)
def display(str):
text = font.render(str, True, (255, 255, 255), (159, 182, 205))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery
screen.blit(text, textRect)
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode( (640,480) )
pygame.display.set_caption('Use Arrow Keys for steering, Escape to exit')
screen.fill((159, 182, 205))
font = pygame.font.Font(None, 17)
num = 0
done = False
while not done:
pygame.event.pump()
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
orangedc = 62.2
orangePin.ChangeDutyCycle(orangedc)
elif keys[K_RIGHT]:
orangedc = 94.5
orangePin.ChangeDutyCycle(orangedc)
else:
orangedc = 78
orangePin.ChangeDutyCycle(orangedc)
if keys[K_UP]:
bluedc = 94.35645
bluePin.ChangeDutyCycle(bluedc)
elif keys[K_DOWN]:
bluedc = 61.5
bluePin.ChangeDutyCycle(bluedc)
else:
bluedc = 78
bluePin.ChangeDutyCycle(bluedc)
if keys[K_ESCAPE]:
done = True
bluePin.stop() # stop PWM
orangePin.stop() # stop PWM
GPIO.cleanup() # resets GPIO ports used back to input modei to use pin number when referencing GPIO pins.