Controlling a Brushless Motor With Raspberry Pi¶

The focus of this post is to control a brushless motor from an automation point of view. Imagine an autonomous vehicle that grabs boxes from a storage rack. It has several motors, and a small computer (a Raspberry Pi for example) that controls the motors based on what it is told over, say, an internet connection. The vehicle might also do image recognition, which is why choosing something more powerful than an Arduino might be a good idea.

There is quite a bit of information out there on the Internet about how to talk to an Electronic Speed Controller (ESC - the thing that actually controls the motor). Those pages explain the topic in the context of microcontroller programming, or even in the context of a ready-made flight/drone firmware that is put onto Arduinos.

This post shows how to run the motor with not one single line of code. We use the Linux sysfs PWM interface from the commandline to run the motor.

Setup¶

First, the wiring …

This is much the same as in the PWM topic. There, we used channel 0 (pin 6) to control the brighness of an LED. Here, we use the same pin as input to the ESC’s signal wire.

See the PWM topic for an explanation of what PWM is, and how to use a Raspberry Pi and the PCA9685 to generate PWM signals. This is information that is heavily used in the remainder of this post.

Note

Unfortunately, the default permissions of the sysfs PWM interface are root/root, so we have to be logged in as root. sudo -i will do the job, for example.

>     sleep 0.5
> done


Conclusion¶

While there was some fun to have from this prototype project, I can imagine that autonomous vehicles will not be equipped with such kinds of motors/controllers. I tried this out for a friend who is into logistics and automation, and whose hobby is RC airplanes (which might explain much). PWM is a rather poor way of communication; the calibration procedure is nothing that is wanted in production. There are ESCs that use CAN, for example, which is a real and much higher level protocol that has no quirks.

It is a starting point however - in most cases it is much more important to know what you don’t want, and why, than to know what you want (you never do).

The fun part for me is to show how hardware is integrated into Linux, and how this fits into the good old Unix paradigms. For PWM, for example, sysfs is used to expose PWM chips and their channels to the user - who is then able to control it using simple shell commands. It does not require too much fantasy to come up with a programmatic to do what we did on the commandline - one can use any language that is capable of doing file I/O. Much unlike bare metal microcontroller programming - this usually requires a good understanding of C/C++ and all the pitfalls such languages and environments involve.