Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Working Keyboard controller motor code on Rpi #33

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8b9ecfd
basic bootstrap functions for RPI wheel controller
ArnoldYing Mar 4, 2023
5f55e77
need to test on RPI
ArnoldYing Mar 4, 2023
e8c9e91
need to test on RPI
ArnoldYing Mar 4, 2023
cb463a0
added keyboard controller
ArnoldYing Mar 4, 2023
2c8fab1
added keyboard listener
ArnoldYing Mar 18, 2023
83c4b52
testing
ArnoldYing Mar 18, 2023
15c7a93
testing on rpi
ArnoldYing Mar 18, 2023
16ac313
testing on rpi
ArnoldYing Mar 18, 2023
976cd42
testing on rpi
ArnoldYing Mar 18, 2023
8bd9c4f
testing on rpi
ArnoldYing Mar 18, 2023
162123b
testing on rpi
ArnoldYing Mar 18, 2023
4e0614d
testing on rpi
ArnoldYing Mar 18, 2023
b795114
testing on rpi
ArnoldYing Mar 18, 2023
1d8a2b9
testing on rpi
ArnoldYing Mar 18, 2023
f3d15dc
testing on rpi
ArnoldYing Mar 18, 2023
834a439
testing on rpi
ArnoldYing Mar 18, 2023
3ab9df6
testing on rpi
ArnoldYing Mar 18, 2023
70a9a23
testing on rpi
ArnoldYing Mar 18, 2023
96174be
testing on rpi
ArnoldYing Mar 18, 2023
bed9b35
testing on rpi
ArnoldYing Mar 18, 2023
5052de1
testing on rpi
ArnoldYing Mar 18, 2023
ad2f84e
testing on rpi
ArnoldYing Mar 18, 2023
32b0dc1
testing on rpi
ArnoldYing Mar 18, 2023
dc4d405
testing on rpi
ArnoldYing Mar 18, 2023
fc65a0d
testing on rpi
ArnoldYing Mar 18, 2023
e42dcd0
wheel controller refactoring
ArnoldYing Mar 18, 2023
1682619
debuggin pi
ArnoldYing Apr 1, 2023
9bd1ff5
debugging on py
ArnoldYing Apr 1, 2023
597097e
Progress
ArnoldYing Apr 1, 2023
7dde396
refactored rpi code
ArnoldYing Apr 8, 2023
92550a6
merge refactor changes
ArnoldYing Apr 8, 2023
1e834ac
fixing imports
ArnoldYing Apr 8, 2023
76335ec
fixing imports
ArnoldYing Apr 8, 2023
d7c7b2c
Merge branch 'rpi-debug' of https://github.com/UBCAgroBot/jetson-ros-…
ArnoldYing Apr 8, 2023
67f66b6
working on rpi
ArnoldYing Apr 8, 2023
d367870
merged main
ArnoldYing Apr 8, 2023
04b986a
removed unused folder
ArnoldYing Apr 8, 2023
6fd0624
updated rpi files
ArnoldYing Apr 28, 2023
bd76e2e
added gamepad code
ArnoldYing Apr 28, 2023
8ea8fae
gamepad works on pi
ArnoldYing Apr 28, 2023
3ff617e
added gamepad controller
ArnoldYing Apr 28, 2023
66dfdd7
debugged on rpi
ArnoldYing Apr 28, 2023
f3cc4f9
autopep8 action fixes
ArnoldYing Apr 28, 2023
ce0c794
Merge pull request #34 from UBCAgroBot/autopep8-patches/rpi-debug
ArnoldYing Apr 28, 2023
0cae4b1
improve reset angleg
ArnoldYing Apr 28, 2023
f578723
autopep8
ArnoldYing Apr 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Navigation
69 changes: 69 additions & 0 deletions RaspberryPi/GamePad_Controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import RPi.GPIO as GPIO
from time import sleep, time
from pynput import keyboard
from gamepad import run_gamepad
from constants import *
from rpi_helper import turn_wheels, rotate_wheels, generate_pwm, reset_angle, debug_print, setup_rpi
import signal
import sys


# global variables
current_angle = 0.0
movement_arr = [V_Directions.halted, H_Directions.straight]
pwm_controls = []


def process_movement_turning(movement, turning):
global current_angle
debug_print(movement, turning, "angle ", current_angle)
try:
if movement == V_Directions.forward:
debug_print("Moving forward")
rotate_wheels(DIR_FORWARD, pwm_controls=pwm_controls)
elif movement == V_Directions.backward:
debug_print("Moving backward")
rotate_wheels(DIR_BACKWARD, pwm_controls=pwm_controls)
else:
# stop moving
generate_pwm(0, pwm_controls=pwm_controls)

if turning == H_Directions.left:
if current_angle <= -MAX_ANGLE:
debug_print("Max angle reached")
else:
debug_print("Turning left")
current_angle -= REV_ANGLE
turn_wheels(DIR_LEFT)
elif turning == H_Directions.right:
if current_angle >= MAX_ANGLE:
debug_print("Max angle reached")
else:
debug_print("Turning right")
current_angle += REV_ANGLE
turn_wheels(DIR_RIGHT)
else:
# reset angle
current_angle = reset_angle(current_angle=current_angle)

except AttributeError:
print("Attribute Error")


def handler(signum, frame):
# reset angle
reset_angle(current_angle=current_angle)
# stop moving
generate_pwm(0, pwm_controls=pwm_controls)
sys.exit(0)


signal.signal(signal.SIGINT, handler)


def main():
setup_rpi(pwm_controls=pwm_controls)
run_gamepad(process_movement_turning)


main()
173 changes: 92 additions & 81 deletions RaspberryPi/Keyboard_Controller.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,104 @@
"""
Listens to keyboard presses and sends a string message to the Arduino
Output Format: "XY" where
X = (H = Halt, F = Forward, B = Backward)
Y = (S = Straight, L = Left, R = Right)
To be used in conjunction with the StringReceiver.ino
"""
import time
import RPi.GPIO as GPIO
from time import sleep, time
from pynput import keyboard
import serial
from constants import *
from rpi_helper import turn_wheels, rotate_wheels, generate_pwm, reset_angle, debug_print, setup_rpi
import signal
import sys

rpi_path = '/dev/ttyACM0'
ser = serial.Serial(
port=rpi_path,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter = 0

'''
Debug flag for button press outputs and output messages
0 = no debug(only final string printed), 1 = output message array, 2 = key presses
'''
debug_level = 1
# global variables
current_angle = 0.0
movement_arr = [V_Directions.halted, H_Directions.straight]
pwm_controls = []


def debug_print(output, level=0):
"""
Function that handles debug levels
"""
if debug_level >= level:
print(output)
def run():
global current_angle, movement_arr
movement, turning = movement_arr
debug_print(movement, turning, "angle ", current_angle)
try:
if movement == V_Directions.forward:
debug_print("Moving forward")
rotate_wheels(DIR_FORWARD, pwm_controls=pwm_controls)
elif movement == V_Directions.backward:
debug_print("Moving backward")
rotate_wheels(DIR_BACKWARD, pwm_controls=pwm_controls)
else:
# stop moving
generate_pwm(0, pwm_controls=pwm_controls)

if turning == H_Directions.left:
if current_angle <= -MAX_ANGLE:
debug_print("Max angle reached")
else:
debug_print("Turning left")
current_angle -= REV_ANGLE
turn_wheels(DIR_LEFT)
elif turning == H_Directions.right:
if current_angle >= MAX_ANGLE:
debug_print("Max angle reached")
else:
debug_print("Turning right")
current_angle += REV_ANGLE
turn_wheels(DIR_RIGHT)
else:
# reset angle
current_angle = reset_angle(current_angle=current_angle)

# We will keep track of movement state in this array
send_str_arr = ["H", "S"]
except AttributeError:
print("Attribute Error")

"""
Setup keyboard listener
"""

# testing with keyboard
def setup_keyboard():
def on_press(key):
try:
if key.char == "w":
movement_arr[0] = V_Directions.forward
elif key.char == "s":
movement_arr[0] = V_Directions.backward
elif key.char == "a":
movement_arr[1] = H_Directions.left
elif key.char == "d":
movement_arr[1] = H_Directions.right
except AttributeError:
print(f'special key {key} pressed')

def on_press(key):
try:
debug_print(f'alphanumeric key {key.char} pressed', 2)
if key.char == "w":
send_str_arr[0] = "F"
elif key.char == "s":
send_str_arr[0] = "B"
elif key.char == "a":
send_str_arr[1] = "L"
elif key.char == "d":
send_str_arr[1] = "R"
except AttributeError:
debug_print(f'special key {key} pressed', 2)
def on_release(key):
try:
if key.char == "w" or key.char == "s":
movement_arr[0] = V_Directions.halted
elif key.char == "a" or key.char == "d":
movement_arr[1] = H_Directions.straight
debug_print(f'{key} released')
except AttributeError:
if key == keyboard.Key.esc:
# Stop listener
return False

# Start the keyboard listener in a non-blocking manner
listener = keyboard.Listener(on_press=on_press, on_release=on_release)

def on_release(key):
try:
if key.char == "w":
send_str_arr[0] = "H"
elif key.char == "s":
send_str_arr[0] = "H"
elif key.char == "a":
send_str_arr[1] = "S"
elif key.char == "d":
send_str_arr[1] = "S"
debug_print(f'{key} released', 2)
except AttributeError:
debug_print(f'special key {key} released', 2)
if key == keyboard.Key.esc:
# Stop listener
return False


# Start the keyboard listener in a non-blocking manner
listener = keyboard.Listener(on_press=on_press, on_release=on_release)

listener.start()

# Run the program
while True:
message = send_str_arr.copy()
message.append("\n")
debug_print("".join(message), 0)
message = [ord(c) for c in message]
debug_print(message, 1)
ser.write(message)
time.sleep(1)
pass
listener.start()


def handler(signum, frame):
# reset angle
reset_angle(current_angle=current_angle)
# stop moving
generate_pwm(0, pwm_controls=pwm_controls)
sys.exit(0)


signal.signal(signal.SIGINT, handler)


def main():
setup_rpi(pwm_controls=pwm_controls)
setup_keyboard()
while True:
run()


main()
Loading