Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins/nxt_plugin.py
blob: 4551f568d18eb0e4b65aeafb5113701cb614ae97 (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python
#Copyright (C) 2011 Emiliano Pastorino <epastorino@plan.ceibal.edu.uy>

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import os
import dbus

import lib.nxt as nxt

from gettext import gettext as _
from dbus.mainloop.glib import DBusGMainLoop


from plugin import Plugin

from TurtleArt.tapalette import make_palette
from TurtleArt.talogo import primitive_dictionary
from TurtleArt.taconstants import BLACK, WHITE, CONSTANTS
from TurtleArt.tautils import debug_output

from dbus.mainloop.glib import DBusGMainLoop
from lib.nxt.locator import BrickNotFoundError
from lib.nxt.motor import PORT_A, PORT_B, PORT_C, Motor, SynchronizedMotors
from lib.nxt.sensor import PORT_1, PORT_2, PORT_3, PORT_4, Touch, Color20, \
     Ultrasonic, Type

NXT_SENSORS = {'nxttouch': 0, 'nxtultrasonic': 1, 'nxtcolor': 2}

class Nxt_plugin(Plugin):

    def __init__(self, parent):
        self.tw = parent
        self.nxtbrick = False

	"""
        The following code will search for a NXT device.
        It is necessary to set the permission for these devices to 0666.
        You can do this by adding a rule to /etc/udev/rules.d

        As root (using sudo or su), copy the following text into a new file in
        /etc/udev/rules.d/99-lego.rules

        BUS=="usb", ATTRS{idVendor}=="0694", ATTRS{idProduct}=="0002", MODE="0666"

        You only have to do this once.
        """

        try:
            self.nxtbrick = nxt.locator.find_one_brick()
            debug_output("NXT found")
            
	except BrickNotFoundError:
            pass


    def setup(self):
        if self.nxtbrick and self.tw:
            palette = make_palette('nxt', ["#A0A0A0", "#606060"],
                        _('Palette of LEGO Mindstorms NXT objects'))

            primitive_dictionary['nxtturnmotor'] = self._prim_nxtturnmotor
            palette.add_block('nxtturnmotor',
                      style='basic-style-3arg',
                      label=[_('turn motor'), _('port'), _('power'),
                             _('rotations')],
                      default=['None', 1, 100],
                      help_string=_('Turn a motor'),
                      prim_name='nxtturnmotor')
            self.tw.lc.def_prim('nxtturnmotor', 3,
                lambda self, x, y, z:
                primitive_dictionary['nxtturnmotor'](x, y, z))

            primitive_dictionary['nxtporta'] = self._prim_nxtporta
            palette.add_block('nxtporta',
                      style='box-style',
                      label=_('PORT A'),
                      help_string=_('Port A'),
                      prim_name='nxtporta')
            self.tw.lc.def_prim('nxtporta', 0,
                lambda self: primitive_dictionary['nxtporta']())

            primitive_dictionary['nxtportb'] = self._prim_nxtportb
            palette.add_block('nxtportb',
                      style='box-style',
                      label=_('PORT B'),
                      help_string=_('Port B'),
                      prim_name='nxtportb')
            self.tw.lc.def_prim('nxtportb', 0,
                lambda self: primitive_dictionary['nxtportb']())

            primitive_dictionary['nxtportc'] = self._prim_nxtportc
            palette.add_block('nxtportc',
                      style='box-style',
                      label=_('PORT C'),
                      help_string=_('Port C'),
                      prim_name='nxtportc')
            self.tw.lc.def_prim('nxtportc', 0,
                lambda self: primitive_dictionary['nxtportc']())

            primitive_dictionary['nxtplaytone'] = self._prim_nxtplaytone
            palette.add_block('nxtplaytone',
                      style='basic-style-2arg',
                      label=[_('play tone'), _('freq'), _('time')],
                      default=[433, 500],
                      help_string=_('Play a tone'),
                      prim_name='nxtplaytone')
            self.tw.lc.def_prim('nxtplaytone', 2,
                lambda self, x, y: primitive_dictionary['nxtplaytone'](x, y))

            primitive_dictionary['nxttouch'] = self._prim_nxttouch
            palette.add_block('nxttouch',
                      style='box-style',
                      label=_('touch'),
                      help_string=_('Touch sensor'),
                      prim_name='nxttouch')
            self.tw.lc.def_prim('nxttouch', 0,
                lambda self: primitive_dictionary['nxttouch']())

            primitive_dictionary['nxtport1'] = self._prim_nxtport1
            palette.add_block('nxtport1',
                      style='box-style',
                      label=_('PORT 1'),
                      help_string=_('Port 1'),
                      prim_name='nxtport1')
            self.tw.lc.def_prim('nxtport1', 0,
                lambda self: primitive_dictionary['nxtport1']())

            primitive_dictionary['nxtport2'] = self._prim_nxtport2
            palette.add_block('nxtport2',
                      style='box-style',
                      label=_('PORT 2'),
                      help_string=_('Port 2'),
                      prim_name='nxtport2')
            self.tw.lc.def_prim('nxtport2', 0,
                lambda self: primitive_dictionary['nxtport2']())

            primitive_dictionary['nxtport3'] = self._prim_nxtport3
            palette.add_block('nxtport3',
                      style='box-style',
                      label=_('PORT 3'),
                      help_string=_('Port 3'),
                      prim_name='nxtport3')
            self.tw.lc.def_prim('nxtport3', 0,
                lambda self: primitive_dictionary['nxtport3']())

            primitive_dictionary['nxtport4'] = self._prim_nxtport4
            palette.add_block('nxtport4',
                      style='box-style',
                      label=_('PORT 4'),
                      help_string=_('Port 4'),
                      prim_name='nxtport4')
            self.tw.lc.def_prim('nxtport4', 0,
                lambda self: primitive_dictionary['nxtport4']())

            primitive_dictionary['nxtultrasonic'] = self._prim_nxtultrasonic
            palette.add_block('nxtultrasonic',
                      style='box-style',
                      label=_('ultrasonic'),
                      help_string=_('Distance sensor'),
                      prim_name='nxtultrasonic')
            self.tw.lc.def_prim('nxtultrasonic', 0,
                lambda self: primitive_dictionary['nxtultrasonic']())

            primitive_dictionary['nxtcolor'] = self._prim_nxtcolor
            palette.add_block('nxtcolor',
                      style='box-style',
                      label=_('color'),
                      help_string=_('Color sensor'),
                      prim_name='nxtcolor')
            self.tw.lc.def_prim('nxtcolor', 0,
                lambda self: primitive_dictionary['nxtcolor']())

            primitive_dictionary['nxtreadsensor'] = self._prim_nxtreadsensor
            palette.add_block('nxtreadsensor',
                      style='number-style-block',
                      label=[_('read'), _('sensor'), _('port')],
                      help_string=_('Read sensor output'),
                      prim_name='nxtreadsensor')
            self.tw.lc.def_prim('nxtreadsensor', 2,
                lambda self, x, y:
                primitive_dictionary['nxtreadsensor'](x, y))

            primitive_dictionary['nxtsyncmotors'] = self._prim_nxtsyncmotors
            palette.add_block('nxtsyncmotors',
                      style='basic-style-3arg',
                      label=[_('sync\nmotors'), _('power'), _('rotations'),
                             _('steering')],
                      default=[100, 0, 1],
                      help_string=_('Synchronize motors'),
                      prim_name='nxtsyncmotors')
            self.tw.lc.def_prim('nxtsyncmotors', 3,
                lambda self, x, y, z:
                primitive_dictionary['nxtsyncmotors'](x, y, z))

            primitive_dictionary['nxtstartmotor'] = self._prim_nxtstartmotor
            palette.add_block('nxtstartmotor',
                      style='basic-style-2arg',
                      label=[_('start motor'), _('port'), _('power')],
                      default=['None', 100],
                      help_string=_('Run motor forever'),
                      prim_name='nxtstartmotor')
            self.tw.lc.def_prim('nxtstartmotor', 2, lambda self, x, y:
                primitive_dictionary['nxtstartmotor'](x, y))

            primitive_dictionary['nxtbrake'] = self._prim_nxtbrake
            palette.add_block('nxtbrake',
                      style='basic-style-1arg',
                      label=_('brake motor'),
                      help_string=_('Brake specified motor'),
                      prim_name='nxtbrake')
            self.tw.lc.def_prim('nxtbrake', 1, lambda self, x:
                primitive_dictionary['nxtbrake'](x))

            primitive_dictionary['nxtsetcolor'] = self._prim_nxtsetcolor
            palette.add_block('nxtsetcolor',
                      style='basic-style-2arg',
                      label=[_('set light'), _('color'), _('port')],
                      help_string=_('Set color sensor light'),
                      prim_name='nxtsetcolor')
            self.tw.lc.def_prim('nxtsetcolor', 2, lambda self, x, y:
                primitive_dictionary['nxtsetcolor'](x, y))

    def start(self):
        # This gets called by the start button
        pass

    def stop(self):
        # This gets called by the stop button
        try:
            self._prim_nxtbrake(PORT_A)
            self._prim_nxtbrake(PORT_B)
            self._prim_nxtbrake(PORT_C)
        except:
            pass


    def goto_background(self):
        # This gets called when your process is sent to the background
        pass

    def return_to_foreground(self):
        # This gets called when your process returns from the background
        pass

    def quit(self):
        # This gets called by the quit button
        pass

    def _prim_nxtturnmotor(self, port, turns, power):
        return Motor(self.nxtbrick, port).turn(power, int(turns*360))

    def _prim_nxtsyncmotors(self, power, steering, turns):
        motorB = Motor(self.nxtbrick, PORT_B)
        motorC = Motor(self.nxtbrick, PORT_C)
        syncmotors = SynchronizedMotors(motorB, motorC, steering)
        return syncmotors.turn(power, int(turns*360))

    def _prim_nxtplaytone(self, freq, time):
        return self.nxtbrick.play_tone(freq, time)

    def _prim_nxttouch(self):
        return NXT_SENSORS['nxttouch']
        
    def _prim_nxtultrasonic(self):
        return NXT_SENSORS['nxtultrasonic']

    def _prim_nxtcolor(self):
        return NXT_SENSORS['nxtcolor']

    def _prim_nxtport1(self):
        return PORT_1

    def _prim_nxtport2(self):
        return PORT_2

    def _prim_nxtport3(self):
        return PORT_3

    def _prim_nxtport4(self):
        return PORT_4

    def _prim_nxtporta(self):
        return PORT_A

    def _prim_nxtportb(self):
        return PORT_B

    def _prim_nxtportc(self):
        return PORT_C

    def _prim_nxtreadsensor(self, sensor, port):
        """ Read sensor at specified port"""
        debug_output("_prim_nxtreadsensor: %s, %s"%(sensor, port))
        colors = [None, BLACK, CONSTANTS['blue'], CONSTANTS['green'],
                  CONSTANTS['yellow'], CONSTANTS['red'], WHITE]
        if sensor == NXT_SENSORS['nxtcolor']:
            return colors[Color20(self.nxtbrick, port).get_sample()]
        elif sensor == NXT_SENSORS['nxtultrasonic']:
            return Ultrasonic(self.nxtbrick, port).get_sample()
        elif sensor == NXT_SENSORS['nxttouch']:
            return Touch(self.nxtbrick, port).get_sample()
        else:
            return None

    def _prim_nxtstartmotor(self, port, power):
        return Motor(self.nxtbrick, port).weak_turn(power, 0)

    def _prim_nxtbrake(self, port):
        return Motor(self.nxtbrick, port).brake()

    def _prim_nxtsetcolor(self, color, port):
        if color == WHITE:
            color = Type.COLORFULL
        elif color == CONSTANTS['red']:
            color = Type.COLORRED
        elif color == CONSTANTS['green']:
            color = Type.COLORGREEN
        elif color == CONSTANTS['blue']:
            color = Type.COLORBLUE
        else:
            color = Type.COLORNONE
        Color20(self.nxtbrick, port).set_light_color(color)