summaryrefslogtreecommitdiffhomepage
path: root/digital/io/tools/test_simu.py
blob: 158b006e214342c0def75914c4a21e9e5e2f46c9 (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
# io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
#
# Copyright (C) 2008 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.
#
# }}}
import math

import mex.hub
import utils.forked

import asserv
import asserv.init
import io
import io.init
from proto.popen_io import PopenIO

import simu.view.table_eurobot2008 as table
import simu.robots.giboulee.link.bag as robot_link
import simu.robots.giboulee.model.bag as robot_model
import simu.robots.giboulee.view.bag as robot_view

from simu.inter.inter_node import InterNode
from Tkinter import *

class TestSimu (InterNode):
    """Interface, with simulated programs."""

    robot_start_pos = {
            False: (200, 2100 - 70, math.radians (-90)),
            True: (3000 - 200, 2100 - 70, math.radians (-90))
            }

    def __init__ (self, asserv_cmd, io_cmd):
        # Hub.
        self.hub = mex.hub.Hub (min_clients = 2)
        self.forked_hub = utils.forked.Forked (self.hub.wait)
        # InterNode.
        InterNode.__init__ (self)
        def time ():
            return self.node.date / self.node.tick
        # Asserv.
        self.asserv = asserv.Proto (PopenIO (asserv_cmd), time,
                **asserv.init.host)
        self.asserv.async = True
        self.tk.createfilehandler (self.asserv, READABLE, self.asserv_read)
        # Io.
        self.io = io.Proto (PopenIO (io_cmd), time, **io.init.host)
        self.io.async = True
        self.tk.createfilehandler (self.io, READABLE, self.io_read)
        # Add table.
        self.table = table.Table (self.table_view)
        # Add robot.
        self.robot_link = robot_link.Bag (self.node)
        self.robot_model = robot_model.Bag (self.robot_link)
        self.robot_view = robot_view.Bag (self.table, self.actuator_view,
                self.sensor_frame, self.robot_model)
        for adc in self.robot_link.io.adc:
            adc.value = 0
        # Color switch.
        self.robot_model.color_switch.register (self.change_color)
        self.change_color ()

    def close (self):
        self.forked_hub.kill ()
        import time
        time.sleep (1)
        self.asserv.close ()
        self.io.close ()

    def asserv_read (self, file, mask):
        self.asserv.proto.read ()
        self.asserv.proto.sync ()

    def io_read (self, file, mask):
        self.io.proto.read ()
        self.io.proto.sync ()

    def step (self):
        """Overide step to handle retransmissions, could be made cleaner using
        simulated time."""
        InterNode.step (self)
        self.asserv.proto.sync ()
        self.io.proto.sync ()

    def change_color (self, *dummy):
        i = self.robot_model.color_switch.state
        self.asserv.set_simu_pos (*self.robot_start_pos[i]);

if __name__ == '__main__':
    app = TestSimu (('../../asserv/src/asserv/asserv.host', '-m', 'giboulee'),
            ('../src/io.host'))
    app.mainloop ()