2.3.2.1 Autonomous Driving - Brickwolves/CodeCampWiki GitHub Wiki

Goal: Strafe forwards and backwards a given distance

NOTE: we'll be writing all of this in your Mecanum class

General Idea:

We'll be using this strafe() method specifically in a LinearOpMode. If you forget the difference between a Linear and an Iterative OpMode please check out the corresponding wiki page earlier in the curriculum. Basically, we want to call the strafe method ONCE like so:

// Strafe the robot 1000 ticks
robot.drivetrain.strafe(1000)
  1. Reset the motor encoders so our initial position is 0
  2. Use a while-loop to move our robot forwards or backwards till we reach a target distance
  3. Stop all power

Step-By-Step

  1. We need to reset the motors' tracked distance to 0.0 before every strafe. Write a resetMotors() method that takes in no parameters, has no return-type, and simply calls the setMode() method of each motor with the parameter STOP_AND_RESET_ENCODER. If you're confused check out the FTC SDK documentation.

   /**
     * Reset the DcMotors using the setMode() method and the STOP_AND_RESET_ENCODER enum.
     * We'll also need to set the motors back so they aren't stopped anymore. Uses
     * the setMode() method again with the RUN_WITHOUT_ENCODER (don't worry it still uses encoders)
     */
    public void resetMotors(){

        // Example of reseting a motor encoder
        motor.setMode(STOP_AND_RESET_ENCODER);

        // Example of putting a motor back into rotational mode
        motor.setMode(RUN_WITHOUT_ENCODER);

        // You need to complete these for each of the fr, fl, br, bl motors
    }
  1. Write a getPosition() method that averages the return-value for each motor's getCurrentPosition() call.
   /**
     *
     * @return average of all motor's getCurrentPosition()
     */
    public double getPosition(){
        double motorPosition = motor.getCurrentPosition();
    }
  1. At the top of the strafe() method use our resetMotors() method to reset the motor encoders to 0
  2. Make a local variable of type double to store our current distance as we strafe. Let's name it current_distance and set to 0.0 to start out
  3. Make a local variable of type double to store the power we supply to the motors. For now let's set the power to 0.5. Since we want the ability to travel forwards AND backwards, we might have a situation in which the number of ticks is negative. Write logic so that if the ticks are negative, we negate the power to also be negative.
  4. Now we want to supply power to our robot to move until we've reached our target distance, ticks. Write a while-loop that will loop while the current distance, current_distance, is less than the target distance, ticks, AND ALSO while the OpMode is active (I made a static isActive() method in the OpModeUtils class that you can call). Specifically, since ticks can be negative, you'll need to check the absolute value of each of these components otherwise you'll get some weird strafing! In our while-loop we'll be updating our current position at the start of every loop, and then setting the power to our motors accordingly. If you'd like, it's VERY helpful to log out the current distance traveled and the actual distance we're traveling to using the multTelemetry. In your while-loop complete the following:
    1. Update current_distance with a call to getPosition()
    2. Use the setAllPower() method to set the power to all motors using the local power variable you made above the while-loop
    3. Log out the current distance and the distance to travel using multTelemetry's addData() and update() method
  5. After we finish strafing, use the setAllPower() method to stop every motor from moving. (So we're supplying a power of 0.0)
    1. NOTE: You use the addData() method to add all of your information first, and then you call the update() method once after those lines of code to actually send that data to the phone


    /**
     * Translates the robot autonomously to a certain distance known as ticks
     * @param ticks
     */
    public void strafe(double ticks){

    }

Challenges:

  1. Add a parameter power that will control how fast your robot actually goes.
  2. Integrate your PID into your strafe() method so it always stays facing its initial angle! (Hint: Checkout the turn wiki-page to see how to add a PID instance into your Mecanum class. Use the setDrivePower() method where you supply the PID output as the turn value)
  3. Move onto Advanced Strafing 3.3