# Raspberry Pi for two wheel robot?

I want to create a two wheel remote controlled robot. I have worked out a lot of the logic with regards to balancing. I have started to read up on motor control and arduino vs beagleboard black vs raspberry pi.

Is the multitasking nature of a full linux OS a problem I need to be concerned with for this application?

I expect that I have to adjust the motors at least 20 times per second, but I don't think a slight variation in the update loop interval to be a problem. Possibly, I will face problems if I need to do PWM myself?

Basically, the way I plan to make the robot work is by using an accelerometer to have a reference to where down is. The robot will autonomously work to keep the direction of the accelerometer down. The remote control will simply adjust the readings from the accelerometer, and the balancing loop will then react as if the robot is falling and accelerate the wheels.

• I don't think it relates to the question, but I just thought to put it out there. You would need to look into control algorithms such as PID or PI. They will help you generate a PWM value from the error amount (difference from the reference point), but they need some calibrating and some trial and error to calculate the constants... Mar 17 '14 at 4:35
• I've never heard about the PID algorithm, but it looks like the approach I was going to take intuitively. I've been thinking for a long time about this. The only thing I'm worried about is, how can I attach "arbitrary" mass to the robot (changing its center of gravity), and make it maintain its position - i.e. not start moving in a direction. Mar 17 '14 at 14:18
• Use interrupts. You can be sure youll get an even distribution of short tasks 20 times a second if you run it on an interrupt. All other tasks on the CPU will be preempted during an interrupt. Mar 19 '14 at 21:36

With an Arduino, you are guaranteed to have dedicated processing power for your balancing application. That said, the calculations for PID control are not terribly complicated and I would be very surprised to hear that a Raspberry Pi could not keep up with 20 calculations per second, even under load.

• The problem would be if I could expect a "hang" of say 100-200 ms at arbitrary times. If all the 20 calculations are performed at the end of a second, it doesn't help much. Mar 17 '14 at 14:20
• If you're worried about hangs due to multitasking, you could always increase the priority of the process that does the balancing. I'm not sure what operations would cause your system to hang for 100-200ms (assuming you are not using blocking I/O in the thread that does the balancing).
– Ian
Jul 6 '14 at 15:22

A Raspberry Pi should be sufficient for the control you intend to do with it.

In designing a controller under a full multitasking operating system, like the Linux operating systems that are available for the Raspberry Pi, you have to be careful about the real-time requirements, and if the time share chunk of processor made available to your software will be enough to keep up with the rt nature of cyber-physical interactions.

In small words: if you don't run much beyond the controller code in the RPi, it should suffice, but if you want to have other pieces of code running, like some higher level supervisory control or even a Java virtual machine for some reason, you will have to assess the response time, and even take the risk of some runaway thread end up blocking your controller for some time.

The solution for that, and if you take it you will most likely have no headaches in the process, is to have non-preemptive multi-tasking OS, or, even better, a Real Time OS running on the Raspberry Pi.

And you can have both (not at the same time, of course!). Raspberry Pi has support for the RISC OS Operating System, which employs cooperative multitasking, where you can give absolute priority for the control loop, and only run other pieces of code once the control loop is done for the cycle.

And, my personal best, you have Xenomai running on it. Xenomai is a hard real-time supervisor that has Linux running under it, so you can have a full deploy of a Linux machine, with all the benefits of having all the pre-packaged software and other amenities, and you also have a Real Time layer where you can run your control loop without any risks of getting interrupted out of time. You can find instructions on how to have Xenomai installed on the RPi here. There used to be a ready SD image of it available, but it seems to have gone offline at this time.