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
  • A robot of circular shape

The robot has 2 circular edge sensors (detect the edges of the table). The robot's controller is a primitive analog circuit that reverses the direction of both motors' 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.

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.robot.Robot;
  7.     import org.MARS.robot.control.BrainOne;
  8.     import org.MARS.robot.locomotion.DifferentialSteering;
  9.     import org.MARS.robot.sensors.EdgeSensor;
  10.     import org.MARS.simulation.Environment;
  11.     import org.MARS.simulation.Simulation;
  12.     import org.MARS.util.IOUtil;
  13.     import org.MARS.util.LocomotionUtil;
  14.     import org.MARS.util.StartButton;
  15.     import org.MARS.view.shape.Circle;
  16.     import org.MARS.view.shape.Rectangle;
  17.     import org.MARS.view.shape.SymmetricPolygon;
  18.    
  19.     // Set the stage dimensions  and color here
  20.     [SWF(width="800", height="600", backgroundColor="#333333")]
  21.    
  22.     public class MARS extends Sprite
  23.     {
  24.         private var robot:Robot;
  25.         private var s:Simulation;
  26.         private var e:Environment;
  27.        
  28.         public function MARS()
  29.         {
  30.             //set the framerate
  31.             stage.frameRate = IOUtil.FRAMERATE;
  32.            
  33.             //get Simulation and Environment instances
  34.             s = Simulation.getInstance();
  35.             e = Environment.getInstance();
  36.            
  37.             //define table shape
  38.             e.addTableShape(new Rectangle(100, 100, 600, 400));
  39.            
  40.             //robot parts
  41.             var wheels:Array = LocomotionUtil.createTwoWheelSystem(7, 3, 20);
  42.             var brain:BrainOne = new BrainOne(0.2, 0.3);
  43.             var locomotion:DifferentialSteering = new DifferentialSteering(wheels);
  44.             var robotShape:Circle = new Circle(0, 0, 30, null, 0x00FF00, 0.7, 0x000000, 1, 3);
  45.            
  46.             //sensors
  47.             var s1:Circle = new Circle(20, 25, 7, null, 0x0000BB);
  48.             var s2:Circle = new Circle(20, -25, 7, null, 0x0000BB);
  49.             var sen1:EdgeSensor = new EdgeSensor(s1);
  50.             var sen2:EdgeSensor = new EdgeSensor(s2);
  51.            
  52.             //connect sensors to brain
  53.             brain.getPort(0).connectPorts(sen1.getPort(0));
  54.             brain.getPort(1).connectPorts(sen2.getPort(0));
  55.            
  56.             //connect brain to locomotion
  57.             brain.getPort(2).connectPorts(locomotion.getPort(0));
  58.             brain.getPort(3).connectPorts(locomotion.getPort(1));
  59.             brain.getPort(4).connectPorts(locomotion.getPort(2));
  60.             brain.getPort(5).connectPorts(locomotion.getPort(3));
  61.            
  62.             //assemble the robot
  63.             robot = new Robot(650, 300, 0, robotShape, brain, locomotion, Simplification.AVERAGE_MASS);
  64.            
  65.             robot.addSensor(sen1);
  66.             robot.addSensor(sen2);
  67.            
  68.             robot.addToEnvironment();
  69.            
  70.             //attach the simulation to stage and stay still until the start button is pressed
  71.             addChild(s);
  72.             s.useMouseDragger(true);
  73.             s.focusObject = robot;
  74.             s.still();
  75.            
  76.             //add the output panel
  77.             IOUtil.createTextPanel(this);
  78.            
  79.             //add a start button
  80.             addChild(StartButton.getInstance());
  81.         }
  82.     }
  83. }