From c3f8b56ef5a44d0569566497b2a24550842c7512 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 6 May 2011 23:51:45 +0200 Subject: digital/{ai,io}: move simu test script --- digital/ai/tools/marcel.py | 39 ++++++ digital/ai/tools/robospierre.py | 39 ++++++ digital/ai/tools/test_simu.py | 151 ++++++++++++++++++++++ digital/ai/tools/test_simu_control_marcel.py | 108 ++++++++++++++++ digital/ai/tools/test_simu_control_robospierre.py | 101 +++++++++++++++ 5 files changed, 438 insertions(+) create mode 100644 digital/ai/tools/marcel.py create mode 100644 digital/ai/tools/robospierre.py create mode 100644 digital/ai/tools/test_simu.py create mode 100644 digital/ai/tools/test_simu_control_marcel.py create mode 100644 digital/ai/tools/test_simu_control_robospierre.py (limited to 'digital/ai/tools') diff --git a/digital/ai/tools/marcel.py b/digital/ai/tools/marcel.py new file mode 100644 index 00000000..32995326 --- /dev/null +++ b/digital/ai/tools/marcel.py @@ -0,0 +1,39 @@ +import simu.model.table_eurobot2010 +import simu.view.table_eurobot2010 + +import simu.robots.marcel.link.bag +import simu.robots.marcel.model.bag +import simu.robots.marcel.view.bag + +import asserv +import asserv.init +import mimot +import mimot.init +import io +import io.init + +from proto.popen_io import PopenIO +import math + +class Robot: + + def __init__ (self, proto_time): + self.table_model = simu.model.table_eurobot2010 + self.table_view = simu.view.table_eurobot2010 + self.robot_link = simu.robots.marcel.link.bag + self.robot_model = simu.robots.marcel.model.bag + self.robot_view = simu.robots.marcel.view.bag + asserv_cmd = ('../../asserv/src/asserv/asserv.host', '-m9', 'marcel') + mimot_cmd = ('../../mimot/src/dirty/dirty.host', '-m9', 'marcel') + io_cmd = ('../../io/src/io.host') + self.asserv = asserv.Proto (PopenIO (asserv_cmd), proto_time, + **asserv.init.host) + self.mimot = mimot.Proto (PopenIO (mimot_cmd), proto_time, + **mimot.init.host) + self.io = io.Proto (PopenIO (io_cmd), proto_time, + **io.init.host) + self.robot_start_pos = { + False: (300, 2100 - 305, math.radians (-270)), + True: (3000 - 300, 2100 - 305, math.radians (-270)) + } + diff --git a/digital/ai/tools/robospierre.py b/digital/ai/tools/robospierre.py new file mode 100644 index 00000000..23fb19a3 --- /dev/null +++ b/digital/ai/tools/robospierre.py @@ -0,0 +1,39 @@ +import simu.model.table_eurobot2011 +import simu.view.table_eurobot2011 + +import simu.robots.robospierre.link.bag +import simu.robots.robospierre.model.bag +import simu.robots.robospierre.view.bag + +import asserv +import asserv.init +import mimot +import mimot.init +import io +import io.init + +from proto.popen_io import PopenIO +import math + +class Robot: + + def __init__ (self, proto_time): + self.table_model = simu.model.table_eurobot2011 + self.table_view = simu.view.table_eurobot2011 + self.robot_link = simu.robots.robospierre.link.bag + self.robot_model = simu.robots.robospierre.model.bag + self.robot_view = simu.robots.robospierre.view.bag + asserv_cmd = ('../../asserv/src/asserv/asserv.host', '-m9', 'marcel') + mimot_cmd = ('../../mimot/src/dirty/dirty.host', '-m9', 'marcel') + io_cmd = ('../../io/src/io.host') + self.asserv = asserv.Proto (PopenIO (asserv_cmd), proto_time, + **asserv.init.host) + self.mimot = mimot.Proto (PopenIO (mimot_cmd), proto_time, + **mimot.init.host) + self.io = io.Proto (PopenIO (io_cmd), proto_time, + **io.init.host) + self.robot_start_pos = { + False: (700, 2100 - 250, math.radians (-270)), + True: (3000 - 700, 2100 - 250, math.radians (-270)) + } + diff --git a/digital/ai/tools/test_simu.py b/digital/ai/tools/test_simu.py new file mode 100644 index 00000000..80008bb9 --- /dev/null +++ b/digital/ai/tools/test_simu.py @@ -0,0 +1,151 @@ +# io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ +# +# 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. +# +# }}} +import mex.hub +import utils.forked + +import simu.model.round_obstacle as obstacle_model +import simu.view.round_obstacle as obstacle_view + +from simu.inter.inter_node import InterNode +from Tkinter import * + +class ObstacleWithBeacon (obstacle_view.RoundObstacle): + + def __init__ (self, onto, model, beacon_model): + obstacle_view.RoundObstacle.__init__ (self, onto, model) + self.beacon_model = beacon_model + + def __notified (self): + self.pos = self.model.pos + self.update () + + def draw (self): + obstacle_view.RoundObstacle.draw (self) + if self.pos: + self.draw_circle ((0, 0), self.beacon_model.radius, + fill = '#505050') + +class TestSimu (InterNode): + """Interface, with simulated programs.""" + + def __init__ (self, robot_class): + # Hub. + self.hub = mex.hub.Hub (min_clients = 4) + self.forked_hub = utils.forked.Forked (self.hub.wait) + # InterNode. + InterNode.__init__ (self) + def proto_time (): + return self.node.date / self.node.tick + # Robot parameters. + robot = robot_class (proto_time) + self.robot = robot + # Asserv. + self.asserv = robot.asserv + self.asserv.async = True + self.tk.createfilehandler (self.asserv, READABLE, self.asserv_read) + # Mimot. + self.mimot = robot.mimot + self.mimot.async = True + self.tk.createfilehandler (self.mimot, READABLE, self.mimot_read) + # Io. + self.io = robot.io + self.io.async = True + self.tk.createfilehandler (self.io, READABLE, self.io_read) + # Add table. + self.table_model = robot.table_model.Table () + self.table = robot.table_view.Table (self.table_view, self.table_model) + self.obstacle = obstacle_model.RoundObstacle (150) + self.table_model.obstacles.append (self.obstacle) + self.obstacle_beacon = obstacle_model.RoundObstacle (40, 2) + self.table_model.obstacles.append (self.obstacle_beacon) + self.obstacle_view = ObstacleWithBeacon (self.table, self.obstacle, + self.obstacle_beacon) + self.table_view.bind ('<2>', self.place_obstacle) + # Add robot. + self.robot_link = robot.robot_link.Bag (self.node) + self.robot_model = robot.robot_model.Bag (self.node, self.table_model, + self.robot_link) + self.robot_view = robot.robot_view.Bag (self.table, + self.actuator_view, self.sensor_frame, self.robot_model) + # Color switch. + self.robot_model.color_switch.register (self.change_color) + + def close (self): + self.forked_hub.kill () + import time + time.sleep (1) + + def asserv_read (self, file, mask): + self.asserv.proto.read () + self.asserv.proto.sync () + + def mimot_read (self, file, mask): + self.mimot.proto.read () + self.mimot.proto.sync () + + def io_read (self, file, mask): + self.io.proto.read () + self.io.proto.sync () + + def step (self): + """Overide step to handle retransmissions, could be made cleaner using + simulated time.""" + InterNode.step (self) + self.asserv.proto.sync () + self.mimot.proto.sync () + self.io.proto.sync () + + def change_color (self, *dummy): + i = self.robot_model.color_switch.state + self.asserv.set_simu_pos (*self.robot.robot_start_pos[i]); + + def place_obstacle (self, ev): + pos = self.table_view.screen_coord ((ev.x, ev.y)) + self.obstacle.pos = pos + self.obstacle_beacon.pos = pos + self.obstacle.notify () + self.obstacle_beacon.notify () + +def run (default_robot, test_class = TestSimu): + import optparse + parser = optparse.OptionParser () + parser.add_option ('-r', '--robot', help = "use specified robot", + metavar = 'NAME', default = default_robot) + (options, args) = parser.parse_args () + if args: + parser.error ("too many arguments") + if options.robot == 'marcel': + import marcel + robot = marcel.Robot + elif options.robot == 'robospierre': + import robospierre + robot = robospierre.Robot + else: + parser.error ("unknown robot") + app = test_class (robot) + app.mainloop () + app.close () + +if __name__ == '__main__': + run ('marcel') diff --git a/digital/ai/tools/test_simu_control_marcel.py b/digital/ai/tools/test_simu_control_marcel.py new file mode 100644 index 00000000..2a43b7bb --- /dev/null +++ b/digital/ai/tools/test_simu_control_marcel.py @@ -0,0 +1,108 @@ +# io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ +# +# 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. +# +# }}} +from test_simu import TestSimu, run +from Tkinter import * +import math + +class TestSimuControl (TestSimu): + """Interface with extra control.""" + + def __init__ (self, robot_class): + TestSimu.__init__ (self, robot_class) + + def create_widgets (self): + TestSimu.create_widgets (self) + self.control_frame = Frame (self) + self.control_frame.pack (side = 'left', before = self.table_view, + fill = 'y') + self.clamp_var = IntVar () + self.clamp_button = Checkbutton (self.control_frame, text = 'Clamp', + indicatoron = False, + variable = self.clamp_var, command = self.clamp_command) + self.clamp_button.pack () + self.elevator_var = IntVar () + self.elevator_button = Checkbutton (self.control_frame, + text = 'Elevator', indicatoron = False, + variable = self.elevator_var, command = self.elevator_command) + self.elevator_button.pack () + self.gate_var = IntVar () + self.gate_button = Checkbutton (self.control_frame, + text = 'Gate', indicatoron = False, + variable = self.gate_var, command = self.gate_command) + self.gate_button.pack () + self.loader_up_button = Button (self.control_frame, + text = 'Loader up', padx = 0, pady = 0, + command = self.loader_up_command) + self.loader_up_button.pack () + self.loader_down_button = Button (self.control_frame, + text = 'Loader down', padx = 0, pady = 0, + command = self.loader_down_command) + self.loader_down_button.pack () + self.table_view.bind ('<1>', self.move) + self.table_view.bind ('<3>', self.orient) + + def move (self, ev): + pos = self.table_view.screen_coord ((ev.x, ev.y)) + self.asserv.goto (pos[0], pos[1]) + + def orient (self, ev): + x, y = self.table_view.screen_coord ((ev.x, ev.y)) + robot_pos = self.robot_model.position.pos + if robot_pos is not None: + a = math.atan2 (y - robot_pos[1], x - robot_pos[0]) + self.asserv.goto_angle (a) + + def clamp_command (self): + if self.clamp_var.get (): + self.mimot.clamp ('a0', 0x60, 0x100) + self.mimot.clamp ('a1', 0x60, 0x100) + else: + self.mimot.goto_pos ('a0', 0) + self.mimot.goto_pos ('a1', 0) + + def elevator_command (self): + if self.elevator_var.get (): + pos = 7089 + else: + pos = 0 + self.asserv.goto_pos ('a0', pos) + + def gate_command (self): + if self.gate_var.get (): + pos = -0x1d6b + else: + pos = 0 + self.asserv.goto_pos ('a1', pos) + + def loader_up_command (self): + self.io.loader ('u') + + def loader_down_command (self): + self.io.loader ('d') + + def change_color (self, *dummy): + pass + +if __name__ == '__main__': + run ('marcel', TestSimuControl) diff --git a/digital/ai/tools/test_simu_control_robospierre.py b/digital/ai/tools/test_simu_control_robospierre.py new file mode 100644 index 00000000..9da57196 --- /dev/null +++ b/digital/ai/tools/test_simu_control_robospierre.py @@ -0,0 +1,101 @@ +# io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ +# +# Copyright (C) 2011 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. +# +# }}} +from test_simu import TestSimu, run +from Tkinter import * +import math + +class TestSimuControl (TestSimu): + """Interface with extra control.""" + + ELEVATION_STROKE = 0x3b0b + + ROTATION_STROKE = 0x11c6 + + def __init__ (self, robot_class): + TestSimu.__init__ (self, robot_class) + + def create_widgets (self): + TestSimu.create_widgets (self) + self.control_frame = Frame (self) + self.control_frame.pack (side = 'left', before = self.table_view, + fill = 'y') + self.clamp_var = IntVar () + self.clamp_var.set (1) + self.clamp_button = Checkbutton (self.control_frame, text = 'Clamp', + indicatoron = False, + variable = self.clamp_var, command = self.clamp_command) + self.clamp_button.pack () + self.elevation_up_button = Button (self.control_frame, + text = 'Elevation up', padx = 0, pady = 0, + command = self.elevation_up_command) + self.elevation_up_button.pack () + self.elevation_down_button = Button (self.control_frame, + text = 'Elevation down', padx = 0, pady = 0, + command = self.elevation_down_command) + self.elevation_down_button.pack () + self.rotation_cw_button = Button (self.control_frame, + text = 'Rotation cw', padx = 0, pady = 0, + command = self.rotation_cw_command) + self.rotation_cw_button.pack () + self.rotation_ccw_button = Button (self.control_frame, + text = 'Rotation ccw', padx = 0, pady = 0, + command = self.rotation_ccw_command) + self.rotation_ccw_button.pack () + self.table_view.bind ('<1>', self.move) + self.table_view.bind ('<3>', self.orient) + + def move (self, ev): + pos = self.table_view.screen_coord ((ev.x, ev.y)) + self.asserv.goto (pos[0], pos[1]) + + def orient (self, ev): + x, y = self.table_view.screen_coord ((ev.x, ev.y)) + robot_pos = self.robot_model.position.pos + if robot_pos is not None: + a = math.atan2 (y - robot_pos[1], x - robot_pos[0]) + self.asserv.goto_angle (a) + + def clamp_command (self): + if self.clamp_var.get (): + self.io.pwm_set (-0x7fff, 255) + else: + self.io.pwm_set (0x7fff, 255) + + def elevation_up_command (self): + self.mimot.speed_pos ('a0', self.ELEVATION_STROKE / 2) + + def elevation_down_command (self): + self.mimot.speed_pos ('a0', -self.ELEVATION_STROKE / 2) + + def rotation_cw_command (self): + self.mimot.speed_pos ('a1', self.ROTATION_STROKE / 2) + + def rotation_ccw_command (self): + self.mimot.speed_pos ('a1', -self.ROTATION_STROKE / 2) + + def change_color (self, *dummy): + pass + +if __name__ == '__main__': + run ('robospierre', TestSimuControl) -- cgit v1.2.3