Class PretendDevices

java.lang.Object
com.crossbotics.ftchelper.testkit.PretendDevices
All Implemented Interfaces:
Devices

public class PretendDevices extends Object implements 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 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

      public PretendMotor motor(String name)
      The pretend motor with this name (made now if nothing asked for it yet).
      Parameters:
      name - the configuration name, e.g. "lift_left"
    • servo

      public PretendServo servo(String name)
      The pretend servo with this name (made now if needed).
    • crServo

      public PretendCRServo crServo(String name)
      The pretend CR servo with this name (made now if needed).
    • pinpoint

      public PretendPinpoint pinpoint(String name)
      The pretend goBILDA Pinpoint with this name (made now if needed).
    • press

      public PretendDevices press(String name, boolean pressed)
      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

      public PretendDevices analog(String name, double volts, double maxVolts)
      Sets a pretend analog input (an absolute encoder): volts now, out of maxVolts (3.3 on a REV hub).
    • notConfigured

      public PretendDevices notConfigured(String... names)
      These names are NOT in the configuration: asking for one stops (like the real hardwareMap), and an optional() part that uses one is off. Use it to test a part not on the robot yet.
    • battery

      public PretendDevices battery(double volts)
      The battery the hubs read, volts (default: none, NaN).
    • batteryVolts

      public double batteryVolts()
      Description copied from interface: Devices
      The 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 uses Devices.batteryReader() instead.
      Specified by:
      batteryVolts in interface Devices
    • has

      public boolean has(String name)
      Description copied from interface: Devices
      True if the configuration has a device with this name, of any type. Used for parts marked Module.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.
      Specified by:
      has in interface Devices
      Parameters:
      name - the configuration name (exact, case matters)
    • tryGet

      public <T> T tryGet(Class<? extends T> type, String name)
      Like get(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.
      Specified by:
      tryGet in interface Devices
    • get

      public <T> T get(Class<? extends T> type, String name)
      Description copied from interface: Devices
      The device with this name and type. Stops with a clear message (the names that DO exist) if it is not in the configuration.
      Specified by:
      get in interface Devices
      Parameters:
      type - the device type, e.g. DcMotorEx.class
      name - the configuration name (exact, case matters)