Robot control comes in many different forms. Now that you have walked through programming a drivetrain, we can apply those concepts to controlling other mechanisms. Since this guide utilizes the Class Bot the focus will be on the basics of controlling it's main mechanism, a single jointed arm.
Controlling an arm requires a different thought process than the one you used to control the drivetrain. While the drivetrain uses the rotation motion of the motors to drive along a linear distance, an arm rotates along a central point, or joint. When working with an arm you will have to head caution to the physical limitations of the robot this includes load bearing, range of motion, and other forces that may apply.
In this section you will learn how to use the gamepad Dpad
controls and the installed Touch Sensor to control the arm. However, the focus of this section is using code to limit the range of motion of the arm.
Start by creating a basic op mode called HelloRobot_ArmControl
.
For more information on how to create an op mode type check out the Test Bed - Blocks section.
Save the op mode and try running the code. Consider the following questions.
What happens if you press up on the Dpad
?
What happens if you press down on the Dpad
?
Try saving and running the op mode again. Pay attention to the speed of the arm going up versus going down. Does the speed seem the same?
Working with an arm introduces different factors for consideration than what you have seen previously with drivetrains. For instance, did you notice a difference in speeds when moving the arm up or down? Unlike the drivetrain, where the effect of gravity impacts the motors consistently across either direction, gravity plays a significant role in the speed of the arm motor.
Another consideration to make is the physical limitations of your arm mechanism. Certain mechanisms may have a physical limitation, that when exceeded runs the risk of damaging the mechanism or another component of the robot. There are a few ways to limit the mechanism with sensors that will help reduce the potential of a mechanism exceeding its physical limitations. In this section we will focus on using a limit switch to limit the motion range of the arm.
This section assumes that you have a basic knowledge of limit switches form the Test Bed section and the Digital Sensors article.
As you may recall from the Test Bed section limit switches use Boolean logic to dictate when a limit has been met. Limit switches typically come in the form of digital sensors, like the Touch Sensor, as digital sensors report a Boolean on/off to the system, much like a light switch.
If you are using a Class Bot your robot should have a Touch Sensor mounted to the front of your robot chassis. You also have a Limit Switch Bumper installed. Together these items create a limit switch system. By utilizing the limit switch system you can keep your Class Bot arm from exceeding the lower physical limit, or what will be known as our starting position. Lets go ahead and start coding!
Before proceeding with code please make sure that your mechanism is interfacing with, and pressing the Touch Sensor. If you have the Class Bot this entails making sure your bumper is actively pressing the Touch Sensor when the arm comes down.
Save the op mode and run it.
What happens when the Touch Sensor is pressed?
One of the common features of a limit switch, like the Touch Sensor, is the ability to reset to its default state. If you press the Touch Sensor with your finger, you may notice that as soon as you release the pressure you are applying the Touch Sensor will return to its default "not pressed" state. However, you have to release the pressure in order to accomplish this.
Make sure that the mechanism is actually interfacing with the Touch Sensor. For the Class Bot, you may need to adjust the Touch Sensor so that the Limit Switch Bumper is interfacing with it more consistently.
The code in the info block above dictates that when the Touch Sensor is pressed the arm motor is set to zero. This would work in a mechanism where the Touch Sensor is allowed to return to its default state on its own. However, once the arm presses the Touch Sensor, the weight of the mechanism will keep the Touch Sensor from returning to its default state. The combination of the weight of the mechanism and the logic of the info block code means that once the arm meets its limit it will not be able to move again.
In the Encoder Navigation section the concept of moving the motor to a specific position based on encoder ticks was introduced. The process highlighted in Encoder Navigation focused on how to convert from encoder ticks to rotations to a linear distance. A similar procedure can be utilized to move the arm to a particular position. However, unlike the drivetrain, the arm does not follow a linear path. Rather than convert to a linear distance it makes more sense to convert the encoder ticks into an angle measured in degrees.
In the image below two potential positions are showcased for the ClassBot arm. One of the positions - highlighted in blue below - is the position where the arm meets the limit of the touch sensor. Due to the limit, this position will be our default or starting position. From the Class Bot build guide, it is known that the Extrusion supporting the battery sits a 45 degree angle. Since the arm is roughly parallel to these extrusion when it is in the starting position, we can estimate that the default angle of the arm is roughly 45 degrees.
The goal of this section is to determine the amount of encoder ticks it will take to move the arm from its starting position to a position around 90 degrees. There are a few different ways this can be accomplished. An estimation can be done by moving the arm to the desired position and recording the telemetry feedback from the Driver Station. Another option is to do to the math calculations to find the amount of encoder ticks occur per degree moved. Follow through this section to walk through both options and determine which is the best for your team.
To estimate the position of the arm using telemetry and testing, lets start with the initial code we created at the start of the Basics of Programming an Arm, section.
For now you can move the limit switch related blocks to the side of your project.
Save the op mode and run it. Use the gamepad commands to move the arm to the 90 degree position. Once you have the arm properly positioned read the telemetry off the Driver Station to determine the encoder count relative to the position of the arm.
Recall from the Basic Encoder Concepts section that the encoder position is set to 0 each time the Control Hub is turned on. This means that if your arm is in a position other than the starting position when the Control Hub is turned on, that position becomes zero instead of the starting position.
The number given in the image above is not necessarily an accurate encoder count for the 90 degree position. To get the most accurate encoder reading for your robot make sure that your starting position reads as 0 encoder counts. To further increase accuracy consider doing several testing runs before deciding on the number of counts.
If you try running this code you may notice that the arm oscillates around the 90 degree position. When this behavior is present you should also notice the telemetry output for the encoder counts fluctuating. RUN_TO_POSITION
is a Closed Loop Control, which means that if the arm does not perfectly reach the target position, the motor will continue to fluctuate until it does. When motors continue to oscillate and never quite reach the target position this may be a sign that the factors determining tolerances and other aspects of the closed loop are not tuned to this particular motor or mechanism. There are ways to tune the motor, but for now we want to focus on working with the arm and expanding on how limits and positions work with regards to the mechanism.
In the initial introduction to run to position, you worked through the calculations needed to convert the ticks per rotation of a motor into ticks per mm moved. Now we want to focus on how to convert ticks per rotation of the motor to ticks per degree moved. From the previous section you should have a rough estimate of the amount of ticks you need to get to the 90 degree position. The goal of this section is to work through how to get a more exact position.
To start you will need some of the same variables we used in Encoder Navigation:
Recall, that ticks per revolution of the encoder shaft is different than the ticks per revolution of the shaft that is controlling a mechanism. We saw this in the Encoder Navigation section when the ticks per revolution at the motor was different from the ticks per revolution of the wheel. As motion is transmitted from a motor to a mechanism, the resolution of the encoder ticks changes.
For more information on the effect of motion transmission across a mechanism check out the Compound Gearing section.
The amount of ticks per revolution of the encoder shaft is dependent on the motor and encoder. Manufacturers of motors with built-in encoders will have information on the amount of ticks per revolution.
Visit the manufacturers website for your motor or encoders for more information on encoder counts. For HD Hex Motors or Core Hex Motors visit the Motor documentation.
In the Core Hex Motor specifications there are two different Encoder Counts per Revolution numbers:
At the motor - 4 counts/revolution
At the output - 288 counts/revolution
At the motor is the number of encoder counts on the shaft that encoder is on. This number is equivalent to the 28 counts per revolution we used for the HD Hex Motor. The 288 counts "at the output" accounts for the change in resolution after the motion is transmitted from the motor to the built in 72:1 gearbox. Lets use the 288 as ticks per revolution so that we do not have to account for the gearbox in our total gear reduction variable.
Since we built the the gear reduction from the motor gearbox into the ticks per revolution the main focus of this section is calculating the gear reduction of the arm joint. The motor shaft drives a 45 tooth gear that transmits motion to a 125 tooth gear. The total gear ratio is 125T:45T. To calculate the gear reduction for this gear train, we can simply divide 125 by 45.
To summarize, for the Class Bot V2 the following information is true:
Now that we have this information lets create two constant variables:
COUNTS_PER_MOTOR_REV
GEAR_REDUCTION
The common naming convention for constant variables is known as CONSTANT_CASE, where the variable name is in all caps and words are separated by and underscore.
Add the variablesCOUNTS_PER_MOTOR_REV
and GEAR_REDUCTION
variables to the initialization section of the op mode.
Now that these two variables have been defined, we can use them to calculate two other variables: the amount of encoder counts per rotation of the 125T driven gear and the number of counts per degree moved.
Calculating counts per revolution of the 125T gear (or COUNTS_PER_GEAR_REV
)is the same formula used in Encoder Navigation for ourCOUNTS_PER_WHEEL_REV
variable. So to get this variable we can multipleCOUNTS_PER_MOTOR_REV
by GEAR_REDUCTION
.
To calculate the number of counts per degree or moved or COUNTS_PER_DEGREE
divide the COUNTS_PER_GEAR_REV
variable by 360.
Add both these variables to the op mode in the initialization section of the op mode.
Finally we need to create a non-constant variable that will act as our position. Create a variable called arm position.
To get to the 90 degree position, the arm needs to move roughly 45 degrees. Set arm position equal to COUNTS_PER_DEGREE
times 45.
In the previous sections you worked on some of the building blocks for restricting an arms range of motion. From those sections you should have the foundation you need to perform basic arm control. However, there are some other creative ways you can use encoder positions and limits to expand the control you have over your arm.
This section will cover two additional types of control. The first type of control we will explore is the idea of soft limits. In the Adding a Limit Switch section we discuss the concept of physical limits of a mechanism however, there may be times you need to limit the range of motion of an arm without installing a physical limit. To do this a position based code can be used to create a range for the arm.
Once you have a basic idea of how to create soft limits, we will explore how to use a limit switch (like a touch sensor) to reset the range of motion. This type of control reduces the risk of getting stuck outside of your intended range of motion, which can affect the expected behavior of your robot.
To set the soft limits we will use some of the basic logic we established in previous sections, with some edited changes. Start with a Basic Op Mode
and add the constant variables from the Calculating Target Position section to the op mode.
Next we need to create our upper and lower limits. Create two new variables one called minPosition
and one called maxPosition
. Add both of these to the initialization section of the op mode.
An if/else if
statement needs to be added to control the arm, for this we can use the same basic logic we use in the Basics of Programming an Arm section.
To set the limit we need to edit our if/else if
statement so that the limits are built in. If DpadUp
is selected and the position of the arm is less than the maxPosition
then the arm will move to the maxPosition
. If DpadDown
is selected and the position of the is greater that the minPosition
then the arm will move towards the minPosition
.
The current code configuration will stop the motor at any point that the conditions to power the motor are not met. Depending to factors like the weight of the mechanism and any load that it is bearing, when the motor stops the arm may drop below the maxPosition
. Take time to test out the code and confirm that it behaves in the way you expect it to.
One of the benefits of having a soft limit is being able to exceed that limit. Since encoders zero tick position is determined by the position of the arm when the Control Hub powers on; if attention is not payed to what position the arm is on power up the range of motion of the arm is affected. For instance, if we have to reset the Control Hub while the arm is in the 90 degree position, the 90 degree position is equal to 0 encoder ticks. One way around this is to create an override for the range of motion.
There are a few different ways an override of sorts can be created, in our case we are going to use the a button and touch sensor to help reset our range.
Now that we have this change in place, when the a
button is pressed the arm will move toward the starting position. When the arm reaches and presses the touch sensor we want toSTOP_AND_RESET_ENCODER
.
So, if the touch sensor returns false
(or is pressed) the motor run mode STOP_AND_RESET_ENCODER
will be activated causing the motor encoder to reset to 0 ticks.
Now that this code is done, try testing it!
Unlike the joystick, which sends values corresponding to the position of the joystick, the Dpad
on the gamepad inputs Boolean FALSE/TRUE
. In order to tell the arm to move when Dpad Up
or Dpad Down
are selected, anif/else if
statement needs to be used. Select an block. Use the settings drop down to change the block to an block. Do this by switching the out for an.
Add the block to the op mode while loop.
Add the and blocks to the condition statements of the block.
One of the purposes of using the Dpad
is to help delineate which direction the arm needs to move in. In this case, should correspond with the arm moving upwards. Add a block to the do portion of the block below the block. Change the power from 1 to 0.2.
Add another block to the do portion of the block below the block. Change the power from 1 to -0.2.
Right now the logic of the statement declares that when is true (has been pressed) the motor will run in the forward ( or in this case upwards) direction at 20% duty cycle. If is true the motor will run in reverse at 20% duty cycle. If you ran the code at this stage you may have noticed that even when you released the Dpad
the motor continued to run in the selected direction. The current statement tells the robot when the motor should move and in what direction, but nothing tells the motor to stop, thus the arm is continuing to run without limits.
To fix this edit the block to be a instead. To do this select the settings icon on the block and add an below the .
Add the the else portion of the block.
Start by grabbing the statement made in the previous section, and dragging it to the side of the blocks project.
Add a new block to the while loop. Add the block to the conditional portion of the block.
Add the block set back to the code in the do port of the block.
If you recall from the initial Limit switch section, the touch sensor operates on a FALSE/TRUE
binary. When the touch sensors is not pressed the block reports true; when the touch sensor is pressed the block reports false. At this point the logic of the code states that when touch sensor is not pressed, the gamepad commands that were previously chosen operate normally. To function as a limit switch the motor needs to stop when the touch sensor is pressed.
Try adding a block to the else portion of the block.
To remedy this, an action to move the arm in the opposite direction of of the limit needs to be added to the else statement. To do this lets use another block. Since the Touch Sensor is a lower limit for the arm, the arm will need to move up (or the motor in the forward direction) to move away from the touch sensor. Following the earlier convention for moving the arm, add the as the condition in the . In the do portion of the block add the block. Change the duty cycle from 100% to 20%. In the else portion add the block.
Add this block set to the else portion of the block set.
Within the loop add a block. Add the to the number portion of the block. Change the key string from the default "key"
to "Arm Test."
To add the RUN_TO_POSITION
code the if/else
statement must first be edited back into an block, as shown in the code below.
Now recall that in order to execute RUN_TO_POSITION
the following three blocks need to be added to both sections of the block.
When DpadUp
is pressed, the arm should move to the the 90 degree position. When DpadDown
is pressed the arm should move back to the starting position. To do this set the first equal to the number of ticks it took your arm to get to 90 degrees, for this example we will use 83 ticks.
Since we want DpadDown
to return the arm to the starting position, keeping the block set to 0 will allow us to accomplish this. Set both blocks equal to 0.5.
Recall that the target position dictates which direction the motor moves, taking over the directionality control from the block, so both blocks can be set to a positive value since they will control the speed.
Once the variables are created and added to the op mode, use the blocks to set the variables to the respective values
Add this variable to the section of the statement, as this section dictates the 90 degree position. Add the block to the block.
We could set equal to . However, it is a good practice to create a variable in situations like this. If we want to add another position later, we can easily edit the variable to fit our needs.
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 .
Start by editing the to add another condition. Use the block as the condition. Add a block to the do portion of the block.
We can create an statement that focuses on performing this stop and reset when the touch sensor is pressed. Since the touch sensor reports true
when its not pressed and false
when it is, we will need to use the block.
The not operator can be used in conditional binary statements when you need inverse whether something is true
of false
. For instance, an if
statement activates when something is true, but when the Touch Sensor reports true
it is not pressed. In our case we want this if statement to activate when the touch sensor is pressed thus we need to use the not operator.
Sections
Goals of Section
Introduction to coding an arm for teleoperated control and working with a limit switch
Using motor encoders to move an arm to a specific position, such as from 45 degrees to 90 degrees.
Working with the basics of arm control, motor encoder, and limit switches to control the range of motion for an arm.
Ticks per revolution
288 ticks
Total gear reduction
2.777778