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

import os
import gtk
import sys

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 interface.window import Window


class LegoJAM(Window):

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

        self.base_box = None
        # button_motor_A = None
        # button_motor_B = None

        self.set_layout()
        self.show_all()

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

        self.robot = Pancho()

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

        num = 0
        for i in ['A', 'B', 'C']:
            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()
        num += 1

        canvas.pack_start(vbox, True, True)

        self.set_canvas(canvas)

    def run_motor(self, combobox):
        if combobox.get_active() == 0:
            self.robot.run_motor("A")
        elif combobox.get_active() == 1:
            self.robot.run_motor("B")
        elif combobox.get_active() == 2:
            self.robot.run_motor("C")

    def stop_motor(self, combobox):
        if combobox.get_active() == 0:
            self.robot.stop_motor("A")
        elif combobox.get_active() == 1:
            self.robot.stop_motor("B")
        elif combobox.get_active() == 2:
            self.robot.stop_motor("C")

    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 Pancho():
    def __init__(self):
        self.nombre = 'Pancho'
        self.motorA = None
        self.motorB = None
        self.motorC = None
        self.funciones = []
        self.set_up()

    def set_up(self):
        try:
            self.robot = nxt.locator.find_one_brick()
            for funcion in dir(self.robot):
                self.funciones.append(funcion)
                print funcion
            print self.robot.get_device_info()
        except:
            print "El Robot se Encuentra Desconectado o Apagado"
            sys.exit(0)
        self.motorA = Mimotor("A", self.robot, nxt.PORT_A)
        self.motorB = Mimotor("B", self.robot, nxt.PORT_B)
        self.motorC = Mimotor("C", self.robot, nxt.PORT_C)

    def run_motor(self, motor):
        if motor == "A":
            self.motorA.run(power=127, regulated=False)
        elif motor == "B":
            self.motorB.run(power=127, regulated=False)
        elif motor == "C":
            self.motorC.run(power=127, regulated=False)

    def stop_motor(self, motor):
        if motor == "A":
            self.motorA.brake()
        elif motor == "B":
            self.motorB.brake()
        elif motor == "C":
            self.motorC.brake()


class Mimotor(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)'''