Code to turn Robot | Mecanum Drive

Hello,

I was wondering if anyone had any resources on the best way to make a mecanum drive robot turn left and right. This is currently the best I came up with, but this is based on Arcade Drive turn logic. I was wondering if this code would function / any ways that were more effective.

Please provide any feedback you can,

Thanks

(Inside drive train subsystem (frontLeft etc represent corresponding motors))

public void setMotorSpeed(double xAxis, double yAxis, double zAxis){
    Robot.m_robotDrive.driveCartesian(xAxis, yAxis, zAxis, 0.0);
  }

  public void leftTurn(double xAxis){
    frontLeft.set(xAxis);
    reafLeft.set(xAxis);
    frontRight.set(-xAxis);
    rearRight.set(-xAxis);
  }
  public void rightTurn(double xAxis){
    frontLeft.set(-xAxis);
    reafLeft.set(-xAxis);
    frontRight.set(xAxis);
    rearRight.set(xAxis);
  }

(Inside MecanumDriver Execute Command)

if (rightStickX > 0) {
      Robot.m_driveTrain.rightTurn(rightStickX);
    }
    if (rightStickX < 0){
      Robot.m_driveTrain.leftTurn(rightStickX);
    }

    Robot.m_driveTrain.setMotorSpeed(leftStickX, leftStickY, leftStickZ);
 

Devil’s advocate: Why do you currently believe it would or wouldn’t function?

As far as analysis goes…

Most robot code should follow the general pattern of:

  1. Read inputs once
  2. Do math and processing to calculate what your outputs should be
  3. Assign outputs once

Can you break down what you’ve written into those three steps?

1 Like

The leftTurn and rightTurn methods are unnecessary. In the second snippet you posted replace leftStickZ (an axis that doesn’t exist) with rightStickX and your robot should rotate in place the way you want.

Sorry I shouldn’t have left that snip in.

That’s actually the method in charge of of using the left stick to drive the robot. The reason that I’m using the right stick to turn the robot is because the X axis on the left stick determines how much the robot strafes by.

I can provide a larger snippet of code if that helps. Or if I misunderstood what you were saying please correct me.

Interesting,

I technically don’t know it won’t work, I’m just inexperienced so I was looking for any oversights I might’ve missed.

as for analysis

  1. The code reads the x inputs of the right stick
  2. It sets the motors to this value based on whether the value is greater or not than 0.
  3. It runs this input through the if statement determining the value each motor needs to be set to.

So I can feel for ya - I’ve been in this position before, many times. The critical thing I’ve learned from those positions - Getting a yes/no answer on whether a given piece of code works will not actually be useful.

To be able to be flexible and grow your codebase as needed, you need to be able to generate the answer yourself, and more importantly know why the answer is yes or no.

I think your analysis is a good start, but I have some feedback:

1 seems reasonable
2 is already talking about “setting motors” - this sounds like writing to an output to me

Additionally, you’ve not addressed the logic inside driveCartesian cartisian. Do you know which of the three steps it takes care of? Or if it takes care of more than one? In particular, look into its source code if you like - this may be helpful.

The above questions look into more “depth” of your particular chosen implementation. I do have another, more “high level” question about your choice in code though:

WPILib provides an example for a mechanum drivetrain. Your code looks like you have chosen to add more functionality. Can you describe if you intended to diverge from their example? And, if the divergence was expected, can you describe why you chose to go this route?

Knowing what you’re attempting to accomplish above and beyond the example could be helpful in advising on the best solution.

1 Like

Update

I realized that I had been misunderstanding the way the .driveCartasian worked. When I read the variables it requires it listed an Z axis, which I assumed meant a third z axis on the joystick.

But after thinking about it properly (and some research) I found that the Z axis was meant to represent the rotation you wanted the robot to make meaning this value can be set to the right stick x axis.

The .driveCartasian function will run all the background math equations to make the mecanum drive move 360 and rotate, you just need to provide the proper axis from your joy stick.

Thanks to everyone who helped

3 Likes

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