Limelight Autonomous Targeting with Mecanum (Cartesian) Drive

Hi there!

Using the very handy Limelight documentation, I was able to program our tank drive robot to move into position using a Limelight with reflective tape. The code I used is below (I converted it to Java)
https://docs.limelightvision.io/en/latest/cs_aiming.html

However, our robot this year has a Mecanum wheel drivetrain. I’ve attempted implementing a system similar to the one in the tank drive example, and using the driveCartesian() method to implement it, but I’ve been stuck for a while. Can anyone give me a push in the right direction of how one might go about implementing this? I’ve looked around lots of forum posts, and it doesn’t seem like anyone else has this issue. My idea for how this might work is below.

  xHeading_error = -x;
  yHeading_error = -y;
  x_adjust = 0.0f;
  y_adjust = 0.0f;
  if (x > 1.0)
  {
          x_adjust = Kp*xHeading_error - min_command;
  }
  else if (x < 1.0)
  {
          x_adjust = Kp*xHeading_error + min_command;
  }
  left_command += x_adjust;
  right_command -= x_adjust;

  if (y > 1.0)
  {
    y_adjust = Kp*yHeading_error - min_command;
  }
  else if (y < 1.0)
  {
    y_adjust = Kp*yHeading_error + min_command;
  }
  forward_command += y_adjust;
  backward_command -= y_adjust;
  SmartDashboard.putNumber("Forward Command", forward_command);
  SmartDashboard.putNumber("Backward Command", backward_command);
  SmartDashboard.putNumber("Left Command", left_command);
  SmartDashboard.putNumber("Right Command", right_command);

  m_robotDrive.driveCartesian(backward_command+forward_command, 
  left_command+right_command, 0.0);

The x and y values are retrieved from the limelight.

Any help would be appreciated! Let me know if you need more information, and I’d be happy to provide

Could you tell us a little bit more about what happens when you run this code?

1 Like

When ran, the code does not appear to target the goal properly. It would move seemingly randomly, yet consistently in one direction when I began running the targeting code. Once I calibrated it, it stopped moving at all, even when it wasn’t aligned with the reflective tape. Could it be that my Limelight x & y variables aren’t being updated enough?

UPDATE: I had temporarily commented out the portion of code that updates the values. I will test this to see if anything changes as soon as I can, but in the meantime, does the formatting look correct here?

There’s a lot of language in your code that I don’t necessarily understand It would be helpful if we could just look at GitHub or something in order to see what min command means and how you’re getting your limelight values

Also what’s your KP?

Sorry, Had to figure out how to use GitHub. I’m a newer coder, so it took a second.

Here’s my entire Robot.java code. I copied the KP from the limelight code used for tank drive targeting. I’m not entirely sure what it does.

Ok I think I see what you did.

The example uses differential drive which commands the left side and right side of the drive train.

With Cartesian drive, you really need x drive and y drive, not forward backwards and left right.

That’s really what I’m trying to do here. The program calculates the distance it needs to move either forward or backward, left or right, and adds them together to get a net movement. Do you know if there is a better way of implementing this?

Cartesian drive takes 3 parameters speedX speedY, and speedrotation.

Your limelight gives you a value multiplied by a kP. You have lots of unneeded lines of code.

If absolutevalue(limelightxvalue) > minDegrees {xadjust = kP*limelightxvalue} else xadjust = 0

//Do the same for your y value

CartesianDrive(xadjust + xjoystickorauto, yadjust + yjoystickorauto, joystickorautorotation )

Couple things here…
Robot code thinks x is forward backward convention, so make sure your putting your joystick values which are opposite convention in the right place.

Instead of having your robot translate left and right, consider having your robot ROTATE left or right… Especially with a round goal, your robot may unintentionally circle the hub as a result.

2 Likes

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