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 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.control.IController;
  9.     import org.MARS.robot.control.LineFollowingBrain;
  10.     import org.MARS.robot.locomotion.DifferentialSteering;
  11.     import org.MARS.robot.sensors.LineSensor;
  12.     import org.MARS.robot.sensors.TouchSensor;
  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.util.StartButton;
  19.     import org.MARS.view.shape.Circle;
  20.     import org.MARS.view.shape.Rectangle;
  21.     import org.MARS.view.shape.SymmetricPolygon;
  22.  
  23.     // Set the stage dimensions  and color here
  24.     [SWF(width="800", height="600", backgroundColor="#333333")]
  25.    
  26.     public class MARS extends Sprite
  27.     {
  28.         public function MARS()
  29.         {
  30.             //set the framerate
  31.             stage.frameRate = IOUtil.FRAMERATE;
  32.            
  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.             //connect sensors to brain
  63.             brain.getPort(0).connectPorts(line_left.getPort(0));
  64.             brain.getPort(1).connectPorts(line_right.getPort(0));
  65.            
  66.             //connect brain to locomotion
  67.             brain.getPort(2).connectPorts(locomotion.getPort(0));
  68.             brain.getPort(3).connectPorts(locomotion.getPort(1));
  69.             brain.getPort(4).connectPorts(locomotion.getPort(2));
  70.             brain.getPort(5).connectPorts(locomotion.getPort(3));
  71.            
  72.             //assemble the robot
  73.             var robot:Robot = new Robot(650, 300, MathUtil.degreesToRadians(180), robotShape, brain, locomotion, Simplification.AVERAGE_MASS);
  74.            
  75.             robot.addSensor(line_left);
  76.             robot.addSensor(line_right);
  77.            
  78.             robot.addToEnvironment();
  79.            
  80.             //attach the simulation to stage and stay still until the start button is pressed
  81.             addChild(s);
  82.             s.useMouseDragger(true);
  83.             s.focusObject = robot;
  84.             s.still();
  85.            
  86.             //add the output panel
  87.             IOUtil.createTextPanel(this);
  88.            
  89.             //add a start button
  90.             addChild(StartButton.getInstance());
  91.         }
  92.     }
  93. }