YAMS
Yet Another Mechanism System
Loading...
Searching...
No Matches
yams::motorcontrollers::simulation::Sensor Class Reference

#include <Sensor.hpp>

Public Member Functions

 Sensor (std::string sensorName, std::vector< SensorData > fields)
 Sensor (const yams::mechanisms::config::SimSensorConfig &cfg)
SensorData & GetField (const std::string &name)
const SensorData & GetField (const std::string &name) const
double GetAsDouble (const std::string &name) const
int GetAsInt (const std::string &name) const
bool GetAsBoolean (const std::string &name) const
int64_t GetAsLong (const std::string &name) const
HAL_SimDeviceHandle GetDevice () const
void AddSimTrigger (const std::string &field, HAL_Value value, std::function< bool()> trigger)

Detailed Description

Simulated sensor backed by a HAL SimDevice.

All fields report the real supplier value on a physical robot. In simulation they surface in the Glass UI and can be overridden with trigger-based values for automated testing scenarios.

Basic usage (construct directly)

// Beam-break sensor: one boolean field backed by a real DIO channel.
wpi::hardware::DigitalInput beamBreak{0};
Sensor sensor{"BeamBreak", {
SensorData{"Triggered", [&beamBreak] { return !beamBreak.Get(); }, false},
}};
// Read the value anywhere in the robot loop:
bool triggered = sensor.GetAsBoolean("Triggered");
bool GetAsBoolean(const std::string &name) const

Builder usage (SimSensorConfig)

wpi::hardware::DigitalInput beamBreak{0};
wpi::hardware::Encoder encoder{1, 2};
SimSensorConfig cfg{"IntakeSensors"};
cfg.WithField("BeamBreak", [&beamBreak] { return !beamBreak.Get(); }, false)
.WithField("EncoderCount", [&encoder] { return encoder.Get(); }, 0)
// In simulation, assert the beam is broken from t=1s to t=3s:
.WithSimulatedValue("BeamBreak", 1_s, 3_s, true);
Sensor& sensor = cfg.GetSensor();
bool triggered = sensor.GetAsBoolean("BeamBreak");
int ticks = sensor.GetAsInt("EncoderCount");
Sensor(std::string sensorName, std::vector< SensorData > fields)

Trigger-based override

// Force the beam-break to appear triggered whenever the arm is retracted.
sensor.AddSimTrigger("BeamBreak",
[&arm] { return arm.GetAngle() < 10_deg; });
static HAL_Value Convert(double value)

Constructor & Destructor Documentation

◆ Sensor() [1/2]

yams::motorcontrollers::simulation::Sensor::Sensor ( std::string sensorName,
std::vector< SensorData > fields )

Construct a sensor from a name and an explicit list of fields.

Parameters
sensorNameName shown in the Glass simulation window.
fieldsFields to register with the SimDevice.

◆ Sensor() [2/2]

yams::motorcontrollers::simulation::Sensor::Sensor ( const yams::mechanisms::config::SimSensorConfig & cfg)
explicit

Construct from a SimSensorConfig builder.

Member Function Documentation

◆ AddSimTrigger()

void yams::motorcontrollers::simulation::Sensor::AddSimTrigger ( const std::string & field,
HAL_Value value,
std::function< bool()> trigger )

Set field to value whenever trigger returns true.

◆ GetAsBoolean()

bool yams::motorcontrollers::simulation::Sensor::GetAsBoolean ( const std::string & name) const

◆ GetAsDouble()

double yams::motorcontrollers::simulation::Sensor::GetAsDouble ( const std::string & name) const

◆ GetAsInt()

int yams::motorcontrollers::simulation::Sensor::GetAsInt ( const std::string & name) const

◆ GetAsLong()

int64_t yams::motorcontrollers::simulation::Sensor::GetAsLong ( const std::string & name) const

◆ GetDevice()

HAL_SimDeviceHandle yams::motorcontrollers::simulation::Sensor::GetDevice ( ) const

The underlying SimDevice handle (invalid on a real robot).

◆ GetField() [1/2]

SensorData & yams::motorcontrollers::simulation::Sensor::GetField ( const std::string & name)

Get the SensorData for name (throws if not found).

◆ GetField() [2/2]

const SensorData & yams::motorcontrollers::simulation::Sensor::GetField ( const std::string & name) const

The documentation for this class was generated from the following file:
  • /home/runner/work/YAMS/YAMS/yams/cpp/include/yams/motorcontrollers/simulation/Sensor.hpp