Can you run commands in Test methods of Robot.java

I’m simply trying to run the command scheduler in testperiodic, but when I do, no commands are being scheduled or run. The robot does not move any mechanicsm, but all networktable values update accordingly. All commands run perfectly fine in teleoperiodic, so I know they work. Does anyone know how to run a command in testperiodic? Any help is much appreciated (: !

I don’t think that test allows you to schedule things in this way, if there is a way I’d love to know. I spent a lot of time last year trying to hack a solution together for this exact thought. Test commands all get run in test mode, then when they are all configured they grow up to be full commands that are then moved from the test harness into the source code normally.

Never got it working, stopped spending time on it because doing the same thing in Teleop works just fine.

That’s what I figured. I didn’t want to clutter teleop and teleop with a bunch of test commands, but I guess I’ll have to. Thanks and if anyone else figured something out let me know

I believe I figured out how to do this. It doesn’t really make sense to me why I had to do what I did to get it working, but it does work. Here was my process:

I knew that the CommandScheduler was being run, because robotPeriodic() runs the scheduler (which is the default when you make a new project) and robotPeriodic() is called no matter what mode you’re in. I first assumed that the CommandScheduler must be considering Test mode to be “Disabled”, and I remembered reading about overriding runsWhenDisabled() in commands from this year’s “What Changed” docs. I tried overriding it to return true on the test command we created. No change – the command still didn’t run.

I dug deeper and found that CommandScheduler.run() doesn’t actually check runsWhenDisabled() when its private m_disabled variable is false. The first thing the run() method does is check m_disabled, and if it’s false, it just returns. I set a breakpoint and found that in Disabled mode, m_disabled is false. But in Test mode, m_disabled is true. This seems backwards.

But as it turns out, there’s a way to explicitly enable the CommandScheduler, and doing so makes it possible to schedule commands in Test mode that actually get run. So now in our testInit() method we have this:

    // Cancels all running commands at the start of test mode.
    CommandScheduler.getInstance().cancelAll();
    // Re-enables the scheduler.
    CommandScheduler.getInstance().enable();

Hope this helps.

When Test mode is entered, LiveWindow is enabled and commands are canceled. I think only the enable is necessary. https://github.com/wpilibsuite/allwpilib/blob/master/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/CommandScheduler.java#L90

Good to know, thanks!

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