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- package {
- import flash.display.Sprite;
- import org.MARS.dynamics.element.body.RigidBody;
- import org.MARS.dynamics.enum.Simplification;
- import org.MARS.math.Vector;
- import org.MARS.robot.Robot;
- import org.MARS.robot.actuators.LED;
- import org.MARS.robot.control.Battery;
- import org.MARS.robot.control.IController;
- import org.MARS.robot.control.SquidTestBrain;
- import org.MARS.robot.locomotion.DifferentialSteering;
- import org.MARS.robot.locomotion.ILocomotion;
- import org.MARS.robot.sensors.EdgeSensor;
- import org.MARS.robot.sensors.IRDetector;
- import org.MARS.robot.sensors.LineSensor;
- import org.MARS.simulation.Environment;
- import org.MARS.simulation.Simulation;
- import org.MARS.util.IOUtil;
- import org.MARS.util.LocomotionUtil;
- import org.MARS.util.MathUtil;
- import org.MARS.util.StartButton;
- import org.MARS.view.shape.Circle;
- import org.MARS.view.shape.Shape;
- import org.MARS.view.shape.SymmetricPolygon;
- // Set the stage dimensions and color here
- [SWF(width="800", height="600", backgroundColor="#333333")]
- public class MARS extends Sprite
- {
- public function MARS()
- {
- //set the framerate
- stage.frameRate = IOUtil.FRAMERATE;
- //get Simulation and Environment instances
- var s:Simulation = Simulation.getInstance();
- var e:Environment = Environment.getInstance();
- //define table shape
- e.addTableShape(new Circle(400, 300, 300));
- //add lines
- e.addLineShape(new Vector(160, 170), new Vector(160, 210), 40);
- e.addLineShape(new Vector(160, 180), new Vector(200, 420));
- e.addLineShape(new Vector(200, 420), new Vector(270, 210));
- e.addLineShape(new Vector(270, 210), new Vector(330, 400));
- e.addLineShape(new Vector(330, 400), new Vector(400, 330));
- e.addLineShape(new Vector(400, 330), new Vector(600, 350));
- e.addLineShape(new Vector(600, 350), new Vector(600, 160));
- //add a heavy detectable object
- var heavyPoligonalObject:RigidBody = new RigidBody(400, 200, new SymmetricPolygon(0, 0, 6, 50), Simplification.LARGE_MASS);
- Environment.getInstance().addDynamicObject(heavyPoligonalObject, true);
- //create the Squid
- var squidWheels:Array = LocomotionUtil.createTwoWheelSystem(7, 2, 10);
- var squidLocomotion:ILocomotion = new DifferentialSteering(squidWheels);
- var squidShape:Shape = new SymmetricPolygon(0, 0, 5, 40, 0, null, 0x0000FF, 0.7, 0x000000, 1, 3);
- var squidBattery:Battery = new Battery();
- squidBattery.getPort(0).connectPorts(squidLocomotion.getPort(0));
- squidBattery.getPort(1).connectPorts(squidLocomotion.getPort(1));
- squidBattery.getPort(2).connectPorts(squidLocomotion.getPort(2));
- squidBattery.getPort(3).connectPorts(squidLocomotion.getPort(3));
- squidBattery.leftWheelForward();
- squidBattery.rightWheelStop();
- var squid:Robot = new Robot(400, 300, 0, squidShape, squidBattery, squidLocomotion, Simplification.LARGE_MASS);
- squid.addToEnvironment();
- //robot parts
- var robotWheels:Array = LocomotionUtil.createTwoWheelSystem(7, 2, 20);
- var robotBrain:IController = new SquidTestBrain();
- var robotLocomotion:ILocomotion = new DifferentialSteering(robotWheels);
- var robotShape:Shape = new Circle(0, 0, 30, null, 0x00FF00, 0.5, 0x000000, 1, 3);
- //sensors
- var edge_left:EdgeSensor = new EdgeSensor(new Circle(20, -25, 7, null, 0x0000BB));
- var edge_right:EdgeSensor = new EdgeSensor(new Circle(20, 25, 7, null, 0x0000BB));
- var ir_left:IRDetector = new IRDetector(20, 20, MathUtil.degreesToRadians(-45));
- var ir_right:IRDetector = new IRDetector(20, -20, MathUtil.degreesToRadians(45));
- var line_left:LineSensor = new LineSensor(10, -4);
- var line_right:LineSensor = new LineSensor(10, 4);
- //actuators
- var led:LED = new LED(new Vector(20, 0), 3);
- //connect sensors to brain
- robotBrain.getPort(SquidTestBrain.LEFT_LINE_SENSOR_PORT).connectPorts(line_left.getPort(0));
- robotBrain.getPort(SquidTestBrain.RIGHT_LINE_SENSOR_PORT).connectPorts(line_right.getPort(0));
- robotBrain.getPort(SquidTestBrain.LEFT_IR_SENSOR_PORT).connectPorts(ir_left.getPort(0));
- robotBrain.getPort(SquidTestBrain.RIGHT_IR_SENSOR_PORT).connectPorts(ir_right.getPort(0));
- robotBrain.getPort(SquidTestBrain.LEFT_EDGE_SENSOR_PORT).connectPorts(edge_left.getPort(0));
- robotBrain.getPort(SquidTestBrain.RIGHT_EDGE_SENSOR_PORT).connectPorts(edge_right.getPort(0));
- //connect actuators to brain
- robotBrain.getPort(SquidTestBrain.LED_PORT).connectPorts(led.getPort(0));
- //connect brain to locomotion
- robotBrain.getPort(SquidTestBrain.LEFT_MOTOR_PORT_A).connectPorts(robotLocomotion.getPort(0));
- robotBrain.getPort(SquidTestBrain.LEFT_MOTOR_PORT_B).connectPorts(robotLocomotion.getPort(1));
- robotBrain.getPort(SquidTestBrain.RIGHT_MOTOR_PORT_A).connectPorts(robotLocomotion.getPort(2));
- robotBrain.getPort(SquidTestBrain.RIGHT_MOTOR_PORT_B).connectPorts(robotLocomotion.getPort(3));
- //assemble the robot
- var robot:Robot = new Robot(650, 300, MathUtil.degreesToRadians(180), robotShape, robotBrain, robotLocomotion, Simplification.AVERAGE_MASS);
- robot.addSensor(line_left);
- robot.addSensor(line_right);
- robot.addSensor(ir_left);
- robot.addSensor(ir_right);
- robot.addSensor(edge_left);
- robot.addSensor(edge_right);
- robot.addActuator(led);
- robot.addToEnvironment();
- //attach the simulation to stage and stay still until the start button is pressed
- addChild(s);
- s.useMouseDragger(true);
- s.focusObject = robot;
- s.still();
- //add the output panel
- IOUtil.createTextPanel(this);
- }
- }
- }
