-
Notifications
You must be signed in to change notification settings - Fork 1
/
FtcTestRevHub.java
199 lines (182 loc) · 8.02 KB
/
FtcTestRevHub.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
/*
* Copyright (c) 2017 Titan Robotics Club (http://www.titanrobotics.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package TrcFtcSamples;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import TrcCommonLib.trclib.TrcGameController;
import TrcCommonLib.trclib.TrcGyro;
import TrcCommonLib.trclib.TrcRobot;
import TrcFtcLib.ftclib.FtcAnalogInput;
import TrcFtcLib.ftclib.FtcColorSensor;
import TrcFtcLib.ftclib.FtcDcMotor;
import TrcFtcLib.ftclib.FtcDigitalInput;
import TrcFtcLib.ftclib.FtcGamepad;
import TrcFtcLib.ftclib.FtcImu;
import TrcFtcLib.ftclib.FtcOpMode;
import TrcFtcLib.ftclib.FtcServo;
/**
* This opmode demonstrates the use of REV Expansion Hub.
*/
@TeleOp(name="Test: REV Expansion Hub", group="TrcFtcSamples")
@Disabled
public class FtcTestRevHub extends FtcOpMode
{
private static final int NUM_MOTORS = 4;
private static final int NUM_SERVOS = 6;
private static final int NUM_ANALOG_INPUTS = 4;
private static final int NUM_DIGITAL_INPUTS = 8;
private FtcGamepad gamepad;
private TrcGyro gyro;
private FtcColorSensor colorSensor;
private final FtcDcMotor[] motors = new FtcDcMotor[NUM_MOTORS];
private final FtcServo[] servos = new FtcServo[NUM_SERVOS];
private final FtcAnalogInput[] analogInputs = new FtcAnalogInput[NUM_ANALOG_INPUTS];
private final FtcDigitalInput[] digitalInputs = new FtcDigitalInput[NUM_DIGITAL_INPUTS];
private boolean dpadRightPressed = false;
@Override
public void robotInit()
{
gamepad = new FtcGamepad("GamePad", gamepad1, this::buttonEvent);
gamepad.setYInverted(true);
gyro = new FtcImu(
"imu", RevHubOrientationOnRobot.LogoFacingDirection.UP,
RevHubOrientationOnRobot.UsbFacingDirection.FORWARD);
colorSensor = new FtcColorSensor("colorSensor");
for (int i = 0; i < motors.length; i++)
{
motors[i] = new FtcDcMotor("motor" + i);
}
for (int i = 0; i < servos.length; i++)
{
servos[i] = new FtcServo("servo" + i);
}
for (int i = 0; i < analogInputs.length; i++)
{
analogInputs[i] = new FtcAnalogInput("analog" + i);
}
for (int i = 0; i < digitalInputs.length; i++)
{
digitalInputs[i] = new FtcDigitalInput("digital" + i);
}
} //robotInit
@Override
public void startMode(TrcRobot.RunMode prevMode, TrcRobot.RunMode nextMode)
{
gyro.resetZIntegrator();
gyro.setEnabled(true);
} //startMode
@Override
public void stopMode(TrcRobot.RunMode prevMode, TrcRobot.RunMode nextMode)
{
gyro.setEnabled(false);
} //stopMode
@Override
public void periodic(double elapsedTime, boolean slowPeriodicLoop)
{
if (slowPeriodicLoop)
{
//
// Test I2C IMU.
//
telemetry.addData("Heading", "Heading: x=%6.1f,y=%6.1f,z=%6.1f",
gyro.getXHeading().value, gyro.getYHeading().value, gyro.getZHeading().value);
telemetry.addData("TurnRate", "TurnRate: x=%6.1f,y=%6.1f,z=%6.1f",
gyro.getXRotationRate().value, gyro.getYRotationRate().value,
gyro.getZRotationRate().value);
// telemetry.addData("ImuAngle", "Angle:x=%6.1f,y=%6.1f,z=%6.1f",
// getAngularOrientation(
// AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).firstAngle,
// getAngularOrientation(
// AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).secondAngle,
// getAngularOrientation(
// AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle);
// telemetry.addData("Accel", "x=%6.1f,y=%6.1f,z=%6.1f",
// imu.accel.getXAcceleration().value, imu.accel.getYAcceleration().value,
// imu.accel.getZAcceleration().value);
// telemetry.addData("Vel", "Vel: x=%6.1f,y=%6.1f,z=%6.1f",
// imu.accel.getXVelocity().value, imu.accel.getYVelocity().value,
// imu.accel.getZVelocity().value);
// telemetry.addData("Dist", "x=%6.1f,y=%6.1f,z=%6.1f",
// imu.accel.getXDistance().value, imu.accel.getYDistance().value,
// imu.accel.getZDistance().value);
//
// Test I2C Color Sensor.
//
telemetry.addData("ColorRGB", "Color=%x,rgb=%f/%f/%f",
colorSensor.getRawData(0, FtcColorSensor.DataType.COLOR_NUMBER).value.intValue(),
colorSensor.getRawData(0, FtcColorSensor.DataType.RED).value,
colorSensor.getRawData(0, FtcColorSensor.DataType.GREEN).value,
colorSensor.getRawData(0, FtcColorSensor.DataType.BLUE).value);
telemetry.addData("ColorHSV", "HSV=%f/%f/%f",
colorSensor.getRawData(0, FtcColorSensor.DataType.HUE).value,
colorSensor.getRawData(0, FtcColorSensor.DataType.SATURATION).value,
colorSensor.getRawData(0, FtcColorSensor.DataType.VALUE).value);
//
// Test DC motors.
//
double motorPower = gamepad.getRightStickY(true);
for (int i = 0; i < motors.length; i++)
{
motors[i].setPower(motorPower);
telemetry.addData("Motor" + i, "power=%.1f, enc=%d", motorPower, motors[i].getPosition());
}
//
// Test Servos.
//
double pos = dpadRightPressed ? 1.0 : 0.0;
for (FtcServo servo : servos)
{
servo.setPosition(pos);
}
telemetry.addData("Servo", "pos=%.0f", pos);
//
// Test Analog Inputs.
//
double data;
for (int i = 0; i < analogInputs.length; i++)
{
data = analogInputs[i].getData(0).value;
telemetry.addData("Analog" + i, "volt=%.2f", data);
}
//
// Test Digital Inputs.
//
boolean state;
for (int i = 0; i < digitalInputs.length; i++)
{
state = digitalInputs[i].isActive();
telemetry.addData("Digital" + i, "state=%s", state);
}
telemetry.update();
}
} //periodic
private void buttonEvent(TrcGameController gameCtrl, int button, boolean pressed)
{
switch(button)
{
case FtcGamepad.GAMEPAD_DPAD_RIGHT:
dpadRightPressed = pressed;
break;
}
} //buttonEvent
} //class FtcTestRevHub