Periodic-Oriented Programming for FIRST Robotics
26 points by robalex
26 points by robalex
FRC is quite weird from a software point of view. I would say that probably 75% of teams struggle to design/build the hardware reliable enough to play the game. For these teams (I was on one of them) it doesn't really matter what software is running, you can't patch bad hardware. This is where a lot of the bits of advice in the article make less sense - it's impossible to not store arm position in software (estimation) if you want to attempt autonomous period work if you don't have a sensor (well, you can also do it purely on time here too). It's also impossible to be position-based instead of time-based without various sensors for position.
For the next 20% of teams that have competent hardware, then I would totally agree that dead simple periodic code without commands is the most appropriate fit. And these teams generally end up well funded enough to have sensors to be able to measure these and use it. (It seems like this is where you fit from a look at the blue alliance). But this is what I also see a lot of, though often quite spaghetti (these are high schoolers, after all).
I'll add here that WPILib (especially Tyler of 3512? iirc) has done a lot of wonderful work here on bringing a lot control theory beyond PID to be easier to implement for these teams, and having a lot of turnkey components for this, something previously limited to the top teams (or people using LABVIEW). Kudos also to Dalton (who I think works for CTRE now?) for doing a lot of work on documentation.
The top 5% of teams is where this changes. Listed in the advanced section of documentation, Command-based makes significantly more sense when your robot is heavily automated, and you have half a dozen different preplanned trajectories for your (hypothetical) double arm grabber claw mechanism for the different goals. You have a desire to do the many different "modes" of operation this article describes because you are doing many different things. This is mostly driven by team factors, and the current year's game, not software.
I'll add also that I believe command-based waz significantly reworked by (iirc) someone rom one of the top-scoring Israel teams. This is where it fits and makes sense.
Maybe this has changed, as I haven't been involved in a few years, but I do not recall seeing many command-based teams in the wild.
I graduated last year and it very well might just have been the teams I was interacting with and looking at their code but command based seems to have become far more common and at least for our team was the default choice. I am quite curious what the rewrite will look like as I graduated before we got to use it (and systemcore which is very sad). The WpiLib team has done a really good job trying to make it intuitive to use but the main issue from my standpoint was too many very similarly named options and very few docs beyond just javadoc. There are very different options that are all setup as commands almost identically to each other.
I may be coming at this from the 5% angle though. Our team was never super competitive but we did always have a workable bot that let us push for autonomous control (which was also very much my hobby. At least from the limited set of regional competitions I went to it seemed like most of them had a pretty good grasp on hardware. I’ve been really surprised how fast the manufacturing techniques have advanced for a lot of teams as it is largely not feeling possible be competitive without having a large metal shop and people who can design custom parts.
edit: This post seemed super familiar to me and I went digging back through our team slack and we discussed this approach about a year ago. It honestly would be a good idea to simplify a lot of frc code. I personally loved my baby of an amalgamation that was the codebase but that is a pretty terrible argument against simplicity
My impression from talking to many teams is that command-based is the default approach for teams with a serious software subteam. And my impression is also that ~all of them would be better off without commands.
It’s funny how much it feels like these structures are helping you, when most of your time is spent organizing the codebase instead of making the robot do things. I’ve been there. Time spent shuffling commands around feels very productive. It’s just…well, take an honest look at how much time you spend debugging it, and decide for yourself whether it was worth it.
it's impossible to not store arm position in software (estimation) if you want to attempt autonomous period work if you don't have a sensor
These days it’s so common to use turnkey brushless motors for FRC that literally everything has an encoder in it, and in turn you can almost always derive your elevator height, arm angle, etc. from that. So sensor-driven control is vastly more practical for many teams than it used to be! (BOY do I not miss installing terrible quadrature encoders.)
I’m not entirely sure what to make of your remark about the bottom 20% of teams who just can’t get a robot together…yes it’s true that software is not their bottleneck, but surely the dead-simple periodic approach is the right choice for them too. The last thing they need is command-based bugs on top of everything else.
As a side note, I just wanted to mention that a lot of my advice in this post echoes the Carmack article that I linked in passing - even more than I initially realized. I first read that post at least a decade ago, and I think Carmack's advice holds strongly true in an FRC context, and he does a great job justifying why the common practices lead to bugs.
If there's one line from his post that best sums up my argument, it's this:
For instance, having one check in the player think code for health <= 0 && !killed is almost certain to spawn less bugs than having KillPlayer() called in 20 different places.
A close second:
Minimize control flow complexity and "area under ifs", favoring consistent execution paths and times over "optimally" avoiding unnecessary work.
I would offer that the critique of command-based in this article largely seems to be referencing the old Commands v1 way of doing commands, which has changed significantly in the last few years. You now just define a single function to define a command now, rather than having to write an entire class. The semantics are also much clarified. Take a look at a modern robot project before deciding against it for the reason of verbosity.
That said, a lot of the critiques in this article are entirely fair, given different circumstances for different teams.
If you’re talking about the composition utilities and RunCommand etc., then those don’t change a single one of my points (except maybe the estimate of line count). They change absolutely zero about the stateful problems of commands, because the semantics are literally identical.
In case it wasn’t clear, my problem with commands isn’t boilerplate, it’s all the state: what commands are running, which commands will start when other commands end, what order does anything happen, etc. Commands v2 didn’t save anyone from the “initialize after end” problem, or the “my default command got cancelled problem”, etc. I’m not hopeful for commands v3 to improve anything either because I’ve come to realize that coroutines are just as stateful as anything else!
I am not seriously convinced by this article.
During my time in FRC, my team went through a transition from a periodic-oriented codebase to a command-oriented codebase. While a periodic-oriented codebase might be "simpler", people build abstractions for a reason -- a command-oriented codebase was significantly easier to reason about at a high level, and more importantly, much more amenable to modularity. We could give a programmer -- even a new programmer -- total ownership over their subsystem. This was very important on our team, where we made a point of the students being the ones running the team and doing all the work, with the mentors there to provide guidance and input, but never to write the code (or design the robot, or mill the parts, etc., with fairly limited exceptions).
Specifically, an issue with "simple" periodic code is that you either commit to making all your state explicit, or you build ad-hoc abstractions atop it -- neither of which is good. Our state machine core (pre-transition to commands) consisted of an enum with probably a ~dozen constructors, describing an idealized state of the robot -- which, as a common source of bugs, did not necessarily have to line up with reality. This sucked. And transitions between these states were also often buggy... commands didn't completely solve this, of course, but by introducing good and standard abstractions, they much alleviated invalid state hell (contrary to the opinion of the article).
Also as others have addressed, this article is criticizing the old and outdated command-based framework. The new command-based framework has been around since 2020 (!!!), and indeed is a very useful abstraction for complex autonomous routines, which has only become easier to do.
The distinction between “periodic-oriented” and “spaghetti” can be a fine one indeed, but the key thing is that my approach should allow you to more easily have less state. The command-based approach allows you to reorganize the state and draw some boxes around it, but they don’t actually reduce the statefulness of your program.
I’m not surprised that the comments are all focused on commands, but the “do everything every tick without exception” mindset is probably the more important takeaway. Even during disabled, 95% of all our code is running on every periodic. This makes several common categories of bugs appear instantly during development—for example, for the past several years we’ve literally never had the robot program crash on the field.
The command-based approach, regardless of syntax sugar, is not amenable to “run all the code all the time”, because it encourages you to chop up your code into tiny pieces that are selectively run in unpredictable ways.
And man, I really cannot believe that people think the 2020 syntax sugar changed anything fundamental about commands. They still have initialize, execute, isFinished, and end. They still require subsystems and cancel other commands with the same requirements. Lambdas and .alongWith builder methods don’t change that! And that’s the part I think is bad!
I suppose we simply disagree. I don't think that collapsing state is always a good thing. It can lead to less (or simpler) code, but this doesn't necessarily lead to code that's any easier to reason about. Rather, I am indeed much more concerned about modularity and the boxes around such state.
I think if you are doing rather simple code -- nothing wrong with that, different teams have different specialties -- a non-abstracted periodic approach, as you describe here, can work. My old team has historically been heavy on autonomous, however (perhaps to the detriment of driver practice), and a monolithic periodic-oriented approach did not work for us, and we were glad to switch to commands.
In what world is simpler code not easier to reason about? (Also, we had 10 different autonomous routines in the year I’ve been highlighting, so I dispute the idea that we were focused on teleop at auto’s expense.)