Motor Control

How to use motors to make the robot move.

What is a motor?

To start out writing code for the motors, we need to add a 'double' function to each of the motors. This gives them the ability to hold a number value.

The first step to coding motors

A configuration basically names your hardware. Hardware being your motors, servos, sensors, etc. First, make sure both of your phones are connected to each other. Then on either phone, press in the upper right hand corner and press "Configure Robot". Press "New". Select your expansion hub(For example: Expansion Hub 6) and press "Done". If not found, click "Scan", and search for the hub.

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

How to drive and turn

Driving and turning are very similar in that driving forwards and backwards is based on having motors rotate in a direction so that the wheels of the robot are rotating the same way. Turning is different in that either one side of the robot is rotating (called a gate turn) or that both sides go in opposite directions, creating a pivot turn. In FTC you can have two drivers. If you want to use the 2nd controller, you can replace gamepad1 with gamepad2. This is done by typing this:

```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

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

The last two lines basically tells 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). Power is defined from 0% to 100% (0 to 1). .45 is the same as 45%.

How to use buttons to run motors

We are now going to 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:

`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 and A at the same time. If the controller was accepted, a green gamepad icon in the top right of the screen will show up. Then, press the on the right of the screen and select "MyFirstTeleOp". Then, press INIT, and then . You can now control your robot. Next we are going to learn how to use servos.

chevron_up