Walkthrough
Programming
Tutorials
Lessons
How to successfully create 3d printed parts for FTC
How to Design the Best Parts You Can Possibly Make!
Miscellaneous
Motors in FTC
Vinay Saravana Ruban
2024-04-22
software | motors | programming | java
Introduction
This guide is written assuming that you have completed the installation instructions and have a basic understanding of java.
In FTC robotics, motors serve the crucial purpose of providing mechanical motion to different parts of the robot, such as wheels, arms, and mechanisms. Note that if you are looking for precise angular movement, servos are recommended, as motors are for continuous and high torque rotation.
Instantiating
To begin, create a class, and for this example, will extend LinearOpMode.
@TeleOp
public class Drive extends LinearOpMode {
@Override
public void runOpMode() throws InterruptedException {
...
}
}
Note: TeleOp refers to tele-operated, meaning that the motors will be operated with a controller.
In FTC, you will be using DC motors. The two classes that can be used to read and write to the motor is DcMotor
and DcMotorEx
. The latter provides some extra functionality on top of the former, and as long it is supported there is no downside to using it.
Begin by importing the class. In Android Studio, you can press Alt+Enter if the class is referenced without being imported. Once imported, you can create the object using its class name, like this:
DcMotorEx motor;
After creating the object, you need to instantiate it using the hardwareMap
. Here's an example of how to instantiate the motor:
motor = hardwareMap.get(DcMotorEx.class, "Lift Motor");`
Methods
Direction
As the programmer, you can set the direction the motor will turn when given an input. When given as input of 1, with the shaft facing away the user when the controller is held normally, this will move away the player as expected in the direction of the joystick:
motor.setDirection(DcMotor.Direction.FORWARD);
While this will move towards the player:
motor.setDirection(DcMotor.Direction.REVERSE);
Zero Power Behavior
Additionally, you can choose what will happen when there is no input given. Either you can make the robot slow to a halt manually:
motor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
Or, make friction and air resistance do all the work:
motor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
Mode
Finally, you can set mode/behavior of the the robot. There treat how the encoders, which take your inputs and turn it into outputs will functions. These are:
motor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
In the above mode, the Control Hub will not use encoders to to maintain a constant speed. Speed will vary depending on external and real world factors. Perfect for drivetrain motors, driven using a joystick
motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
The encoder will try and regulate the speed to account for external factors. This will not apply a percentage of the motor's power, but rather a specific velocity. Perfect for independent operations that will occur after a button is pressed.
motor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
Will go to a specified location rather than based on speed. You can specify a maximum speed. Can be used for various items that have have set positions such as intakes or arms. Inadvisable for drivetrains that have multiple motors
motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
Stops the motor and sets it encode value back to zero. Use this to zero your motors at the start of a program, and whenever you want to reach its default position.
Running a motor
With this code, assuming that you are using RUN_WITHOUT_ENCODER as we will be programming a drivetrain, the motor will be at 50% of its power.
motor.setPower(0.5)
Direction can be added with two motors, and two inputs from two joysticks:
leftMotor.setPower(direction + rotation);
rightMotor.setPower(direction - rotation);
This is possible as the left wheels will spin forward while the right spin backward, causing rotation. The opposite will apply while pushing the stick left.
To get and input from a joystick, just use gamepad1
for now. Then access the appropriate property. E.g.
double power = -gamepad1.left_stick_y;
Summary
To provide some basic summary code that is super simple.
@TeleOp
public class Drive extends LinearOpMode {
//Initialise
private DcMotorEx LeftMotor;
private DcMotorEx RightMotor;
@Override
public void runOpMode() {
//Instatiate
LeftMotor = hardwareMap.get(DcMotorEx.class, "LeftMotor");
RightMotor = hardwareMap.get(DcMotorEx.class, "RightMotor");
//Tell us
telemetry.addData("Status", "Initialized");
telemetry.update();
// Wait for START to be pressed
waitForStart();
//Return is STOP is pressed
if (isStopRequested()) return;
//Run while START is pressed
while (opModeIsActive()) {
//Y stick is reversed
double power = -gamepad1.left_stick_y;
double rotation = gamepad1.right_stick_x;
leftMotor.setPower(power + rotation);
rightMotor.setPower(power - rotation);
}
}
}
To acquire more advanced code pertaining to drivetrains visit gm0's page on mecanum drives, what we recommend as your drivetrain.
Code may be used for competition purposes.