forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViper.java
More file actions
150 lines (129 loc) · 5.29 KB
/
Viper.java
File metadata and controls
150 lines (129 loc) · 5.29 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
package org.firstinspires.ftc.teamcode;
import static java.lang.Thread.sleep;
import androidx.annotation.NonNull;
import com.acmerobotics.dashboard.telemetry.TelemetryPacket;
import com.acmerobotics.roadrunner.Action;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.opencv.core.Mat;
public class Viper {
private DcMotor viper = null;
private Servo claw = null;
private ElapsedTime runtime = new ElapsedTime();
public Viper(HardwareMap hardwareMap) {
viper = hardwareMap.get(DcMotor.class, "viper");
viper.setDirection(DcMotor.Direction.REVERSE);
viper.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
viper.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
viper.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
claw = hardwareMap.get(Servo.class, "claw"); // ehub 3
}
public void initialize() {
claw.setPosition(XBot.CLAW_CLOSE);
}
public Action driveToPositionInInches(double inches) {
return new Action () {
private boolean initialized = false;
@Override
public boolean run(@NonNull TelemetryPacket packet) {
if (!initialized) {
driveToPosition(XBot.VIPER_DRIVE_SPEED, inches, 1000, packet);
initialized = true;
}
double pos = viper.getCurrentPosition();
packet.put("viper position", pos);
return Math.abs(pos - inches) < 0.5;
}
};
}
public Action getReadyToDropSpecimen() {
return new Action () {
private boolean initialized = false;
@Override
public boolean run(@NonNull TelemetryPacket packet) {
if (!initialized) {
driveToPosition(XBot.VIPER_DRIVE_SPEED, XBot.VIPER_DROP_SPECIMEN, 1000, packet);
initialized = true;
}
double pos = viper.getCurrentPosition();
packet.put("viper position", pos);
return Math.abs(pos - XBot.VIPER_DROP_SPECIMEN) < 0.5;
}
};
}
public Action testAction() {
return new Action() {
private int count = 0;
@Override
public boolean run(TelemetryPacket packet) {
count++;
System.out.println("Run call #" + count);
packet.put("Run call #: ", count);
boolean isComplete = (count > 10); // Mark action as complete after 10 iterations
System.out.println("Returning: " + isComplete);
packet.put("Returning: ", isComplete);
System.out.flush();
return isComplete;
}
};
}
// public Action dropSpecimen() {
// return new Action () {
// @Override
// public boolean run(@NonNull TelemetryPacket packet) {
// driveToPosition(XBot.VIPER_DRIVE_SPEED, XBot.DROPPED_SPECIMEN, 1000, packet);
// try {
// sleep(200);
// claw.setPosition(XBot.CLAW_OPEN);
// sleep(200);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// driveToPosition(XBot.VIPER_DRIVE_SPEED, XBot.VIPER_HOME, 1000, packet);
// return false;
// }
// };
// }
public Action openClaw() {
return new Action () {
@Override
public boolean run(@NonNull TelemetryPacket packet) {
claw.setPosition(XBot.CLAW_OPEN);
return false;
//return Math.abs(claw.getPosition() - XBot.CLAW_OPEN) > 0.1;
}
};
}
public Action closeClaw() {
return new Action () {
@Override
public boolean run(@NonNull TelemetryPacket packet) {
claw.setPosition(XBot.CLAW_CLOSE);
// return false;
return Math.abs(claw.getPosition() - XBot.CLAW_CLOSE) > 0.1;
}
};
}
private void driveToPosition(double maxSpeed, double inches, double timeoutS, TelemetryPacket packet) {
// Ensure that the OpMode is still active
int newTarget = (int)(inches * XBot.COUNTS_PER_INCH);
viper.setTargetPosition(newTarget);
// Turn On RUN_TO_POSITION
viper.setMode(DcMotor.RunMode.RUN_TO_POSITION);
runtime.reset();
viper.setPower(Math.abs(maxSpeed));
// keep looping while we are still active, and there is time left, and viper motor is running.
// Note: We use (isBusy()) in the loop test, which means that when viper motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
while ((runtime.seconds() < timeoutS) && (viper.isBusy())) {
// Set motor power
viper.setPower(maxSpeed);
packet.put("viper position", viper.getCurrentPosition());
}
// Stop all motion;
viper.setPower(0.015);
}
}