Giter VIP home page Giter VIP logo

Comments (2)

abidingabi avatar abidingabi commented on August 13, 2024 2

Make it a subsection of the mecanum drive page, talk about how to use the imu to get heading.

from gm0.

NoahBres avatar NoahBres commented on August 13, 2024
@TeleOp
public class FieldCentric extends LinearOpMode {
  @Override public void runOpMode() {
    // Declare our motors
    // Make sure your ID's match your configuration
    DcMotor motorFrontLeft = hardwareMap.dcMotor.get("motorFrontLeft");
    DcMotor motorBackLeft = hardwareMap.dcMotor.get("motorBackLeft");
    DcMotor motorFrontRight = hardwareMap.dcMotor.get("motorFrontRight");
    DcMotor motorBackRight = hardwareMap.dcMotor.get("motorBackRight");

    // Reverse the right side motors
    // Reverse left motors if you are using NeveRests
    motorFrontRight.setDirection(DcMotorSimple.Direction.REVERSE);
    motorBackRight.setDirection(DcMotorSimple.Direction.REVERSE);

    // Initialize imu
    BNO055IMU imu = hardwareMap.get(BNO055IMU.class, "imu");
    BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
    parameters.angleUnit = BNO055IMU.AngleUnit.RADIANS;
    imu.initialize(parameters);

    waitForStart();

    while(opModeIsActive()) {
      double x = gamepad1.left_stick_x;
      double y = -gamepad1.left_stick_y;
      double turn = gamepad1.right_stick_x;
      
      // Read inverse imu heading
      double botHeading = -imu.getAngularOrientation().firstAngle;

      // Rotate gamepad input x/y by hand
      // See: https://matthew-brett.github.io/teaching/rotation_2d.html
      double rotX = x * Math.cos(botHeading) - y * Math.sin(botHeading);
      double rotY = x * Math.sin(botHeading) + y * Math.cos(botHeading);

      double frontLeftPower = rotY + rotX + turn;
      double backLeftPower = rotY - rotX + turn;
      double frontRightPower = rotY - rotX - turn;
      double backRightPower = rotY + rotX - turn;
    
      // Put powers in the range of -1 to 1 only if they aren't already
      // Not checking would cause us to always drive at full speed
      if (Math.abs(frontLeftPower) > 1 || Math.abs(backLeftPower) > 1 ||
          Math.abs(frontRightPower) > 1 || Math.abs(backRightPower) > 1) {
          // Find the largest power
          double max = 0;
          max = Math.max(Math.abs(frontLeftPower), Math.abs(backLeftPower));
          max = Math.max(Math.abs(frontRightPower), max);
          max = Math.max(Math.abs(backRightPower), max);

          // Divide everything by max (it's positive so we don't need to worry
          // about signs)
          frontLeftPower /= max;
          backLeftPower /= max;
          frontRightPower /= max;
          backRightPower /= max;
       }

       motorFrontLeft.setPower(frontLeftPower);
       motorBackLeft.setPower(backLeftPower);
       motorFrontRight.setPower(frontRightPower);
       motorBackRight.setPower(backRightPower);
    }
  }
}

from gm0.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.