Periodic-Oriented Programming for FIRST Robotics

26 points by robalex


juliaaa

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.

bvisness

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.

bputh

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.

apropos

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.