Drivetrain Encoders - Blocks
Last updated
Last updated
Moving the motors to a specific position, using the encoders, removes any potential inaccuracies or inconsistencies from using Elapsed Time. The focus of this section is to move the robot to a target position using encoders.
For this tutorial, our OpMode is named HelloRobot_Encoder!
Before diving in too far, recall that for certain drivetrains, like the Class Bot V2, one of the motors needs to be reversed as the motors are mirrored. In our example, we are adding the block under the .
As introduced in Using Encoders, using RUN_TO_POSITION
mode requires a three step process.
If we want our robot to travel a specific distance we will need to do a bit of math beforehand to calculate the TargetPosition. But for now let's start simple by setting the target position to 1000 ticks.
Order matters! The TargetPosition block must come before RUN_TO_POSITION mode is set or it will result in an error.
As mentioned, normally there would be more math involved to help determine how fast the motors should move to reach the desired position. But for testing purposes, we are going to start by keeping it simple!
Save your OpMode and give it a test. What happens once you press play? What happens if you stop the program then start it again?
For our demo code we will want to request our motors reset their encoders during the initialization process of the program.
Let's say we want our program to run only for however long it takes for the motors to reach designated position. Or maybe we intend for the robot to do something else after reaching the destination. For this we will need to edit our whileLoop block!
Even though we are ending a new exit case for our loop, we must always have our call to check opModeIsActive or our program will instantly timeout!
Save your OpMode and give it a try!
As soon as the motors hit the desired position the program will end instead of continuously run in the event they do not perfectly hit the position.
The first step is setting target position. To do so, grab the block and add it to under the comment. For this example, we are setting our position after pressing Initialize, but before we hit Play on the Driver Hub.
The next step is to set both motors to the RUN_TO_POSITION
mode. Place the block beneath the block.
Add the block beneath the block. Let's go ahead and change the duty cycle (or power) of both motors to 0.8, instead of 1.
Grab an block from the logic menu and add it to the while loop. On the left side of the block add the block. On the right side add the block.
Embed the in another block. Place the on the right side of the block. Our call for the OpMode will go in the lefthand side slot.
Right now the while loop is waiting for the right and left motors to reach their respective targets. There may be occasions when you want to wait for both motors to reach their target position, in this case the can be used such as: