I am trying to get some help for my programmer. He is having issues making our robot (Mk4i, Neo, Navx) move during autonomous. We can move our elevator/which and trigger our pneumatics for a high cube every time, but we can’t get the robot to move after. He is using a time based because that is what he knows and is comfortable with. Here is our code and here is a video of what we are getting now. His last attempt is disabling the robot when it gets to the moving part.
Any help is greatly appreciated!
Thanks!
The video shows that you are losing robot code. This is usually due to an exception being thrown due to a programming error. The exception will be in the log. I think I saw it go by in the video but it is too blurry to be sure. It will point you to the line of code that is failing. Also, I do not think the auto in your github is the one being shown in the video as the one in github appears to start with driving and not scoring.
The code is trying to use a mix of command-based and timed. This is challenging to pull off. The most immediate problem I see is that it’s trying to create two instances of CANSparkMax to control the same motors, IDs 13 and 14. You cannot do this, it will throw an error with message A CANSparkMax instance has already been created with this device ID: {id}. Another issue I see is that m_Swerve in Robot is always null, so it’ll throw a NullPointerException when it’s called.
Since the rest of the code is command-based, the autonomous should also be command based. At the very least, the autonomous code must use the subsystems, not attempt to drive the motors directly. But, if you attempt this, watch out because TeleopSwerve is the default command, and it is also controlling the drivetrain.
Scanning through the code I see something else. I think the code is attempting to schedule a command. It creates a new RunCommand but never schedules it.
if (time - startTime > 6.75) {
new RunCommand(() -> m_Swerve.drive(0.1, 0, 0, true, true), m_Swerve);}
Although at this point it throws an exception because m_Swerve is null.
Error at java.base/java.util.Objects.requireNonNull(Objects.java:233): Unhandled exception: java.lang.NullPointerException: Parameter requirement in method addRequirements was null when it should not have been! Check the stacktrace to find the responsible line of code - usually, it is the first line of user-written code indicated in the stacktrace. Make sure all objects passed to the method in question were properly initialized - note that this may not be obvious if it is being called under dynamically-changing conditions! Please do not seek additional technical assistance without doing this first!
at java.base/java.util.Objects.requireNonNull(Objects.java:233)
at edu.wpi.first.util.ErrorMessages.requireNonNullParam(ErrorMessages.java:27)
at edu.wpi.first.wpilibj2.command.CommandBase.addRequirements(CommandBase.java:39)
at edu.wpi.first.wpilibj2.command.FunctionalCommand.<init>(FunctionalCommand.java:46)
at edu.wpi.first.wpilibj2.command.RunCommand.<init>(RunCommand.java:25)
at frc.robot.Robot.autonomousPeriodic(Robot.java:103)
at edu.wpi.first.wpilibj.IterativeRobotBase.loopFunc(IterativeRobotBase.java:344)
at edu.wpi.first.wpilibj.TimedRobot.startCompetition(TimedRobot.java:130)
at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:349)
at edu.wpi.first.wpilibj.RobotBase.lambda$startRobot$0(RobotBase.java:422)
at java.base/java.lang.Thread.run(Thread.java:833)
Warning at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:364): The robot program quit unexpectedly. This is usually due to a code error.
The above stacktrace can help determine where the error occurred.
See https://wpilib.org/stacktrace for more information.
Error at edu.wpi.first.wpilibj.RobotBase.runRobot(RobotBase.java:371): The startCompetition() method (or methods called by it) should have handled the exception above.
I tried to run the RunCommand with .withTimeout but got the same result. I also tired a Pathplanner generated path, when that is in it doesn’t disable robot code but it doesn’t move either.
.withTimeout does not schedule a command either, it decorates the command and returns a new command. You would schedule a command with .schedule().
BUT, because you’re trying to do your whole autonomous in a single robot iteration, the command won’t actually run. The autonomousPeriodic method should be allowed to run once every 20ms, you can’t put your whole autonomous routine in there and expect commands to work.
Also, you are not even getting far enough to attempt to schedule that command. I see two causes of errors before that:
You’re creating two instance of CANSparkMax for the same ID.
m_Swerve is null.
I highly advise you reconsider making your autonomous command-based. It will be a struggle to get it to run this way.