forked from Hutouben/FTC-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteam34235364758765435678986543245789876543
More file actions
186 lines (150 loc) · 6.06 KB
/
team34235364758765435678986543245789876543
File metadata and controls
186 lines (150 loc) · 6.06 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.IMU;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import com.qualcomm.robotcore.hardware.Gamepad;
@TeleOp(name="Rangers Robot", group="Linear OpMode")
public class Team3 extends LinearOpMode {
DcMotorEx lf;
DcMotorEx rf;
DcMotorEx lb;
DcMotorEx rb;
DcMotorEx arm;
Servo wrist;
Servo claw;
IMU imu;
static double headingOffset = 0;
double integralSum = 0;
static double kp = 0.02;
static double ki = 0.002;
static double kd = 0.0002;
static double f = 0.5;
double armTarget = 0;
double lastError = 0;
ElapsedTime elapsedTime = new ElapsedTime();
final double ticks_in_degrees = 537.7 / 360;
final double minArmPos = 0;
final double maxArmPos = 300;
double speedIncrementer = 0;
@Override
public void runOpMode() {
init(hardwareMap);
Gamepad previousGamepad1 = new Gamepad();
previousGamepad1.copy(gamepad1);
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
while (opModeIsActive()) {
driveFieldXYW(gamepad1.left_stick_y, gamepad1.left_stick_x, gamepad1.right_stick_x);
moveArm(gamepad2.left_trigger, gamepad2.right_trigger);
//telemetry.addData("gamepad1.y: ", gamepad1.y);
//telemetry.addData("previousGamePad1.y" , previousGamepad1.y);
if (gamepad1.y && previousGamepad1.y) {
setHeading(0);
telemetry.addData("Status" , "Reset Heading");
previousGamepad1.copy(gamepad1);
}
// if (!gamepad1.y && previousGamepad1.y) {
// //does nothing
// }
previousGamepad1 = gamepad1;
telemetry.addData("Status", "Running");
telemetry.addData("IMU heading", getIMUHeading());
telemetry.addData("wrist pos: ", wrist.getPosition());
telemetry.addData("claw pos: ", claw.getPosition());
telemetry.update();
}
}
private void moveArm(double lt, double rt) {
int armPos = arm.getCurrentPosition();
if (lt > 0) {
armTarget = armTarget + lt + speedIncrementer;
}
if(rt > 0) {
armTarget = armTarget - rt - speedIncrementer;
}
if (armTarget > maxArmPos){
armTarget = maxArmPos;
}
if (armTarget < minArmPos){
armTarget = minArmPos;
}
arm.setPower(pidController(armTarget, armPos));
// Add code to move the wrist and claw using w, cl, and cr
}
public void init(HardwareMap hardwareMap) {
lf = initDcMotor(hardwareMap, "fl", DcMotor.Direction.REVERSE);
rf = initDcMotor(hardwareMap, "fr", DcMotor.Direction.FORWARD);
lb = initDcMotor(hardwareMap, "bl", DcMotor.Direction.REVERSE);
rb = initDcMotor(hardwareMap, "br", DcMotor.Direction.FORWARD);
arm = initDcMotor(hardwareMap, "arm", DcMotor.Direction.FORWARD);
arm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
arm.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
wrist = hardwareMap.get(Servo.class,"wrist");
claw = hardwareMap.get(Servo.class,"claw");
initIMU(hardwareMap);
}
public DcMotorEx initDcMotor(HardwareMap hardwareMap,
String name,
DcMotor.Direction dir) {
DcMotorEx m = hardwareMap.get(DcMotorEx.class, name);
m.setDirection(dir);
m.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
return m;
}
public void driveXYW(double rx, double ry, double rw) {
double lfPower = rx - ry - rw;
double rfPower = rx + ry + rw;
double lbPower = rx + ry - rw;
double rbPower = rx - ry + rw;
lf.setPower(lfPower);
rf.setPower(rfPower);
lb.setPower(lbPower);
rb.setPower(rbPower);
}
public void driveFieldXYW(double fx, double fy, double fw) {
// rotate field orientation to robot orientation
double theta = Math.toRadians(getHeading());
double rx = fx * Math.cos(-theta) - fy * Math.sin(-theta);
double ry = fx * Math.sin(-theta) + fy * Math.cos(-theta);
driveXYW(rx, ry, fw);
}
public double pidController(double target, double state){
double error = target - state;
integralSum += error * elapsedTime.seconds();
double derivative = (error - lastError) / elapsedTime.seconds();
lastError = error;
elapsedTime.reset();
double pid = (error * kp) + (derivative * kd) + (integralSum * ki);
double ff = Math.cos(Math.toRadians(target / ticks_in_degrees)) * f;
telemetry.addData("pid: ", pid);
telemetry.addData("ff: ", ff);
telemetry.addData("target: ", target);
telemetry.addData("armPos", state);
return pid + ff;
}
public void initIMU(HardwareMap hardwareMap) {
imu = hardwareMap.get(IMU.class, "imu");
IMU.Parameters params = new IMU.Parameters(
new RevHubOrientationOnRobot(RevHubOrientationOnRobot.LogoFacingDirection.UP,
RevHubOrientationOnRobot.UsbFacingDirection.BACKWARD));
imu.initialize(params);
}
public double getIMUHeading() {
return imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES);
}
public void setHeading(double h) {
headingOffset = h - getIMUHeading();
imu.resetYaw();
}
public double getHeading() {
return AngleUnit.normalizeDegrees(headingOffset + getIMUHeading());
}
}