From 69305ae1ed6da084b6ee65dba3ce44f0d6cbe2d1 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 30 Apr 2009 09:53:44 +0200 Subject: * host/simu, digital/io/tools: - new simulation infrastructure. --- host/simu/view/__init__.py | 0 host/simu/view/servo.py | 54 ++++++++++++++++++++ host/simu/view/switch.py | 40 +++++++++++++++ host/simu/view/table_eurobot2008.py | 98 +++++++++++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 host/simu/view/__init__.py create mode 100644 host/simu/view/servo.py create mode 100644 host/simu/view/switch.py create mode 100644 host/simu/view/table_eurobot2008.py (limited to 'host/simu/view') diff --git a/host/simu/view/__init__.py b/host/simu/view/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/host/simu/view/servo.py b/host/simu/view/servo.py new file mode 100644 index 00000000..7c67df09 --- /dev/null +++ b/host/simu/view/servo.py @@ -0,0 +1,54 @@ +# 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. +# +# }}} +"""Generic servo motor.""" +from simu.inter.drawable import Drawable +from math import pi, sin, cos + +class Servo (Drawable): + + def __init__ (self, onto, model, coord = (0, 0), l = 1, start = 0, + extent = pi / 2): + Drawable.__init__ (self, onto) + self.model = model + self.coord = coord + self.l = l + self.start = start + self.extent = extent + self.model.register (self.__notified) + self.__notified () + + def __notified (self): + self.value = self.model.value + self.update () + + def draw (self): + self.reset () + if self.value is not None: + self.draw_arc (self.coord, self.l, start = self.start, + extent = self.extent, style = 'arc', outline = '#808080') + a = self.start + self.value * self.extent + self.draw_line (self.coord, (self.coord[0] + self.l * cos (a), + self.coord[1] + self.l * sin (a))) + Drawable.draw (self) + diff --git a/host/simu/view/switch.py b/host/simu/view/switch.py new file mode 100644 index 00000000..c0ec2d58 --- /dev/null +++ b/host/simu/view/switch.py @@ -0,0 +1,40 @@ +# 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. +# +# }}} +"""Generic switch.""" +from Tkinter import * + +class Switch: + + def __init__ (self, frame, model, text): + self.var = IntVar () + self.button = Checkbutton (frame, variable = self.var, + command = self.__update, text = text, indicatoron = False) + self.button.pack () + self.model = model + self.__update () + + def __update (self): + self.model.state = self.var.get () != 0 + self.model.notify () + diff --git a/host/simu/view/table_eurobot2008.py b/host/simu/view/table_eurobot2008.py new file mode 100644 index 00000000..b956101a --- /dev/null +++ b/host/simu/view/table_eurobot2008.py @@ -0,0 +1,98 @@ +# simu - Robot simulation. {{{ +# +# 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. +# +# }}} +"""Eurobot 2008 table.""" +from simu.inter.drawable import Drawable + +class Table (Drawable): + """The table and its elements.""" + + def draw (self): + # Redraw. + self.reset () + # Table. + self.draw_rectangle ((-22, -22 - 80), (3000 / 2, 2100 + 22), fill = '#ff1f1f') + self.draw_rectangle ((3000 / 2, -22 - 80), (3000 + 22, 2100 + 22), fill = '#201fff') + self.draw_rectangle ((0, 0), (3000, 2100), fill = '#a49d8b') + self.draw_rectangle ((0, -22 - 80), (3000, -22), fill = '#a49d8b') + self.draw_rectangle ((0, 2100 - 500), (500, 2100), fill = '#201fff') + self.draw_rectangle ((3000 - 500, 2100 - 500), (3000, 2100), fill = '#ff1f1f') + self.draw_line ((3000 / 2, -22 - 80), (3000 / 2, 2100 + 22)) + # Axes. + self.draw_line ((0, 200), (0, 0), (200, 0), arrow = 'both') + # Beacons and baskets. + self.draw_rectangle ((-22, 2100), (-22 - 80, 2100 + 80), fill = '#5b5b5d') + self.draw_rectangle ((-22, 1050 - 40), (-22 - 80, 1050 + 40), fill = '#5b5b5d') + self.draw_rectangle ((-22, 500), (-22 - 80, 500 + 80), fill = '#5b5b5d') + self.draw_rectangle ((-22, -80), (-22 - 80, 0), fill = '#5b5b5d') + self.draw_rectangle ((-22, 0), (-22 - 80, 500), fill = '#5b5b5d') + self.draw_rectangle ((-22 - 80 - 250, 0), (-22 - 80, 500), fill = '#6d6dad', stipple = 'gray75') + self.draw_rectangle ((3000 + 22, 2100), (3000 + 22 + 80, 2100 + 80), fill = '#5b5b5d') + self.draw_rectangle ((3000 + 22, 1050 - 40), (3000 + 22 + 80, 1050 + 40), fill = '#5b5b5d') + self.draw_rectangle ((3000 + 22, 500), (3000 + 22 + 80, 500 + 80), fill = '#5b5b5d') + self.draw_rectangle ((3000 + 22, -80), (3000 + 22 + 80, 0), fill = '#5b5b5d') + self.draw_rectangle ((3000 + 22, 0), (3000 + 22 + 80, 500), fill = '#5b5b5d') + self.draw_rectangle ((3000 + 22 + 80 + 250, 0), (3000 + 22 + 80, 500), fill = '#6d6dad', stipple = 'gray75') + # Vertical dispensers. + self.draw_rectangle ((-22, 2100 - 750 - 85 / 2), (0, 2100 - 750 + 85 / 2), fill = '#5b5b5b') + self.draw_circle ((40, 2100 - 750), 40) + self.draw_rectangle ((700 - 85 / 2, 2100), (700 + 85 / 2, 2100 + 22), fill = '#5b5b5b') + self.draw_circle ((700, 2100 - 40), 40) + self.draw_rectangle ((3000 + 22, 2100 - 750 - 85 / 2), (3000, 2100 - 750 + 85 / 2), fill = '#5b5b5b') + self.draw_circle ((3000 - 40, 2100 - 750), 40) + self.draw_rectangle ((3000 - 700 + 85 / 2, 2100), (3000 - 700 - 85 / 2, 2100 + 22), fill = '#5b5b5b') + self.draw_circle ((3000 - 700, 2100 - 40), 40) + # Horizontal dispenser. + self.draw_rectangle ((3000 / 2 - 924 / 2, 2100 + 22), (3000 / 2 + 924 / 2, 2100 + 22 + 80 + 22), fill = '#5b5b5b') + self.draw_rectangle ((3000 / 2 - 924 / 2 + 22, 2100 + 22), (3000 / 2 + 924 / 2 - 22, 2100 + 22 + 80), fill = '#5b5b5b') + self.draw_rectangle ((3000 / 2 - 880 / 2 - 35 - 60, 2100), (3000 / 2 - 880 / 2 - 35, 2100 + 44), fill = '#5b5b5b') + self.draw_rectangle ((3000 / 2 + 880 / 2 + 35 + 60, 2100), (3000 / 2 + 880 / 2 + 35, 2100 + 44), fill = '#5b5b5b') + # Balls. + balls = [ (800, 200, 'rb'), (800, 400, 'RB'), (800, 600, 'ww'), + (1300, 200, 'rb'), (1300, 400, 'rb'), (1300, 600, 'ww'), + (520, 800, 'WW'), (700, 40, 'RB'), (40, 750, 'WW'), + (450, 1120, 'ww'), (750, 1070, 'ww'), (1050, 1020, 'ww'), + (1500 - 72 / 2, -22 - 80 / 2, 'BR'), + (1500 - 72 / 2 - 1 * 73, -22 - 80 / 2, 'RB'), + (1500 - 72 / 2 - 2 * 73, -22 - 80 / 2, 'BR'), + (1500 - 72 / 2 - 3 * 73, -22 - 80 / 2, 'RB'), + (1500 - 72 / 2 - 4 * 73, -22 - 80 / 2, 'BR'), + (1500 - 72 / 2 - 5 * 73, -22 - 80 / 2, 'RB'), + (1500, 1000, 'W'), + ] + balls_config = { 'r': { 'outline': '#bf4141' }, 'R': { 'fill': '#bf4141' }, + 'b': { 'outline': '#4241bf' }, 'B': { 'fill': '#4241bf' }, + 'w': { 'outline': '#bfbfbf' }, 'W': { 'fill': '#bfbfbf' } } + for b in balls: + self.draw_circle ((3000 - b[0], 2100 - b[1]), 72 / 2, + **balls_config[b[2][0]]) + if len (b[2]) > 1: + self.draw_circle ((b[0], 2100 - b[1]), 72 / 2, + **balls_config[b[2][1]]) + Drawable.draw (self) + +if __name__ == '__main__': + from simu.inter.inter import Inter + app = Inter () + Table (app.table_view) + app.mainloop () -- cgit v1.2.3