Class PretendDevices
java.lang.Object
com.crossbotics.ftchelper.testkit.PretendDevices
- All Implemented Interfaces:
Devices
A pretend robot configuration for JUnit tests on a laptop: give it to your
Robot instead of
the hardwareMap, and every motor, servo and switch it asks for is made on the spot. Your test then
moves the pretend encoders, presses the pretend switches and checks the powers and positions sent.
No robot, no Control Hub: a test runs in a second, after every change.
public class LiftTest {
@Test public void bothLiftMotorsGetTheSamePower() {
PretendDevices devices = new PretendDevices();
Robot robot = new Robot();
robot.setup(devices); // INIT, with pretend devices (Tunables.java as written)
robot.startForTests(); // START
robot.lift.goTo("high");
robot.all().update(); // one loop
assertTrue(devices.motor("lift_left").power > 0);
assertEquals(devices.motor("lift_left").power, devices.motor("lift_right").power, 1e-9);
}
}
Why in the library (not only in its own tests): so a team's tests in TeamCode/src/test can use it with no extra Gradle lines. It is a few small classes; nothing in it runs on the robot unless a test makes it. Docs: docs/modules/testkit.md, docs/tests/03-checks-before-practice-and-matches.md
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets a pretend analog input (an absolute encoder): volts now, out ofmaxVolts(3.3 on a REV hub).battery(double volts) The battery the hubs read, volts (default: none, NaN).doubleThe battery's voltage now (volts), from the hubs (Hw.batteryVolts(com.qualcomm.robotcore.hardware.HardwareMap)); NaN with no voltage sensor (tests).The pretend CR servo with this name (made now if needed).<T> TThe device with this name and type.booleanTrue if the configuration has a device with this name, of any type.The pretend motor with this name (made now if nothing asked for it yet).notConfigured(String... names) These names are NOT in the configuration: asking for one stops (like the real hardwareMap), and anoptional()part that uses one is off.The pretend goBILDA Pinpoint with this name (made now if needed).Presses (true) or lets go of (false) a pretend switch: a touch sensor, or a magnetic switch on a digital port (pressed = it reads LOW, like the REV one).The pretend servo with this name (made now if needed).<T> TLikeget(java.lang.Class<? extends T>, java.lang.String), but null for a name that is not configured, and for a kind of device the kit has no pretend one for (a camera, a color sensor): parts that can do without it (they warn at INIT) then go on as on a robot where it is unplugged.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.crossbotics.ftchelper.robot.Devices
batteryReader, hardwareMap
-
Constructor Details
-
PretendDevices
public PretendDevices()A fresh pretend configuration. It also forgets what the library keeps between programs in one robot app (tuned numbers in memory, a turret's last angle, a launcher's last trim, autonomous's alliance and pose), and points the settings file at an empty temporary file: so every test starts like a robot just turned on, whatever ran before it. And motors read their pretend encoder every time it is asked (on a robot a reading is reused for 3 ms), so a test can move it and update at once.
-
-
Method Details
-
motor
The pretend motor with this name (made now if nothing asked for it yet).- Parameters:
name- the configuration name, e.g. "lift_left"
-
servo
The pretend servo with this name (made now if needed). -
crServo
The pretend CR servo with this name (made now if needed). -
pinpoint
The pretend goBILDA Pinpoint with this name (made now if needed). -
press
Presses (true) or lets go of (false) a pretend switch: a touch sensor, or a magnetic switch on a digital port (pressed = it reads LOW, like the REV one).- Parameters:
name- the switch's configuration name, e.g. "lift_bottom"
-
analog
Sets a pretend analog input (an absolute encoder): volts now, out ofmaxVolts(3.3 on a REV hub). -
notConfigured
These names are NOT in the configuration: asking for one stops (like the real hardwareMap), and anoptional()part that uses one is off. Use it to test a part not on the robot yet. -
battery
The battery the hubs read, volts (default: none, NaN). -
batteryVolts
public double batteryVolts()Description copied from interface:DevicesThe battery's voltage now (volts), from the hubs (Hw.batteryVolts(com.qualcomm.robotcore.hardware.HardwareMap)); NaN with no voltage sensor (tests). Each call asks the hubs and waits (a few milliseconds): not for the loop. The launcher usesDevices.batteryReader()instead.- Specified by:
batteryVoltsin interfaceDevices
-
has
Description copied from interface:DevicesTrue if the configuration has a device with this name, of any type. Used for parts markedModule.optional(): one whose device is not there is left off instead of stopping INIT. Default: asks the hardwareMap; with no hardwareMap (pretend devices in tests) every name is there. -
tryGet
Likeget(java.lang.Class<? extends T>, java.lang.String), but null for a name that is not configured, and for a kind of device the kit has no pretend one for (a camera, a color sensor): parts that can do without it (they warn at INIT) then go on as on a robot where it is unplugged. -
get
Description copied from interface:DevicesThe device with this name and type. Stops with a clear message (the names that DO exist) if it is not in the configuration.
-