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- 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.control.IController;
- import org.MARS.robot.control.LineFollowingBrain;
- import org.MARS.robot.locomotion.DifferentialSteering;
- import org.MARS.robot.sensors.LineSensor;
- import org.MARS.robot.sensors.TouchSensor;
- 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.Rectangle;
- 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;
- var s:Simulation = Simulation.getInstance();
- var e:Environment = Environment.getInstance();
- //define table shape
- e.addTableShape(new Rectangle(100, 100, 300, 400));
- e.addTableShape(new Rectangle(400, 100, 300, 300));
- //add lines
- e.addLineShape(new Vector(160, 140), 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(630, 120));
- //add a heavy detectable object
- var heavyObject:RigidBody = new RigidBody(400, 200, new SymmetricPolygon(0, 0, 6, 50), Simplification.LARGE_MASS);
- e.addDynamicObject(heavyObject, true);
- //robot parts
- var wheels:Array = LocomotionUtil.createTwoWheelSystem(7, 2, 20);
- var brain:IController = new LineFollowingBrain();
- var locomotion:DifferentialSteering = new DifferentialSteering(wheels);
- var robotShape:Circle = new Circle(0, 0, 30, null, 0x00FF00, 0.5, 0x000000, 1, 3);
- //sensors
-
var line_left:LineSensor = new LineSensor(10, -4);
-
var line_right:LineSensor = new LineSensor(10, 4);
- //connect sensors to brain
- brain.getPort(0).connectPorts(line_left.getPort(0));
- brain.getPort(1).connectPorts(line_right.getPort(0));
- //connect brain to locomotion
- brain.getPort(2).connectPorts(locomotion.getPort(0));
- brain.getPort(3).connectPorts(locomotion.getPort(1));
- brain.getPort(4).connectPorts(locomotion.getPort(2));
- brain.getPort(5).connectPorts(locomotion.getPort(3));
- //assemble the robot
- var robot:Robot = new Robot(650, 300, MathUtil.degreesToRadians(180), robotShape, brain, locomotion, Simplification.AVERAGE_MASS);
- robot.addSensor(line_left);
- robot.addSensor(line_right);
- 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);
- //add a start button
- addChild(StartButton.getInstance());
- }
- }
- }
