Skip to content

Commit

Permalink
Use working refactored Python code!
Browse files Browse the repository at this point in the history
  • Loading branch information
NicktheGreek1985 committed Oct 3, 2018
1 parent bb03947 commit 71bffef
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 61 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
93 changes: 93 additions & 0 deletions App/JS/Resources/CurrentEmuBotCode2/basic_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
""" Basic classes to be used by more advanced classes in wheel_arm_class.py """

import threading
import time
import connect

class Joint(threading.Thread):
""" A Joint class representing a moving node on parts of the emubot """

def __init__(self, identity, position):
""" __init__(identity, position): parameters represent identification and position """
threading.Thread.__init__(self)
self.direction = "neutral"
self.id = identity
self.position = position
self.move_joint(self.position, 900)

def run(self):
""" Step through a sequence of moves """
while self.direction != "":
if self.direction == "decrease":
if self.position > 200:
self.position -= 15
elif self.direction == "increase":
if self.position < 800:
self.position += 15
if self.direction != "neutral":
self.move_joint(self.position, 900)
time.sleep(0.1)

def move_joint(self, position, speed):
""" Move the joint """
output = ''

if self.id < 10:
output += '0' + str(self.id)
else:
output += str(self.id)

for _ in range(4 - len(str(position))):
output += '0'

output += str(position)

for _ in range(4 - len(str(speed))):
output += '0'

output += str(position)

output += '\n'
connect.SOCKET.sendall(bytes(output, 'utf-8'))

class Wheel(object):
""" A Wheel class representing a wheel of the emubot """

def __init__(self, identity):
""" Wheel.__init__(ID): parameters - the ID of the wheel """
self.id = str(identity)
self.direction = "none"
self.speed = 0

def move_wheel(self, speed):
""" Move the wheel """
self.speed = speed
output = '0' + self.id

if self.speed < 11 and self.speed > -11:
self.speed = 0
elif self.speed < 0:
output += '-'
self.speed = self.speed*-1

for _ in range(4 - len(str(self.speed))):
output += '0'

output += str(self.speed)
connect.SOCKET.sendall(bytes(output+"\n", 'utf-8'))

class Switch(object):
""" A Switch class representing a switch on the emubot """

def __init__(self):
""" Switch.__init__(): initialise variables """
self.on = True

def change_status():
""" Change the status of the switch """
if self.on:
connect.SOCKET.sendall(bytes("OFF\n", "utf-8"))
self.on = False
else:
connect.SOCKET.sendall(bytes("ON\n", "utf-8"))
self.on = True
160 changes: 160 additions & 0 deletions App/JS/Resources/CurrentEmuBotCode2/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
""" The movement client for the emubot """

import basic_classes
import constants
import controller
import wheel_arm_class

LEFT_MOVEMENT = 0
RIGHT_MOVEMENT = 0
FORWARDS_MOVEMENT = 0

LOCAL_SWITCH_ON = True
BACKWARDS_L = 1
BACKWARDS_R = 1

ARM = wheel_arm_class.Arm([5, 6, 7])
WHEEL_L1 = basic_classes.Wheel(1)
WHEEL_R2 = basic_classes.Wheel(2)
WHEEL_L3 = basic_classes.Wheel(3)
WHEEL_R4 = basic_classes.Wheel(4)

def move_left_wheels(speed):
""" Move the wheels on the left side of the emubot """
if speed == 0:
WHEEL_L1.move_wheel(0)
WHEEL_L3.move_wheel(0)
else:
WHEEL_L1.move_wheel(speed)
WHEEL_L3.move_wheel(speed)

def move_right_wheels(speed):
""" Move the wheels on the right side of the emubot """
if speed == 0:
WHEEL_R2.move_wheel(0)
WHEEL_R4.move_wheel(0)
else:
WHEEL_R2.move_wheel(speed)
WHEEL_R4.move_wheel(speed)

print("Event loop starting...")

for event in controller.gamepad.read_loop():
try:
event_code = event.code
event_value = event.value

if event_code != 0:
if event_code == constants.LEFT_TRG:
if LEFT_MOVEMENT == 3 or event_value <= 0:
if event_value > 0:
move_left_wheels(event_value * 4 * BACKWARDS_L)
else:
move_left_wheels(0)
LEFT_MOVEMENT = 1
else:
LEFT_MOVEMENT = LEFT_MOVEMENT + 1

elif event_code == constants.RIGHT_TRG:
if RIGHT_MOVEMENT == 3 or event_value <= 0:
if event_value > 0:
move_right_wheels(-(event_value * 4 * BACKWARDS_R))
else:
move_right_wheels(0)
RIGHT_MOVEMENT = 1
else:
RIGHT_MOVEMENT = RIGHT_MOVEMENT + 1

elif event_code == constants.LB:
if event_value == 1:
if BACKWARDS_L > 0:
BACKWARDS_L = -1
else:
BACKWARDS_L = 1

elif event_code == constants.RB:
if event_value == 1:
if BACKWARDS_R > 0:
BACKWARDS_R = -1
else:
BACKWARDS_R = 1

elif event_code == constants.LHORIZ:
if FORWARDS_MOVEMENT == 3 or event_value <= 0:
if event_value > 0:
move_left_wheels(event_value / 40)
move_right_wheels(event_value / 40)
elif event_value < 0:
move_left_wheels(event_value / 40)
move_right_wheels(event_value / 40)
else:
move_left_wheels(0)
move_right_wheels(0)
FORWARDS_MOVEMENT = 1

else:
FORWARDS_MOVEMENT = FORWARDS_MOVEMENT + 1

elif event_code == constants.LVERT:
move_left_wheels(-(event_value / 40))
move_right_wheels((event_value / 40))

elif event_code == constants.D_PAD_HORIZONTAL:
if event_value == 1:
ARM.move_pan(direction="increase")
elif event_value == -1:
ARM.move_pan(direction="decrease")
elif event_value == 0:
ARM.move_pan(direction="neutral")

elif event_code == constants.D_PAD_VERTICAL:
if event_value == -1:
ARM.move_tilt(direction="increase")
elif event_value == 1:
ARM.move_tilt(direction="decrease")
elif event_value == 0:
ARM.move_tilt(direction="neutral")

elif event_code == constants.Y:
if event_value == 1:
ARM.move_shoulder(direction="increase")
elif event_value == 0:
ARM.move_shoulder(direction="neutral")

elif event_code == constants.A:
if event_value == 1:
ARM.move_shoulder(direction="decrease")
elif event_value == 0:
ARM.move_shoulder(direction="neutral")

elif event_code == constants.X:
ARM.move_arm_set_position(shoulder_pos=512, tilt_pos=200, pan_pos=512)

elif event_code == constants.B:
if LOCAL_SWITCH_ON and event_value != 0:
LOCAL_SWITCH_ON = False
print("OFF")

elif event_value != 0:
LOCAL_SWITCH_ON = True
print("ON")

elif event_code == constants.START:
# Reset all servos to original positions
ARM.reset()
WHEEL_L1.move_wheel(0)
WHEEL_L3.move_wheel(0)
WHEEL_R2.move_wheel(0)
WHEEL_R4.move_wheel(0)

elif event_code == constants.BACK:
WHEEL_L1.move_wheel(0)
WHEEL_L3.move_wheel(0)
WHEEL_R2.move_wheel(0)
WHEEL_R4.move_wheel(0)

else:
print("Unidentified Event:", event)

except BrokenPipeError as error:
print(error)
20 changes: 20 additions & 0 deletions App/JS/Resources/CurrentEmuBotCode2/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
""" Establishes the connection between the input device and the emubot """

print("connect setup")
import socket

HOST, PORT = "192.168.100.1", 9999 #"169.254.44.240", 9999
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("trying to establish a connection")

try:
SOCKET.connect((HOST, PORT))
print("connect ready")
except:
print("""
CONNECTION FAILED.")
have you run the code on the raspberry pi?
P.S. dont break the pi please
""")

print(SOCKET)
45 changes: 45 additions & 0 deletions App/JS/Resources/CurrentEmuBotCode2/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
""" Constants module for this directory """

# Event Constants
LEFT_TRG = 2
RIGHT_TRG = 5
RB = 311
LB = 310
LCLICK = 317
RCLICK = 318
RVERT = 4
RHORIZ = 3
LVERT = 1
LHORIZ = 0
START = 315
BACK = 314
A = 304
B = 305
Y = 308
X = 307
D_PAD_VERTICAL = 17
D_PAD_HORIZONTAL = 16

# Shoulder Direction
INCREASE = 1
DECREASE = 2
NEUTRAL = 3
NOTHING = 0

# Switches
SWITCHON = 1
SWITCHOFF = 0

# Arm Movement Divisor
MOVEMENT_SCALING = 4000

# Wheel and Arm Identifcations
HAND = 7
ARM = 6
SHOULDER = 5

# From the back view of the robot:
FRONT_LEFT_WHEEL = 1
FRONT_RIGHT_WHEEL = 2
BACK_LEFT_WHEEL = 3
BACK_RIGHT_WHEEL = 4
22 changes: 22 additions & 0 deletions App/JS/Resources/CurrentEmuBotCode2/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
""" Initialises the input source by which the emubot is controlled """

import evdev

devices = [evdev.InputDevice(input_source) for input_source in evdev.list_devices()]

controllers = {"Logitech Gamepad F310",
"Logitech Gamepad F710",
"Microsoft X-Box 360 pad",
"Logitech Logitech Cordless RumblePad 2",
"Logitech Logitech Dual Action"}

gamepad = None
for i in devices:
if i.name in controllers:
gamepad = i
break

if gamepad is None:
raise TypeError("Variable 'gamepad' is None")

print("Gamepad is:", gamepad)
13 changes: 13 additions & 0 deletions App/JS/Resources/CurrentEmuBotCode2/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
""" A module defining a function to change the state of SOCKET """

import connect
import constants

def switch(state):
""" Invert the state of SOCKET """
if state == constants.SWITCHON:
connect.SOCKET.sendall(bytes("OFF\n", "utf-8"))
return constants.SWITCHOFF

connect.SOCKET.sendall(bytes("ON\n", "utf-8"))
return constants.SWITCHON
Loading

0 comments on commit 71bffef

Please sign in to comment.