syncMotors( B, C, 30, 100 ) until abs(gyroSensor()) > 90 print( gyroSensor() ) stopAllMotors( true )
syncMotors( B, C, 30, -200 ) sleep(2000) syncMotors( B, C, 30, 200 ) sleep(2000)
syncMotors( B, C, 30, -100 ) sleep(2000) syncMotors( B, C, 30, 100 ) sleep(2000)
syncMotors( B, C, 30, -40 ) sleep(2000) syncMotors( B, C, 30, 40 ) sleep(2000)
// Move in a Square Pattern using resetGyroSensor() // ------------------------------------------------ // Make the robot move around a square // The robot will move straight for 2 seconds and then // reset the gyro and turn 90 degrees counter-clockwise // The for() loop will repeat this action 4 times. for(i=1;i<=4;i++) { // Loop for 4 times syncMotors( 2, 3, 30 ) // Start driving straight sleep(2000) // For 2 seconds resetGyroSensor() // Reset the gyroSensor syncMotors( B, C, 30, -100 ) // Start turning (swing turn) waitHereUntil abs(gyroSensorValue())>=88 // Turn for 88 degrees }
// Turn one wheel faster than the other syncMotors( B, C, 30, -60 ) sleep(2000) syncMotors( B, C, 30, 60 ) sleep(2000) syncMotors( B, C, 30, -60 ) sleep(2000) syncMotors( B, C, 30, 60 ) sleep(2000)
// Swing turns using the Gyro // --------------------------- // This program uses the gyroSensor and two motors (on ports B and C). // A swing turn is when one of the drive motors is active while the other drive motor is stopped. // The syncMotors() function can create a swing turn if the turning ratio parameter is either 100 or -100. // After starting the robot in a syncMotors() turn, reset the gyroSensor and check it in a while loop // syncMotors( B, C, 30, -100 ) // Start a swing turn resetGyroSensor() // Reset the sensor while abs(gyroSensorValue()) < 90 // While the absolute value of the sensor is less than 90 degrees sleep(50) // Move for a period of time // Repeat the loop stopAllMotors() // Stop the motors sleep(2000) // for 2 seconds syncMotors( B, C, 30, 100 ) // Start a swing turn in the other direction resetGyroSensor() // Reset the sensor while abs(gyroSensorValue()) < 90 // While the absolute value of the sensor is less than 90 degrees sleep(50) // Move for a period of time // Repeat the loop
// Swing turns using Odometry // --------------------------- // This program uses the stepMotor() function on one motor to perform a swing turn. // A swing turn is when one of the drive motors is active while the other drive motor is stopped. // To perform this maneuver using odometry, you will need to enter two important dimensions // of the robot: the wheel (tire) diameter and the distance between the two wheels. // // Example dimensions are shown below in millimeters. If you prefer to use inches, you may use inches // as the unit of measurements for the wheel diameter and distance between the wheels. Just make // sure that you are using the same unit of measurement (either inches, centimeters or millimeters). // const pi = 3.14159 // Used for many of the computations of circumference wheelDiameter = 55 // In this example, millimeters are being used wheelCircumference = wheelDiameter * pi // Circumference is the diameter times pi distanceBetweenWheels = 97 // You may need to increase or decrease this number depending // on the amount of friction created by the non-rotating tire. swingTurnDiameter = distanceBetweenWheels * 2 // For a swing turn, the distance between the tires represents the radius // of the turn the robot will make. To calculate the diameter, multiply // this radius (i.e., the distance between the wheels ) by 2. swingTurnCircumference = swingTurnDiameter * pi // Simple calculation of circumference // Define a function to perform a swing turn. You may want to name your function something more specific // such as: swingTurnClockwise() or swingTurnCounterClockwise(). This, of course depends on the orientation of // your motors and which ports they are connected to. // A default value of 90 is being used in the definition of the function. If no degree value is passed into the function, // the robot will make a 90 degree turn. function swingTurn(degreesRobotShouldTurn = 90) { let motorDegreesPerRobotDegree = (swingTurnCircumference / wheelCircumference ) // Depending on the dimensions of the wheel and the robot, the motor that turns the wheel may have to rotate several degrees // for each degree that the robot should turn. For example, if you want the robot to turn 90 degrees, then the motor may // need to turn 300 or 400 degrees in order to perform the robot's maneuver. The number of degrees that the motor has to // rotate for each degree that the robot itself rotates is simply a ratio between the swingTurnCircumference and the wheel's // circumference. Calculate this ratio and store it in a variable called: motorDegreesPerRobotDegree // The stepMotor() function is being used in this example because it includes parameters for acceleration and deceleration // of the motor which should assist in making accurate turns. In the example below, 15% of the turn will be used for // acceleration, 70% will be used for travel at the specified top speed, and the remaining 15% will be used for deceleration. let acceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree let atTopSpeed = 0.70 * degreesRobotShouldTurn * motorDegreesPerRobotDegree let deceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree stepMotor(B, 25, acceleration, atTopSpeed, deceleration) // Pass the parameters to the stepMotor() function. waitHereWhile getMotorSpeed(B)==0 // stepMotor() function starts with the speed of the motor set to zero. Wait here while the speed is zero. waitHereUntil getMotorSpeed(B)==0 // Wait here as the motor is turning and only exit from this line when the speed returns to back to zero. stopAllMotors(true) // Apply the brake to the motor(s) } // Perform a complete 360 degree turn. You may need to adjust the value being used for the distance between the wheels (see note below). for( j = 1; j<=4; j++ ) swingTurn(90), sleep(500) // Note: The above for() loop should make the robot turn a full 360 degrees by making 4 individual 90 degree turns. // However, the width of the tire may impact the accuracy of the turns. A wide tire may turn less accurately than // a thinner tire based on the additional friction that may be cause by the wider tire. // If the robot turns more than it should, decrease the value used for the distanceBetweenWheels. // If the robot turns less than it should, increase the value used for the distanceBetweenWheels.
// Point turns using Odometry // --------------------------- // This program uses the stepMotor() function on two motors to perform a point turn. // A point turn is when the drive motors turn in opposite directions. // To perform this maneuver using odometry, you will need to enter two important dimensions // of the robot: the wheel (tire) diameter and the distance between the two wheels. // // Example dimensions are shown below in millimeters. If you prefer to use inches, you may use inches // as the unit of measurements for the wheel diameter and distance between the wheels. Just make // sure that you are using the same unit of measurement (either inches, centimeters or millimeters). // const pi = 3.14159 // Used for many of the computations of circumference wheelDiameter = 55 // In this example, millimeters are being used wheelCircumference = wheelDiameter * pi // Circumference is the diameter times pi distanceBetweenWheels = 97 // You may need to increase or decrease this number depending // on the amount of friction created by the non-rotating tire. pointTurnCircumference = distanceBetweenWheels * pi // For a point turn, the distance between the tires represents the diameter // of the turn the tires will make. // Define a function to perform a point turn. You may want to name your function something more specific // such as: pointTurnClockwise() or pointTurnCounterClockwise(). This, of course depends on the orientation of // your motors and which ports they are connected to. // A default value of 90 is being used in the definition of the function. If no degree value is passed into the function, // the robot will make a 90 degree turn. function pointTurn(degreesRobotShouldTurn = 90) { let motorDegreesPerRobotDegree = (pointTurnCircumference / wheelCircumference ) // Depending on the dimensions of the wheel and the robot, the motor that turns the wheel may have to rotate several degrees // for each degree that the robot should turn. For example, if you want the robot to turn 90 degrees, then the motors may // need to turn 300 or 400 degrees in order to perform the robot's maneuver. The number of degrees that the motor has to // rotate for each degree that the robot itself rotates is simply a ratio between the pointTurnCircumference and the wheel's // circumference. Calculate this ratio and store it in a variable called: motorDegreesPerRobotDegree // The stepMotor() function is being used in this example because it includes parameters for acceleration and deceleration // of the motor which should assist in making accurate turns. In the example below, 15% of the turn will be used for // acceleration, 70% will be used for travel at the specified top speed, and the remaining 15% will be used for deceleration. let acceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree let atTopSpeed = 0.70 * degreesRobotShouldTurn * motorDegreesPerRobotDegree let deceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree stepMotor(B, 25, acceleration, atTopSpeed, deceleration) // Pass the parameters to the stepMotor() function. stepMotor(C, -25, acceleration, atTopSpeed, deceleration) // Notice that one of the motors has positive speed and the other negative speed. // Now check the speed of the motors to see if the robot has started or completed the maneuver. // Because two motors are being used to make the turn, you will need to check the speed of both motors. // The stepMotor() function always starts with the speed at zero. waitHereWhile getMotorSpeed(B)==0 || getMotorSpeed(C)==0 // Wait here while the robot has not yet started to move. waitHereUntil getMotorSpeed(B)==0 && getMotorSpeed(C)==0 // Wait here as the motor is turning and only exit from this line when the speed returns to back to zero. stopAllMotors(true) // Apply the brake to the motor(s) } // Perform a complete 360 degree turn. You may need to adjust the value being used for the distance between the wheels (see note below). for( j = 1; j<=4; j++ ) pointTurn(90), sleep(500) // Note: The above for() loop should make the robot turn a full 360 degrees by making 4 individual 90 degree turns. // However, the width of the tire may impact the accuracy of the turns. A wide tire may turn less accurately than // a thinner tire based on the additional friction that may be cause by the wider tire. // If the robot turns more than it should, decrease the value used for the distanceBetweenWheels. // If the robot turns less than it should, increase the value used for the distanceBetweenWheels.
// Examples - Turning Functions // These functions use the gyroSensor() to facilitate the turning // Open the EV3 console panel to watch the gyro sensor values changing over time // Define the turning functions function turnRight(degrees) { // Positive values for speed turn one direction syncMotors( B, C, 20, 100 ) // Motor1 & Motor2, speed, turning ratio resetGyroSensor() // Resets the sensor to zero degrees until abs(gyroSensor())>degrees print(gyroSensor()), sleep(20) print(gyroSensor()) // Print the final value after exiting the loop stopAllMotors( true ) // Done turning now. True=with breaking on. } function turnLeft(degrees) { // Negative values for speed turn the other direction syncMotors( B, C, -20, 100 ) // Motor1 & Motor2, speed, turning ratio resetGyroSensor() // Resets the sensor to zero degrees until abs(gyroSensor())>degrees print(gyroSensor()), sleep(20) print(gyroSensor()) // Print the final value after exiting the loop stopAllMotors( true ) // Done turning now. True=with breaking on. } // Implement the turning functions turnRight(60) sleep(100) turnLeft(60)
const pi = 3.14159 // Used for many of the computations of circumference wheelDiameter = 55 // In this example, millimeters are being used wheelCircumference = wheelDiameter * pi // Circumference is the diameter times pi distanceBetweenWheels = 97 // You may need to increase or decrease this number depending // on the amount of friction created by the non-rotating tire. pointTurnCircumference = distanceBetweenWheels * pi resetGyroSensor() function pointTurnLeft(degreesRobotShouldTurn = 90) { motorDegreesPerRobotDegree = (pointTurnCircumference / wheelCircumference ) acceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree atTopSpeed = 0.70 * degreesRobotShouldTurn * motorDegreesPerRobotDegree deceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree stepMotor(B, 25, acceleration, atTopSpeed, deceleration) // Pass the parameters to the stepMotor() function. stepMotor(C, -25, acceleration, atTopSpeed, deceleration) // Notice that one of the motors has positive speed and the other negative speed. waitHereWhile getMotorSpeed(B)==0 || getMotorSpeed(C)==0 // Wait here while the robot has not yet started to move. waitHereUntil getMotorSpeed(B)==0 && getMotorSpeed(C)==0 // Wait here as the motor is turning and only exit from this line when the speed returns to back to zero. stopAllMotors(true) // Apply the brake to the motor(s) } function pointTurnRight(degreesRobotShouldTurn) { motorDegreesPerRobotDegree = (pointTurnCircumference / wheelCircumference ) acceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree atTopSpeed = 0.70 * degreesRobotShouldTurn * motorDegreesPerRobotDegree deceleration = 0.15 * degreesRobotShouldTurn * motorDegreesPerRobotDegree stepMotor(B, -25, acceleration, atTopSpeed, deceleration) // Pass the parameters to the stepMotor() function. stepMotor(C, 25, acceleration, atTopSpeed, deceleration) // Notice that one of the motors has positive speed and the other negative speed. waitHereWhile getMotorSpeed(B)==0 || getMotorSpeed(C)==0 // Wait here while the robot has not yet started to move. waitHereUntil getMotorSpeed(B)==0 && getMotorSpeed(C)==0 // Wait here as the motor is turning and only exit from this line when the speed returns to back to zero. stopAllMotors(true) // Apply the brake to the motor(s) } function turnToVector(targetVector = 0) { let currentVector = vectorValue() // The vectorValue() function returns the current vector of the gyroSensor. // Convert both the currentVector and the targetVector into numbers that range from 0 to 359. if targetVector > 360 targetVector = targetVector % 360 if targetVector < 0 targetVector = (targetVector % 360 + 360) % 360 // At this point in the program, the currentVector is a number that ranges from 0 to 359 and the targetVector also ranges from 0 to 359 // Now calculate differences between the targetVector and currentVector. // For example, if the currentVector is 45 and the targetVector is 200, then the robot will either need to turn 155 degrees in one direction or 205 degrees in the other direction. turnToTheRight = targetVector - currentVector // The degrees to turn to the right is calculated as the targetVector minus the currentVector if ( sensorIsMountedUpsideDown ) turnToTheRight *= -1 // The orientation of the sensor affects whether the degrees increase or decrease when turning to the right if ( turnToTheRight < 0 ) turnToTheRight += 360 // Add 360 if needed to avoid turning to the right a negative number of degrees turnToTheLeft = currentVector - targetVector // The degrees to turn to the left is calculated as the current gyro minus the targetVector if ( sensorIsMountedUpsideDown ) turnToTheLeft *= -1 // The orientation of the sensor affects whether the degrees increase or decrease when turning to the left if ( turnToTheLeft < 0 ) turnToTheLeft += 360 // Add 360 if needed to avoid turning to the left a negative number of degrees if ( turnToTheLeft < turnToTheRight ) pointTurnLeft(targetVector, turnToTheLeft * 1.06) if ( turnToTheRight <= turnToTheLeft ) pointTurnRight(targetVector, turnToTheRight) currentVector = vectorValue() } turnToVector(90) sleep(1000) turnToVector(345) sleep(1000) turnToVector(210) sleep(1000) turnToVector() sleep(1000) beep()