Navx MXP Gyro Simulation in Java

Trying to simulate a Navx gyro and am not having any luck even though the documentation here Java | navX-MXP

states that it is possible.

How do I create the simulation gyro in Java code?

From the documentation:

int dev = SimDeviceDataJNI.getSimDeviceHandle(“navX-Sensor[0]”);
SimDouble angle = new SimDouble(SimDeviceDataJNI.getSimValueHandle(dev, “Yaw”));
angle.set(5.0);

so your simulationPeriodic would look something like:

public void simulationPeriodic() {
  // Set the inputs to the system. Note that we need to convert
  // the [-1, 1] PWM signal to voltage by multiplying it by the
  // robot controller voltage.
  m_driveSim.setInputs(m_leftMotor.get() * RobotController.getInputVoltage(),
                       m_rightMotor.get() * RobotController.getInputVoltage());

  // Advance the model by 20 ms. Note that if you are running this
  // subsystem in a separate thread or have changed the nominal timestep
  // of TimedRobot, this value needs to match it.
  m_driveSim.update(0.02);

  // Update all of our sensors.
  m_leftEncoderSim.setDistance(m_driveSim.getLeftPositionMeters());
  m_leftEncoderSim.setRate(m_driveSim.getLeftVelocityMetersPerSecond());
  m_rightEncoderSim.setDistance(m_driveSim.getRightPositionMeters());
  m_rightEncoderSim.setRate(m_driveSim.getRightVelocityMetersPerSecond());
  int dev = SimDeviceDataJNI.getSimDeviceHandle(“navX-Sensor[0]”);
  SimDouble gyroSimAngle = new SimDouble(SimDeviceDataJNI.getSimValueHandle(dev, “Yaw”));
  gyroSimAngle.set(-m_driveSim.getHeading().getDegrees();
}

I think you could also have gyroSimAngle as a global variable in your drive class so you are not redeclaring it every time

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.