From 5532fb5e50d071be08e3c37404d1755748762d7c Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 29 Apr 2010 23:04:20 +0200 Subject: host/simu/robots/marcel: add actuator simulation, refs #131 --- host/simu/robots/marcel/link/bag.py | 2 + host/simu/robots/marcel/model/bag.py | 3 + host/simu/robots/marcel/model/loader.py | 135 ++++++++++++++++++++++++++++++++ host/simu/robots/marcel/view/bag.py | 5 +- host/simu/robots/marcel/view/loader.py | 76 ++++++++++++++++++ host/simu/robots/marcel/view/robot.py | 36 ++++++++- 6 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 host/simu/robots/marcel/model/loader.py create mode 100644 host/simu/robots/marcel/view/loader.py diff --git a/host/simu/robots/marcel/link/bag.py b/host/simu/robots/marcel/link/bag.py index e702078b..7cb34c28 100644 --- a/host/simu/robots/marcel/link/bag.py +++ b/host/simu/robots/marcel/link/bag.py @@ -24,10 +24,12 @@ """Marcel bag of links.""" import io.mex import asserv.mex +import mimot.mex class Bag: def __init__ (self, node): self.asserv = asserv.mex.Mex (node) self.io = io.mex.Mex (node) + self.mimot = mimot.mex.Mex (node) diff --git a/host/simu/robots/marcel/model/bag.py b/host/simu/robots/marcel/model/bag.py index 5cf276de..ce8eaab4 100644 --- a/host/simu/robots/marcel/model/bag.py +++ b/host/simu/robots/marcel/model/bag.py @@ -24,6 +24,7 @@ """Marcel bag of models.""" from simu.model.switch import Switch from simu.model.position import Position +from simu.robots.marcel.model.loader import Loader from simu.model.distance_sensor_sensopart import DistanceSensorSensopart from math import pi @@ -34,6 +35,8 @@ class Bag: self.color_switch = Switch (link_bag.io.color_switch) self.contact = [ Switch (contact) for contact in link_bag.io.contact ] self.position = Position (link_bag.asserv.position) + self.loader = Loader (table, self.position, link_bag.mimot.aux[0], + link_bag.mimot.aux[1], link_bag.asserv.aux[0]) self.distance_sensor = [ DistanceSensorSensopart (link_bag.io.adc[0], scheduler, table, (30 - 20, 0), 0, (self.position, ), 2), diff --git a/host/simu/robots/marcel/model/loader.py b/host/simu/robots/marcel/model/loader.py new file mode 100644 index 00000000..00741808 --- /dev/null +++ b/host/simu/robots/marcel/model/loader.py @@ -0,0 +1,135 @@ +# simu - Robot simulation. {{{ +# +# Copyright (C) 2010 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. +# +# }}} +"""Marcel food loader.""" +from utils.observable import Observable +from simu.utils.trans_matrix import TransMatrix +from math import pi + +class Loader (Observable): + + CLAMP_PULLEY_RADIUS = 5.5 + CLAMP_WIDTH = 250 + CLAMP_LENGTH = 100 + + ELEVATOR_PULLEY_RADIUS = 10 + ELEVATOR_LINEAR_STROKE = 50 + ELEVATOR_ROTATING_STROKE = 100 + + FRONT_ZONE_X_MIN = 120 + 15 + FRONT_ZONE_X_MAX = 120 + CLAMP_LENGTH - 15 + + def __init__ (self, table, robot_position, left_clamp_link, right_clamp_link, + elevator_link): + Observable.__init__ (self) + self.table = table + self.robot_position = robot_position + self.clamp_link = (left_clamp_link, right_clamp_link) + self.elevator_link = elevator_link + self.clamp_load = [ ] + self.load = [ ] + self.clamp_pos = [ None, None ] + self.clamp_link[0].register (self.__left_clamp_notified) + self.clamp_link[1].register (self.__right_clamp_notified) + self.elevator_link.register (self.__elevator_notified) + + def __elevator_notified (self): + if self.elevator_link.angle is None: + self.elevator_height = None + self.elevator_angle = None + else: + # Update elevator position. + self.elevator_height = (self.elevator_link.angle + * self.ELEVATOR_PULLEY_RADIUS) + if self.elevator_height < self.ELEVATOR_LINEAR_STROKE: + self.elevator_angle = 0 + else: + self.elevator_angle = ((self.elevator_height - + self.ELEVATOR_LINEAR_STROKE) + / self.ELEVATOR_ROTATING_STROKE * pi) + self.notify () + + def __clamp_notified (self, clamp): + if self.clamp_link[clamp].angle is None: + self.clamp_pos[clamp] = None + else: + # Update clamp position. + self.clamp_pos[clamp] = (self.clamp_link[clamp].angle + * self.CLAMP_PULLEY_RADIUS) + # If elevator is low, pick elements. + if (not self.clamp_load + and self.elevator_height < self.ELEVATOR_LINEAR_STROKE): + elements = self.__get_front_elements () + if elements: + black = sum (hasattr (e, 'black') and e.black for e in + elements) + tickness = sum (e.radius * 2 for e in elements) + if (not black and self.CLAMP_WIDTH - sum (self.clamp_pos) + <= tickness): + for e in elements: + e.pos = None + e.notify () + self.clamp_load = elements + # If elevator is high, drop elements. + if (self.clamp_load + and self.elevator_height > self.ELEVATOR_LINEAR_STROKE + + self.ELEVATOR_ROTATING_STROKE * 2 / 3): + tickness = sum (e.radius * 2 for e in self.clamp_load) + if self.CLAMP_WIDTH - sum (self.clamp_pos) > tickness: + self.load += self.clamp_load + self.clamp_load = [ ] + self.notify () + + def __left_clamp_notified (self): + self.__clamp_notified (0) + + def __right_clamp_notified (self): + self.__clamp_notified (1) + + def __get_front_elements (self): + """Return a list of elements in front of the robot, between clamp.""" + elements = [ ] + if (self.robot_position is None + or self.clamp_pos[0] is None + or self.clamp_pos[1] is None): + return None + # Matrix to transform an obstacle position into robot coordinates. + m = TransMatrix () + m.translate ((-self.robot_position.pos[0], + -self.robot_position.pos[1])) + m.rotate (-self.robot_position.angle) + # Look up elements. + # This could be used if clamp blocking is handled or elements are + # pushed: + #ymin = -self.CLAMP_WIDTH / 2 + self.clamp_pos[1] + #ymax = self.CLAMP_WIDTH / 2 - self.clamp_pos[0] + ymin = -self.CLAMP_WIDTH + 40 + ymax = self.CLAMP_WIDTH - 40 + for o in self.table.obstacles: + if o.level == 1 and o.pos is not None: + pos = m.apply (o.pos) + if (pos[0] > self.FRONT_ZONE_X_MIN + and pos[0] < self.FRONT_ZONE_X_MAX + and pos[1] > ymin and pos[1] < ymax): + elements.append (o) + return elements diff --git a/host/simu/robots/marcel/view/bag.py b/host/simu/robots/marcel/view/bag.py index 75f7ff50..fb2182a8 100644 --- a/host/simu/robots/marcel/view/bag.py +++ b/host/simu/robots/marcel/view/bag.py @@ -27,6 +27,7 @@ from simu.view.distance_sensor_us import DistanceSensorUS from simu.view.path import Path from simu.view.pos_report import PosReport from simu.robots.marcel.view.robot import Robot +from simu.robots.marcel.view.loader import Loader class Bag: @@ -34,7 +35,9 @@ class Bag: self.jack = Switch (sensor_frame, model_bag.jack, 'Jack') self.color_switch = Switch (sensor_frame, model_bag.color_switch, 'Color') - self.robot = Robot (table, model_bag.position) + self.robot = Robot (table, model_bag.position, model_bag.loader) + self.loader = Loader (actuator_view.add_view (Loader.width, + Loader.height), model_bag.loader) self.distance_sensor = [DistanceSensorUS (self.robot, ds) for ds in model_bag.distance_sensor] self.path = Path (table, model_bag.path) diff --git a/host/simu/robots/marcel/view/loader.py b/host/simu/robots/marcel/view/loader.py new file mode 100644 index 00000000..e640157b --- /dev/null +++ b/host/simu/robots/marcel/view/loader.py @@ -0,0 +1,76 @@ +# simu - Robot simulation. {{{ +# +# Copyright (C) 2010 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. +# +# }}} +"""Marcel loader.""" +from simu.inter.drawable import Drawable + +from simu.view.table_eurobot2010 import RED, corn_attr + +class Loader (Drawable): + + width = 420 + height = 370 + + def __init__ (self, onto, model): + Drawable.__init__ (self, onto) + self.model = model + self.model.register (self.__notified) + + def __notified (self): + self.update () + + def draw (self): + self.reset () + self.trans_translate ((-100, -125)) + self.draw_line ((25, 100), (25, 50), (300, 0), fill = '#808080') + if self.model.load: + # Draw load. + y = 0 + x = 0 + for e in self.model.load: + if hasattr (e, 'black'): + attr = corn_attr[e.black] + else: + attr = dict (fill = RED) + cx = x + e.radius + cy = y + cx * 50 / 300 + e.radius + self.draw_circle ((300 - cx, cy), e.radius, **attr) + x += e.radius + if x > 200: + x = 0 + y += 40 + if self.model.elevator_height is not None: + self.trans_identity () + self.trans_rotate (-self.model.elevator_angle) + self.trans_translate ((-100, -100 + self.model.elevator_height)) + # Draw clamp load. + if self.model.clamp_load: + elements = self.model.clamp_load + for e in elements: + if hasattr (e, 'black'): + self.draw_rectangle ((-25, -25), (-75, 125), + **corn_attr[e.black]) + else: + self.draw_circle ((-50, 0), e.radius, fill = RED) + # Draw clamp. + self.draw_line ((-100, -25), (0, -25), (0, 25), (-100, 25)) diff --git a/host/simu/robots/marcel/view/robot.py b/host/simu/robots/marcel/view/robot.py index 181d348b..4e2135f2 100644 --- a/host/simu/robots/marcel/view/robot.py +++ b/host/simu/robots/marcel/view/robot.py @@ -23,14 +23,19 @@ # }}} """Marcel robot view.""" import simu.inter.drawable +from math import cos + +from simu.view.table_eurobot2010 import RED, corn_attr class Robot (simu.inter.drawable.Drawable): - def __init__ (self, onto, position_model): + def __init__ (self, onto, position_model, loader_model): """Construct and make connections.""" simu.inter.drawable.Drawable.__init__ (self, onto) self.position_model = position_model self.position_model.register (self.__position_notified) + self.loader_model = loader_model + self.loader_model.register (self.__loader_notified) def __position_notified (self): """Called on position modifications.""" @@ -38,6 +43,11 @@ class Robot (simu.inter.drawable.Drawable): self.angle = self.position_model.angle self.update () + def __loader_notified (self): + """Called on loader modifications.""" + self.clamp_pos = self.loader_model.clamp_pos + self.update () + def draw (self): """Draw the robot.""" self.reset () @@ -57,6 +67,30 @@ class Robot (simu.inter.drawable.Drawable): self.draw_line ((0, +f / 2), (0, -f / 2), fill = axes_fill) self.draw_line ((-wr, f / 2), (+wr, f / 2), fill = axes_fill) self.draw_line ((-wr, -f / 2), (+wr, -f / 2), fill = axes_fill) + # Draw robot clamp. + if (self.clamp_pos[0] is not None + and self.clamp_pos[1] is not None): + l = self.loader_model.CLAMP_LENGTH * cos ( + self.loader_model.elevator_angle) + y = self.loader_model.CLAMP_WIDTH / 2 - self.clamp_pos[0] + self.draw_line ((120, y - 10), (120 + 0.15 * l, y), + (120 + 0.85 * l, y), (120 + l, y - 10)) + y = -self.loader_model.CLAMP_WIDTH / 2 + self.clamp_pos[1] + self.draw_line ((120, y + 10), (120 + 0.15 * l, y), + (120 + 0.85 * l, y), (120 + l, y + 10)) + # Draw clamp load. + if self.loader_model.clamp_load: + elements = self.loader_model.clamp_load + tickness = sum (e.radius * 2 for e in elements) + y = tickness / 2 + for e in elements: + if hasattr (e, 'black'): + attr = corn_attr[e.black] + else: + attr = dict (fill = RED) + self.draw_circle ((120 + l / 2, y - e.radius), + e.radius, **attr) + y -= e.radius * 2 # Extends. simu.inter.drawable.Drawable.draw (self) -- cgit v1.2.3