From 78d2f3d295c56e976c13b187b7723670411e4101 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 24 Apr 2013 22:44:30 +0200 Subject: digital/io-hub/src/apbirthday, host/simu: add cherries cannon simulation --- host/simu/model/table_eurobot2013.py | 8 ++++++++ host/simu/robots/apbirthday/link/bag.py | 2 +- host/simu/robots/apbirthday/model/bag.py | 3 ++- host/simu/robots/apbirthday/model/cannon.py | 27 +++++++++++++++++++++++++-- host/simu/robots/apbirthday/view/robot.py | 3 +++ host/simu/view/table_eurobot2013.py | 14 ++++++++++++++ 6 files changed, 53 insertions(+), 4 deletions(-) (limited to 'host') diff --git a/host/simu/model/table_eurobot2013.py b/host/simu/model/table_eurobot2013.py index d73b0564..0181ac00 100644 --- a/host/simu/model/table_eurobot2013.py +++ b/host/simu/model/table_eurobot2013.py @@ -22,6 +22,7 @@ # # }}} """Table model for Eurobot 2013.""" +from utils.observable import Observable import simu.model.table from simu.model.round_obstacle import RoundObstacle from simu.model.rectangular_obstacle import RectangularObstacle @@ -29,6 +30,12 @@ from math import pi import math import random +class Cherries (Observable): + + def __init__ (self): + Observable.__init__ (self) + self.cherries = [ ] + class Table (simu.model.table.Table): def __init__ (self, cards = None): @@ -94,6 +101,7 @@ class Table (simu.model.table.Table): for py in (250, 600, 1000, 1400, 1750): add_plate ((200, py), False) add_plate ((3000 - 200, py), True) + self.cherries = Cherries () # Gifts. self.gifts = [ ] def add_gift (pos, color): diff --git a/host/simu/robots/apbirthday/link/bag.py b/host/simu/robots/apbirthday/link/bag.py index f27f65fa..52c5c75f 100644 --- a/host/simu/robots/apbirthday/link/bag.py +++ b/host/simu/robots/apbirthday/link/bag.py @@ -34,7 +34,7 @@ class Bag: self.asserv = asserv.mex.Mex (node, '%s:asserv0' % instance, aux_nb = 0) self.io_hub = io_hub.mex.Mex (node, '%s:io0' % instance, gpios = True, - adc_channels = True) + adc_channels = True, potentiometer = True) for gpio in io_hub.apbirthday.gpios: setattr (self, gpio, MexGpio (self.io_hub.gpios, gpio)) adc_channels = self.io_hub.adc_channels diff --git a/host/simu/robots/apbirthday/model/bag.py b/host/simu/robots/apbirthday/model/bag.py index bbf5438e..c7330598 100644 --- a/host/simu/robots/apbirthday/model/bag.py +++ b/host/simu/robots/apbirthday/model/bag.py @@ -82,6 +82,7 @@ class Bag: link_bag.cherry_plate_clamp, scheduler, 0., 1., 10., 10., 0.), (Switch (link_bag.cherry_plate_left_contact), - Switch (link_bag.cherry_plate_right_contact))) + Switch (link_bag.cherry_plate_right_contact)), + link_bag.io_hub.potentiometer) self.pos_report = link_bag.io_hub.pos_report diff --git a/host/simu/robots/apbirthday/model/cannon.py b/host/simu/robots/apbirthday/model/cannon.py index 74810fd6..4fc78fd8 100644 --- a/host/simu/robots/apbirthday/model/cannon.py +++ b/host/simu/robots/apbirthday/model/cannon.py @@ -25,22 +25,29 @@ from utils.observable import Observable from simu.utils.trans_matrix import TransMatrix from simu.utils.vector import vector +import random +from math import pi class Cannon (Observable): + # TODO: update distance with real robot. + cannon_hit = (1000, 80) + def __init__ (self, table, robot_position, - arm_cyl, clamp_cyl, contacts): + arm_cyl, clamp_cyl, contacts, pot): Observable.__init__ (self) self.table = table self.robot_position = robot_position self.arm_cyl = arm_cyl self.clamp_cyl = clamp_cyl self.contacts = contacts + self.pot = pot self.plate = None self.cherries = [ ] self.robot_position.register (self.__robot_position_notified) self.arm_cyl.register (self.__arm_notified) self.clamp_cyl.register (self.__arm_notified) + self.pot.register (self.__pot_notified) def __robot_position_notified (self): if self.robot_position.pos is None: @@ -82,8 +89,24 @@ class Cannon (Observable): elif (self.plate is not None and self.plate.cherries and self.arm_cyl.pos < .1): # Load cherries. - self.cherries = self.plate.cherries + self.cherries.extend (self.plate.cherries) self.plate.cherries = [ ] + self.__pot_notified () + self.notify () + + def __pot_notified (self): + if self.cherries and self.pot.wiper[0] > 0.5: + m = TransMatrix () + m.translate (self.robot_position.pos) + m.rotate (self.robot_position.angle) + hit = vector (*m.apply (self.cannon_hit)) + for c in self.cherries: + c.pos = hit + vector.polar (random.uniform (-pi, pi), + random.uniform (0, 50)) + c.notify () + self.table.cherries.cherries.extend (self.cherries) + self.table.cherries.notify () + self.cherries = [ ] self.notify () def __plate_drop_point (self): diff --git a/host/simu/robots/apbirthday/view/robot.py b/host/simu/robots/apbirthday/view/robot.py index e4153232..9675aff5 100644 --- a/host/simu/robots/apbirthday/view/robot.py +++ b/host/simu/robots/apbirthday/view/robot.py @@ -27,6 +27,7 @@ from simu.view.table_eurobot2013 import PINK, colors COLOR_ROBOT = '#000000' COLOR_AXES = '#202040' +COLOR_CANNON = '#808080' class Robot (simu.inter.drawable.Drawable): @@ -69,6 +70,8 @@ class Robot (simu.inter.drawable.Drawable): # Draw robot body. self.draw_polygon ((102, 140), (102, -140), (-108, -140), (-108, 70), (-58, 140), fill = COLOR_ROBOT) + self.draw_circle ((70, self.cannon_model.cannon_hit[1]), 20, + fill = COLOR_CANNON) # Draw Robot axis. self.draw_line ((-50, 0), (50, 0), fill = COLOR_AXES, arrow = 'last') diff --git a/host/simu/view/table_eurobot2013.py b/host/simu/view/table_eurobot2013.py index 6acd847d..abb87906 100644 --- a/host/simu/view/table_eurobot2013.py +++ b/host/simu/view/table_eurobot2013.py @@ -90,6 +90,19 @@ class Plate (Drawable): self.draw_circle (c.pos, c.radius, fill = colors[c.color]) Drawable.draw (self) +class Cherries (Drawable): + + def __init__ (self, onto, model): + Drawable.__init__ (self, onto) + self.model = model + self.model.register (self.update) + + def draw (self): + self.reset () + for c in self.model.cherries: + if c.pos: + self.draw_circle (c.pos, c.radius, fill = colors[c.color]) + class Gift (Drawable): def __init__ (self, onto, model): @@ -122,6 +135,7 @@ class Table (Drawable): Glass (self, e) for e in self.model.plates: Plate (self, e) + Cherries (self, self.model.cherries) for e in self.model.gifts: Gift (self, e) -- cgit v1.2.3