Hi! I’ve been designated team coder and, knowing next to nothing about coding, am feeling very out of my depth. I was wondering if anyone could help me figure out a solution to our problem.
So we’ve decided to add a “fine control mode” to our robot’s drive base where all the robots movements are slowed to a quarter. The way we’ve done this is created a variable that we multiply the joystick inputs by which switches between 1 and 0.25. We wanted to be able to press a button on the Xbox controller and switch and then be able to press it again and switch back and so on.
If anyone is able to explain, step by step, how to do this, I’d be very grateful (I’ve never really done any coding before). Thanks in advance!
PS- we are using a command based coding system if that helps
While I’m not experienced with the programming side of things, I would refrain from making that functionality based on a toggleable button, unless there is very obvious feedback (rumble motor, lights, etc) of which mode the robot is in. A button with a single known function is usually much less confusing than a multi-function button for drivers.
I agree with troy_dietz in that it is better to use multiple buttons to control such an operation if you do not have any visual feedback. That being said, you could still this proceed using the getPressed() method of the joystick which will return true once for every time the joystick button goes from unpressed to pressed. In addition, you could use an indicator on the smart dashboard / shuffleboard to tell your drivers what mode they’re in.
Another thing you may want to consider (if you’re not doing this already) is the squareInputs idea where you square the inputs from the joystick so that they’re more sensitive at lower values (0.2^2 = 0.04, while 1^2 still = 1).
Our first robot was very top heavy and our drivers like to use Xbox controllers. When someone who is not a typical driver tried to drive the robot (for demos and such), they could not control it.
So, we put a limit in (multiplied the drive output) and died this to a slider on the dashboard.
Then, the drive team loved it. So, we kept it.
We also have a go fast button that enables a trigger to act as an overdrive throttle. Here is a link to our snippet that does this.
The drivers liked this so much, they asked a student to add it to the output direction of the intake me Jainism to make shooting cubes easier when they wanted it, and more difficult to do accidentally.
If it were me, I’d have this mode on only when one of the triggers was pulled. Otherwise, it would be too easy to accidentally turn it on.
You also have to think about what happens when the robot is going down the field full speed and the button is pressed – if you instantly cut the motors by 75%, you’re going to be fighting an awful lot of momentum. That’s more of a general issue (you also don’t want the robot going from 0 to full-throttle instantly when somebody pushes the joysticks forward) to be aware of.
How we implement this feature using the command based framework is by using the whileHeld method to set a variable to the desired speed percentile, and then when the command has finished running setting that variable back to the default value.
We do this all the time, and we call it ‘snail mode’. We multiply by 0.4 rather than clamp to 0.4. This allows the drivers to have a wider range of low-speed maneuvers. We also hold the button down so the driver knows snail mode is activated.
// First do the math to figure out the left and right outputs
// This is psuedo-code...
final double snailModePower = 0.4;
leftMotor *= snailModePower ;
rightMotor *= snailModePower ;
As for toggles, here is the code I whipped out for a practice frame. To correctly toggle, you’ll need to keep track of button presses separately from the desired output. That way the desired output is always set in each robot cycle, but only changes when the button state changes from un-pressed to pressed.