Change function of joysticks using modifier button?

Is it possible to write code to change the function of a joystick axis when the driver is holding down a modifier button? For example:

Normally (without holding down a modifier button) axis A is programmed to drive the robot forward (with a positive value) and backward (with a negative value). Axis B is programmed to turn right (positive) and left (negative). The left/right functionality is intuitive for kids when the robot is oriented away from the driver. When the robot turns around and needs to drive toward the driver, the left/right functionality becomes confusing for some kids…left becomes “right” and right becomes “left”. I’d like to give them an option to code a solution to make this driving scenario less confusing.

So, when the robot is oriented back toward the driver, the idea is that they could press and hold the “L down” button while they continue to use the left joystick to drive the robot…but while the “L down” button is being held down, the values of the B axis are reversed…so when the driver pushes the stick left, the robot turns left from the perspective of the driver and not the robot. But when the “L down” button is not being pressed and held, the B axis values are not reversed.

Kind of complicated to describe in words…hopefully the idea comes across clearly. :slight_smile:

Another simpler use-case might be:

Without holding a modifier button down, I limit the motor velocity to 50% of the value of the A axis, so the robot drives slowly.

But if I press and hold a modifier button, I do not limit the motor velocity, so the robot drives faster while the button is held.

Well, after a bunch of trial and error, I have mostly working code for my second (easier) scenario:

38 PM

One small problem is that it usually requires an actual change in the A axis value in order for the turbo to kick on/off (you can’t just have the stick pegged at 100% and then press/release the turbo button). It will actually work pegged at 100% once, but then won’t register the release unless you slightly move the stick.

Frustratingly, the exact logic is significant. I tried using an if else block instead of the while block, but that didn’t allow turbo engagement to work in either scenario without some slight stick movement. I also tried the while block, but with the logic reversed: while Turbo instead of while not Turbo but that allowed disengagement to work, but not engagement. I settled on this logic, which allows turbo engagement to work while the stick is pegged at 100%, but disengagement only works with some slight stick movement…which seems more natural.

No variable are really needed… If LUp is pressed, it will run full power, otherwise it will run it at half.

image

Thanks for the input @sankeydd!

I agree that you don’t really need the variable and the boolean, but the more advanced your program gets, the more useful that method becomes…especially if you are dealing with more complicated drivetrains, like holonomic x-drives, for instance.

About the control flow, though…I tried using an if...else block as well, but it suffers from the problem I described: it requires a change in the value of A in order to work. If you’re driving without the turbo button as fast as it will go (you’ve got the stick pegged all the way up, so the robot is driving forward at 50% speed), you want to be able to just push the turbo button and it starts driving faster. With the if...else block, that doesn’t work. You have to let off the stick for it to work, which is unintuitive for a kid used to how video games work. :wink: The while not true block gives me half of that functionality, but not all. (Works correctly when you press the turbo button, but not when you release the turbo button.)