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 irregular shape
- 3 thin walls
- A light circular object (undetectable by robots' touch sensors)
- A heavy pentagonal object
- A robot of circular shape
The robot has 2 circular edge sensors (detect the edges of the table) and 2 whisker-shaped touch sensors (detect walls and sufficiently heavy objects). The robot's controller is a primitive analog circuit that reverses the direction of the motor rotation for almost a second whenever any of the sensors is triggered.
The objective of the robot is to avoid falling off of the table and colliding with the walls using its sensors.
Refer to the BrainOne 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.BrainOne;
- import org.MARS.robot.locomotion.DifferentialSteering;
- import org.MARS.robot.sensors.EdgeSensor;
- 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.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
- {
- private var robot:Robot;
- private var object1:RigidBody = new RigidBody(220, 400, new SymmetricPolygon(0, 0, 5, 40), Simplification.TWICE_THE_AVERAGE_MASS);
- private var object2:RigidBody = new RigidBody(300, 200, new Circle(0, 0, 20), Simplification.SMALL_MASS, 0, 0, 0.1, 0.9);
- private var s:Simulation;
- private var e:Environment;
- public function MARS()
- {
- //set the framerate
- stage.frameRate = IOUtil.FRAMERATE;
- //get Simulation and Environment instances
- s = Simulation.getInstance();
- e = Environment.getInstance();
- //define table shape
- e.addTableShape(new Rectangle(100, 100, 300, 400));
- e.addTableShape(new Rectangle(400, 100, 300, 300));
- //add walls
- e.addWallShape(new Rectangle(200, 100, 430, 10));
- e.addWallShape(new Rectangle(100, 100, 10, 300));
- e.addWallShape(new Rectangle(100, 490, 300, 10));
- //add objects
- e.addDynamicObject(object1, true);
- e.addDynamicObject(object2, false);
- //robot parts
- var wheels:Array = LocomotionUtil.createTwoWheelSystem(7, 3, 20);
- var brain:BrainOne = new BrainOne(0.2, 0.3);
- var locomotion:DifferentialSteering = new DifferentialSteering(wheels);
- var robotShape:Rectangle = new Rectangle(-20, -20, 40, 40, 0, null, 0x00FF00, 0.7, 0x000000, 1, 3);
- //sensors
- var s1:Circle = new Circle(20, 25, 7, null, 0x0000BB);
- var s2:Circle = new Circle(20, -25, 7, null, 0x0000BB);
- var s3:Rectangle = new Rectangle(30, 3, 3, 25, 0, null, 0x0000BB);
- var s4:Rectangle = new Rectangle(30, -28, 3, 25, 0, null, 0x0000BB);
- var sen1:EdgeSensor = new EdgeSensor(s1);
- var sen2:EdgeSensor = new EdgeSensor(s2);
- var sen3:TouchSensor = new TouchSensor(s3);
- var sen4:TouchSensor = new TouchSensor(s4);
- //connect sensors to brain
- brain.getPort(0).connectPorts(sen1.getPort(0));
- brain.getPort(1).connectPorts(sen2.getPort(0));
- brain.getPort(0).connectPorts(sen3.getPort(0));
- brain.getPort(1).connectPorts(sen4.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
- robot = new Robot(650, 300, 0, robotShape, brain, locomotion, Simplification.AVERAGE_MASS);
- robot.addSensor(sen1);
- robot.addSensor(sen2);
- robot.addSensor(sen3);
- robot.addSensor(sen4);
- 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);
- }
- }
- }
