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 circular shape
  • A heavy pentagonal object
  • Several lines leading from one side of the table to the goal on the other side
  • A moving robot in the center of the table, 'Squid'
  • A robot of circular shape

The robot has 2 circular edge sensors (detect the edges of the table), 2 IR range detectors (detect walls and sufficiently large objects) and 2 small line sensors. The robot's controller is a programmable controller, i.e. Basic Stamp. Whenever the robot enters the 'searching for the line' mode, the LED on its front lights up.

The objective of the robot is to find it's way to the goal on the other side of a table by following the lines and avoiding the moving 'Squid' robot.

Refer to the SquidTestBrain 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.Battery;
  10.     import org.MARS.robot.control.IController;
  11.     import org.MARS.robot.control.SquidTestBrain;
  12.     import org.MARS.robot.locomotion.DifferentialSteering;
  13.     import org.MARS.robot.locomotion.ILocomotion;
  14.     import org.MARS.robot.sensors.EdgeSensor;
  15.     import org.MARS.robot.sensors.IRDetector;
  16.     import org.MARS.robot.sensors.LineSensor;
  17.     import org.MARS.simulation.Environment;
  18.     import org.MARS.simulation.Simulation;
  19.     import org.MARS.util.IOUtil;
  20.     import org.MARS.util.LocomotionUtil;
  21.     import org.MARS.util.MathUtil;
  22.     import org.MARS.util.StartButton;
  23.     import org.MARS.view.shape.Circle;
  24.     import org.MARS.view.shape.Shape;
  25.     import org.MARS.view.shape.SymmetricPolygon;
  26.  
  27.     // Set the stage dimensions  and color here
  28.     [SWF(width="800", height="600", backgroundColor="#333333")]
  29.    
  30.     public class MARS extends Sprite
  31.     {
  32.         public function MARS()
  33.         {
  34.             //set the framerate
  35.             stage.frameRate = IOUtil.FRAMERATE;
  36.            
  37.             //get Simulation and Environment instances
  38.             var s:Simulation = Simulation.getInstance();
  39.             var e:Environment = Environment.getInstance();
  40.            
  41.             //define table shape
  42.             e.addTableShape(new Circle(400, 300, 300));
  43.            
  44.             //add lines
  45.             e.addLineShape(new Vector(160, 170), new Vector(160, 210), 40);
  46.             e.addLineShape(new Vector(160, 180), new Vector(200, 420));
  47.             e.addLineShape(new Vector(200, 420), new Vector(270, 210));
  48.             e.addLineShape(new Vector(270, 210), new Vector(330, 400));
  49.             e.addLineShape(new Vector(330, 400), new Vector(400, 330));
  50.             e.addLineShape(new Vector(400, 330), new Vector(600, 350));
  51.             e.addLineShape(new Vector(600, 350), new Vector(600, 160));
  52.            
  53.             //add a heavy detectable object
  54.             var heavyPoligonalObject:RigidBody = new RigidBody(400, 200, new SymmetricPolygon(0, 0, 6, 50), Simplification.LARGE_MASS);
  55.             Environment.getInstance().addDynamicObject(heavyPoligonalObject, true);
  56.            
  57.             //create the Squid
  58.             var squidWheels:Array = LocomotionUtil.createTwoWheelSystem(7, 2, 10);
  59.             var squidLocomotion:ILocomotion = new DifferentialSteering(squidWheels);
  60.             var squidShape:Shape = new SymmetricPolygon(0, 0, 5, 40, 0, null, 0x0000FF, 0.7, 0x000000, 1, 3);
  61.             var squidBattery:Battery = new Battery();
  62.            
  63.             squidBattery.getPort(0).connectPorts(squidLocomotion.getPort(0));
  64.             squidBattery.getPort(1).connectPorts(squidLocomotion.getPort(1));
  65.             squidBattery.getPort(2).connectPorts(squidLocomotion.getPort(2));
  66.             squidBattery.getPort(3).connectPorts(squidLocomotion.getPort(3));
  67.            
  68.             squidBattery.leftWheelForward();
  69.             squidBattery.rightWheelStop();
  70.            
  71.             var squid:Robot = new Robot(400, 300, 0, squidShape, squidBattery, squidLocomotion, Simplification.LARGE_MASS);
  72.            
  73.             squid.addToEnvironment();
  74.            
  75.             //robot parts
  76.             var robotWheels:Array = LocomotionUtil.createTwoWheelSystem(7, 2, 20);
  77.             var robotBrain:IController = new SquidTestBrain();
  78.             var robotLocomotion:ILocomotion = new DifferentialSteering(robotWheels);
  79.             var robotShape:Shape = new Circle(0, 0, 30, null, 0x00FF00, 0.5, 0x000000, 1, 3);
  80.            
  81.             //sensors
  82.             var edge_left:EdgeSensor = new EdgeSensor(new Circle(20, -25, 7, null, 0x0000BB));
  83.             var edge_right:EdgeSensor = new EdgeSensor(new Circle(20, 25, 7, null, 0x0000BB));
  84.             var ir_left:IRDetector = new IRDetector(20, 20, MathUtil.degreesToRadians(-45));
  85.             var ir_right:IRDetector = new IRDetector(20, -20, MathUtil.degreesToRadians(45));
  86.             var line_left:LineSensor = new LineSensor(10, -4);
  87.             var line_right:LineSensor = new LineSensor(10, 4);
  88.            
  89.             //actuators
  90.             var led:LED = new LED(new Vector(20, 0), 3);
  91.            
  92.             //connect sensors to brain
  93.             robotBrain.getPort(SquidTestBrain.LEFT_LINE_SENSOR_PORT).connectPorts(line_left.getPort(0));
  94.             robotBrain.getPort(SquidTestBrain.RIGHT_LINE_SENSOR_PORT).connectPorts(line_right.getPort(0));
  95.             robotBrain.getPort(SquidTestBrain.LEFT_IR_SENSOR_PORT).connectPorts(ir_left.getPort(0));
  96.             robotBrain.getPort(SquidTestBrain.RIGHT_IR_SENSOR_PORT).connectPorts(ir_right.getPort(0));
  97.             robotBrain.getPort(SquidTestBrain.LEFT_EDGE_SENSOR_PORT).connectPorts(edge_left.getPort(0));
  98.             robotBrain.getPort(SquidTestBrain.RIGHT_EDGE_SENSOR_PORT).connectPorts(edge_right.getPort(0));
  99.            
  100.             //connect actuators to brain
  101.             robotBrain.getPort(SquidTestBrain.LED_PORT).connectPorts(led.getPort(0));
  102.            
  103.             //connect brain to locomotion
  104.             robotBrain.getPort(SquidTestBrain.LEFT_MOTOR_PORT_A).connectPorts(robotLocomotion.getPort(0));
  105.             robotBrain.getPort(SquidTestBrain.LEFT_MOTOR_PORT_B).connectPorts(robotLocomotion.getPort(1));
  106.             robotBrain.getPort(SquidTestBrain.RIGHT_MOTOR_PORT_A).connectPorts(robotLocomotion.getPort(2));
  107.             robotBrain.getPort(SquidTestBrain.RIGHT_MOTOR_PORT_B).connectPorts(robotLocomotion.getPort(3));
  108.            
  109.             //assemble the robot
  110.             var robot:Robot = new Robot(650, 300, MathUtil.degreesToRadians(180), robotShape, robotBrain, robotLocomotion, Simplification.AVERAGE_MASS);
  111.            
  112.             robot.addSensor(line_left);
  113.             robot.addSensor(line_right);
  114.             robot.addSensor(ir_left);
  115.             robot.addSensor(ir_right);
  116.             robot.addSensor(edge_left);
  117.             robot.addSensor(edge_right);
  118.             robot.addActuator(led);
  119.            
  120.             robot.addToEnvironment();
  121.            
  122.             //attach the simulation to stage and stay still until the start button is pressed
  123.             addChild(s);
  124.             s.useMouseDragger(true);
  125.             s.focusObject = robot;
  126.             s.still();
  127.            
  128.             //add the output panel
  129.             IOUtil.createTextPanel(this);
  130.         }
  131.     }
  132. }