forked from robotika/osgar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
176 lines (145 loc) · 4.93 KB
/
remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python
"""
Remote control of John Deere via WiFi
usage:
./driver.py <notes> | [<metalog> [<F>]]
"""
import sys
import math
from socket import timeout
from threading import Thread,Event,Lock
from apyros.metalog import MetaLog, disableAsserts
from apyros.sourcelogger import SourceLogger
from gps import DummyGPS as DummySensor # TODO move to apyros, as mock
from can import CAN, DummyMemoryLog, ReplayLogInputsOnly, ReplayLog
from johndeere import JohnDeere, setup_faster_update, ENC_SCALE
HOST = "192.168.1.201"
PORT = 5555
SOCKET_TIMEOUT = 0.2
class RemoteThread(Thread):
def __init__(self, soc):
Thread.__init__(self)
self.setDaemon(True)
self.lock = Lock()
self.shouldIRun = Event()
self.shouldIRun.set()
self.soc = soc
self.data = None
self.started = False
def run(self):
while self.shouldIRun.isSet():
try:
self.data = self.soc.recv(2000)
self.started = True
except timeout:
if self.started:
self.data = 'STOP\n'
else:
self.data = None
def requestStop(self):
self.shouldIRun.clear()
def get_data(self):
ret = self.data
self.data = None
return ret
def remote_data_extension(robot, id, data):
if id=='remote':
robot.remote_data = data
def drive_remotely(metalog):
assert metalog is not None
can_log_name = metalog.getLog('can')
if metalog.replay:
if metalog.areAssertsEnabled():
can = CAN(ReplayLog(can_log_name), skipInit=True)
else:
can = CAN(ReplayLogInputsOnly(can_log_name), skipInit=True)
else:
can = CAN()
can.relog(can_log_name)
can.resetModules(configFn=setup_faster_update)
robot = JohnDeere(can=can)
robot.UPDATE_TIME_FREQUENCY = 20.0 # TODO change internal and integrate setup
robot.localization = None # TODO
soc = metalog.createLoggedSocket('remote', headerFormat=None) # TODO fix headerFormat
soc.bind( ('',PORT) )
if not metalog.replay:
soc.soc.setblocking(0)
soc.soc.settimeout( SOCKET_TIMEOUT )
remote_cmd_log_name = metalog.getLog('remote_cmd')
if metalog.replay:
robot.remote = DummySensor()
function = SourceLogger(None, remote_cmd_log_name).get
else:
robot.remote = RemoteThread(soc)
function = SourceLogger(robot.remote.get_data, remote_cmd_log_name).get
max_speed = None
robot.remote_data = None
robot.register_data_source('remote', function, remote_data_extension)
robot.remote.start()
robot.canproxy.stop()
robot.canproxy.set_turn_raw(0)
print "Waiting for remote commands ..."
while robot.remote_data is None:
robot.update()
print "received", robot.remote_data.strip()
moving = False
turning = False
while robot.remote_data != 'END\n':
robot.update()
if robot.remote_data == 'STOP\n' and (moving or turning):
if moving:
robot.canproxy.stop()
print "STOP"
moving = False
if turning:
robot.canproxy.stop_turn()
print "STOP turn"
turning = False
elif robot.remote_data == 'UP\n' and not moving:
if max_speed is None:
robot.canproxy.go()
print "GO"
else:
robot.set_desired_speed(max_speed)
print "GO", max_speed
moving = True
elif robot.remote_data == 'DOWN\n' and not moving:
if max_speed is None:
robot.canproxy.go_back()
print "GO back"
else:
robot.set_desired_speed(-max_speed)
print "GO back", max_speed
moving = True
elif robot.remote_data == 'LEFT\n':
robot.canproxy.set_turn_raw(200)
print "Left"
turning = True
elif robot.remote_data == 'RIGHT\n':
robot.canproxy.set_turn_raw(-200)
print "Right"
turning = True
elif robot.remote_data == 'SPEED0\n':
max_speed = None
elif robot.remote_data.startswith('SPEED'):
max_speed = int(robot.remote_data[len('SPEED'):])/10.0
print "Max Speed", max_speed
print "received", robot.remote_data.strip()
robot.canproxy.stop_turn()
robot.remote.requestStop()
robot.wait(3.0)
if __name__ == "__main__":
if len(sys.argv) < 2:
print __doc__
sys.exit(2)
metalog=None
if 'meta_' in sys.argv[1]:
metalog = MetaLog(filename=sys.argv[1])
elif len(sys.argv) > 2:
metalog = MetaLog(filename=sys.argv[2])
if len(sys.argv) > 2 and sys.argv[-1] == 'F':
disableAsserts()
if metalog is None:
metalog = MetaLog()
drive_remotely(metalog)
# vim: expandtab sw=4 ts=4