Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
At the moment, our motors are set to power on to a full forward at the start of our program. For reference, the image below shows the full scale of movement between forward and reverse:
Let's take this information and think back to when we first programmed a motor to move with our gamepad. During that section our motor was able to rotate at different power levels depending on how far and in which direction our joystick moved. However, do you recall the problem we had with this set up?
While using our previous code our motor only spun when the joystick was moved along the y-axis. Moving to the left or right did not ask the motor to power on, but it would begin to stutter some at the diagonals.
This is where adding some math to our code comes into play. Remember on an arcade drive both motors are being controlled by a single joystick. We need our robot to be able to calculate for both motors how much they should power on and in which direction. Thankfully, once we have it all set up our robot will be able to handle the calculations itself as the program runs!
By the end, we should be able to create situations like the following charts where the motors respond to create different forms of motion:
What happens when we set the power of the rightmotor to 0.3 and leftmotor to 1?
What happens when we set the power of the leftmotor to 0.5 and rightmotor to 1?
What happens when we set the power of the leftmotor to -0.4 and rightmotor to 0.4?
After testing different combinations, let's look at a quick breakdown of how power between the motors effects movement:
rightMotor power = leftMotor power
Straight Forward or Reverse
rightMotor power > leftMotor power
Left Turn
rightMotor power < leftMotor power
Right Turn
For our arcade drive, the goal is for our joystick inputs to calculate to the following motor outputs:
Joystick Direction
rightmotor
leftmotor
Movement
(0,1)
1
1
Forward
(0,-1)
-1
-1
Reverse
(-1,0)
1
-1
Turn left
(1,0)
-1
1
Turn right
To get the outputs expressed in the table above, the gamepad values must be assigned to each motor in a meaningful way. To do so we are going to set up two equations in our code using the variables we have already established:
You may not expect it, but there is a little bit of math that needs to be done to get our robot moving smoothly. But before we dive too deeply into that let's start with the basics of movement we'll need.
To start, create two variables x and y . This can be done within the Variable menu on the lefthand side.
For a quick reference let's take a look at what number each variable would be assigned at their far ends:
Right now we have x and y assigned values based on our joystick's movement, but what does that mean? Why is that useful?
Maybe you have seen in a math class before something like this:
In this case, a is our variable that has been assigned some value. For this example, we can determine that value is 7. But what does that mean in programming?
Variables used in programming follow this same principle. We can define a variable within our code to hold a set value or a value that changes, such as we are doing here. Then whenever that variable is referenced the robot will read it as that assigned value!
So using our example above if I had:
My robot would know my variable of a is equal to 7 and therefore calculate the answer as 17 for me!
Consider for a moment, why should we use a variable when we could just use the number on its own?
We'll be using variables in greater detail in later sections, but even for our drive code you will be able to see the use of variables helps keep our program clean and easier to follow.
Thus far we've tackled a lot of the basics to get parts of our robot moving in response to our gamepad or sensors. So what comes next?
In Part 2 of Hello Robot we're going to look at working with a full, functional robot. By the end of this section your robot will be able controlled with the gamepad!
Before continuing it is recommended to complete, at minimum, a drivetrain. There are a few different options depending on the kit being used. We recommend looking at the , such as what is used with our , or the Class Bot V2!
For this guide the Class Bot V2 is used. for the Class Bot V2!
The graphic below highlights the major hardware components of the Class Bot V2. These components are important to understand for the configuration process.
In order to continue forward with the Robot Control programming sections, a new configuration file must be made for the components on the robot. It is your choice what variable names you would like to assign to your robot, but for reference this guide will use the following names for each hardware component.
Before continuing it is important to understand the mechanical behavior of different drivetrains. The two most common drivetrain categories types are Differential and Omnidirectional.
Differential Drivetrains are the standard starting drivetrain. They are able to move in forward/reverse, as well as rotate either direction around a central point. There are different styles of directional drivetrains depending on the type of wheels, number of motors, and wheel positions. These include 4WD, 6WD, West Coast, and C-Channel drivetrains.
By comparison, omnidirectional drivetrains can move in any direction with each wheel typically being controlled separately. This allows for advanced forms of navigation, such as strafing, but requires a more complex program. Omnidirectional drivetrains include the use of omni wheels in Y or X configurations, mecanum wheel drivetrains, swerve drive, and other forms of holonomic drives.
The Class Bot V2 uses a directional drivetrain, which will be the drivetrain of focus for this tutorial!
While driving our robot with teleop control, we will be giving the robot inputs from our gamepad connected to our Driver Hub. Its job is to translate those inputs to the robot to perform the specified actions. How your robot drives and what joystick does what can be largely dependent on what you or your team's driver is comfortable using. Let's take a look at two of the more common methods of control: Tank Drive and Arcade Drive.
For tank drive, each side of the differential drivetrain is mapped to its own joystick so both will be used. Changing the position of each joystick allows the drivetrain to steer and change its heading. Sample code exists in the Robot Controller Application to control a differential drivetrain in this way.
For arcade drive, each side of the differential drivetrain is controlled by a single joystick. Changing position of the joystick changes the power applied to each side of the drivetrain allowing for a given command.
Arcade drives typically have left/right movement of the joystick set to spin the robot about its axis with forward/back moving the robot forward and reverse.
From the math menu grab the and blocks and add them to the respective motor in the block.
From there the robot will handle the rest as the program runs! Each time our loop cycles, the robot will check the position of our joysticks and quickly adjust the motor power based on its calculations.
Save your OpMode and give it a try!
How our robot moves is dependent on how much power each motor is receiving. Before continuing, we can explore with our current program how the robot reacts when changing the values in our block.
Rather than setting a static numerical value for our motors, the and blocks will help our robot to translate the motion of the joysticks into a power level.
( , )
Once created, add the andblocks to the while loop above your existing power block.
Our y variable will be assigned as , which is the y-axis of the right joystick. Remember just like in the y-axis will need to be inversed using the block available from the Math menu.
Next assign x as the , which is the x-axis of the right gamepad joystick. The x-axis of the joystick does not need to be inverted.
The and block sets assign values from the gamepad joystick to x and y. Depending on the orientation of the joystick, these valuables will receive some value between -1 and 1.
By using setting our variable at the beginning of our code we can inverse it without needing to do so every time we may reference the joystick's y-axis. Within a longer program, having our variables defined at the start would allow us to quickly change a value without having to hunt down or double check that every possible instance in the code has been updated to reflect this change. Instead we are able to change it once and continue testing!
The section focused on configuring the components in the Test Bed.
Arcade Drive may also be configured as a Split Arcade Drive where one joystick turns the robot while the other controls forward/back. An example of a Split Arcade Drive robot can be found as part of our
Next add the and variables to the formula blocks.
Hardware Component
Hardware Type
Name
Right Drive Motor
REV Robotics UltraPlanetary HD Hex Motor
rightmotor
Left Drive Motor
REV Robotics UltraPlanetary HD Hex Motor
leftmotor
Arm Motor
REV Robotics Core Hex Motor
arm
Claw Servo
Servo
claw
Touch Sensor
REV Touch Sensor
touch
Joystick Direction
0
1
0
-1
-1
0
1
0
In Part 1 we learned how to control a single motor by giving it power or input from a joystick. For controlling a drivetrain, we need to be able to control two motors simultaneously to help the robot move. While we could try adding each motor individually, Blocks has a dual motor block available already for just this purpose.
To access the dual block you will need to select the actuators dropdown menu:
Not seeing DcMotor under the actuators menu? Make sure your configuration has been properly set up and activated before getting started!
Any code from Part 1 should be moved to the side of the workspace or deleted before continuing this section. Alternatively, you may choose to create a new program.
When there are multiple of the same type of variable (such as multiple DcMotors) the variable specific blocks will choose a default variable based on alphabetical order. For this example, OpMode DcMotor blocks will default to the arm variable.
Use the variable drop down menu on the block to change from arm to rightmotor.
Before running your code for the first time, pause and think about the following:
What do you expect your robot to do once the program is activated?
Now save your OpMode using the button in the upper lefthand corner and give your program a go!
Always keep the Driver Hub within reach in the case of the event that a robot does not perform as expected. When in doubt, disable your robot to keep you and it safe.
DC Motors are capable of spinning in two different directions depending on the current flow provided. When a positive power value is applied the motors will spin in a clockwise direction. The opposite will happen when using a negative power value, meaning the motors will spin in a counter clockwise direction.
But how does that help with our current spinning robot? Let's take a closer look at our physical robot to find out:
Notice how the motors on your robot are currently mirrored from each other as part of the drivetrain. Now think about how we learned that when giving the motors a positive value they should turn clockwise. This is still how, however while they may both be rotating clockwise, the direction they know to be as clockwise is opposite.
Try activating your robot's code again, but this time watching which direction the wheels turn. You may consider supporting the robot's frame so the wheels are suspended to make this easier to see.
There are a couple ways we could adjust our program to help our robot not to be a spinning top. For example, we could make sure the power is set to a negative value whenever one of our motors is called. Or we could simple reverse our motor's direction during initialization.
Go ahead and give it a try!
The time has come to program our robot to respond to our gamepad inputs so we can drive it around! To start we will be focusing on controlling our drivetrain.
Think back to the very start of for a moment. We will be programming our robot using the Arcade Style of TeleOp. This means our forward and back movements will be controlled using the y-axis of the joystick while turning will be controlled by the x-axis.
This section will introduce the use of variables as we create our program. This will allow us to set up calculations in our program that will determine what power the motors should receive based on our gamepad's input. How much power each motor receives changes how the robot will move, so it is important to also review this relationship will establishing our code!
Below is a sneak peek at our final program:
Right Stick vs. Left?
Which joystick is used for driving the robot is largely based on driver preference.
Something to consider is the physical limitations of your arm mechanism. Just like you have limitations in how far you can move your arms, our robot's arm can only move up or down so far. However, while you have nerves to help you know when you've hit your limit, we need to add something to help prevent the robot from damaging itself or things around it.
This is where the importance of using sensors comes into play. There are a few ways we could limit our mechanism. What do you think they could be?
In this section we're going to look at how to add a limit switch to stop our robot's arm from extending too far. You might recall in our "Programming Touch Sensors" section that we discussed the touch sensor can act like an on/off switch when programmed. Essentially we're going to have the arm of our robot turn its motor off once the limit is met!
This section is designed with the REV Touch Sensor or Magnetic Limit Switch in mind. There may be additional requirements for 3rd party touch sensors.
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 installed.
For the moment, let's grab the statement made in the previous section to be set off to the side for later use.
Think back to the "Programming Touch Sensors" section, where you learned how to create a basic limit switch program, similar to the one below:
We also learned how the touch sensor operates on a TRUE/FALSE binary
. So what is our program above asking the robot to do?
While testing, double check the arm mechanism is aligned with the Touch Sensor.
For the Class Bot V2, you may need to adjust the Touch Sensor so that the Limit Switch Bumper is connecting with it more consistently.
Save the op mode and give it a try!
When you tested the above code what happened? You may have encountered a problem where once the touch sensor was pressed the arm could no longer move. This is probably not ideal so why do you think this happened?
One of the advantages of a limit switch, like the touch sensor, is the ability to easily reset to its default state. All it takes is the pressure being released from the button, but right now all our robot knows is that if the switch is pressed it needs to turn off power!
So how do we fix that?
To remedy this, an action to move the arm in the opposite direction of the limit needs to be added to the do statement. Since the touch sensor serves as the lower limit for the arm, it will need to move up (or the motor in the forward direction) to back away from the touch sensor.
To do this we can create an if/else
statement similar to our existing gamepad Gamepad if/else if
statement. In this instance, when the touch sensor andDpadUp
are pressed together the arm moves away from the touch sensor. Once the touch sensor no longer reports true, the normal gamepad operations will takeover again!
Now we can snap this into our do statement to complete our code:
Add the block to op mode while loop.
Add to your program, under the comment block. If you recall, blocks placed in this section will run AFTER the intilizal button is pressed on the Driver Hub, but BEFORE the play button is clicked.
Now with the block added the direction for the rightmotor will always be reversed for this program. Our power values do not need to be changed.
For Hello Robot, we will be referencing using the right stick due to the arm control using the Dpad. This can be changed at any time by selecting your preferred option from the dropdown menu on the blocks!
Add the block set back to the code in the else port of the block.
It's time to bring everything together so to have a fully mobile robot! Returning to our HelloRobot_TeleOp program we can add our arm control to our loop below the drivetrain code.
Right Stick vs. Left?
Which joystick is used for driving the robot is largely based on driver preference.
These premade programs do not include control of the Class Bot V2's "claw" servo. To learn about programming a servo visit Programming Servos.
Note: This configuration file includes the test_motor from Part 1 and no color sensor.
Now that our robot is able to drive around let's get our arm up and moving!
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.
Unlike our drivetrain, our arm has physical limitations for how far it can rotate. We don't want our robot to damage itself so we'll be making use of our touch sensor to act as a limit switch.
For this section, we will start by creating a new program called HelloRobot_ArmControl. We will be able to add this to our drivetrain OpMode later, but for now keeping it separate will help us to focus just on the arm.
To control our arm we will be using the Dpad on our gamepad. While our joystick provides a range of possible values or float data, our Dpad will only be read as 1 or 0. To us these numbers translate to true, the button has been pressed, or false, the button has not been pressed.
With this in place our robot will be checking if the Dpad Up or Dpad Down button are pressed before proceeding with the appropriate action. But what do we want our arm to do?
For now, our easiest path is to have our arm move up with DpadUp and down with DpadDown. You may decide later to change which buttons are being used, but the logic found here should be similar.
Save your OpMode and give it a go! Consider the following as test your program:
What happens if you press up on the Dpad?
What happens if you press down on the Dpad?
What happens when neither button is actively pressed?
Did the robot move as you expected?
With this change in place, save your program and give it another test!
For Hello Robot, we will be referencing using the right stick due to the arm control using the Dpad. This can be changed at any time by selecting your preferred option from the dropdown menu on the blocks!
Let's start by adding an block to our active loop. Use the settings dropdown to change the block to an block.
Now the skeleton of our if/else if statement is ready. We can add the and blocks next.
Let's add a block to each "do" section of our statement. While testing our movement we will want to reduce the power to a more manageable range. For now, we will set our up to 0.2 and down to -0.2.
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. Ideally, we want our arm to move ONLY when a button is pressed.
To fix this we can edit the block to have an extra at the end of the statement.
Then add a block to our new "else" section.