-
Notifications
You must be signed in to change notification settings - Fork 1
/
FtcTestSensorSampleTime.java
236 lines (203 loc) · 7.45 KB
/
FtcTestSensorSampleTime.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package TrcFtcSamples;
import android.util.Log;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import java.util.Locale;
import TrcCommonLib.trclib.TrcGyro;
import TrcCommonLib.trclib.TrcRobot;
import TrcFtcLib.ftclib.FtcAnalogGyro;
import TrcFtcLib.ftclib.FtcAndroidGyro;
import TrcFtcLib.ftclib.FtcDcMotor;
import TrcFtcLib.ftclib.FtcMRGyro;
import TrcFtcLib.ftclib.FtcOpMode;
/**
* This opmode tests sensor performance on I2C bus by determine sensor read loop time.
*/
@TeleOp(name="Test: Sensor Sample Time", group="TrcFtcSamples")
@Disabled
public class FtcTestSensorSampleTime extends FtcOpMode
{
private enum SensorType
{
DRIVEBASE_ENCODERS,
MR_GYRO,
ANALOG_GYRO,
ANDROID_GYRO
}
private static final String TAG = "TrcDbg";
private static final double DRIVE_POWER = 0.2;
private static final double TURN_POWER = 0.5;
private static final SensorType sensorType = SensorType.MR_GYRO;
private static final boolean LEFTWHEEL_INVERTED = false;
private static final boolean RIGHTWHEEL_INVERTED = true;
private static final double ANALOG_GYRO_VOLT_PER_DEG_PER_SEC = 0.007;
private FtcDcMotor lfWheel;
private FtcDcMotor rfWheel;
private FtcDcMotor lbWheel;
private FtcDcMotor rbWheel;
private TrcGyro gyro = null;
private long minLoopInterval = Long.MAX_VALUE;
private long maxLoopInterval = Long.MIN_VALUE;
private long loopCount = 0;
private long prevLoopTime = 0;
private long minSampleInterval = Long.MAX_VALUE;
private long maxSampleInterval = Long.MIN_VALUE;
private long sampleCount = 0;
private long prevSampleTime;
private long startTime;
private double prevSample;
@Override
public void robotInit()
{
Log.i(TAG, "initRobot started...");
lfWheel = new FtcDcMotor("lfWheel");
rfWheel = new FtcDcMotor("rfWheel");
lbWheel = new FtcDcMotor("lbWheel");
rbWheel = new FtcDcMotor("rbWheel");
lfWheel.setMotorInverted(LEFTWHEEL_INVERTED);
lbWheel.setMotorInverted(LEFTWHEEL_INVERTED);
rfWheel.setMotorInverted(RIGHTWHEEL_INVERTED);
rbWheel.setMotorInverted(RIGHTWHEEL_INVERTED);
lfWheel.resetPosition();
rfWheel.resetPosition();
lbWheel.resetPosition();
rbWheel.resetPosition();
switch (sensorType)
{
case MR_GYRO:
gyro = new FtcMRGyro("gyroSensor");
((FtcMRGyro)gyro).calibrate();
break;
case ANALOG_GYRO:
gyro = new FtcAnalogGyro("analogGyro", ANALOG_GYRO_VOLT_PER_DEG_PER_SEC);
((FtcAnalogGyro)gyro).calibrate();
break;
case ANDROID_GYRO:
gyro = new FtcAndroidGyro("androidGyro");
((FtcAndroidGyro)gyro).calibrate();
break;
}
Log.i(TAG, "initRobot completed!");
} //robotInit
@Override
public void startMode(TrcRobot.RunMode prevMode, TrcRobot.RunMode nextMode)
{
switch (sensorType)
{
case MR_GYRO:
case ANALOG_GYRO:
case ANDROID_GYRO:
gyro.setEnabled(true);
}
startTime = System.nanoTime();
prevSampleTime = startTime;
Log.i(TAG, String.format("startMode [%d]", System.nanoTime()));
prevSample = getSensorValue();
} //startMode
@Override
public void stopMode(TrcRobot.RunMode prevMode, TrcRobot.RunMode nextMode)
{
lfWheel.setPower(0.0);
lbWheel.setPower(0.0);
rfWheel.setPower(0.0);
rbWheel.setPower(0.0);
switch (sensorType)
{
case MR_GYRO:
case ANALOG_GYRO:
case ANDROID_GYRO:
gyro.setEnabled(false);
}
long endTime = System.nanoTime();
Log.i(TAG, String.format(
"Loop: MinInterval=%7.3f, MaxInterval=%7.3f, AvgInterval=%7.3f",
minLoopInterval/1000000.0, maxLoopInterval/1000000.0,
(endTime - startTime)/1000000.0/loopCount));
Log.i(TAG, String.format(
"Sensor: MinSampleInterval=%7.3f, MaxSampleInterval=%7.3f, AvgSampleInterval=%7.3f",
minSampleInterval/1000000.0, maxSampleInterval/1000000.0,
(endTime - startTime)/1000000.0/sampleCount));
} //stopMode
@Override
public void periodic(double elapsedTime, boolean slowPeriodicLoop)
{
long currTime = System.nanoTime();
double currSample = getSensorValue();
Log.i(TAG, String.format("RunContinuous [%7.3f]", (currTime - startTime)/1000000.0));
if (prevLoopTime != 0)
{
long loopInterval = currTime - prevLoopTime;
if (currSample != prevSample)
{
long sampleTime = currTime - prevSampleTime;
sampleCount++;
prevSample = currSample;
prevSampleTime = currTime;
if (sampleTime < minSampleInterval)
minSampleInterval = sampleTime;
else if (sampleTime > maxSampleInterval)
maxSampleInterval = sampleTime;
}
if (loopInterval < minLoopInterval)
{
minLoopInterval = loopInterval;
}
else if (loopInterval > maxLoopInterval)
{
maxLoopInterval = loopInterval;
}
runRobot(String.format(Locale.US, "[%4d:%7.3f] LoopInterval=%7.3f, Sensor=%7.2f: ",
loopCount, (currTime - startTime)/1000000.0, loopInterval/1000000.0,
getSensorValue()));
}
prevLoopTime = currTime;
loopCount++;
} //periodic
private double getSensorValue()
{
double value = 0;
switch (sensorType)
{
case DRIVEBASE_ENCODERS:
value = (lfWheel.getPosition() + rfWheel.getPosition() +
lbWheel.getPosition() + rbWheel.getPosition())/4.0;
break;
case MR_GYRO:
case ANALOG_GYRO:
case ANDROID_GYRO:
value = gyro.getZHeading().value;
break;
}
return value;
}
private void runRobot(String prefix)
{
switch (sensorType)
{
case DRIVEBASE_ENCODERS:
//
// Driving forward and checking encoders.
//
lfWheel.setPower(DRIVE_POWER);
rfWheel.setPower(DRIVE_POWER);
lbWheel.setPower(DRIVE_POWER);
rbWheel.setPower(DRIVE_POWER);
Log.i(TAG, prefix + String.format("lf=%.0f, rf=%.0f, lb=%.0f, rb=%.0f",
lfWheel.getPosition(), rfWheel.getPosition(),
lbWheel.getPosition(), rbWheel.getPosition()));
break;
case MR_GYRO:
case ANALOG_GYRO:
case ANDROID_GYRO:
//
// Turning right and checking gyro.
//
lfWheel.setPower(TURN_POWER);
lbWheel.setPower(TURN_POWER);
rfWheel.setPower(-TURN_POWER);
rbWheel.setPower(-TURN_POWER);
Log.i(TAG, prefix);
break;
}
}
} //class FtcTestSensorSampleTime