Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/LegoJAM.py
blob: d7e60f4977b2090f3c181fc60ec389a6b46ef71f (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
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import gtk
import sys
import gobject

BASE = os.path.dirname(__file__)

sys.path.append("nxt_plugin")

# Import the API functions
# Importamos las funciones de la API
import nxt
from nxt import Motor
from nxt.sensor import *

from interface.window import Window


class LegoJAM(Window):
    motor_names = ('A', 'B', 'C')

    def __init__(self):
        super(LegoJAM, self).__init__()
        self.set_title("LegoJAM")
        #self.set_icon_from_file(os.path.join(BASE, "Iconos", "legojam.png"))

        self.canvas = gtk.VBox()
        self.set_canvas(self.canvas)

        self.connect("delete_event", self.delete_event)

        robots = list(nxt.locator.find_bricks())

        liststore = gtk.ListStore(gtk.gdk.Pixbuf, int, str)
        treeview = gtk.TreeView(liststore)
        column = gtk.TreeViewColumn('Robots')
        celltext = gtk.CellRendererText()
        cellicon = gtk.CellRendererPixbuf()
        column.pack_start(cellicon, expand=False)
        column.pack_start(celltext, expand=False)
        column.add_attribute(cellicon, 'pixbuf', 0)
        column.add_attribute(celltext, 'text', 2)
        treeview.append_column(column)

        self.canvas.pack_start(treeview, True, True, 0)

        for num, robot in enumerate(robots):
            if robot.type == 'bluetooth':
                robot_id = robot.host
                icon = gtk.gdk.pixbuf_new_from_file_at_size(
                                        '          icons/bluetooth.png', 24, 24)

            elif robot.type == 'usb':
                robot_id = num + 1
                icon = gtk.gdk.pixbuf_new_from_file_at_size(
                                                        'icons/usb.png', 24, 24)

            robot_str = ' %s: %s' % (robot.type.upper(), robot_id)
            liststore.append([icon, num, robot_str])

        treeview.connect('row-activated', self.select_robot, robots)

        self.show_all()

    def select_robot(self, treeview, treepath, column, robots):
        treeview.destroy()
        self.set_layout(robots[treepath[0]])

    def set_layout(self, robot):
        self.robot = Robot(robot)
        store = gtk.ListStore(str, int)
        combobox = gtk.ComboBox(store)
        cell = gtk.CellRendererText()
        combobox.pack_start(cell)
        combobox.add_attribute(cell, 'text', 0)
        self.canvas.pack_start(combobox, True, False)
        combobox.show()

        for num, i in enumerate(LegoJAM.motor_names):
            store.append(['Motor %s' % i, num])
        combobox.set_active(0)

        vbox = gtk.VBox()
        run_button = gtk.Button("Encender")
        run_button.show()
        run_button.connect_object("clicked", self.run_motor, combobox)
        vbox.pack_start(run_button)
        stop_button = gtk.Button("Apagar")
        stop_button.show()
        stop_button.connect_object("clicked", self.stop_motor, combobox)
        vbox.pack_start(stop_button)
        vbox.show()

        battery_status = gtk.Label(
            'Estado de la bateria: %s' % self.robot.robot.get_battery_level())
        vbox.pack_end(battery_status)

        self.robot.connect('battery-label-changed',
                           lambda w, l: battery_status.set_text('Estado de la bateria: %s' % l))

        self.canvas.pack_start(vbox, True, True)
        self.show_all()

    def run_motor(self, combobox):
        motor_index = combobox.get_active()
        motor_name = LegoJAM.motor_names[motor_index]
        self.robot.run_motor(motor_name)

    def stop_motor(self, combobox):
        motor_index = combobox.get_active()
        motor_name = LegoJAM.motor_names[motor_index]
        self.robot.stop_motor(motor_name)

    def delete_event(self, widget, event):
        self.salir()
        return False

    def salir(self, widget=None):
        sys.exit(0)


# robot.get_battery_level()
# robot.get_device_info()
# robot.get_current_program_name
# robot.get_firmware_version()
# robot.get_input_values() takes exactly 2 arguments (1 given)
# robot.get_output_state() takes exactly 2 arguments (1 given)

class Robot(gobject.GObject):

    __gsignals__ = {'battery-label-changed': (gobject.SIGNAL_RUN_LAST,
                                              gobject.TYPE_NONE,
                                              [str])}

    def __init__(self, robot):
        gobject.GObject.__init__(self)

        self.motorA = None
        self.motorB = None
        self.motorC = None
        self.funciones = []
        self.set_up(robot)

    def set_up(self, robot):
        self.robot = robot.connect()
        self.motorA = MotorJAM("A", self.robot, nxt.PORT_A)
        self.motorB = MotorJAM("B", self.robot, nxt.PORT_B)
        self.motorC = MotorJAM("C", self.robot, nxt.PORT_C)

        # Si los sensores no se encuentran en los puertos correspondientes
        # la aplicaciĆ³n falla.
        self.presion = Touch(self.robot, PORT_1)
        self.sonido = Sound(self.robot, PORT_2)
        self.ultrasonido = Ultrasonic(self.robot, PORT_4)
        self.luz = Light(self.robot, PORT_3)

        gobject.timeout_add(1000, self.handle)

    def run_motor(self, motor):
        method_name = 'motor%s' % motor
        method = getattr(self, method_name)
        method.run(power=127, regulated=False)

    def stop_motor(self, motor):
        method_name = 'motor%s' % motor
        method = getattr(self, method_name)
        method.brake()

    def handle(self):
        valor_boton = self.presion.get_sample()
        print 'PresiĆ³n: ', valor_boton

        valor_sonido = self.sonido.get_sample()
        print 'Sonido: ', valor_sonido

        valor_ultrasonido = self.ultrasonido.get_sample()
        print 'Ultrasonido: ', valor_ultrasonido

        valor_luz = self.luz.get_sample()
        print 'Luz: ', valor_luz

        self.emit('battery-label-changed', str(self.robot.get_battery_level()))

        return True


class MotorJAM(Motor):
    # motor._get_state()
    def __init__(self, nombre, brick, port):
        Motor.__init__(self, brick, port)
        self.nombre = nombre

        #self.turn(power = 100, tacho_units = 360, emulate = True)
        #self.turn(
        #power = 100,
        #tacho_units= 0, brake = True, timeout = 1, emulate = True)
        #self.run(power = 100, regulated=False)

if __name__ == "__main__":
    LegoJAM()
    gtk.main()

'''
for x in range(5000):
   ....:     robot.play_tone_and_wait(x, 1000)'''