I’ve attached a new joystick class derived from the standard WPILib Joystick class with additional functionality.
For omni-directional control systems (such as mechanum, etc.), it may be beneficial to receive your input from a joystick as a polar coordinate (radius and angle/direction) as opposed to the standard cartesian coordinate (x and y values). Doing so presents a problem, however, as due to special right triangles, pushing the joystick into any of the corners generates a farther distance from the center than pushing the joystick to any of the sides (since a right triangle with two legs 1 unit long generates a hypotenuse of the square root of 2 units long).
I’ve created a fix for this by writing an algorithm that “normalizes” the distance from the center. The algorithm takes the slope of the line that intersects the origin (the center point of the joystick) and the actual x-y coordinate of the joystick and uses this slope to project the line all the way to the edge of the joystick bounds. The length of this line is divided by the length of the line from the origin to the joystick coordinate.
The end result of all this is that the polar coordinate generated remains at the maximum value while moving the joystick all the way around its bounds.
Also included is a simpler function that gives the angle of the joystick (in degrees) from the center point.
These two functions are used to derive the OmniJoystick class from the standard Joystick class. The OmniJoystick class has the functions GetAngle() and GetR() along with the standard GetX() and GetY() functions.
OmniJoystick.zip (2.11 KB)
OmniJoystick.zip (2.11 KB)