-
Notifications
You must be signed in to change notification settings - Fork 4
/
rodi.py
executable file
·270 lines (224 loc) · 6.47 KB
/
rodi.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
# Copyright (C) 2015 Manuel Kaufmann - [email protected]
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
'''
RoDI (Robot Didactico Inalambrico) module
'''
import time
import json
import requests # fades.pypi
def wheel(wheel_pos):
'''
Input a value 0 to 255 to get a color value.
The colours are a transition r - g - b - back to r.
'''
wheel_pos = 255 - wheel_pos
if wheel_pos < 85:
return (255 - wheel_pos * 3, 0, wheel_pos * 3)
if wheel_pos < 170:
wheel_pos -= 85
return (0, wheel_pos * 3, 255 - wheel_pos * 3)
wheel_pos -= 170
return (wheel_pos * 3, 255 - wheel_pos * 3, 0)
class RoDI(object):
'''
The RoDI (Robot Didactico Inalambrico) class
'''
_URL = 'http://{ip}:{port}/{method}/{args}'
BLINK_METHOD = 1
SENSE_METHOD = 2
MOVE_METHOD = 3
SING_METHOD = 4
SEE_METHOD = 5
PIXEL_METHOD = 6
LIGHT_METHOD = 7
LED_METHOD = 8
def __init__(self, ip='192.168.4.1', port='1234'):
'''
Constructor for the robot
'''
self.robot_ip = ip
self.port = port
def _build_url(self, method, args):
'''
Helper method to construct the server url
'''
args = map(str, args)
url = self._URL.format(
ip=self.robot_ip,
port=self.port,
method=method,
args='/'.join(args),
)
return url
def blink(self, milliseconds):
'''
Makes the robot blink its led for the specified time
'''
url = self._build_url(
self.BLINK_METHOD,
[milliseconds]
)
requests.get(url)
def move(self, left_wheel_speed, right_wheel_speed):
'''
Makes the robot move
'''
url = self._build_url(
self.MOVE_METHOD,
[left_wheel_speed, right_wheel_speed]
)
requests.get(url)
def move_left(self):
'''
Moves the robot (rotates) to the left
'''
self.move(-100, 100)
def move_right(self):
'''
Moves the robot (rotates) to the right
'''
self.move(100, -100)
def move_forward(self):
'''
Moves the robot forward
'''
self.move(100, 100)
def move_backward(self):
'''
Moves the robot backwards
'''
self.move(-100, -100)
def move_stop(self):
'''
Stops the robot
'''
self.move(0, 0)
def sing(self, note, duration):
'''
Makes the robot sing
You need to specify a note and a duration in miliseconds
(Notes can be found in http://arduino.cc/en/tutorial/tone)
'''
url = self._build_url(
self.SING_METHOD,
[note, duration]
)
requests.get(url)
def see(self):
'''
Makes the robot "see"
It returns the distance of an object in front of the robot in cm
'''
url = self._build_url(
self.SEE_METHOD,
[]
)
response = requests.get(url)
return json.loads(response.content)
def sense(self):
'''
Senses the status of the infrarred sensors (line follower)
Returns the reflectance of the object beneath the robot
with values from 0 (black) to 1023 (white)
'''
url = self._build_url(
self.SENSE_METHOD,
[]
)
response = requests.get(url)
return json.loads(response.content)
def pixel(self, red, green, blue):
'''
Changes the color of the Pixel in the robot
Takes thre values, red, green and blue from 0 to 255
'''
url = self._build_url(
self.PIXEL_METHOD,
[red, green, blue]
)
requests.get(url)
def light(self):
'''
Senses the status of the light sensors
Returns the luminosity of the ambient with values from 0 to 1023
'''
url = self._build_url(
self.LIGHT_METHOD,
[]
)
response = requests.get(url)
return json.loads(response.content)
def led(self, state):
'''
Turns the led on or off
values for state are 0: off and 1: on
'''
url = self._build_url(
self.LED_METHOD,
[state]
)
requests.get(url)
def run_test(self):
'''
Method to run some tests for the robot
'''
print "RoDI turn led on"
self.led(1)
time.sleep(1)
print "RoDI turn led off"
self.led(0)
time.sleep(1)
print "RoDI blink"
self.blink(200)
time.sleep(1)
print "RoDI move forward"
self.move_forward()
time.sleep(1)
print "RoDI rotate left"
self.move_left()
time.sleep(1)
print "RoDI move forward"
self.move_forward()
time.sleep(1)
print "RoDI rotate right"
self.move_right()
time.sleep(1)
print "RoDI move backward"
self.move_backward()
time.sleep(1)
print "RoDI stop"
self.move_stop()
time.sleep(1)
print "RoDI sing"
self.sing(33, 1000)
time.sleep(1)
print "RoDI do a rainbow"
for j in range(256):
red, green, blue = wheel(j)
self.pixel(red, green, blue)
time.sleep(0.005)
self.pixel(0, 0, 0)
print "RoDI see"
print " - I see something at %d cm" % self.see()
time.sleep(1)
print "RoDI sense"
print " - My sensors sense: %s" % self.sense()
time.sleep(1)
print "RoDI see light"
print " - My light sensor senses: %s" % self.light()
self.blink(0)
if __name__ == '__main__':
ROBOT = RoDI()
ROBOT.run_test()