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
  • Several connected white lines on the surface of the table
  • A robot of circular shape
  • A heavy object on the surface

The robot has 2 IR line sensors. The robot's brain is a programmable microcontroller. The outputs of the brain are also connected to the four LEDs.

The objective of the robot is to find the line and follow it.

Refer to the LineFollowingBrain 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.actuators.LED;
  9.     import org.MARS.robot.control.IController;
  10.     import org.MARS.robot.control.LineFollowingBrain;
  11.     import org.MARS.robot.locomotion.DifferentialSteering;
  12.     import org.MARS.robot.sensors.LineSensor;
  13.     import org.MARS.simulation.Environment;
  14.     import org.MARS.simulation.Simulation;
  15.     import org.MARS.util.IOUtil;
  16.     import org.MARS.util.LocomotionUtil;
  17.     import org.MARS.util.MathUtil;
  18.     import org.MARS.view.shape.Circle;
  19.     import org.MARS.view.shape.Rectangle;
  20.     import org.MARS.view.shape.SymmetricPolygon;
  21.    
  22.     // Set the stage dimensions  and color here
  23.     [SWF(width="800", height="600", backgroundColor="#333333")]
  24.    
  25.     public class MARS extends Sprite
  26.     {
  27.         public function MARS()
  28.         {
  29.             //set the framerate
  30.             stage.frameRate = IOUtil.FRAMERATE;
  31.            
  32.             //get Simulation and Environment instances
  33.             var s:Simulation = Simulation.getInstance();
  34.             var e:Environment = Environment.getInstance();
  35.            
  36.             //define table shape
  37.             e.addTableShape(new Rectangle(100, 100, 300, 400));
  38.             e.addTableShape(new Rectangle(400, 100, 300, 300));
  39.            
  40.             //add lines
  41.             e.addLineShape(new Vector(160, 140), new Vector(200, 420));
  42.             e.addLineShape(new Vector(200, 420), new Vector(270, 210));
  43.             e.addLineShape(new Vector(270, 210), new Vector(330, 400));
  44.             e.addLineShape(new Vector(330, 400), new Vector(400, 330));
  45.             e.addLineShape(new Vector(400, 330), new Vector(600, 350));
  46.             e.addLineShape(new Vector(600, 350), new Vector(630, 120));
  47.            
  48.             //add a heavy detectable object
  49.             var heavyObject:RigidBody = new RigidBody(400, 200, new SymmetricPolygon(0, 0, 6, 50), Simplification.LARGE_MASS);
  50.             e.addDynamicObject(heavyObject, true);
  51.            
  52.             //robot parts
  53.             var wheels:Array = LocomotionUtil.createTwoWheelSystem(7, 2, 20);
  54.             var brain:IController = new LineFollowingBrain();
  55.             var locomotion:DifferentialSteering = new DifferentialSteering(wheels);
  56.             var robotShape:Circle = new Circle(0, 0, 30, null, 0x00FF00, 0.5, 0x000000, 1, 3);
  57.            
  58.             //sensors
  59.             var line_left:LineSensor = new LineSensor(10, -4);
  60.             var line_right:LineSensor = new LineSensor(10, 4);
  61.            
  62.             //actuators
  63.             var led1:LED = new LED(new Vector(5, 18), 3);
  64.             var led2:LED = new LED(new Vector(-5, 18), 3);
  65.             var led3:LED = new LED(new Vector(5, -18), 3);
  66.             var led4:LED = new LED(new Vector(-5, -18), 3);
  67.            
  68.             //connect sensors to brain
  69.             brain.getPort(0).connectPorts(line_left.getPort(0));
  70.             brain.getPort(1).connectPorts(line_right.getPort(0));
  71.            
  72.             //connect brain to locomotion
  73.             brain.getPort(2).connectPorts(locomotion.getPort(0));
  74.             brain.getPort(3).connectPorts(locomotion.getPort(1));
  75.             brain.getPort(4).connectPorts(locomotion.getPort(2));
  76.             brain.getPort(5).connectPorts(locomotion.getPort(3));
  77.            
  78.             brain.getPort(2).connectPorts(led1.getPort(0));
  79.             brain.getPort(3).connectPorts(led2.getPort(0));
  80.             brain.getPort(4).connectPorts(led3.getPort(0));
  81.             brain.getPort(5).connectPorts(led4.getPort(0));
  82.            
  83.             //assemble the robot
  84.             var robot:Robot = new Robot(650, 300, MathUtil.degreesToRadians(180), robotShape, brain, locomotion, Simplification.AVERAGE_MASS);
  85.            
  86.             robot.addSensor(line_left);
  87.             robot.addSensor(line_right);
  88.            
  89.             robot.addActuator(led1);
  90.             robot.addActuator(led2);
  91.             robot.addActuator(led3);
  92.             robot.addActuator(led4);
  93.            
  94.             robot.addToEnvironment();
  95.            
  96.             //attach the simulation to stage and stay still until the start button is pressed
  97.             addChild(s);
  98.             s.useMouseDragger(true);
  99.             s.focusObject = robot;
  100.             s.still();
  101.            
  102.             //add the output panel
  103.             IOUtil.createTextPanel(this);
  104.         }
  105.     }
  106. }