Help with my code

Robot.java
OI.java

So I made this piston code in my teleopPeriodic in Robot.java. However, since I’m writing in Command, OI would be useless so here’s two things I need help.

  1. How do I connect my pistonFront and pistonRear in my OI to my piston code in Robot.java?
  2. How do I make my code work when I press the button once, the piston comes out and when pressed again, it retracts instead of holding the button?

Hi there, we have this kind of toggle functionality working on our robot this year. Take a look at the repo: https://github.com/team6637/Robot2019/tree/master/src/main/java/frc/robot

In the oi, we created a button
Button frontJack = new JoystickButton(controlPanel, 11);

then in the constructor we had it call a command:
frontJack.whenPressed(new JackFront());

In the subsystem, we setup the methods to handle the solenoid:

Finally, we wrote the command that uses a static boolean to keep track of the state of the cylinder:

Hope this helps!

Ah okay, thank you. I did separate subsystems and commands on both rear and front pistons.

Separate subsystems would be just fine.

Ah okay, let me see if I can put it in one command.

https://hastebin.com/xorihohepi.cpp (OI.java)
https://hastebin.com/zuwonutefe.cpp (Subsystem for rear pistons) https://hastebin.com/qikufucata.cpp (Subsystem for front pistons) https://hastebin.com/eqokixuyod.java (Command for all the pistons)

Would this work in theory if I pressed the flight stick’s trigger once and again?

frontPiston = !frontPiston

and

rearPiston = !rearPiston

is necessary at the end of the if-else statement…

So as it stands, pushing either button will fire the command.

Both frontPiston and rearPiston booleans are false.

Both if statements will run reading that the vars are false and will call the frontRaise() and rearRaise() methods.

The booleans aren’t designated as true ever, so if you press another button, the command will continue to fire frontRaise() and rearRaise().

This isn’t what you want.

First of all, you have to swap the boolean after the if statement with the frontPiston = !frontPiston, so it becomes the opposite of what it was. This will give you the toggle state you are looking for.

To continue with one command you could pass an argument into the command telling it which if statement you want to fire, then check for that value in the if statement, but that would be more complex.

Personally, I would make two commands, one for each button that raises the front or back separately.

One more thing. When you go to test this for the first time, be careful. If the solenoid air flow is backwards to what you expect, the cylinders may raise the front/back when the code fires up.

You are putting rearLower() and frontLower() in the constructor function of your subsystem. This is correct. But if you have the air flow backwards, rearLower() would actually raise the rear.

You can fix this by swapping the solenoid ports, changing kForward to kReverse or physically swapping which air line goes to which port on the cylinders.

Just be aware this might happen. We put our robot on a couple crates because we didn’t have a chance to test the final solution before the cylinders were installed on the robot.

I don’t know why but this static toggle wasn’t working for us today. I didn’t have time to mess with it, so we came up with a different solution. Instead of firing the command on .whenPressed(), we are going to use .whileHeld(). In the command initialize() method we will extend the cylinder. In the end() method, we will retract the cylinder. This way, the driver has to hold the button for it to stay extended. I think this will make it easier on the driver as well.

Ah okay, I’ll see about this cause we can’t play with the code until tomorrow.

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