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, surrounded with walls
- A robot of circular shape
- A heavy object on the surface
The robot has 2 whisker-like touch sensors. The robot's controller is a primitive analog circuit that reverses the direction of both motors' rotation for almost a second whenever any of the sensors is triggered.
The objective of the robot is to avoid colliding with any objects.
Refer to the BrainTwo 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.robot.Robot;
- import org.MARS.robot.control.BrainTwo;
- import org.MARS.robot.locomotion.DifferentialSteering;
- 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;
- //get Simulation and Environment instances
- var s:Simulation = Simulation.getInstance();
- var e:Environment = Environment.getInstance();
- //define table shape
- e.addTableShape(new Rectangle(100, 100, 600, 400));
- //add walls
- e.addWallShape(new Rectangle(100, 100, 600, 10));
- e.addWallShape(new Rectangle(100, 100, 10, 400));
- e.addWallShape(new Rectangle(100, 490, 600, 10));
- e.addWallShape(new Rectangle(690, 100, 10, 400));
- //add a heavy detectable object
- var heavyObject:RigidBody = new RigidBody(400, 400, new SymmetricPolygon(0, 0, 6, 50), Simplification.LARGE_MASS);
- e.addDynamicObject(heavyObject, true);
- //robot parts
- var wheels:Array = LocomotionUtil.createTwoWheelSystem(7, 3, 20);
- var brain:BrainTwo = new BrainTwo(0.2, 0.3);
- var locomotion:DifferentialSteering = new DifferentialSteering(wheels);
- var robotShape:Circle = new Circle(0, 0, 30, null, 0x00FF00, 0.7, 0x000000, 1, 3);
- //sensors
- var whisker_shape_left:Rectangle = new Rectangle(30, 0, 1, 35, MathUtil.degreesToRadians(200), null, 0x0000BB);
- var whisker_shape_right:Rectangle = new Rectangle(30, 0, 1, 35, MathUtil.degreesToRadians(-20), null, 0x0000BB);
- var touch_left:TouchSensor = new TouchSensor(whisker_shape_left);
- var touch_right:TouchSensor = new TouchSensor(whisker_shape_right);
- //connect sensors to brain
- brain.getPort(0).connectPorts(touch_left.getPort(0));
- brain.getPort(1).connectPorts(touch_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(touch_left);
- robot.addSensor(touch_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());
- }
- }
- }
