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, surrounded with walls
  • A robot of circular shape
  • A heavy object on the surface

The robot has 2 whisker-like touch sensors. 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 colliding with any objects.

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