Sensors
Wrap a beam break, limit switch or other sensor so your code can read it in simulation as well as on the robot.
YAMS simulates your mechanisms well, but most sensors have no simulation of their own: in simulation a beam break never breaks and a limit switch is never pressed. A YAMS Sensor fixes that.
What is a Sensor?
A Sensor wraps a real sensor on your robot, such as a beam break, a limit switch, a CANrange, a LaserCAN or a CanandColor. It has one or more named fields, and each field reads a value from the real sensor. In simulation you can also say what each field should read, and when: while the match time (the time left in the period, counting down) is in a window, or while a condition is true, such as the arm being at the angle where it breaks the beam.
Fields hold boolean, int, long or double values.

How do I create a Sensor?
Build it with a SensorConfig, which takes the sensor's name, then a field for each value it reports.
Reading through a Sensor is a little slower than reading the input directly. For a beam break or a limit switch that does not matter.
How do I add simulated values to a sensor?
You can add a simulated value to a Sensor you already have with addSimTrigger(field, value, condition). The value is a HALValue (org.wpilib.hardware.hal.HALValue), for example HALValue.makeBoolean(true).

What does this look like?
![The simulation GUI with the Other Devices panel open. Below the SPARK MAX entries is a Sensor[CoralDetectorBeamBreak] entry, circled in red, with its Beam field reading false in a drop-down that can be changed by hand.](https://yams.yassrobotics.com/~gitbook/image?url=https%3A%2F%2F1354553280-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FZM0CFmYiQzcrY4zDcTtZ%252Fuploads%252FxZnE2jTxxjCEg91Q7rhP%252Fimage.png%3Falt%3Dmedia%26token%3Dbc957101-6b79-4283-a53b-a4e1069017aa&width=768&dpr=3&quality=100&sign=3a7a49be2b81cda3eecdc39e11edcb06&sv=3)
The circled value turns true while the arm is within 2° of 40°. You can also change it by hand in the simulation GUI.
How do I use the value from a Sensor?
Read the field from the Sensor, not from the input it wraps:
getAsInt, getAsLong and getAsDouble read the other kinds of field.
How do I simulate the sensor value?
You can set a field by hand: get it with getField and call set. The new value stays until it is changed again, from the simulation GUI or with set.
How do I simulate a sensor the mechanism triggers?
Some sensors are triggered by the mechanism itself, like a limit switch at a hard stop, or a beam break the arm passes through. Simulate those from where the simulated mechanism really is, rather than from what its encoder says. The motor's sim supplier (SmartMotorController.getSimSupplier()) has the simulated mechanism's position. It is empty on a real robot, so the condition is false there.
For an elevator, convert the simulated position to a height with the motor controller's config: smcConfig.convertFromMechanism(sim.getMechanismPosition()).
A real limit switch is pressed by the mechanism, not by its encoder. If the encoder is off, say after a belt skips, a switch simulated from the encoder (arm.isNear(...)) is pressed in the wrong place. One simulated from the sim supplier is pressed at the hard stop, as the real one is.
A SystemCore Smart IO port reads true while a normally open switch, wired between the signal and power pins, is pressed. So the field is input::get, not !input.get().
Next
To reset the encoder each time a limit switch is pressed, see How do I home a mechanism with limit switches? YAMG generates the sensor and the homing code when you turn on Homing.
Last updated
Was this helpful?
