From 75cc934b072310f22c5fe349168e5e4cdc231c4c Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 28 Mar 2013 23:28:13 +0100 Subject: host/simu/robots/apbirthday: add cake arm simulation Still need color sensors simulation. --- host/simu/robots/apbirthday/model/bag.py | 15 +++++ host/simu/robots/apbirthday/model/cake_arm.py | 91 +++++++++++++++++++++++++++ host/simu/robots/apbirthday/view/bag.py | 2 +- host/simu/robots/apbirthday/view/robot.py | 14 ++++- 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 host/simu/robots/apbirthday/model/cake_arm.py (limited to 'host/simu/robots/apbirthday') diff --git a/host/simu/robots/apbirthday/model/bag.py b/host/simu/robots/apbirthday/model/bag.py index bef099f9..5488305e 100644 --- a/host/simu/robots/apbirthday/model/bag.py +++ b/host/simu/robots/apbirthday/model/bag.py @@ -26,6 +26,8 @@ from simu.model.switch import Switch from simu.model.position import Position from simu.model.round_obstacle import RoundObstacle from simu.model.distance_sensor_sensopart import DistanceSensorSensopart +from simu.model.pneumatic_cylinder import PneumaticCylinder +from simu.robots.apbirthday.model.cake_arm import CakeArm from math import pi import random @@ -49,4 +51,17 @@ class Bag: DistanceSensorSensopart (link_bag.adc_dist[3], scheduler, table, (-50, -120), pi, (self.position, ), 4), ] + self.cake_arm = CakeArm (table, self.position, + PneumaticCylinder ( + link_bag.cake_arm_in, + link_bag.cake_arm_out, + scheduler, 0., 1., 1., 1., 1.), + PneumaticCylinder ( + link_bag.cake_push_far_in, + link_bag.cake_push_far_out, + scheduler, 0., 1., 10., 10., 0.), + PneumaticCylinder ( + link_bag.cake_push_near_in, + link_bag.cake_push_near_out, + scheduler, 0., 1., 10., 10., 0.)) diff --git a/host/simu/robots/apbirthday/model/cake_arm.py b/host/simu/robots/apbirthday/model/cake_arm.py new file mode 100644 index 00000000..0d91b7af --- /dev/null +++ b/host/simu/robots/apbirthday/model/cake_arm.py @@ -0,0 +1,91 @@ +# simu - Robot simulation. {{{ +# +# Copyright (C) 2013 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. +# +# }}} +"""APBirthday cake arm model.""" +from utils.observable import Observable +from simu.utils.trans_matrix import TransMatrix + +class CakeArm (Observable): + + far_x = 51 + far_y = 350 + near_x = 2 + near_y = 247 + + def __init__ (self, table, robot_position, arm_cyl, far_cyl, near_cyl): + Observable.__init__ (self) + self.table = table + self.robot_position = robot_position + self.arm_cyl = arm_cyl + self.far_cyl = far_cyl + self.near_cyl = near_cyl + self.far_pushed = False + self.near_pushed = False + self.arm_cyl.register (self.notify) + self.far_cyl.register (self.__push_notified) + self.near_cyl.register (self.__push_notified) + self.robot_position.register (self.__robot_position_notified) + + def __push_notified (self): + if self.arm_cyl.pos > .9: + if not self.far_pushed and self.far_cyl.pos > .5: + self.far_pushed = True + self.__push (self.far_x, self.far_y, 3) + elif self.far_pushed and self.far_cyl.pos < .9: + self.far_pushed = False + if not self.near_pushed and self.near_cyl.pos > .5: + self.near_pushed = True + self.__push (self.near_x, self.near_y, 2) + elif self.near_pushed and self.near_cyl.pos < .9: + self.near_pushed = False + self.notify () + + def __push (self, x, y, level): + """Push a candle under coordinates x, y.""" + margin = 60 + # Matrix to transform an obstacle position into robot coordinates. + m = self.__get_robot_matrix () + # Look up elements. + for o in self.table.obstacles: + if (o.level == level and hasattr (o, 'state') and o.state == False + and o.pos is not None): + pos = m.apply (o.pos) + if (pos[0] > x - margin + and pos[0] < x + margin + and pos[1] > y - margin + and pos[1] < y + margin): + o.state = True + o.notify () + + def __robot_position_notified (self): + # TODO: update color sensors. + pass + + def __get_robot_matrix (self): + """Return robot transformation matrix.""" + m = TransMatrix () + m.rotate (-self.robot_position.angle) + m.translate ((-self.robot_position.pos[0], + -self.robot_position.pos[1])) + return m + diff --git a/host/simu/robots/apbirthday/view/bag.py b/host/simu/robots/apbirthday/view/bag.py index dfea21fa..7956e5f2 100644 --- a/host/simu/robots/apbirthday/view/bag.py +++ b/host/simu/robots/apbirthday/view/bag.py @@ -32,7 +32,7 @@ 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.cake_arm) self.distance_sensor = [DistanceSensorUS (self.robot, ds) for ds in model_bag.distance_sensor] diff --git a/host/simu/robots/apbirthday/view/robot.py b/host/simu/robots/apbirthday/view/robot.py index 398d34c8..cb489fb0 100644 --- a/host/simu/robots/apbirthday/view/robot.py +++ b/host/simu/robots/apbirthday/view/robot.py @@ -29,11 +29,13 @@ COLOR_AXES = '#202040' class Robot (simu.inter.drawable.Drawable): - def __init__ (self, onto, position_model): + def __init__ (self, onto, position_model, cake_arm_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.cake_arm_model = cake_arm_model + self.cake_arm_model.register (self.update) def __position_notified (self): """Called on position modifications.""" @@ -59,6 +61,16 @@ class Robot (simu.inter.drawable.Drawable): self.draw_line ((0, +f / 2), (0, -f / 2), fill = COLOR_AXES) self.draw_line ((-wr, f / 2), (+wr, f / 2), fill = COLOR_AXES) self.draw_line ((-wr, -f / 2), (+wr, -f / 2), fill = COLOR_AXES) + # Draw arm. + m = self.cake_arm_model + f = m.arm_cyl.pos + for x, y, pos in ((m.far_x, m.far_y, m.far_cyl.pos), + (m.near_x, m.near_y, m.near_cyl.pos)): + e = (y - 140) * f + 140 + gray = pos * 0x40 + fill = '#%02x%02x%02x' % (gray, gray, gray) + self.draw_polygon ((x - 20, 140), (x - 20, e), (x + 20, e), + (x + 20, 140), fill = fill) # Extends. simu.inter.drawable.Drawable.draw (self) -- cgit v1.2.3