forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlidesVertical.java
93 lines (77 loc) · 2.87 KB
/
SlidesVertical.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
package org.firstinspires.ftc.teamcode;
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.DcMotorSimple;
import com.qualcomm.robotcore.hardware.HardwareMap;
import org.firstinspires.ftc.robotcore.external.Telemetry;
/**
* The Slides mechanism, representing both Slide motors.
* See {@link org.firstinspires.ftc.teamcode.auto.PathMasterTheTestingNavigator}
*/
public class SlidesVertical {
private final DcMotor slidesLeft, slidesRight;
//sets limits of slides extension
private static final int UPPER_BOUND = 5200;
private static final int LOWER_BOUND = 0;
public enum VSlides {
LOWERED, // Slides at the lower bound
RAISED, // Slides at the upper bound
MIDDLE //Slides in the middle
}
public VSlides fsm = VSlides.LOWERED;
//makes sure FSM is updated based on encoder positions
public void updateFSM(){
if (getEncoders()<=LOWER_BOUND+5) fsm = VSlides.LOWERED;
if (getEncoders()>=UPPER_BOUND-5) fsm = VSlides.RAISED;
else fsm = VSlides.MIDDLE;
}
public void moveToLowerBound() {
if(!(fsm == VSlides.LOWERED)) setPosition(LOWER_BOUND);
fsm = VSlides.LOWERED;
}
public void moveToUpperBound() {
if(!(fsm== VSlides.RAISED)) setPosition(UPPER_BOUND);
fsm = VSlides.RAISED;
}
public SlidesVertical(HardwareMap map) {
slidesLeft = map.get(DcMotor.class, "slidesL");
slidesRight = map.get(DcMotor.class, "slidesR");
slidesRight.setDirection(DcMotorSimple.Direction.REVERSE);
}
public void setPower(double power) {
slidesLeft.setPower(power);
slidesRight.setPower(power);
}
public int getEncoders() {
return (slidesLeft.getCurrentPosition() + slidesRight.getCurrentPosition()) / 2;
}
//simply moves up or down based on input from controller
public void slidesMove(double input, boolean overrideButton, Telemetry telemetry){
int pos = getEncoders();
telemetry.addData("Slides position: ",pos);
if(!overrideButton && ((pos > UPPER_BOUND && input > 0) || (pos < LOWER_BOUND && input < 0) )) {
setPower(0);
}else{
setPower(input);
}
updateFSM();
}
//moves based on position inputted
public void setPosition(int targetPosition) {
final int TOLERANCE = 5;
int error = targetPosition - getEncoders();
if (Math.abs(error) > TOLERANCE) {
double power = Math.max(0.1, Math.min(1.0, Math.abs(error) / 1000.0));
if (error > 0) {
setPower(power);
} else {
setPower(-power);
}
} else {
setPower(0);
}
updateFSM();
}
}