Advertisement
Guest User

Robot X Localizer

a guest
Jan 17th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode.drive.localizer;
  2.  
  3. import android.support.annotation.NonNull;
  4.  
  5. import com.acmerobotics.dashboard.config.Config;
  6. import com.acmerobotics.roadrunner.geometry.Pose2d;
  7. import com.acmerobotics.roadrunner.localization.ThreeTrackingWheelLocalizer;
  8. import com.acmerobotics.roadrunner.localization.TwoTrackingWheelLocalizer;
  9. import com.qualcomm.hardware.bosch.BNO055IMU;
  10. import com.qualcomm.hardware.bosch.JustLoggingAccelerationIntegrator;
  11. import com.qualcomm.robotcore.hardware.DcMotor;
  12. import com.qualcomm.robotcore.hardware.HardwareMap;
  13.  
  14. import java.util.Arrays;
  15. import java.util.List;
  16.  
  17. /*
  18. * Sample tracking wheel localizer implementation assuming the standard configuration:
  19. *
  20. * /--------------\
  21. * | ____ |
  22. * | gyro |
  23. * | || |
  24. * | || |
  25. * | |
  26. * | |
  27. * \--------------/
  28. *
  29. * Note: this could be optimized significantly with REV bulk reads
  30. */
  31. @Config
  32. public class RAPSLocalizerTest extends TwoTrackingWheelLocalizer {
  33. public static double TICKS_PER_REV = 360;
  34. public static double WHEEL_RADIUS = 0.98425; // in
  35. public static double GEAR_RATIO = 0.2526475591; // output (wheel) speed / input (encoder) speed
  36.  
  37. public static double LATERAL_DISTANCE = 10; // in; distance between the left and right wheels
  38. public static double FORWARD_OFFSET = 4; // in; offset of the lateral wheel
  39.  
  40. private DcMotor xEncoder, yEncoder;
  41. private BNO055IMU imu;
  42.  
  43.  
  44. public RAPSLocalizerTest(HardwareMap hardwareMap) {
  45. super(Arrays.asList(
  46. new Pose2d(0, -LATERAL_DISTANCE / 2, 0), // right
  47. new Pose2d(FORWARD_OFFSET, 0, Math.toRadians(90)) // front
  48. ));
  49.  
  50. BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
  51. parameters.angleUnit = BNO055IMU.AngleUnit.RADIANS;
  52. parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
  53. parameters.calibrationDataFile = "BNO055IMUCalibration.json"; // see the calibration sample opmode
  54. parameters.loggingEnabled = true;
  55. parameters.loggingTag = "IMU";
  56. parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
  57.  
  58. // Retrieve and initialize the IMU. We expect the IMU to be attached to an I2C port
  59. // on a Core Device Interface Module, configured to be a sensor of type "AdaFruit IMU",
  60. // and named "imu".
  61. imu = hardwareMap.get(BNO055IMU.class, "gyroSensor");
  62. imu.initialize(parameters);
  63.  
  64. xEncoder = hardwareMap.dcMotor.get("liftMotor"); //xAxis
  65. yEncoder = hardwareMap.dcMotor.get("stoneArm"); //yAxis
  66. }
  67.  
  68. public static double encoderTicksToInches(int ticks) {
  69. return WHEEL_RADIUS * 2 * Math.PI * GEAR_RATIO * ticks / TICKS_PER_REV;
  70. }
  71.  
  72. @NonNull
  73. @Override
  74. public List<Double> getWheelPositions() {
  75. return Arrays.asList(
  76. encoderTicksToInches((int)-(xEncoder.getCurrentPosition() * 1.003656783206)),
  77. encoderTicksToInches(yEncoder.getCurrentPosition()));
  78. }
  79.  
  80. @Override
  81. public double getHeading() {
  82.  
  83. return imu.getAngularOrientation().firstAngle;
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement