Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import lejos.hardware.Button;
  2. import lejos.hardware.Sound;
  3. import lejos.hardware.lcd.LCD;
  4. import lejos.hardware.motor.BaseRegulatedMotor;
  5. import lejos.hardware.motor.EV3LargeRegulatedMotor;
  6. import lejos.hardware.port.MotorPort;
  7.  
  8. public class RacingCar {
  9.  
  10.     public static void main(String[] args) {
  11.         final int FORWARD_ROTATE_ANGLE = 720;
  12.         final int TURN_ANGLE = 180;
  13.         final int TURN_SPEED = 200;
  14.         final int TURNS = 4;
  15.         final int STALL_ERROR = 10;
  16.         final int STALL_TIME = 2;
  17.  
  18.  
  19.         BaseRegulatedMotor mLeft = new EV3LargeRegulatedMotor(MotorPort.A);
  20.         BaseRegulatedMotor mRight = new EV3LargeRegulatedMotor(MotorPort.B);
  21.        
  22.         final int FORWARD_SPEED = (int) mLeft.getMaxSpeed();
  23.        
  24.         //mLeft.setStallThreshold(STALL_ERROR, STALL_TIME);
  25.         //mRight.setStallThreshold(STALL_ERROR, STALL_TIME);
  26.    
  27.         mLeft.synchronizeWith(new BaseRegulatedMotor[] {mRight});
  28.        
  29.        
  30.         for (int turn = 0; turn < TURNS; turn++) {
  31.             LCD.drawString("Loop: " + turn, 0, 0);
  32.            
  33.             //Sends the robot forward
  34.             Forward(mLeft, mRight, FORWARD_SPEED, FORWARD_ROTATE_ANGLE);
  35.        
  36.             //Wait for motors to stop rotating forward
  37.             mLeft.waitComplete();
  38.            
  39.             //if (turn == 4)break;
  40.             //Turns the robot at the corner
  41.             Turn(mLeft, mRight, TURN_SPEED, TURN_ANGLE);
  42.            
  43.             //Wait for robot to stop turning
  44.             mLeft.waitComplete();
  45.        
  46.         }
  47.        
  48.         //Saves data on screen
  49.         Button.ENTER.waitForPressAndRelease();
  50.        
  51.         mLeft.close();
  52.         mRight.close();
  53.  
  54.     }
  55.    
  56.     private static void Forward(BaseRegulatedMotor mLeft, BaseRegulatedMotor mRight, int motorSpeed, int rotateAngle) {
  57.         //Creates and runs synchronised motors
  58.         mLeft.startSynchronization();
  59.        
  60.         //Set the speed of the motors for going forward
  61.         mLeft.setSpeed(motorSpeed);
  62.         mRight.setSpeed(motorSpeed);
  63.        
  64.         //Rotates both motors in sync moving it forward
  65.         mLeft.rotate(rotateAngle, false);
  66.         mRight.rotate(rotateAngle, false);
  67.        
  68.         //Check if the motors have stalled
  69.         /*while (mLeft.isStalled() || mRight.isStalled()) {
  70.            
  71.             //Stop the motors and play beep
  72.             mLeft.stop();
  73.             mRight.stop();
  74.            
  75.             Sound.playTone(100, 3, 100);
  76.         }*/
  77.         mLeft.endSynchronization();
  78.         LCD.drawString("Moving forward...", 1, 0);
  79.     }
  80.    
  81.     private static void Turn(BaseRegulatedMotor mLeft, BaseRegulatedMotor mRight, int motorSpeed, int rotateAngle) {
  82.         //Creates and synchronises the rotating of the robot
  83.         mLeft.startSynchronization();  
  84.        
  85.         //Sets the motor speeds for turning
  86.         mLeft.setSpeed(motorSpeed);
  87.         mRight.setSpeed(motorSpeed);
  88.        
  89.         //Rotates the motors in opposite direction to turn 90 degrees
  90.         mLeft.rotate(rotateAngle, false);
  91.         mRight.rotate(-rotateAngle, false);
  92.        
  93.         //Check if the motors have stalled
  94.         /*while (mLeft.isStalled() || mRight.isStalled()) {
  95.                    
  96.             //Stop the motors and play beep
  97.             mLeft.stop();
  98.             mRight.stop();
  99.                    
  100.             Sound.playTone(100, 3, 100);
  101.         }*/
  102.         mLeft.endSynchronization();
  103.         LCD.drawString("Turning...", 1, 0);
  104.  
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement