Пример #1
0
class RoverWindow():

    def __init__(self):
        '''
        Initialize class attributes.
        '''
        self.control = Controller()
        self.control_thread = ControllerThread()
        self.root = Tk()
        self.root.title("Robot Control Interface")
        self.mainframe = None
        self.combo_joystick = None
        self.combo_bluetooth = None
        self.__configure_frame()
        self.root.mainloop()

    def __configure_frame(self):
        '''
        '''
        self.mainframe = Frame(self.root, padding="3 3 12 12")
        self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=1)
        self.message = StringVar()
        Label(self.mainframe, textvariable=self.message).grid(column=2, row=2, sticky=W)
        self.disconnect_button = Button(self.mainframe, text="Disconnect", command=self.disconnect, state=DISABLED)
        self.speed = StringVar()
        Label(self.mainframe, textvariable=self.speed).grid(column=2, row=4, sticky=W)
        self.disconnect_button.grid(column=3, row=3, sticky=E)
        self.root.bind('<Return>', self.disconnect)
        self.joystick_section()
        self.bluetooth_section()
        for child in self.mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
        self.combo_joystick.current(0)
        self.combo_bluetooth.current(0)

    def joystick_section(self):
        '''
        '''
        Label(self.mainframe, text="Joystick").grid(column=1, row=1, sticky=W)
        value = StringVar()
        self.combo_joystick = Combobox(self.mainframe, textvariable=value)
        self.combo_joystick.grid(column=2, row=1, sticky=(W, E))
        self.combo_joystick["values"] = self.control.get_joystick_list()
        self.combo_joystick.bind("<<ComboboxSelected>>")

    def bluetooth_section(self):
        '''
        '''
        Label(self.mainframe, text="Bluetooth").grid(column=1, row=3, sticky=W)
        value = StringVar()
        self.combo_bluetooth = Combobox(self.mainframe, textvariable=value)
        self.combo_bluetooth.grid(column=2, row=3, sticky=W)
        self.combo_bluetooth["values"] = self.control.get_bluetooth_list()
        self.combo_bluetooth.bind("<<ComboboxSelected>>")
        self.button_bluetooth = Button(self.mainframe, text="Connect", command=self.connect)
        self.button_bluetooth.grid(column=3, row=1, sticky=E)
        self.root.bind('<Return>', self.connect)

    def speed_update(self):
        '''
        '''
        self.speed.set("300 Km/h")

    def connect(self):
        '''
        '''
        print "Joystick: %s" % self.combo_joystick.get()
        print "Bluetooth: %s" % self.combo_bluetooth.get()
        self.control.connect(self.combo_joystick.get(), self.combo_bluetooth.get())
        self.message.set("Set speed and move the Rover")
        self.button_bluetooth["state"] = "disabled"
        self.disconnect_button["state"] = "enabled"
        self.speed_update()
        self.control_thread.start()

    def disconnect(self):
        '''
        '''
        self.control_thread.stop_communication()
        time.sleep(1)
        counter = 0
        while self.control_thread.isAlive() and counter < 16:
            print "Waiting to release the thread."
            counter = counter + 1
            time.sleep(1)
        self.control.disconnect()
        self.message.set("")
        self.combo_joystick.current(0)
        self.combo_bluetooth.current(0)
        self.disconnect_button["state"] = "disabled"
Пример #2
0
class Rover(QDeclarativeView):
   
    def __init__(self, parent=None):
        '''
        Initialize the attributes class.
        '''
        super(Rover, self).__init__(parent)
        self.setWindowTitle("Robotic Control Interface")
        self.set_default_values()
        self.setSource(QUrl.fromLocalFile("Rover.qml"))
        self.setResizeMode(QDeclarativeView.SizeRootObjectToView)
        self.ro = self.rootObject()
        
        self.control = Controller()
        self.control_thread = ControllerThread()
        self.control_start = False
        
    def set_default_values(self):
        '''
        Set the default values to start the GUI.
        '''
        self.joystick = ""
        self.bluetooth = ""
        self.power = 0.0
        self.angle = 0.0
        self.rc = self.rootContext()
        self.rc.setContextProperty("joystick", self.joystick)
        self.rc.setContextProperty("bluetooth", self.bluetooth)
        self.rc.setContextProperty("power", self.power)
        self.rc.setContextProperty("angle", self.angle)
        
    def set_connection(self):
        '''
        Display the devices selected for the communication.
        '''
        joystick_list = self.control.get_joystick_list()
        for element in joystick_list:
            if "DragonRise" in element:
                self.joystick = element
                break
        self.rc.setContextProperty("joystick", self.joystick)
        
        bluetooth_list = self.control.get_bluetooth_list()
        for element in bluetooth_list:
            if "CURIOSITY" in element:
                self.bluetooth = element
                break
        self.rc.setContextProperty("bluetooth", self.bluetooth)
        
    def set_event_signal(self):
        '''
        Define the GUI interactions with the system.
        '''
        self.ro.connection.connect(self.__connect_hw)
        self.ro.disconnection.connect(self.__disconnect_hw)
        
    def __connect_hw(self):
        '''
        Connect to the CONTROL software.
        '''
        if not self.control_start:
            self.control_start = True
            self.control.connect(self.joystick, self.bluetooth)
            self.control_thread.start()
        
    def __disconnect_hw(self):
        '''
        Disconnect from the CONTROL software.
        '''
        self.control_thread.stop_communication()
        time.sleep(1)            
        self.control.disconnect()
        time.sleep(1)
        sys.exit(0)

    def update(self, observer):
        '''
        Method to update values in the GUI.
        '''
        self.power = observer.power
        self.rc.setContextProperty("power", self.power)
        self.angle = observer.angle
        self.rc.setContextProperty("angle", self.angle)