source: robotics/wheelchair/python/DoraRecieve.py @ f85088d

Last change on this file since f85088d was 1b1715a, checked in by Steve Castellotti <sc@…>, 8 years ago
  • missing Arduino device handled gracefully (for debugging)
  • Property mode set to 100644
File size: 3.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Puzzlebox - Robotics - Wheelchair - Receiver
5#
6# Copyright Puzzlebox Productions, LLC (2013)
7#
8# This code is released under the GNU Pulic License (GPL) version 2
9# For more information please refer to http://www.gnu.org/copyleft/gpl.html
10
11import sys
12import time
13import signal
14import serial
15import select
16
17from PySide import QtCore, QtGui, QtNetwork
18
19import Wheelchair_Control as wheelchair_control
20
21#####################################################################
22# Globals
23#####################################################################
24
25DEBUG=1
26
27SOCKET = 20320
28currentTarget= 'i'
29previousTarget= 'i'
30
31
32#####################################################################
33# Classes
34#####################################################################
35
36class Receiver(QtGui.QDialog):
37       
38        def __init__(self, parent=None):
39               
40                super(Receiver, self).__init__(parent)
41
42                self.statusLabel = QtGui.QLabel("Listening for broadcasted messages")
43                quitButton = QtGui.QPushButton("&Quit")
44
45                self.udpSocket = QtNetwork.QUdpSocket(self)
46                self.udpSocket.bind(SOCKET)
47
48                self.udpSocket.readyRead.connect(self.processPendingDatagrams)
49                quitButton.clicked.connect(self.close)
50
51                buttonLayout = QtGui.QHBoxLayout()
52                buttonLayout.addStretch(1)
53                buttonLayout.addWidget(quitButton)
54                buttonLayout.addStretch(1)
55
56                mainLayout = QtGui.QVBoxLayout()
57                mainLayout.addWidget(self.statusLabel)
58                mainLayout.addLayout(buttonLayout)
59                self.setLayout(mainLayout)
60
61                self.setWindowTitle("Broadcast Receiver")
62               
63               
64                self.wheelchair = wheelchair_control.puzzlebox_brainstorms_wheelchair_control( \
65                        device_address=wheelchair_control.DEFAULT_SERIAL_DEVICE, \
66                        command=wheelchair_control.DEFAULT_COMMAND, \
67                        DEBUG=DEBUG)
68       
69       
70                self.wheelchair.start()
71               
72                if DEBUG:
73                        print "Wheelchair Started"
74
75
76        ##################################################################
77
78        def processPendingDatagrams(self):
79               
80                while self.udpSocket.hasPendingDatagrams():
81
82
83                        datagram, host, port = self.udpSocket.readDatagram(self.udpSocket.pendingDatagramSize())
84
85
86                        try:
87                                # Python v3.
88                                datagram = str(datagram, encoding='ascii')
89                        except:
90                                # Python v2.
91                                pass
92
93
94                        try:
95                                self.wheelchair.sendCommand('1','i')
96                        #except TypeError:
97                                #print "Wheelchair does not blah."
98                        except Exception, e:
99                                if DEBUG:
100                                        print "ERROR: Exception:",
101                                        print e
102                               
103
104
105                        self.statusLabel.setText("Received datagram: \"%s\"" % datagram)
106
107
108#####################################################################
109# Globals
110#####################################################################
111
112if __name__ == '__main__':
113
114        app = QtGui.QApplication(sys.argv)
115        receiver = Receiver()
116        #receiver.show()
117        sys.exit(receiver.exec_())
118
119        # Perform correct KeyboardInterrupt handling
120        # (allows you to hit control-c to quit)
121        signal.signal(signal.SIGINT, signal.SIG_DFL)
122
123        # Collect default settings and command line parameters
124        device = DEFAULT_SERIAL_DEVICE
125        command = DEFAULT_COMMAND
126
127        for each in sys.argv:
128
129                if each.startswith("--device="):
130                        device = each[ len("--device="): ]
131                elif each.startswith("--command="):
132                        command = each[ len("--command="): ]
133
134
135        #app1 = QtCore.QCoreApplication(sys.argv)
136
137        #wheelchair = puzzlebox_brainstorms_wheelchair_control( \
138                        #device_address=device, \
139                        #command=command, \
140                        #DEBUG=DEBUG)
141
142        receiver.show()
143       
144        if DEBUG:
145                print "Receiver Showing"
146        #wheelchair.start()
147        #print "Wheelchair Started"
148
149        sys.exit(app.exec_())
150        #sys.exit(app1.exec_())
Note: See TracBrowser for help on using the repository browser.