步骤1:创建硬件的模拟实例

WPILib仿真框架包含几个“ XXXSim”类,其中“ XXX”代表物理硬件,例如编码器或陀螺仪。这些模拟类可用于根据传动系统模型设置位置和速度(对于编码器)和角度(对于陀螺仪)。

模拟编码器

“EncoderSim”类允许用户在给定的编码器对象上设置编码器位置和速度。在实际硬件上运行时,``编码器’’类与实际传感器进行交互以计算转数(如果配置为自动将其转换为距离单位);但是,在模拟中,无需进行此类测量。 EncoderSim类可以接受来自传动系统模型的这些模拟读数。

注解

无法使用WPILib类模拟直接连接到CAN电机控制器的编码器。有关特定电机控制器的更多信息,请阅读供应商的文档。

// 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);

模拟陀螺仪

与``EncoderSim’’类相似,也为常用的WPILib陀螺仪提供了模拟陀螺仪类-``AnalogGyroSim’’和``ADXRS450_GyroSim’’。它们的构造方式也是相同的。

注解

使用WPILib类无法模拟某些供应商陀螺仪(即Pigeon IMU和NavX)。请阅读各自供应商的文档,以获取有关其支持模拟的信息。

// 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);