I know this sounds silly, but how do you do a wait command properly?
You use the WaitCommand class, and you can create an instance of this through:
new WaitCommand(time you want to wait in seconds)
You can add this in command groups, or as a placeholder for an autonomous command if you don’t want to run anything.
Even better than this - Commands.waitSeconds(seconds)
creates the command.
i’d recommend joining the unofficial frc discord, there are a lot of people there who can help with coding a robot
https://discord.gg/frc
There are also a couple of flavors of WaitUntilCommand - boolean supplier and match time. They come in similar identically functioning forms such as those posted by @Aqtion and @wanderingcarrot .
if you’re not careful that place will turn you into a software engineer
just ask how i know
In some places we have used a simple incrementing integer counter and relied on the 0.02 second time step. if (counter > 50) {} else {counter++;} would wait a second.
You can create a wait command by using Thread.sleep
just like in plain Java.
No, that doesn’t work properly. The main loop is designed to run periodically (nominally every 20 ms) and a Thread.sleep will block everything else from running (e.g. if you have other commands such as joysticks driving your drivetrain, they won’t run). This mode of operation is also called cooperative multitasking. The expectation is that everything in your robot code runs as fast as possible and doesn’t wait or block on anything.
Wow, never realized that! Thank you!
Wpilib has a ‘Timer’ class that you can use. Either by creating an instance of the class, starting the timer and using the .hasElasped(seconds) function. Or storing the the time from the static function .GetFPGATimestamp() at the time you want to start to wait and check if the difference between the return of the function now minus the stored value is bigger than 1.
If you doing this in a command you might want to instead use one of the decorations of ‘Command’ and a ‘WaitCommand’
You can create a new thread and sleep that thread though then run a method if I am not mistaking without sleeping everything
Yes, multithreading can be used, but it’s very risky for beginners, as you have to be careful about avoiding data races using proper synchronization (if you don’t know what these terms mean, don’t use threads). The command framework is designed to all run in a single thread and is not thread-safe.