Turret flipping logic

We are trying to program a turret as a rookie team, and are having problems trying to determine the logic for when to flip the turret around. We currently have it tracking the hub while driving using odometry, and we use the limelight to align more accurately when we are planing to shoot. However, we don’t know what the logic should be when the turret reaches it’s limit when it is driving around. The current code says to set the turret to point at the hub, but if it is limited by the cord, flip 180 degrees to the other side. However, as soon as the flip is complete, it immediately goes back to where it is set to by the odometry controller. Is there a way to prevent it from going back to the original position?

2 Likes

On second thought, this should work if you set the turret to just go the opposite direction when a certain threshold is reached. i.e., when the turret goes over 180 degrees, invert the number to -180 and the turret should flip the other direction without pulling the wires out.

if (x > 180)
    x = -x;
rotateDegrees(x);

I think we would have to see your source code to be truly helpful here. How is your turret limited? Does it have a range of less than 360º, or are you trying to avoid over-twisting a wiring bundle? If the latter, I think you’ll want to have some overlap region whereby it doesn’t flip until it reaches the far edge.

4 Likes

You’ll want to actually subtract 360, not negate the value. Try some numbers past 180 to see why.

1 Like

Our turret only has 360 degrees of rotation, so I think it should work. Subtracting is definitely a cleaner solution though

What happens if you command 270 degrees?

Wpilib had some useful math utils for this:

https://team2168.org/javadoc/wpilib/edu/wpi/first/math/MathUtil.html

A combination of the input modulus and a clamp should get you there.

Modulus to your center of travel +/-180, and clamp to your hard limits, ex:

A 45 → -180 turret would modulus to 112.5 and -247.5 and clamp to 45 and -180.

This would cause your turret to only flip once the other hard limit is closer to the current command.

1 Like

Yeah, it is limited by a cable. That is a good idea not to flip it all the way to the other limit

Oh, thanks, I will check it out :slight_smile:

This worked perfectly! Thanks!

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