Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup() {
- this.maxSpeed = 1600;
- this.minSpeed = 1400;
- this.TakeLeftPath = true;
- }
- function loop(sensors) {
- this.sensors=sensors.toJS();
- DecideNextMove(this);
- }
- // rotates in place to the left
- function setRotateLeftInPlace(robot){
- robot.leftWheel = robot.minSpeed;
- robot.rightWheel = robot.minSpeed;
- }
- // rotates in place to the right
- function setRotateRightInPlace(robot){
- robot.leftWheel = robot.maxSpeed;
- robot.rightWheel = robot.maxSpeed;
- }
- // sets speeds to go straight
- function setGoStraight(robot){
- robot.leftWheel = robot.maxSpeed;
- robot.rightWheel = robot.minSpeed;
- }
- // sets speeds to go straight
- function setGoStraightReverse(robot){
- robot.leftWheel = robot.minSpeed;
- robot.rightWheel = robot.maxSpeed;
- }
- function DecideNextMove(robot){
- var result = 0;
- for(i = 0; i < 5; i++){
- result = result<<1;
- result += robot.sensors[i] ? 1 : 0;
- }
- //
- // what interests us
- // 0 0 1 0 0 -> go straight // 4
- // 0 1 0 0 0 -> turn left // 8
- // 0 0 0 1 0 -> turn right // 2
- switch(result){
- case 2:
- setRotateLeftInPlace(robot);
- break;
- case 4:
- setGoStraight(robot);
- break;
- case 8:
- setRotateLeftInPlace(robot);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement