Advertisement
O_ranged

Untitled

Dec 6th, 2021
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function setup() {
  2.   this.maxSpeed = 1600;
  3.   this.minSpeed = 1400;
  4.   this.TakeLeftPath = true;
  5. }
  6.  
  7. function loop(sensors) {
  8.   this.sensors=sensors.toJS();
  9.   DecideNextMove(this);
  10.  
  11. }
  12.  
  13. // rotates in place to the left
  14. function setRotateLeftInPlace(robot){
  15.   robot.leftWheel = robot.minSpeed;
  16.   robot.rightWheel = robot.minSpeed;
  17. }
  18.  
  19. // rotates in place to the right
  20. function setRotateRightInPlace(robot){
  21.   robot.leftWheel = robot.maxSpeed;
  22.   robot.rightWheel = robot.maxSpeed;
  23. }
  24. // sets speeds to go straight
  25. function setGoStraight(robot){
  26.   robot.leftWheel = robot.maxSpeed;
  27.   robot.rightWheel = robot.minSpeed;
  28. }
  29.  
  30. // sets speeds to go straight
  31. function setGoStraightReverse(robot){
  32.   robot.leftWheel = robot.minSpeed;
  33.   robot.rightWheel = robot.maxSpeed;
  34. }
  35.  
  36. function DecideNextMove(robot){
  37.   var result = 0;
  38.   for(i = 0; i < 5; i++){
  39.     result = result<<1;
  40.     result += robot.sensors[i] ? 1 : 0;
  41.   }
  42.   //
  43.   // what interests us
  44.   // 0 0 1 0 0 -> go straight // 4
  45.   // 0 1 0 0 0 -> turn left // 8
  46.   // 0 0 0 1 0 -> turn right // 2
  47.  
  48.   switch(result){
  49.     case 2:
  50.       setRotateLeftInPlace(robot);
  51.       break;
  52.     case 4:
  53.       setGoStraight(robot);
  54.       break;
  55.     case 8:
  56.       setRotateLeftInPlace(robot);
  57.       break;
  58.   }
  59.  
  60.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement