forked from vbatts/ftc-robot-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArcadeDrive.java
56 lines (43 loc) · 1.87 KB
/
ArcadeDrive.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.firstinspires.ftc.teamcode.ftc26358;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
@TeleOp(group="@Coaches Institute")
public class ArcadeDrive extends OpMode {
public static final double RIGHT_HANDICAP = 0.8;
public static final double LEFT_HANDICAP = 0.8;
DcMotor leftMotor;
DcMotor rightMotor;
@Override
public void init() {
telemetry.addData("Programmer", "Vincent");
leftMotor = hardwareMap.get(DcMotor.class, "left_motor");
rightMotor = hardwareMap.get(DcMotor.class, "right_motor");
// if the negative Y bothers you ...
leftMotor.setDirection(DcMotorSimple.Direction.REVERSE);
// trade 10% power/speed, for better accuracy/control
leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
@Override
public void loop() {
double leftY = gamepad1.left_stick_y * LEFT_HANDICAP;
double leftX = gamepad1.left_stick_x * RIGHT_HANDICAP;
telemetry.addData("l_y", leftY);
telemetry.addData("l_x", leftX);
double leftSpeed = leftY + leftX;
double rightSpeed = leftY - leftX;
setPowers(leftSpeed, rightSpeed);
}
// this "normalizes" the inputs since the motors max at 1.0, but adding the leftY and leftX can be up to 2.0
private void setPowers(double leftSpeed, double rightSpeed) {
double maxSpeed = 1.0;
maxSpeed = Math.max(maxSpeed, Math.abs(leftSpeed));
maxSpeed = Math.max(maxSpeed, Math.abs(rightSpeed));
leftSpeed = leftSpeed / maxSpeed;
rightSpeed = rightSpeed / maxSpeed;
leftMotor.setPower(leftSpeed);
rightMotor.setPower(rightSpeed);
}
}