summaryrefslogtreecommitdiffhomepage
path: root/host/simu/inter
diff options
context:
space:
mode:
authorNicolas Schodet2009-04-30 09:53:44 +0200
committerNicolas Schodet2009-04-30 09:53:44 +0200
commit69305ae1ed6da084b6ee65dba3ce44f0d6cbe2d1 (patch)
treebb898529fc7e986429094dd9b5d28ce4397dbba7 /host/simu/inter
parentfb3cfa5a0ac1acb58c2067cd1aad0339b534f982 (diff)
* host/simu, digital/io/tools:
- new simulation infrastructure.
Diffstat (limited to 'host/simu/inter')
-rw-r--r--host/simu/inter/drawable.py10
-rw-r--r--host/simu/inter/inter.py127
-rw-r--r--host/simu/inter/inter_node.py110
3 files changed, 247 insertions, 0 deletions
diff --git a/host/simu/inter/drawable.py b/host/simu/inter/drawable.py
index d11ad9d6..bcf1823f 100644
--- a/host/simu/inter/drawable.py
+++ b/host/simu/inter/drawable.py
@@ -140,6 +140,16 @@ class DrawableCanvas(Tkinter.Canvas):
self.__updated = [ ]
self._Drawable__children = [ ]
+ def resize (self, width, height, xorigin = None, yorigin = None):
+ """Change size given at initialisation. Will be used on next
+ configure event."""
+ self.__width = width
+ self.__height = height
+ if xorigin is not None:
+ self.__xorigin = xorigin
+ if yorigin is not None:
+ self.__yorigin = yorigin
+
def __resize (self, ev):
# Compute new scale.
w, h = float (ev.width), float (ev.height)
diff --git a/host/simu/inter/inter.py b/host/simu/inter/inter.py
new file mode 100644
index 00000000..b8a889e5
--- /dev/null
+++ b/host/simu/inter/inter.py
@@ -0,0 +1,127 @@
+# simu - Robot simulation. {{{
+#
+# Copyright (C) 2009 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.
+#
+# }}}
+"""Simulation interface."""
+from simu.inter.drawable import *
+from Tkinter import *
+
+class TableView (DrawableCanvas):
+ """This class handle the view of the table and every items inside it."""
+
+ TABLE_WIDTH = 3000
+ TABLE_HEIGHT = 2100
+ MARGIN = 150
+
+ def __init__ (self, master = None,
+ width = TABLE_WIDTH, height = TABLE_HEIGHT):
+ DrawableCanvas.__init__ (self,
+ width + 2 * self.MARGIN, height + 2 * self.MARGIN,
+ -width / 2, -height / 2,
+ master, borderwidth = 1, relief = 'sunken',
+ background = 'white')
+
+class ActuatorView (DrawableCanvas):
+ """This class handle the view of the actuators inside the robot."""
+
+ UNIT = 120
+
+ def __init__ (self, master = None):
+ DrawableCanvas.__init__ (self, 1, 1, 0, 0, master,
+ borderwidth = 1, relief = 'sunken', background = 'white')
+ self.configure (width = self.UNIT, height = self.UNIT)
+ self.size = 0.0
+
+ def add_view (self, width = 1.0, height = 1.0):
+ """Return a drawable suitable for an actuator view."""
+ ratio = float (height) / float (width)
+ self.size += ratio
+ self.resize (1, self.size, 0, self.size / 2)
+ self.configure (width = self.UNIT, height = self.UNIT * self.size)
+ d = Drawable (self)
+ d.trans_scale (1.0 / width)
+ d.trans_translate ((0, - self.size + ratio / 2))
+ return d
+
+class Inter (Frame):
+ """Robot simulation interface."""
+
+ def __init__ (self, master = None):
+ Frame.__init__ (self, master)
+ self.pack (expand = True, fill = 'both')
+ self.create_widgets ()
+
+ def create_widgets (self):
+ # Main layout.
+ self.right_frame = Frame (self)
+ self.right_frame.pack (side = 'right', fill = 'y')
+ self.quit_button = Button (self.right_frame, text = 'Quit',
+ command = self.quit)
+ self.quit_button.pack (side = 'top')
+ # Actuator view.
+ self.actuator_view = ActuatorView (self.right_frame)
+ self.actuator_view.pack (side = 'bottom', fill = 'x')
+ # Sensor frame.
+ self.sensor_frame = Frame (self.right_frame, borderwidth = 1,
+ relief = 'sunken')
+ self.sensor_frame.pack (side = 'bottom', fill = 'x')
+ # Table view.
+ self.table_view = TableView (self)
+ self.table_view.pack (expand = True, fill = 'both')
+
+ def update (self, *args):
+ self.table_view.update ()
+ self.actuator_view.update ()
+
+if __name__ == '__main__':
+ class TestTable (Drawable):
+ def __init__ (self, onto, w, h):
+ Drawable.__init__ (self, onto)
+ self.w, self.h = w, h
+ def draw (self):
+ self.reset ()
+ w, h = self.w, self.h
+ self.draw_rectangle ((0, 0), (w, h), fill = 'blue')
+ Drawable.draw (self)
+ class TestRectangle (Drawable):
+ def __init__ (self, onto, w, h, c1, c2):
+ Drawable.__init__ (self, onto)
+ self.w, self.h = 0.9 * w, 0.9 * h
+ self.c1, self.c2 = c1, c2
+ def draw (self):
+ self.reset ()
+ w, h = self.w, self.h
+ self.draw_rectangle ((-w/2, -h/2), (w/2, h/2), fill = self.c1)
+ self.draw_rectangle ((0, 0), (w/2, h/2), fill = self.c2)
+ Drawable.draw (self)
+ class TestSensor:
+ def __init__ (self, master):
+ self.button = Checkbutton (master, text = 'Sensor',
+ indicatoron = False)
+ self.button.pack (side = 'top')
+ app = Inter ()
+ TestTable (app.table_view, 3000, 2100)
+ TestRectangle (app.actuator_view.add_view (1, 1), 1, 1, 'red', 'green')
+ TestRectangle (app.actuator_view.add_view (2, 1), 2, 1, 'green', 'blue')
+ TestRectangle (app.actuator_view.add_view (1, 2), 1, 2, 'blue', 'red')
+ TestSensor (app.sensor_frame)
+ app.mainloop ()
diff --git a/host/simu/inter/inter_node.py b/host/simu/inter/inter_node.py
new file mode 100644
index 00000000..ab1b5acf
--- /dev/null
+++ b/host/simu/inter/inter_node.py
@@ -0,0 +1,110 @@
+# simu - Robot simulation. {{{
+#
+# Copyright (C) 2009 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.
+#
+# }}}
+"""Simulation interface coupled with a mex node."""
+from simu.inter.inter import Inter
+from mex.node import Node
+from Tkinter import *
+import time
+
+class InterNode (Inter):
+
+ def __init__ (self):
+ Inter.__init__ (self)
+ # Create node and bind to Tk.
+ self.node = Node ()
+ self.node.tick = 900 # tick/s
+ self.tk.createfilehandler (self.node, READABLE, self.read)
+ # Animation attributes.
+ self.date = 0
+ self.synced = True
+ self.step_after = None
+ self.step_time = None
+
+ def create_widgets (self):
+ Inter.create_widgets (self)
+ self.now_label = Label (self.right_frame, text = 'Now: 0 s')
+ self.now_label.pack ()
+ self.step_button = Button (self.right_frame, text = 'Step',
+ command = self.step)
+ self.step_button.pack ()
+ self.step_size_scale = Scale (self.right_frame, orient = HORIZONTAL,
+ from_ = 0.05, to = 1.0, resolution = 0.05)
+ self.step_size_scale.pack ()
+ self.play_var = IntVar ()
+ self.play_button = Checkbutton (self.right_frame,
+ variable = self.play_var, text = 'Play', command = self.play)
+ self.play_button.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.date
+ + int (self.step_size_scale.get () * self.node.tick))
+ self.synced = False
+ self.step_after = None
+ self.step_time = time.time ()
+
+ def play (self):
+ """Activate auto-steping."""
+ if self.play_var.get ():
+ if self.step_after is None and self.synced:
+ self.step ()
+ self.step_button.configure (state = DISABLED)
+ else:
+ if self.step_after is not None:
+ self.after_cancel (self.step_after)
+ self.step_after = None
+ self.step_button.configure (state = NORMAL)
+
+ def read (self, file, mask):
+ """Handle event on the Node."""
+ self.node.read ()
+ if not self.synced and self.node.sync ():
+ self.synced = True
+ self.date = self.node.date
+ self.now_label.configure (text = 'Now: %.2f s'
+ % (float (self.date) / self.node.tick))
+ self.update ()
+ if self.play_var.get ():
+ assert self.step_after is None
+ next = self.step_time + self.step_size_scale.get ()
+ delay = next - time.time ()
+ if delay > 0:
+ self.step_after = self.after (int (delay * 1000),
+ self.step)
+ else:
+ self.step ()
+
+if __name__ == '__main__':
+ import mex.hub
+ import utils.forked
+ h = mex.hub.Hub (min_clients = 1)
+ fh = utils.forked.Forked (h.wait)
+ try:
+ app = InterNode ()
+ app.mainloop()
+ finally:
+ fh.kill ()
+ import time
+ time.sleep (1)