-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder_Tem.java
More file actions
127 lines (95 loc) · 4.08 KB
/
Copy pathEncoder_Tem.java
File metadata and controls
127 lines (95 loc) · 4.08 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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;
import org.firstinspires.ftc.teamcode.Hardware_map;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
@Autonomous(name = "encoder")
public class Encoder_Tem extends LinearOpMode {
private ElapsedTime runtime = new ElapsedTime();
static DcMotor leftFront;
static DcMotor rightFront;
static DcMotor leftBack;
static DcMotor rightBack;
public double COUNTS_PER_MOTOR_REV;
public double DRIVE_GEAR_REDUCTION;
public double WHEEL_DIAMETER_INCH;
public double COUNTS_PER_INCH;
public double ROBOT_DIAMETER;
public double ROBOT_CIRCUMFERENCE;
double power = 0.5;
@Override
public void runOpMode() {
telemetry.addData("Status", "Initialized");
telemetry.update();
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();
runtime.reset();
encoderStrafeINCH(20, 0.2);
}
int leftPos = 0;
int rightPos = 0;
public void encoderStrafeINCH(double inches, double power){
int ticks = (int)(inches*COUNTS_PER_INCH);
leftFront.setTargetPosition(leftFront.getCurrentPosition()-ticks);
leftBack.setTargetPosition(leftBack.getCurrentPosition()+ticks);
rightFront.setTargetPosition(rightFront.getCurrentPosition()+ticks);
rightBack.setTargetPosition(rightBack.getCurrentPosition()-ticks);
leftFront.setPower(power);
rightFront.setPower(power);
leftBack.setPower(power);
rightBack.setPower(power);
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);
//continue moving
while (opModeIsActive() && ((leftFront.isBusy() && rightFront.isBusy() && leftBack.isBusy() && rightBack.isBusy()))) {
}
// Stop all motion;
leftFront.setPower(0);
rightFront.setPower(0);
leftBack.setPower(0);
rightBack.setPower(0);
}
public void encoderMoveINCH(double leftINCH, double rightINCH, double power){
int newLeftTarget = leftFront.getCurrentPosition()+(int)(leftINCH*COUNTS_PER_INCH);
int newRightTarget = rightFront.getCurrentPosition()+(int)(rightINCH*COUNTS_PER_INCH);
double leftSpeed;
double rightSpeed;
leftFront.setTargetPosition(newLeftTarget);
leftBack.setTargetPosition(newLeftTarget);
rightFront.setTargetPosition(newRightTarget);
rightBack.setTargetPosition(newRightTarget);
if (Math.abs(leftINCH) > Math.abs(rightINCH)) {
leftSpeed = power;
rightSpeed = (power * rightINCH) / leftINCH;
} else {
rightSpeed = power;
leftSpeed = (power * leftINCH) / rightINCH;
}
leftFront.setPower(leftSpeed);
rightFront.setPower(rightSpeed);
leftBack.setPower(leftSpeed);
rightBack.setPower(rightSpeed);
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);
//continue movingg
while (opModeIsActive() && ((leftFront.isBusy() && rightFront.isBusy() && leftBack.isBusy() && rightBack.isBusy()))) {
}
// Stop all motion;
leftFront.setPower(0);
rightFront.setPower(0);
leftBack.setPower(0);
rightBack.setPower(0);
}
}