forked from FIRST-Team-2557-The-SOTABots/SensorCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisionStraightOn
More file actions
181 lines (155 loc) · 4.32 KB
/
VisionStraightOn
File metadata and controls
181 lines (155 loc) · 4.32 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
package org.usfirst.frc.team2557.robot.commands.drive;
import org.usfirst.frc.team2557.robot.Robot;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.PIDController;
import edu.wpi.first.wpilibj.PIDOutput;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.PIDSourceType;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
public class VisionDriveStraightOn extends Command {
double angle = 0;
double pixels_height = 240;
double pixels_width = 320;
double fwd = 0.16;
double fwdCmp = 0;
PIDController pidcontrollerrot;
double kProt;
double kIrot;
double kDrot;
double tolerance;
double outputr;
PIDController pidcontrollerstr;
double kPstr;
double kIstr;
double kDstr;
double tolerancestr;
double outputs;
double x;
double a0;
double a1;
double valid;
NetworkTable table;
NetworkTableEntry ta0;
NetworkTableEntry ta1;
NetworkTableEntry tx;
NetworkTableEntry tv;
public VisionDriveStraightOn() {
requires(Robot.gyroSwerveDrive);
table = NetworkTableInstance.getDefault().getTable("limelight");
outputs = 0;
outputr = 0;
valid = 0;
kProt = 0.3;
kIrot = 0.0000175;
kDrot = 0.00;
tolerance = 0.01;
pidcontrollerrot = new PIDController(kProt, kIrot, kDrot, new PIDSource(){
@Override
public void setPIDSourceType(PIDSourceType pidSource) {
}
@Override
public PIDSourceType getPIDSourceType() {
return PIDSourceType.kDisplacement;
}
@Override
public double pidGet() {
return angle;
}
}, new PIDOutput(){
@Override
public void pidWrite(double output) {
outputr = output;
}
});
pidcontrollerrot.setOutputRange(-1, 1);
pidcontrollerrot.setAbsoluteTolerance(tolerance);
kPstr = 0.008;
kIstr = 0.00005;
kDstr = 0.0000;
tolerancestr = 0.01;
pidcontrollerstr = new PIDController(kPstr, kIstr, kDstr, new PIDSource(){
@Override
public void setPIDSourceType(PIDSourceType pidSource) {
}
@Override
public PIDSourceType getPIDSourceType() {
return PIDSourceType.kDisplacement;
}
@Override
public double pidGet() {
return x;
}
}, new PIDOutput(){
@Override
public void pidWrite(double output) {
outputs = output;
}
});
pidcontrollerstr.setOutputRange(-1, 1);
pidcontrollerstr.setAbsoluteTolerance(tolerancestr);
}
@Override
protected void initialize() {
Robot.gyroSwerveDrive.gyroDrive(0, 0, 0);
pidcontrollerrot.reset();
pidcontrollerrot.setSetpoint(0);
pidcontrollerrot.enable();
pidcontrollerstr.reset();
pidcontrollerstr.setSetpoint(0);
pidcontrollerstr.enable();
}
@Override
protected void execute() {
getCamData();
getForward();
getAngle();
SmartDashboard.putNumber("vision rot output", pidcontrollerrot.get());
SmartDashboard.putNumber("ANGLES", angle);
SmartDashboard.putNumber("Vision str output", pidcontrollerstr.get());
if(valid == 1 && (Robot.m_oi.joystick1.getRawAxis(2) > 0.5)) Robot.gyroSwerveDrive.drive(outputs*-1*(-1*Math.abs(pidcontrollerrot.getError() + 0.1)/0.1), fwdCmp, outputr);
else Robot.gyroSwerveDrive.drive(0, fwdCmp, 0);
}
private void getAngle() {
if(a0 > a1){
angle = -1* a1/a0 + 1;
}else{
angle = 1* a0/a1 - 1;
}
angle -= 0.0;
}
private void getForward() {
if ((valid == 1 && Robot.m_oi.joystick1.getRawAxis(2) > 0.5)) fwdCmp = fwd;
else if (Robot.m_oi.joystick1.getRawAxis(2) > 0.5) fwdCmp = fwd*.25;
// else fwdCmp = fwd*.25;
}
public void getCamData() {
tx = table.getEntry("tx");
ta0 = table.getEntry("ta0");
ta1 = table.getEntry("ta1");
tv = table.getEntry("tv");
//read values periodically
x = tx.getDouble(0.0);
a0 = ta0.getDouble(0.0);
a1 = ta1.getDouble(0.0);
valid = tv.getDouble(0.0);
}
@Override
protected boolean isFinished() {
return pidcontrollerrot.onTarget() && pidcontrollerstr.onTarget();
// return false;
}
@Override
protected void end() {
pidcontrollerrot.disable();
pidcontrollerstr.disable();
}
@Override
protected void interrupted() {
pidcontrollerrot.disable();
pidcontrollerstr.disable();
this.end();
}
}