forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugLaunchEncoders.java
More file actions
167 lines (137 loc) · 7.25 KB
/
Copy pathDebugLaunchEncoders.java
File metadata and controls
167 lines (137 loc) · 7.25 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
/* Copyright (c) 2021 FIRST. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided that
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of FIRST nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.robotcore.external.hardware.camera.controls.ExposureControl;
import org.firstinspires.ftc.robotcore.external.hardware.camera.controls.GainControl;
import org.firstinspires.ftc.vision.VisionPortal;
import org.firstinspires.ftc.vision.apriltag.AprilTagDetection;
import org.firstinspires.ftc.vision.apriltag.AprilTagProcessor;
import java.util.List;
import java.util.concurrent.TimeUnit;
@TeleOp(name="DebugLaunchEncoders", group="Decode Challenge")
public class DebugLaunchEncoders extends LinearOpMode {
// Hardware
private ElapsedTime runtime = new ElapsedTime();
private DcMotorEx launcherRight = null;
private DcMotorEx launcherLeft = null;
// RPM Presets
private static final double RPM_PRESET_A = 3700.0;
private static final double RPM_PRESET_B = 4200.0;
private static final double RPM_PRESET_X = 4500.0;
private static final double RPM_PRESET_Y = 5000.0;
private double targetRPM = RPM_PRESET_B; // Default target
@Override
public void runOpMode() {
// Initialize launcher motors with extended interface for velocity control
launcherRight = hardwareMap.get(DcMotorEx.class, "launcher_right");
launcherLeft = hardwareMap.get(DcMotorEx.class, "launcher_left");
// Configure launcher motors for velocity control using encoders
launcherRight.setDirection(DcMotor.Direction.FORWARD);
launcherRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
launcherRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
launcherRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
// to configure. set P = 0, then run and see if target ~= actual. increase or decrease f
// to get them to be close. Then set P = 5 to start and add as needed. P is what keeps it
// at speed (after launching) and ramp up time.
launcherRight.setVelocityPIDFCoefficients(.5,0.0,0.0,18.3);
launcherLeft.setDirection(DcMotor.Direction.FORWARD);
launcherLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
launcherLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
launcherLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
// may be configured differently based on internal resistance or weight or other factors.
// fining this needs a bit less f
launcherLeft.setVelocityPIDFCoefficients(.5,0.0,0.0,19.5);
telemetry.addData("Status", "Initialized");
telemetry.addData("Target RPM", targetRPM);
telemetry.update();
waitForStart();
runtime.reset();
while (opModeIsActive()) {
// RPM Presets
if (gamepad2.y) {
targetRPM = RPM_PRESET_Y;
} else if (gamepad2.x) {
targetRPM = RPM_PRESET_X;
} else if (gamepad2.b) {
targetRPM = RPM_PRESET_B;
} else if (gamepad2.a) {
targetRPM = RPM_PRESET_A;
}
// save the trigger as a bool, so we know f it is on / off more easily.
// if it is != 0 (it is a float)
boolean launchTrigger = gamepad2.right_trigger != 0.0f;
double targetRadPerSec = 0;
// setVelocity is radians / second
if (launchTrigger) {
// OLD CODE
// targetVelocity = (targetRPM / 60.0) * TICKS_PER_REV;
// Formula to get radians per sec is => Target RPM * (2 PI / 60 )
targetRadPerSec = targetRPM * 2.0 * Math.PI / 60.0;
launcherRight.setVelocity(targetRadPerSec);
launcherLeft.setVelocity(-targetRadPerSec);
} else {
launcherRight.setVelocity(0);
launcherLeft.setVelocity(0);
}
// Get actual velocity for telemetry
// getVelocity is radians / second
double actualRadPerSecRight = launcherRight.getVelocity();
double actualRadPerSecLeft = launcherLeft.getVelocity();
// OLD CODE
// double actualRPMRight = (actualVelocityRight / TICKS_PER_REV) * 60.0;
// double actualRPMLeft = (actualVelocityLeft / TICKS_PER_REV) * 60.0;
// opposite formula to get back
// Formula to get RPM => rad per sec * 60 / (2 PI)
double actualRPMRight = actualRadPerSecRight * 60.0 / ( 2 * Math.PI );
double actualRPMLeft = actualRadPerSecLeft * 60.0 / ( 2 * Math.PI );
//Telemetry
telemetry.addData("Status", "Run Time: " + runtime.toString());
telemetry.addLine();
telemetry.addData("Target RPM", targetRPM);
telemetry.addData("Actual RPM Right", actualRPMRight);
telemetry.addData("Actual RPM Left ", actualRPMLeft);
telemetry.addLine();
telemetry.addData("Target Rad/Sec", targetRadPerSec);
telemetry.addData("Actual Rad/Sec Right", actualRadPerSecRight);
telemetry.addData("Actual Rad/Sec Left ", actualRadPerSecLeft);
telemetry.addLine();
telemetry.addData("Trigger", "%b", launchTrigger);
telemetry.addLine();
telemetry.update();
}
}
}