rightmotor.setDirection(DcMotor.Direction.REVERSE);to the code beneath the
rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");code line.
RUN_TO_POSITIONmode requires a three step process. The first step is setting target position. To set target position add the lines
rightmotor.setTargetPosition(1000);to the op mode after the
waitForStart();command. To get a target position that equates to a target distance requires so calculations, which will be covered later. For now, set target position to 1000 ticks.
RUN_TO_POSITIONmode. Add the lines
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);to your code, beneath the
setPowerfunction was covered in previous sections and will communicate to the system what relative speed (or in this case duty cycle) is needed to get to the target, this can be used in the place of
RUN_TO_POSITIONsteps have been added to the code the code can be tested. However, if you want to wait for the motor to reach its target position before continuing in your program, you can use a while loop that checks if the motor is busy (not yet at its target). For this program lets edit the
righmotor.isBusy()functions. This will check if the left motor and right motor are busy running to a target position. The while loop will stop when either motor reaches the target position.
STOP_AND_RESET_ENCODER. Add this block to the op mode in the initialization section. Each time the op mode is initialized, the encoder ticks will be reset to zero.
RUN_TO_POSITIONwas created. The placement of
rightmotor.setTargetPosition(1000);within the code, set the target position to 1000 ticks. What is the distance from the starting point of the robot and the point the robot moves to after running this code?
static final doublevariables. Static allows references to the variables anywhere within the class and final dictates that these variables are constant and unchanged elsewhere within the code. Since these variables are not integers they are classified as double variables.
DRIVE_GEAR_REDUCTIONUse the following formula:
COUNTS_PER_WHEEL_REVvariable within the code. This will also be a
static final doublevariable.
COUNTS_PER_WHEEL_REVis calculated, use it to calculate the counts per mm that the wheel moves. To do this divide the
WHEEL_CIRCUMFERENCE_MM. Use the following formula.
COUNTS_PER_MMvariable within the code. This will also be a
static final doublevariable.
COUNTS_PER_MMwill give you the amount of counts (or ticks) needed to reach that distance.
rightTarget. These variables can be fluctuated and edited in your code to tell the motors what positions to go to, rather than place them with the constant variables, create these variables within the op mode but above the
COUNTS_PER_MM, however you may want to go a distance that is in the imperial system, such as 2 feet (or 24 inches). The target distance in this case will need to be converted to mm. To convert from feet to millimeters use the following formula:
COUNTS_PER_MMvariable to get the number of ticks needed to move the robot 2 feet. Since the intent is to have the robot move in a straight line, set both the
rightTarget, to be equal to 610 *
setTargetPosition();lines so that both motors are set to the appropriate target position. To do this add the
rightTargetvariables to their respective motor.
TPSthe to the op mode under where
COUNTS_PER_WHEEL_REV, to get the amount of ticks per second.
TPSas the parameter for
setVelocity();function is a function of the
DcMotorExInterface is an extension of the
DcMotorInterface, that provides enhanced motor functionality, such as access to closed loop control functions. To use
setVelocity();the motor variables need to be changed to
DcMotorEx. To do this both the private variable creation of the motors, and the hardware mapping need to be changed to
setPower();dictates what direction and speed a motor moves in. On a drivetrain this dictates whether the robot moves in forward, reverse, or turns.
RUN_TO_POSITIONmode the encoder counts ( or
setTargetPosition();) are used instead of
setPower();to dictate directionality of the motor. If a target position value is greater than the current position of the encoder, the motor moves forward. If the target position value is less than the current position of the encoder, the motor moves backwards
setVelocity();need to be edited to get the robot to turn. Consider the following code:
rightTargethas been changed to be a negative target position. Assuming that the encoder starts at zero due to
STOP_AND_RESET_ENCODERthis causes the robot to turn to the right. Velocity remains the same for both motors. If you try running this code, you may notice that the robot pivots along its center of rotation. To get a wider turn changing the velocity so that the right motor is running at a lower velocity than the left motor. Adjust the velocity and target position as needed to get the turn you need.
STOP_AND_RESET_ENCODERSthis will reduce the need to calculate what position to go to after a position has been reached.