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.
Boolean (Dpad, a/b/y/x buttons, bumpers):
Boolean data has two possible values: True and False. These two values can also be represented by On and Off or 1 and 0.
Float (Joysticks and triggers):
Float data is a number that can include decimal places and positive or negative values. On the gamepad, the float data returned will be between 1 and -1 for the joystick's position on each axis.
Let's start by adding an if else block to our active loop. Use the settings dropdown to change the block to an if elif block.
Now the skeleton of our if/else if statement is ready. We can add the Dpad up and Dpad down blocks next.
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.
For now, our easiest path is to have our arm move up with DpadUp and down with DpadDown. Let's add a set motor power 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.
Save your OpMode and give it a go! Consider the following as test your program:
Likely, you noticed even when no button is pressed the motor continues to try to move the last direction inputted. This is more obvious when pressing the DpadUp button, but if you listen closely you'll be able to hear the motor trying to move downward once DpadDown is pressed as well.
Stalled movement such as this is not healthy for our motors nor is it the easiest to control.
The current if elif 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 if elif block to have an extra else at the end of the statement. Then add a set power to 0 block to our new "else" section.
With this change in place, save your program and give it another test!
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.
Boolean (Dpad, a/b/y/x buttons, bumpers):
Boolean data has two possible values: True and False (On/Off, 1/0).
Float (Joysticks and triggers):
Float data is a number that can include decimal places and positive or negative values. Range: -1 to 1.
while (opModeIsActive()) {
if(gamepad1.dpad_up){
}
else if (gamepad1.dpad_down){
}
}
Set arm power to 0.2 for up and -0.2 for down:
if(gamepad1.dpad_up){
arm.setPower(0.2);
}
else if (gamepad1.dpad_down){
arm.setPower(-0.2);
}
Likely, you noticed even when no button is pressed the motor continues to try to move the last direction inputted. Stalled movement such as this is not healthy for our motors.
The current if/else if statement tells the robot when the motor should move and in what direction, but nothing tells the motor to stop. Add an else to set power to 0:
if(gamepad1.dpad_up){
arm.setPower(0.2);
}
else if (gamepad1.dpad_down){
arm.setPower(-0.2);
}
else {
arm.setPower(0);
}