forked from InspirationRobotics/FTC-2021-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncoderEx.java
102 lines (77 loc) · 3.29 KB
/
EncoderEx.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
@Autonomous(name = "encoder")
public class EncoderEx extends LinearOpMode {
//initialization, like setup
public double encoder_ticks_per_rotation; // eg: TETRIX Motor Encoder
public double gear_ratio; // 56/24
public double wheel_circumference; // For figuring circumference
public double encoder_ticks_per_cm;
DcMotor leftFront;
DcMotor rightFront;
DcMotor leftBack;
DcMotor rightBack;
public void runOpMode(){
//also initialization
encoder_ticks_per_rotation = 1440;
gear_ratio = 9.6;
wheel_circumference = 314* Math.PI;
encoder_ticks_per_cm = (encoder_ticks_per_rotation) /
(wheel_circumference * gear_ratio);
leftFront = hardwareMap.dcMotor.get("leftFront");
leftBack = hardwareMap.dcMotor.get("leftBack");
rightFront = hardwareMap.dcMotor.get("rightFront");
rightBack = hardwareMap.dcMotor.get("rightBack");
leftFront.setDirection(DcMotor.Direction.REVERSE);
leftBack.setDirection(DcMotor.Direction.REVERSE);
waitForStart();
//running code
}
//where you put functions
public void encoderDrive(double speed,
double leftCM, double rightCM) {
int newLeftTarget;
int newRightTarget;
double leftSpeed;
double rightSpeed;
//sets motors to move to set position.
leftFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//Sets new position to run to based off encoders
newLeftTarget = leftFront.getCurrentPosition() + (int) (leftCM * encoder_ticks_per_cm);
newRightTarget = rightFront.getCurrentPosition() + (int) (rightCM * encoder_ticks_per_cm);
newLeftTarget = leftBack.getCurrentPosition() + (int) (leftCM * encoder_ticks_per_cm);
newRightTarget = rightBack.getCurrentPosition() + (int) (rightCM * encoder_ticks_per_cm);
leftFront.setTargetPosition(newRightTarget);
rightFront.setTargetPosition(newLeftTarget);
leftBack.setTargetPosition(newRightTarget);
rightBack.setTargetPosition(newLeftTarget);
//starts motion
if (Math.abs(leftCM) > Math.abs(rightCM)) {
leftSpeed = speed;
rightSpeed = (speed * rightCM) / leftCM;
} else {
rightSpeed = speed;
leftSpeed = (speed * leftCM) / rightCM;
}
leftFront.setPower(Math.abs(leftSpeed));
rightFront.setPower(Math.abs(rightSpeed));
leftBack.setPower(Math.abs(leftSpeed));
rightBack.setPower(Math.abs(rightSpeed));
//continue moving
while (opModeIsActive() &&
((leftFront.isBusy() && rightFront.isBusy())))
{
}
// Stop all motion;
leftFront.setPower(0);
rightFront.setPower(0);
leftBack.setPower(0);
rightBack.setPower(0);
}
}