Wednesday, August 30, 2017

Why Python and LabVIEW?

Hi Team!

One of the big questions that I get about coding for the team is why "Python and LabVIEW?”  We could use Java or C++ instead.

The simple answer is the same for both languages.  LabVIEW and Python are easy to learn.

When Mrs. E. and I came to the team in the 2008-9 school year, FIRST FRC was transitioning to an entirely new control system from National Instruments.  From the beginning, it could be programmed in C++, Java, or LabVIEW.  The previous controller used a derivative of C, so it was decided that we would use C++ to program the new system.  It seemed that this would be the best fit.

Four weeks into the build season, our team programmer had spent a LOT of time trying to get the C++ framework to work.  At that point, I was a bit worried that we wouldn’t have any working code ready for the robot.  I sat one of the underutilized team members down at a computer, and in two hours, we were able to replicate everything in LabVIEW that the main programmer had struggled to get working in C++ over four weeks.  On top of that, we even made a working vision system for that year’s game that could discern different colored balls.

Now, please understand, our main programmer was and is a very smart guy.  He eventually got all of the code working in C++, but it took a huge investment on his part.  Nobody else on the team had any hope of understanding how the code worked, as nobody had the time that he had spent on the code.  LabVIEW was and is much easier to get up to speed with.  We have mentors that know LabVIEW and we get sponsorship money from National Instruments for using it.

Our use of Python is a little more complex.  We use it on the nVidia Jetson platform, which is an ARM architecture with a lot of GPU cores.  We could use C++ on this, as OpenCV (the computer vision library we use) is written in C++.  Why don’t we use C++ here instead?

As mentioned before, Python is easy to learn, but that isn’t the whole story.  Python is Python is Python (within the same version).  If you run it on Linux, Windows, or macOS, it works the same.  C++ is NOT the same on those three platforms, as the compilers for C++ are different on each.  Linux uses g++, macOS uses Xcode (llvm Clang underneath), and Windows uses Visual C++.  They all compile C++ code, but do it very differently, and to varying levels of adherence to ANSI standards.  This means that C++ code written on one operating system is not guaranteed to work on another without a lot of testing and re-writing.

Last year, we did encounter differences in how OpenCV that was called from Python behaved on different platforms.  This was primarily due to the use of different compilers on different systems.  The Python code we wrote, however, behaved the same on Windows, Linux, and macOS.  As mentioned in the last e-mail, I am working on a virtual machine solution that we can all use to replicate running on the Jetson.  We might be able to use C++ in this virtual machine, as the compiler will be the same as what the Jetson uses, although the underlying CPU’s will be different.

Perhaps the best reason I have found for using Python for vision is a site that is dedicated to using OpenCV with Python.  Please visit PyImagesearch.com for the best guides I have found for learning computer vision topics.  Adrian’s blog entries are way better than the books that I had found last year.  He really does a good job in breaking things down to be much more understandable.  He also covers topics that are extremely advanced (like machine learning).

I just realized that I didn’t address Java.  Well, I don’t want to make anyone suffer that much.  I’m told that Java 8 is not as bad, but bad is relative, and it is still Java.  In all seriousness, Java is a third-class citizen for robot programming.  The FRC Java framework is a wrapper on the C++ framework.  The C++ framework is itself a second-class citizen, as it usually trails the LabVIEW framework when new features are introduced or bugs are fixed.

If you want to use Java or C++ (or Swift, or C#, etc) for a project, just reply privately and let me know.  All computer languages are just tools, and one of them may be a better tool for something that we do.  The more tools you have, the heavier your toolbox will be.

— Len

No comments:

Post a Comment