class JoystickTest(unittest.TestCase): def setUp(self): ''' Verify environment is setup properly. ''' self.joystick = Joystick() j_list = self.joystick.get_joystick_list() self.joystick.connect(j_list[0]) def tearDown(self): ''' Verify environment is tore down properly. ''' self.joystick.disconnect() pass def test_up(self): ''' Verify that the joystick up movement is working without problems. ''' self.joystick.clear_commmands() value = False while not value: print "Move up using the joystick." j_command = self.joystick.get_commands() value = j_command["UP"] self.assertTrue(value) def test_down(self): ''' Verify that the joystick down movement is working without problems. ''' self.joystick.clear_commmands() value = False while not value: print "Move down using the joystick." j_command = self.joystick.get_commands() value = j_command["DOWN"] self.assertTrue(value) def test_left(self): ''' Verify that the joystick left movement is working without problems. ''' self.joystick.clear_commmands() value = False while not value: print "Move left using the joystick." j_command = self.joystick.get_commands() value = j_command["LEFT"] self.assertTrue(value) def test_right(self): ''' Verify that the joystick right movement is working without problems. ''' self.joystick.clear_commmands() value = False while not value: print "Move right using the joystick." j_command = self.joystick.get_commands() value = j_command["RIGHT"] self.assertTrue(value) def test_speed(self): ''' Verify that the joystick speed command is working without problems. ''' self.joystick.clear_commmands() value = False while not value: print "Speed up using the joystick." j_command = self.joystick.get_commands() value = j_command["SPEED"] self.assertTrue(value) def test_break(self): ''' Verify that the joystick break command is working without problems. ''' self.joystick.clear_commmands() value = False while not value: print "break using the joystick." j_command = self.joystick.get_commands() value = j_command["BREAK"] self.assertTrue(value)
class Controller(object): __metaclass__ = SingletonController def __init__(self): ''' Initialize class attributes. ''' self.joystick = Joystick() self.communication = Communication() self.driving = Driving() self.gear = 0 def get_joystick_list(self): ''' Return the control joystick list. ''' return self.joystick.get_joystick_list() def get_bluetooth_list(self): ''' Return the bluetooth communication list. ''' return self.communication.get_bluetooth_list() def connect(self, joystick_name, bluetooth_name): ''' Initialize the joystick and bluetooth connection. ''' if self.joystick.connect(joystick_name) and \ self.communication.connect(bluetooth_name): return True return False def disconnect(self): ''' Perform the joystick and bluetooth disconnection. ''' if self.joystick.disconnect() and \ self.communication.disconnect(): return True return False def quit_commands(self): ''' Send quit signal to the wait queue. ''' self.joystick.quit_commands() def send_commands(self): ''' Read the joystick commands and send it to the rover using a bluetooth device. ''' try: command = "" j_command = self.joystick.get_commands() if j_command["UP"] == True and \ j_command["DOWN"] == False and \ j_command["LEFT"] == False and \ j_command["RIGHT"] == False: command = "D1" self.communication.send(command) self.driving.set_angle(0.0) elif j_command["UP"] == False and \ j_command["DOWN"] == False and \ j_command["LEFT"] == False and \ j_command["RIGHT"] == True: command = "D3" self.communication.send(command) self.driving.set_angle(90.0) elif j_command["UP"] == False and \ j_command["DOWN"] == True and \ j_command["LEFT"] == False and \ j_command["RIGHT"] == False: command = "D5" self.communication.send(command) self.driving.set_angle(0.0) elif j_command["UP"] == False and \ j_command["DOWN"] == False and \ j_command["LEFT"] == True and \ j_command["RIGHT"] == False: command = "D7" self.communication.send(command) self.driving.set_angle(-90.0) if j_command["SPEED"] == True and self.gear < 6: self.gear = self.gear + 1 self.driving.set_power(self.gear) command = "V%d" % self.gear self.communication.send(command) elif j_command["BREAK"] == True and self.gear > 0: self.gear = self.gear - 1 self.driving.set_power(self.gear) command = "V%d" % self.gear self.communication.send(command) if j_command["UP"] == False and \ j_command["DOWN"] == False and \ j_command["LEFT"] == False and \ j_command["RIGHT"] == False and \ j_command["SPEED"] == False and \ j_command["BREAK"] == False: command = "B0" self.communication.send(command) self.driving.set_angle(0.0) except: print str(traceback.format_exc())