Skip to content

Motor Control

  • Lesson
  • 4 min read
  • 4 of 6 in this chapter

What is a motor?

To start writing code for the motors, we need to do a few things:

  • create variables for the power of each motor (in this case, the drive motors)
  • create variables for the gamepad and their inputs (joysticks, buttons, etc.)
  • set the power of the motors to these variables

The first step to coding motors

Let's go ahead and create our power variables, leftPower and rightPower. DcMotors only accept doubles, so that we will declare these as doubles. 0.99 is the same as 99% power. 0.37 is the same as 37% power.

Remember that this percentage is of the power that the battery can distribute. If the battery is at 50% and you give the motors 0.99 power, it will be 99% of the power from the battery at 50%. If the battery is at 100%, the power will be two times higher.

Java
while (opModeIsActive()) {
 double leftPower; 
 double rightPower; 

How to drive and turn

Driving and turning are very similar in that driving forwards and backward is based on having motors rotate in a direction so that the robot's wheels are rotating the same way.

Turning is different because either side of the robot is rotating (called a gate turn), or both sides go in opposite directions, creating a pivot turn.

In FTC, you can have two drivers, so if you want to use the 2nd controller, you can replace gamepad1 with gamepad2.

Java
double leftPower;
double rightPower;

double drive = -gamepad1.left_stick_y; // drives forward by going up on the left stick
double turn = -gamepad1.right_stick_x; // turns left and right by moving left and right 
leftPower = Range.clip(drive - turn, -1.0, 1.0) ; // tells left motor to go in the reverse direction
rightPower = Range.clip(drive + turn, -1.0, 1.0) ; // tells right motor to go in the given direction

We add the Range.clip function to give a set range of power to send to the robot. This is helpful if your robot moves too fast at 100% power. You can set it to -0.5 and 0.5, so it only runs at 50% of the input it gets.

Pushing the left stick to the top is 100% power, and pushing it to the bottom is -100%. Pushing the right stick to the right is 100%, and pushing it to the left is -100%.

Using a combination of the drive and turn values, we set leftPower and rightPower, like so:

  • Pushing left stick up: leftPower = 1;,rightPower = 1;
  • Pushing left stick down: leftPower = -1;,rightPower = -1;
  • Pushing left stick up and right stick left: leftPower = 2;,rightPower = 0;
  • Pushing left stick up and right stick right: leftPower = 0;,rightPower = 2;

And if you don't push all the way, you will get power on both motors.

Java
leftDrive.setPower(leftPower); 
rightDrive.setPower(rightPower);

The last two lines basically tell the motors to hold whatever power is given by the driver. (if you barely push the joystick, the robot will go slower, and vice versa).

How to use triggers to run motors

We will raise the arm using the arm motor by pressing the left and right triggers. In this code, the left trigger gives the motor negative power, and the right trigger gives the motor positive power. This is done by typing:

Java
armMotor.setPower(-gamepad1.left_trigger + gamepad1.right_trigger);

And that's it! You just programmed your motors. You can now drive around and move the arm.

Plug your controller into the Driver Station and press START + A at the same time. If the phone accepted the controller, a green gamepad icon in the top right of the screen will show up. Then, press the triangle on the right of the screen and select "MyFirstTeleOp". Then, press INIT, and then the play button. You can now control your robot. Next, we are going to learn how to use servos.