summaryrefslogtreecommitdiff
path: root/host/inter/inter_node.py
blob: 14be69084f7a080ab39633fe4eee24ecb13f630e (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
# inter - Robot simulation interface. {{{
#
# 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.
#
# }}}
"""Inter, coupled with a mex Node."""
if __name__ == '__main__':
    import sys
    sys.path.append (sys.path[0] + '/../mex')

from inter import Inter
from Tkinter import *
from mex.node import Node

class InterNode (Inter):
    """Inter, coupled with a mex Node."""

    # There is 900 tick per seconds, to permit AVR to have a 4.44444 ms
    # period.
    TICK = 900.0

    def __init__ (self):
	Inter.__init__ (self)
	self.node = Node ()
	self.node.register (0xa0, self.handle_asserv_0)
	self.node.register (0xa8, self.handle_asserv_8)
	self.tk.createfilehandler (self.node, READABLE, self.read)
	self.step_after = None

    def createWidgets (self):
	Inter.createWidgets (self)
	self.nowLabel = Label (self.rightFrame, text = 'Now: 0 s')
	self.nowLabel.pack ()
	self.stepButton = Button (self.rightFrame, text = 'Step',
		command = self.step)
	self.stepButton.pack ()
	self.stepSizeScale = Scale (self.rightFrame, orient = HORIZONTAL,
		from_ = 0.1, to = 1.0, resolution = 0.1)
	self.stepSizeScale.pack ()
	self.playVar = IntVar ()
	self.playButton = Checkbutton (self.rightFrame, variable =
		self.playVar, text = 'Play', command = self.play)
	self.playButton.pack ()

    def step (self):
	"""Do a step.  Signal to the Hub we are ready to wait to the next step
	date."""
	self.node.wait_async (self.node.date
		+ int (self.stepSizeScale.get () * self.TICK))
	self.step_after = None

    def play (self):
	"""Activate auto-steping."""
	if self.playVar.get ():
	    if self.step_after is None:
		self.step ()
	    self.stepButton.configure (state = DISABLED)
	else:
	    if self.step_after is not None:
		self.after_cancel (self.step_after)
		self.step_after = None
	    self.stepButton.configure (state = NORMAL)

    def read (self, file, mask):
	"""Handle event on the Node."""
	self.node.read ()
	if self.node.sync ():
	    self.synced = True
	    self.nowLabel.configure (text = 'Now: %.1f s' % (self.node.date /
		self.TICK))
	    self.update ()
	    if self.playVar.get ():
		self.step_after = self.after (int (self.stepSizeScale.get ()
		    * self.TICK), self.step)

    def handle_asserv_0 (self, msg):
	x, y, a = msg.pop ('hhl')
	self.tableview.robot.pos = (x, y)
	self.tableview.robot.angle = float (a) / 1024
	self.update (self.tableview.robot)

    def handle_asserv_8 (self, msg):
	a, = msg.pop ('l')
	self.actuatorview.arm.angle = float (a) / 1024
	self.update (self.actuatorview.arm)

if __name__ == '__main__':
    import mex.hub
    import mex.forked
    h = mex.hub.Hub (min_clients = 1)
    fh = mex.forked.Forked (h.wait)
    try:
	app = InterNode ()
	app.mainloop()
    finally:
	fh.kill ()
	import time
	time.sleep (1)