Dpadcontrols 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.
Dpadon the gamepad inputs Boolean
FALSE/TRUE. In order to dictate how the arm moves when you press
if/else ifstatement needs to be used. Create an
if/else ifstatement like the one below.
DpadUpis selected and down with
DpadDownis selected. To do this lets add
arm.SetPower();to each of the actionable parts of the
if/else ifstatement declares that when
gamepad1.dpad_upis true (has been pressed) the motor will run in the forward ( or in this case upwards) direction at 20% duty cycle. If
gamepad1.dpad_downis 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
Dpadthe motor continued to run in the selected direction. The current
if/else ifstatement tells the robot when the motor should move and in what direction, but nothing tells the motor to stop, thus the arm continues to run without limits.
if/else ifstatement to include and action to perform if neither gamepad conditions are true. Since we want the arm to stop moving if neither gamepad conditions are met lets use
arm.setPower(0);to stop the motor .
FALSE/TRUEbinary. When the touch sensors is not pressed
true; when the touch sensor is pressed
false. The logic of the code states that when touch sensor is not pressed, the motor runs at 20% duty cycle.
Gamepad if/else ifstatement within the
Limit Switch if/elsestatement.
elsestatement. 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. To do this we can create an
if/elsestatement similar to our gamepad
Gamepad if/else ifstatement. Instead of having the normal gamepad operations, when the Touch Sensor and
DpadUpare pressed the arm moves away from the Touch Sensor. Once the Touch Sensor no longer reports false the normal gamepad operations return and the arm can move in either direction again.
Gamepad if/else ifcode.
telemetry.addData("Arm Test", arm.getCurrentPosition());and
RUN_TO_POSITIONthe following three lines of cod need to be added to both sections of the Gamepad
DpadUpis pressed, the arm should move to the the 90 degree position. When
DpadDownis pressed the arm should move back to the starting position. To do this set the first
arm.setTargetPosition(0);equal to the number of ticks it took your arm to get to 90 degrees, for this example we will use 83 ticks.
DpadDownto return the arm to the starting position, keeping the
arm.setTargetPosition(0);set to 0 will allow us to accomplish this. Set both
arm.setPower(0);equal to 0.5.
RUN_TO_POSITIONis 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.
GEAR_REDUCTIONvariables to the op mode beneath where the hardware variables are created.
COUNTS_PER_GEAR_REV)is the same formula used in Encoder Navigation for our
COUNTS_PER_WHEEL_REVvariable. So to get this variable we can multiple
COUNTS_PER_GEAR_REVvariable by 360.
if(gaempad1.dpad_up)section of the
Target Position if/else ifstatement, as this section dictates the 90 degree position. To get to the 90 degree position, the arm needs to move roughly 45 degrees. Set arm position equal to
Basic Op Modeand add the constant variables from the Calculating Target Position section to the op mode.
minPositionand one called
maxPosition. Add both of these to the in the initialization section of the op mode above the
minPositionset as our starting position and the
maxPositionset to our 90 degree position. Set
minPositionequal to 0 and set
COUNTS_PER_DEGREEtimes 45 .
if/else ifstatement so that the limits are built in. If
DpadUpis selected and the position of the arm is less than the
maxPositionthen the arm will move to the
DpadDownis selected and the position of the is greater that the
minPositionthen the arm will move towards the
if/else ifstatement to add another
else ifcondition. Use the line
gamepad1.aas the condition. Add a the line
arm.setPower(-0.5);as the action item.
abutton is pressed the arm will move toward the starting position. When the arm reaches and presses the touch sensor we want to
ifstatement that focuses on performing this stop and reset when the Touch Sensor is pressed. Since the Touch Sensor reports
truewhen its not pressed and
falsewhen it is, we will need to use the logical not operator
false(or is pressed) the motor run mode
STOP_AND_RESET_ENCODERwill be activated causing the motor encoder to reset to 0 ticks.