summaryrefslogtreecommitdiff
path: root/digital/ai/tools/test_simu_control_guybrush.py
blob: e895bf395880855a3bf9ec59748604aeee5db740 (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
# io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
#
# Copyright (C) 2012 Nicolas Schodet
#
# APBTeam:
#        Web: http://apbteam.org/
#      Email: team AT apbteam DOT org
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# }}}
from test_simu import TestSimu, run
from Tkinter import *
import math

import simu.robots.guybrush.model.bag

defs = simu.robots.guybrush.model.bag.Bag

class TestSimuControl (TestSimu):
    """Interface with extra control."""

    LOWER_CLAMP_ROTATION_STROKE = int (16 * 250)

    def __init__ (self, robot_class, *args):
        TestSimu.__init__ (self, robot_class, *args,
                color_switch_set_pos = True)
        self.io = self.robots[0].io
        self.asserv = self.robots[0].asserv
        self.mimot = self.robots[0].mimot
        self.robot_model = self.robots[0].model
        self.io.output (1 << defs.OUTPUT_UPPER_CLAMP_UP, 'toggle')
        self.io.output (1 << defs.OUTPUT_UPPER_CLAMP_IN, 'toggle')
        self.io.output (1 << defs.OUTPUT_DOOR_CLOSE, 'toggle')

    def create_widgets (self):
        TestSimu.create_widgets (self)
        self.control_frame = Frame (self)
        self.control_frame.pack (side = 'left', before = self.table_view,
                fill = 'y')
        Button (self.control_frame, text = 'FSM step', padx = 0, pady = 0,
                command = self.fsm_debug).pack ()
        Button (self.control_frame, text = 'Asserv block', padx = 0, pady = 0,
                command = self.asserv_block).pack ()
        Button (self.control_frame, text = 'Clamp block', padx = 0, pady = 0,
                command = self.clamp_block).pack ()
        def out_button (name, toggle):
            def command ():
                self.io.output (toggle, 'toggle')
            button = Button (self.control_frame, text = name,
                    padx = 0, pady = 0, command = command)
            button.pack ()
        out_button ('LClamp 1 open',
                1 << defs.OUTPUT_LOWER_CLAMP_1_CLOSE)
        out_button ('LClamp 2 open',
                1 << defs.OUTPUT_LOWER_CLAMP_2_CLOSE)
        self.lower_clamp_rotate_button = Button (self.control_frame,
                text = 'LClamp rotate', padx = 0, pady = 0,
                command = self.lower_clamp_rotate_command)
        self.lower_clamp_rotate_button.pack ()
        out_button ('UClamp up/down',
                1 << defs.OUTPUT_UPPER_CLAMP_UP
                | 1 << defs.OUTPUT_UPPER_CLAMP_DOWN)
        out_button ('UClamp in/out',
                1 << defs.OUTPUT_UPPER_CLAMP_IN
                | 1 << defs.OUTPUT_UPPER_CLAMP_OUT)
        out_button ('UClamp open',
                1 << defs.OUTPUT_UPPER_CLAMP_OPEN)
        out_button ('Door open',
                1 << defs.OUTPUT_DOOR_OPEN
                | 1 << defs.OUTPUT_DOOR_CLOSE)
        self.tree_detected_button = Button (self.control_frame,
                text = 'Tree Detected', padx = 0, pady = 0,
                command = self.tree_detected)
        self.tree_detected_button.pack()
        self.stop_tree_approach_button = Button (self.control_frame,
                text = 'Stop tree approach', padx = 0, pady = 0,
                command = self.stop_tree_approach)
        self.stop_tree_approach_button.pack()
        self.empty_tree_button = Button (self.control_frame,
                text = 'Empty Tree', padx = 0, pady = 0,
                command = self.empty_tree)
        self.empty_tree_button.pack()
        self.robot_is_back_button = Button (self.control_frame,
                text = 'Robot is back', padx = 0, pady = 0,
                command = self.robot_is_back)
        self.robot_is_back_button.pack()
        self.backward_var = IntVar ()
        self.backward_button = Checkbutton (self.control_frame,
                text = 'Backward', variable = self.backward_var)
        self.backward_button.pack ()
        self.goto_var = IntVar ()
        self.goto_button = Checkbutton (self.control_frame,
                text = 'Goto FSM', variable = self.goto_var)
        self.goto_button.pack ()
        self.table_view.bind ('<1>', self.move)
        self.table_view.bind ('<3>', self.orient)

    def fsm_debug (self):
        self.io.fsm_debug ()

    def asserv_block (self):
        self.asserv.block ()

    def clamp_block (self):
        self.mimot.block ('a0')

    def move (self, ev):
        pos = self.table_view.screen_coord ((ev.x, ev.y))
        if self.goto_var.get ():
            self.io.goto (pos[0], pos[1], self.backward_var.get ())
        else:
            self.asserv.goto (pos[0], pos[1], self.backward_var.get ())

    def orient (self, ev):
        x, y = self.table_view.screen_coord ((ev.x, ev.y))
        robot_pos = self.robot_model.position.pos
        if robot_pos is not None:
            a = math.atan2 (y - robot_pos[1], x - robot_pos[0])
            self.asserv.goto_angle (a)

    def lower_clamp_rotate_command (self):
        self.mimot.speed_pos ('a0', self.LOWER_CLAMP_ROTATION_STROKE / 2)     

    def tree_detected(self):
        self.io.tree_detected()

    def stop_tree_approach(self):
        self.io.stop_tree_approach()

    def empty_tree(self):
        self.io.empty_tree()

    def robot_is_back(self):
        self.io.robot_is_back()

if __name__ == '__main__':
    run ('guybrush', TestSimuControl)