Skip to content

Instantly share code, notes, and snippets.

@pinski1
Created March 17, 2018 12:52
Show Gist options
  • Save pinski1/cc0e7c1daf1a0232b1b0cefe13e41acd to your computer and use it in GitHub Desktop.
Save pinski1/cc0e7c1daf1a0232b1b0cefe13e41acd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
something
https://piwars.org/2018-competition
"""
import sys
import time
import FirstLook
from approxeng.input.selectbinder import ControllerResource
# logging stuff?
""" States """
s_manual = 1
s_speed = 2
s_maze = 4
s_rainbow = 8
s_duck = 16
s_golf = 32
current_state = s_manual
next_state = s_manual
menu_state = s_manual
start_challenge = False
emergency_stop = False
""" Menu """
display_counter = 0
DISPLAY_RATE = 4
def mixer(yaw, throttle, max_power=100):
"""
https://approxeng.github.io/approxeng.input/examples/tiny4wd.html
Mix a pair of joystick axes, returning a pair of wheel speeds. This is where the mapping from
joystick positions to wheel powers is defined, so any changes to how the robot drives should
be made here, everything else is really just plumbing.
:param yaw:
Yaw axis value, ranges from -1.0 to 1.0
:param throttle:
Throttle axis value, ranges from -1.0 to 1.0
:param max_power:
Maximum speed that should be returned from the mixer, defaults to 100
:return:
A pair of power_left, power_right values to send to the motor driver
"""
left = throttle + yaw
right = throttle - yaw
scale = float(max_power) / max(1, abs(left), abs(right))
return int(left * scale), int(right * scale)
def minimal_maze(left, center, right, control):
"""
Autonomous code for simple maze solving
http://piwars.org/2018-competition/challenges/the-minimal-maze/
Approach:
Follow RHS wall until it disappears, including RH turn
Do a fairly serious 180deg turn
Follow LHS wall until both walls disappear, including LH turn
However do not touch either wall at any point
"""
print("Maze")
# wait for button press
return 0, 0
def speed_run(left, center, right, control):
"""
Autonomous code for Straight Line Speed Test
http://piwars.org/2018-competition/challenges/straight-line-speed-test/
Approach:
accelerate to 100% throttle
use distance sensors to keep on centerline
After 8m or if front sensors detects something, stop
"""
print("Speed")
# wait for button press
# 100% to motors
# use range sensors to keep centered
# stop when:
# >8.5m traveled
# object in front
return 0, 0
def rainbow_road(left, center, right, control):
"""
Autonomous code for Somewhere Over The Rainbow
http://piwars.org/2018-competition/challenges/somewhere-over-the-rainbow/
Approach:
Goal is to drive a rounded square
the rounded corners will be <24cm away from the corners
"""
print("Rainbow")
# wait for button press
return 0, 0
def deranged_golf(left, center, right, control):
"""
http://piwars.org/2018-competition/challenges/slightly-deranged-golf/
"""
return 0, 0
def duck_shoot(left, center, right, control):
"""
http://piwars.org/2018-competition/challenges/the-duck-shoot/
"""
return 0, 0
first_look = FirstLook.FirstLook()
while True:
try:
with ControllerResource(dead_zone=0.05, hot_zone=0.05) as joystick:
print('Found a joystick and connected')
print(joystick.controls)
while joystick.connected:
# Get joystick values
left_x, left_y, right_x, right_y = joystick['lx', 'ly', 'rx', 'ry']
#d_left, d_right, d_up, d_down = joystick['dleft', 'dright', 'dup', 'ddown']
#e_stop, c_start = joystick['cross', 'triangle']
# get sensor values
range_left, range_center, range_right = first_look.get_range()
# update menu
joystick.check_presses()
if 'dup' in joystick.presses:
if(menu_state == s_manual):
menu_state = s_speed
elif(menu_state == s_speed):
menu_state = s_maze
elif(menu_state == s_maze):
menu_state = s_rainbow
elif(menu_state == s_rainbow):
menu_state = s_duck
elif(menu_state == s_duck):
menu_state = s_golf
elif(menu_state == s_golf):
menu_state = s_manual
elif 'ddown' in joystick.presses:
if(menu_state == s_manual):
menu_state = s_golf
elif(menu_state == s_golf):
menu_state = s_duck
elif(menu_state == s_duck):
menu_state = s_rainbow
elif(menu_state == s_rainbow):
menu_state = s_maze
elif(menu_state == s_maze):
menu_state = s_speed
elif(menu_state == s_speed):
menu_state = s_manual
if 'dright' in joystick.presses:
next_state = menu_state
if 'triangle' in joystick.presses:
start_challenge = True
else:
start_challenge = False
# update screen
if(display_counter == 0):
display_counter = DISPLAY_RATE - 1
# translate constants to current state
c_state = None
if(current_state == s_manual):
c_state = "Manual"
elif(current_state == s_maze):
c_state = "Minimal Maze"
elif(current_state == s_speed):
c_state = "Speed Run"
elif(current_state == s_rainbow):
c_state = "Rainbow Road"
elif(current_state = s_duck):
c_state = "Duck Shoot")
elif(current_state = s_golf):
c_state = "Deranged Golf")
else:
c_state = "unknown"
# translate constants to next state
m_state = None
if(menu_state == s_manual):
m_state = "Manual"
elif(menu_state == s_maze):
m_state = "Minimal Maze"
elif(menu_state == s_speed):
m_state = "Speed Run"
elif(menu_state == s_rainbow):
m_state = "Rainbow Road"
elif(menu_state = s_duck):
m_state = "Duck Shoot")
elif(menu_state = s_golf):
m_state = "Deranged Golf")
else:
m_state = "unknown"
first_look.update_display(c_state, m_state, right_x, right_y)
else:
display_counter -= 1
# if emergency stop then overide current_state
if 'cross' in joystick.presses:
current_state = 666
# update robot
if (current_state == s_manual):
# update motor values
left, right = mixer(yaw=right_x, throttle=right_y)
first_look.set_speed(left/100, right/100)
if(next_state != current_state):
first_look.set_speed(0, 0)
current_state = next_state
elif (current_state == s_speed):
x, y = speed_run(range_left, range_center, range_right, joystick)
left, right = mixer(x, y)
first_look.set_speed(left/100, right/100)
if(next_state != current_state):
# shut things down
current_state = next_state
elif (current_state == s_maze):
x, y = minimal_maze(range_left, range_center, range_right, joystick)
left, right = mixer(x, y)
first_look.set_speed(left/100, right/100)
if(next_state != current_state):
# shut things down
current_state = next_state
elif (current_state == s_rainbow):
x, y = rainbow_road(range_left, range_center, range_right, joystick)
left, right = mixer(x, y)
first_look.set_speed(left/100, right/100)
if(next_state != current_state):
# shut things down
current_state = next_state
elif (current_state = s_duck):
x, y = duck_shoot(joystick)
left, right = mixer(x, y)
first_look.set_speed(left/100, right/100)
if(next_state != current_state):
# shut things down
current_state = next_state
elif (current_state = s_speed):
x, y = deranged-golf()
left, right = mixer(x, y)
first_look.set_speed(left/100, right/100)
if(next_state != current_state):
# shut things down
current_state = next_state
else:
# error!
first_look.set_speed(0, 0)
next_state = s_manual
current_state = s_manual
time.sleep(0.1)
# Joystick disconnected...
print('Connection to joystick lost')
except IOError:
# No joystick found, wait for a bit before trying again
print('Unable to find any joysticks')
time.sleep(1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment