Paso 1: creación de instancias simuladas de hardware

The WPILib simulation framework contains several XXXSim classes, where XXX represents physical hardware such as encoders or gyroscopes. These simulation classes can be used to set positions and velocities (for encoders) and angles (for gyroscopes) from a model of your drivetrain. See the Device Simulation article for more info about these simulation hardware classes and simulation of vendor devices.

Nota

Simulation objects associated with a particular subsystem should live in that subsystem. An example of this is in the StateSpaceDriveSimulation (Java, C++) example.

Simulación de codificadores

La clase EncoderSim permite a los usuarios establecer las posiciones y velocidades del codificador en un objeto Codificador dado. Cuando se ejecuta en hardware real, la clase Encoder interactúa con sensores reales para contar las revoluciones (y convertirlas en unidades de distancia automáticamente si está configurado para hacerlo); sin embargo, en la simulación no existen tales medidas para realizar. La clase EncoderSim puede aceptar estas lecturas simuladas de un modelo de su transmisión.

Nota

No es posible simular codificadores conectados directamente a controladores de motor CAN utilizando clases WPILib. Para obtener más información sobre su controlador de motor específico, lea la documentación de su proveedor.

// These represent our regular encoder objects, which we would
// create to use on a real robot.
private Encoder m_leftEncoder = new Encoder(0, 1);
private Encoder m_rightEncoder = new Encoder(2, 3);
// These are our EncoderSim objects, which we will only use in
// simulation. However, you do not need to comment out these
// declarations when you are deploying code to the roboRIO.
private EncoderSim m_leftEncoderSim = new EncoderSim(m_leftEncoder);
private EncoderSim m_rightEncoderSim = new EncoderSim(m_rightEncoder);
#include <frc/Encoder.h>
#include <frc/simulation/EncoderSim.h>
...
// These represent our regular encoder objects, which we would
// create to use on a real robot.
frc::Encoder m_leftEncoder{0, 1};
frc::Encoder m_rightEncoder{2, 3};
// These are our EncoderSim objects, which we will only use in
// simulation. However, you do not need to comment out these
// declarations when you are deploying code to the roboRIO.
frc::sim::EncoderSim m_leftEncoderSim{m_leftEncoder};
frc::sim::EncoderSim m_rightEncoderSim{m_rightEncoder};

Simulación de giroscopios

Similar a la clase EncoderSim, también existen clases de giroscopio simulado para giroscopios WPILib de uso común: AnalogGyroSim y ADXRS450_GyroSim. Estos también se construyen de la misma manera.

Nota

It is not possible to simulate certain vendor gyros (i.e. Pigeon IMU and NavX) using WPILib classes. Please read the respective vendors” documentation for information on their simulation support.

// Create our gyro object like we would on a real robot.
private AnalogGyro m_gyro = new AnalogGyro(1);
// Create the simulated gyro object, used for setting the gyro
// angle. Like EncoderSim, this does not need to be commented out
// when deploying code to the roboRIO.
private AnalogGyroSim m_gyroSim = new AnalogGyroSim(m_gyro);
#include <frc/AnalogGyro.h>
#include <frc/simulation/AnalogGyroSim.h>
...
// Create our gyro objectl ike we would on a real robot.
frc::AnalogGyro m_gyro{1};
// Create the simulated gyro object, used for setting the gyro
// angle. Like EncoderSim, this does not need to be commented out
// when deploying code to the roboRIO.
frc::sim::AnalogGyroSim m_gyroSim{m_gyro};