This is part of a continuing series on diagnostics tooling for the mjbots quad series of robots. The previous editions can be found at 1, 2, 3, 4, and 5. Here, I’ll cover the first extension I developed for tplot2 to make it more useful to diagnose dynamic locomotion issues.
Diagnosing problems on robots is hard. The data rates are high, sensing is imperfect, and there are many state variables to keep track of. Keeping track of problems that are related to erroneous perception are doubly challenging. Without a recording of the ground truth of an event, it can be hard to even know if the sensing was off, or if some other aspect was broken. Fortunately, for things the size and scope of small dynamic quadrupeds, video recording provides a great way to keep a record of the ground truth state of the machine. Relatively inexpensive equipment can record high resolution images at hundreds of frames a second documenting exactly where all the extremities of the robot were and what it was doing in time.
To take advantage of that, my task here is to get video playback integrated into tplot2, so that the current image from some video can be shown on the screen synchronized with the timeline scrubber.
Making it happen
Here I was able to use large amounts of the code that I developed for the Mech Warfare control application. I already had the ability to render ffmpeg data to an OpenGL texture. The missing pieces I needed were getting that texture into an imgui window and adding seek support.
The former was straightforward. imgui has a
ImageButton widgets which allow you to draw an arbitrary OpenGL texture into an imgui widget.
The latter was a little more annoying, only because the ffmpeg API had a slightly unusual behavior. Even after
av_seek_frame was called, one frame from the old point would still be emitted. This confused my seeking logic, possibly causing it to ignore frames. However, after discarding that one stale frame, it worked seemingly just fine.
Next I’ll cover the last major piece I added to tplot2 to help with issue diagnosis.
In previous posts, (1, 2, 3, 4), I covered the updates I made to the underlying serialization and log file format used in mjlib and the quad A1. This time I’ll talk about the graphical application that uses that data to investigate live operation.
You might note the “2” in the name and realize that yes, this is the second incarnation in the mjmech repository, tplot being the initial. The original tplot.py was a largely a one-day hack job that glued together the python log bindings I had with matplotlib. It provided a time scrubber, a tree view, and a plot window where any number of things could be plotted against one another.
It did have a number of problems:
- Speed: The original tplot read the entirety of the log into memory before letting you view any of it. Further, while reading it into memory, it converted everything into python structures. This took some time for even relatively short logs.
- Coding efficiency: This might seem paradoxical, but developing GUIs in PySide still takes a decent amount of time, even if you don’t care what they look like at all. Either you have all the overhead of using Qt Designer and thus have to manage either UI file loading or compiling, or you design the layouts in code and have mysterious layout issues because the exact construction requirements to get valid layouts are very hard to determine without looking at the QT source. There are so many signals to connect to slots, and so much state to manage, and anything non-trivial requires deriving custom widget classes with many virtual methods to overload.
- Integration with video: Yes, QT has a video subsystem, but it is intended for live playback, not frame accurate seeking, and also has a lot of overhead to use it effectively.
- Build footprint: Except for tplot, I have moved the entirety of the code and its transitive dependencies for the quad A1 to be built from source under bazel. This makes cross compiling easy, and well as making cross platform and cross distribution support relatively painless. While I have converted some large things to bazel (gstreamer), QT and PySide was a bridge too far.
- Python support: PySide1 only supports QT 4. QT5 had no permissive python bindings until very recently, which while they are in Ubuntu 20.04, didn’t make it into 18.04. That isn’t of course a deal-breaker, just an annoyance.
For tplot2, I decided to try my hand using the Dear Imgui library that I used for the Mech Warfare control interface. It is remarkably concise, very quick to develop for, looks at least “OK”, and has no dependencies other than OpenGL. Once I had multiple axis support in implot, getting to tplot1 level functionality was remarkably quick, maybe a day of effort in total:
Next up, I’ll cover the improvements that I made to tplot2 that made it worth all the effort.
I used Dear Imgui for the simple Mech Warfare control application I built earlier and was relatively impressed with the conciseness with which one could develop effective (although not necessarily the prettiest), interactive and response user interfaces in C++. For some time I had been planning on developing a new diagnostic application for the mjbots quad that would allow plotting like the original tplot.py, but would also integrate recorded video and 3D rendering and diagnostics. I had assumed I would use HTML/JS because it is the cool new thing, but I never got up the energy to make it happen, because every technical step along the way had big hurdles. I figured I would give Dear Imgui a try, but the big thing it was missing was plotting support.
In the original tplot.py, I used matplotlib for plotting integration. It is a high quality python library that can make interactive plots in nearly every imaginable form as well as production quality static plots. It integrates with a number of GUI toolkits, in tplot I used it along with PySide. The downside is, that given that it supports nearly anything under the sun, the code itself is relatively complex and hard to tweak. In order to make tplot.py support multiple axes I had to do some careful source inspection to figure out which undocumented things could be poked.
Dear ImGui itself has a bare bones plotting system, but that doesn’t have anywhere near the feature set I would need. The next system I seriously considered is implot. It is very new, as in its repository is only a few weeks old, but already supported most of what I needed for a diagnostic tool. The biggest thing it didn’t have was support for multiple Y axes.
So I took a stab at adding them!
One weekend later, I was largely successful:
Only a day after that and Evan had fixed up a few remaining problems and got it merged into master: https://github.com/epezent/implot/commit/5eb4b713849