Using Limits to Control Range of Motion

In Part 2: Robot Control the idea of creating a limit switch was introduced using a physical sensor, like a touch sensor. We can make use of our motor's built-in encoder to do something similar. While a physical sensor would be described as a hard limit, using the built-in encoder is called a soft limit.

To set the soft limits we will build off the program created in the last sections (HelloRobot_ArmEncoder)!

HelloRobot_ArmEncoder program

Creating minPosition and maxPosition

To start, we need to create our upper and lower limits. Create two new variables one called minPosition and one called maxPosition to be added to initialization.

maxPosition and minPosition added for our limits

For now we want the minPosition set as our starting position and the maxPosition set to our 90 degree position. Set minPosition equal to and set maxPosition equal to . The block previously used for armPosition can be moved to our maxPosition.

Adjusting our If/Else Statement

To start, our If/Else Statement will be changed back to a simplified format like we had at the beginning of estimating the position of the arm.

If/Else Statement for arm movement

To set the limit we need to edit our if/else statement to include our limits:

  • If up on the Dpad is pressed and the position of the arm is less than the maxPosition, then the arm will move to the maxPosition.

  • If down on the Dpad is pressed and the position of the arm is greater than the minPosition then the arm will move towards the minPosition.

Use the "logic" dropdown to fill out the statement's check

Overriding Limits

One of the benefits of having a soft limit is being able to exceed that limit.

Remember that the encoders zero tick position is determined by the position of the arm when the Control Hub powers on! So if we aren't careful to reset the arm before powering on our robot this will effect the arm's range of motion. For instance, if we have to reset the Control Hub while the arm is in the 90 degree position, the 90 degree position will become equal to 0 encoder ticks.

As a back up, we can create an override for the range of motion. There are a few different ways an override can be created, but in our case we are going to use the "A" button and touch sensor to help reset our range.

Adding a Gamepad Override

Start by editing the to add another condition. Use the block as the condition. Add a block to the do portion of the block and set the power to -0.5.

Adding gamepad "A" override

Now that we have this change in place, when the "A" button is pressed the arm will move toward the starting position.

Adding a Touch Sensor Limit

Next, when the arm reaches and presses the touch sensor we want to STOP_AND_RESET_ENCODER .

We can create an additional statement that focuses on performing this stop and reset when the touch sensor is pressed. Check out Programming Touch Sensors from Part 1: Tackling the Basics for review if needed!

Touch sensor limit switch added as a separate If/Else statement

Save your OpMode and try testing it!

Last updated

Was this helpful?