to Main Page

Example

NOTE: Please click the table surface to allow keyboard event detection (flash must have the focus). Refresh the page to restart the simulation.

Description

The environment of the simulation consists of:

  • A table surface of irregular shape
  • 3 thin walls
  • A light circular object (undetectable by robots' touch sensors)
  • A heavy pentagonal object
  • A robot of circular shape

The robot has 2 circular edge sensors (detect the edges of the table) and 2 whisker-shaped touch sensors (detect walls and sufficiently heavy objects). The robot's controller is a primitive analog circuit that reverses the direction of the motor rotation for almost a second whenever any of the sensors is triggered.

The objective of the robot is to avoid falling off of the table and colliding with the walls using its sensors.

Refer to the BrainOne code example for more information.

Source Code

PLAIN TEXT VIEW
  1. package {
  2.     import flash.display.Sprite;
  3.    
  4.     import org.MARS.dynamics.element.body.RigidBody;
  5.     import org.MARS.dynamics.enum.Simplification;
  6.     import org.MARS.math.Vector;
  7.     import org.MARS.robot.Robot;
  8.     import org.MARS.robot.control.BrainOne;
  9.     import org.MARS.robot.locomotion.DifferentialSteering;
  10.     import org.MARS.robot.sensors.EdgeSensor;
  11.     import org.MARS.robot.sensors.TouchSensor;
  12.     import org.MARS.simulation.Environment;
  13.     import org.MARS.simulation.Simulation;
  14.     import org.MARS.util.IOUtil;
  15.     import org.MARS.util.LocomotionUtil;
  16.     import org.MARS.util.StartButton;
  17.     import org.MARS.view.shape.Circle;
  18.     import org.MARS.view.shape.Rectangle;
  19.     import org.MARS.view.shape.SymmetricPolygon;
  20.  
  21. // Set the stage dimensions  and color here
  22.     [SWF(width="800", height="600", backgroundColor="#333333")]
  23.    
  24.     public class MARS extends Sprite
  25.     {
  26.         private var robot:Robot;
  27.         private var object1:RigidBody = new RigidBody(220, 400, new SymmetricPolygon(0, 0, 5, 40), Simplification.TWICE_THE_AVERAGE_MASS);
  28.         private var object2:RigidBody = new RigidBody(300, 200, new Circle(0, 0, 20), Simplification.SMALL_MASS, 0, 0, 0.1, 0.9);
  29.         private var s:Simulation;
  30.         private var e:Environment;
  31.        
  32.         public function MARS()
  33.         {
  34.             //set the framerate
  35.             stage.frameRate = IOUtil.FRAMERATE;
  36.            
  37.             //get Simulation and Environment instances
  38.             s = Simulation.getInstance();
  39.             e = Environment.getInstance();
  40.            
  41.             //define table shape
  42.             e.addTableShape(new Rectangle(100, 100, 300, 400));
  43.             e.addTableShape(new Rectangle(400, 100, 300, 300));
  44.            
  45.             //add walls
  46.             e.addWallShape(new Rectangle(200, 100, 430, 10));
  47.             e.addWallShape(new Rectangle(100, 100, 10, 300));
  48.             e.addWallShape(new Rectangle(100, 490, 300, 10));
  49.            
  50.             //add objects
  51.             e.addDynamicObject(object1, true);
  52.             e.addDynamicObject(object2, false);
  53.            
  54.             //robot parts
  55.             var wheels:Array = LocomotionUtil.createTwoWheelSystem(7, 3, 20);
  56.             var brain:BrainOne = new BrainOne(0.2, 0.3);
  57.             var locomotion:DifferentialSteering = new DifferentialSteering(wheels);
  58.             var robotShape:Rectangle = new Rectangle(-20, -20, 40, 40, 0, null, 0x00FF00, 0.7, 0x000000, 1, 3);
  59.            
  60.             //sensors
  61.             var s1:Circle = new Circle(20, 25, 7, null, 0x0000BB);
  62.             var s2:Circle = new Circle(20, -25, 7, null, 0x0000BB);
  63.             var s3:Rectangle = new Rectangle(30, 3, 3, 25, 0, null, 0x0000BB);
  64.             var s4:Rectangle = new Rectangle(30, -28, 3, 25, 0, null, 0x0000BB);
  65.             var sen1:EdgeSensor = new EdgeSensor(s1);
  66.             var sen2:EdgeSensor = new EdgeSensor(s2);
  67.             var sen3:TouchSensor = new TouchSensor(s3);
  68.             var sen4:TouchSensor = new TouchSensor(s4);
  69.            
  70.             //connect sensors to brain
  71.             brain.getPort(0).connectPorts(sen1.getPort(0));
  72.             brain.getPort(1).connectPorts(sen2.getPort(0));
  73.             brain.getPort(0).connectPorts(sen3.getPort(0));
  74.             brain.getPort(1).connectPorts(sen4.getPort(0));
  75.            
  76.             //connect brain to locomotion
  77.             brain.getPort(2).connectPorts(locomotion.getPort(0));
  78.             brain.getPort(3).connectPorts(locomotion.getPort(1));
  79.             brain.getPort(4).connectPorts(locomotion.getPort(2));
  80.             brain.getPort(5).connectPorts(locomotion.getPort(3));
  81.            
  82.             //assemble the robot
  83.             robot = new Robot(650, 300, 0, robotShape, brain, locomotion, Simplification.AVERAGE_MASS);
  84.            
  85.             robot.addSensor(sen1);
  86.             robot.addSensor(sen2);
  87.             robot.addSensor(sen3);
  88.             robot.addSensor(sen4);
  89.            
  90.             robot.addToEnvironment();
  91.            
  92.             //attach the simulation to stage and stay still until the start button is pressed
  93.             addChild(s);
  94.             s.useMouseDragger(true);
  95.             s.focusObject = robot;
  96.             s.still();
  97.            
  98.             //add the output panel
  99.             IOUtil.createTextPanel(this);
  100.         }
  101.     }
  102. }