Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
At work, but not for production apps
I don’t use WebAssembly but expect to when the technology matures
I have no plans to use WebAssembly
No plans and I get mad whenever I see the buzzword
CI/CD / Software Development

Off-The-Shelf Hacker: Enhance Motor Control with Positional Feedback

Sep 24th, 2016 6:59am by
Featued image for: Off-The-Shelf Hacker: Enhance Motor Control with Positional Feedback

This week, we’ll finish up our series on adding motion to your projects by talking about basic positional feedback using a potentiometer. We’ll also look at controls like start buttons and E-Stops. Finally, we’ll discuss what else you might do with a motor/driver setup in your projects.

To review (see part 1 and part 2), I bolted a small motor/gearbox to the camera arm on my Steampunk Presentation Machine. The arm is just a set of aluminum links, that pivot at the bottom and allow positioning of the hacked Logitech Webcam so that I can display small parts up on the projector screen. The guvcview program running on the Raspberry Pi is used as the video program connected to the camera.

I simply connected the power to the motor driver and plugged the Arduino into the USB port to make the motor move. I programmed a time-based sequence to cycle the motor back and forth.

First, let’s discuss how you can determine the position of the arm. We need to know where we’re starting from, right?

Positional Feedback

I’ve chosen to capture the position of the arm using a potentiometer connected to the arm, as you can see in this graphic:

Potentiometer Connected To The Arm With A Brass Link

Potentiometer Connected to the Arm with a Brass Link

As the arm swings, it rotates the pot, and the value can be read with an analog pin:

Closeup Of The Potentiometer Linked To The Arm

Closeup of the Potentiometer Linked to the Arm

The code to work this magic is at the bottom in the void() loop:

All we do is use an analogRead on the potentiometer connected input pin (A0) and assign the value to a variable (sensorValue). We then check the value of the variable and while the input registers below a set number, run the motor forward. If the input goes above the value, the motor stops. The 10-bit input pin will map from 0 to 1023 for between 0 and 5 volts.

My particular potentiometer is a 10K Ohm model, linear taper. It’s kind of low quality because you can turn it about 20 degrees before it comes off of zero. Same thing at the other end of rotation. It registers 1023 between 10 and 20 degrees before hitting the stop. These are the practical considerations you’ll need to deal with when building hardware.

And, there’s no way to know these things for sure until you build, test and analyze your design. Remember, fail fast and reroute, if need be.

It’s a simple example, just to show the general concept of positional tracking and feedback.

Naturally, there are challenges with this approach to positional control. Inertia is one problem. Once the arm is moving, it takes a short period, to come to a full stop, especially when there is a gearbox involved. The potentiometer may continue to change its value after the specified value is reached. For example, we may use 250 as the stopping point, but when the arm actually stops moving, the value might have continued to change up until 257. You’ll have to design in some code to account for the “overrun value” if you want to move the motor to specific positions.

Another thing to think about is while you’re checking the analog value, you’re also commanding the motor to move, with the motorA_forward and motorA_backward functions. There might be instances where that process isn’t fast enough, and you’d have to use an interrupt routine, for accurate values from the potentiometer and for moving the motor.

You might also look into using encoders in place of the potentiometer, to measure the angle of the arm and provide positional feedback.

You may have noticed that there’s no power, stop or start button. Well, it is an absolutely minimum viable project. Let’s talk about them for a minute.

All Stop, Power Up and Go

It’s customary and safe to be able to power up a device and have it just sit there until you are ready for it to move.

Everything eventually has a power button. The power button usually connects the plus side of the power supply to the motors, LEDs, microcontrollers and other parts, in the device. No power, no movement or action.

A lot of times, robots have what’s called an E-Stop button. This might be a hardwired power switch that you ‘arm’ (pull it to the up position) before power is supplied to the robot. We’ve all seen those red industrial-looking ‘STOP’ buttons at gas stations and on factory machinery. Hit it and the power is cut, immediately stopping the machine.

E-Stops are also used on robots. It’s a practical way to keep people safe from unexpected robot movements or in case something goes haywire. I remember a large Cincinnati Milicron robot on the factory floor that loaded 30-pound artillery shells into a tool holder. The thing was 10 feet tall and moved pretty fast. You certainly didn’t want to get in the way of that monster. It had several strategically placed E-Stop buttons and a big metal fence around it.

So that take care of the stop and power button. The start button is a little more complicated.

You’ll need to wire up a circuit using a resistor (about 10K ohms) between a digital input pin and ground. The digital pin will also connect one side of a push button that’s connected to +5 volts on the other side. It’s a standard arrangement for reading buttons.

Then, in the code, you simply watch for the value of the push button to change from LOW to HIGH. When that happens, by pushing the button, you can then start executing your motor control code.

Limit switches are used to stop a mechanism from hitting its physical stops. With the arm, you’d station one limit switch near the forward physical limit of travel and another near the rearward limit of travel. You could then use button detecting code to halt motor movement if the arm ever goes as far as to trip the limit switch. I have a couple of magnetic reed switches and a magnet that allow me to detect the upper and low limits of my garage door travel. You can use push buttons, micro switches, reed switches and so on.

Enhancements and Possibilities

We’ve just touched on a few of the basics of robotics and automation with microcontrollers.

I was thinking about maybe adding a second motor to the upper joint on my Steampunk Presentation Machine camera arm, to be able to precisely position the camera over an object on the display platform. The motor could connect to the second channel on the existing motor controller, and I’d have to add another potentiometer, to keep track of the second arm’s position.

Another idea I had was to put the Presentation Machine in an interesting Steampunk themed box. When I open the lid, it will trip a switch to raise the rig up and transform it into ‘presentation’ position. A limit switch would stop the motor at the correct height. I might then use a push button to command the machine to retract back into the box and power the whole thing off.

What enhancements would you suggest?

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.