Current sensing for robot arm with PWM control of hacked servo motors Moderators: adafruit_support_bill, adafruit

Current sensing for robot arm with PWM control of hacked servo motors

Hello wise forum,
I'm using an Adafruit motor shield to control two servos that act as the shoulder and elbow of a small robotic arm (photos and video here in this flickr set http://www.flickr.com/photos/dustynrobots/sets/72157627951190369/). The servos are hacked to remove the control board, so I control the robot directly through Arduino code and the Arduino PID library. I'm sensing the current drawn by the motors by putting a tiny resistor in series with the motor power, sensing the voltage drop across it, then since V=IR and I know V and R I can get the current. Because the voltage drop is tiny, it's amplified by op-amp before the Arduino reads it. Code for the current sense library that was created and the schematic are here: https://github.com/caplingerc/IVSense/ and the Arduino code for the arm is here https://github.com/caplingerc/Roboarm. It's currently programmed to just draw a short vertical line over and over again.

Soooo... my issue is that I'm trying to measure the power consumption each time through the loop by logging the current and voltage. But because the speed of the motor is controlled through PWM (at 1kHz through the motor shield M3 and M4 connections), the voltage input looks like a square wave. And since V=IR, the current looks like a square wave too. However, the point of using PWM to control the speed of the motor is to set an "effective" voltage between 0 and the maximum, by varying the duty cycle, so that if the input is say 0-5V, at 50% duty cycle the motor will only "see" 2.5V, and should "feel" a current pull in a similar way. So ideally both the current and voltage vs. time curves would be smooth because of PWM frequency being so high, but I get lower frequency spikes in the data. Any thoughts? I can only log data to serial at a rate of about 200 Hz, so my first guess is that is too slow and I'm getting aliasing, but it seems that it's not the whole story and I'm not even sure if the whole Nyquist sampling theory applies here.

A picture of the current and voltage readings during the down stroke of drawing the vertical line is below. I can send an excel file if anyone wants it.

Thanks!!

current and voltage graph
current voltage.png (34.07 KiB) Viewed 3688 times

dustynrobots

Posts: 29
Joined: Fri Jul 22, 2011 5:43 pm
Location: New York, NY

Re: Current sensing for robot arm with PWM control of hacked servo motors

since V=IR and I know V and R I can get the current.

Don't forget the "L". Motor coils are inductors. Throw in the commutation noise from the brushed DC motors that power the servos and the picture gets much more complex. If you look at this on a scope, you will see that things are much noisier than just a couple of square waves. You will need to do some filtering to get clean data.

Posts: 48662
Joined: Sat Feb 07, 2009 10:11 am

Re: Current sensing for robot arm with PWM control of hacked servo motors

Right, I figured all that, but my signal processing is weak to nonexistent. Have you seen anyone implement this kind of filtering in Arduino, or have any other good beginner resources?

dustynrobots

Posts: 29
Joined: Fri Jul 22, 2011 5:43 pm
Location: New York, NY

Re: Current sensing for robot arm with PWM control of hacked servo motors

Filtering digitally can be as simple as averaging over "n" samples. But you will want to be sampling quite a bit faster than your PWM frequency. You could do this with a timer and interrupt.(Check out the various timer libraries over at the Arduino Playground. http://arduino.cc/playground/)

Posts: 48662
Joined: Sat Feb 07, 2009 10:11 am

Re: Current sensing for robot arm with PWM control of hacked servo motors

I would suggest that you use a low pass filter. I had a similar issue measuring current while controlling something with PWM. The filter would be just a resistor connected to your ADC input with a capacitor connected to ground. For 200Hz readings you could start with a 47uF cap with a 570 ohm resistor. This should give you a much smoother waveform. Increasing the resistor and/or capacitor values will cause further smoothing but will also increase response time.
coffey

Posts: 1
Joined: Wed May 09, 2012 6:31 pm

Re: Current sensing for robot arm with PWM control of hacked servo motors

I've tried a moving average filter and it doesn't really help until I increase n to be just about the number of data points, which defeats the purpose. I did check out the timer libraries here: http://arduino.cc/playground/Main/LibraryList#Timing (thanks for the suggestion!) and have used interrupts before, but my limiting factor for sample speed seems to not be the actual sampling rate, but the rate at which I can print to serial. Running the serial monitor at 115200 and optimizing the code as best I can still gets me at only 200Hz, so I don't think an interrupt will help here, right? I'm just using CoolTerm to capture and save the data files. I've tried writing out to SD card too and that was WAY slower, but if you know of a way to push the data out faster when connected directly through USB to a computer please do let me know.

A hardware based low pass filter could work, but there are no obvious high frequency noise issues and I don't want to lose data before I know what's going on. I'd kind of rather get as much data as possible and then do the filtering on the software side.

Will try a scope in the morning and see if I can make some more sense of what's actually going on directly. Thanks!

dustynrobots

Posts: 29
Joined: Fri Jul 22, 2011 5:43 pm
Location: New York, NY

Re: Current sensing for robot arm with PWM control of hacked servo motors

my limiting factor for sample speed seems to not be the actual sampling rate, but the rate at which I can print to serial.

If you are averaging samples, you don't have to print all of them.
I've tried writing out to SD card too and that was WAY slower, but if you know of a way to push the data out faster...

Search these forums. Fat16lib has posted a sketch that samples and logs to SD at 40KHz.

Posts: 48662
Joined: Sat Feb 07, 2009 10:11 am

Re: Current sensing for robot arm with PWM control of hacked servo motors

Perhaps there's a way to get around the sampling rate being limited by the data output rate. If you sample in bursts between sending data out you could get a much higher sample rate. It would look like this:

1. sample 25 times at 10kHz. (this would take 2.5mS, half of your 200 bytes per second window)
2. average all all those samples and output the data.
3. repeat every 5ms.

something else to note, as adafruit_support pointed out, is that the load is inductive. depending on the actual value of the inductance, the algebraic ohm's law may not hold. likewise the algebraic P = VI relationship won't work either, because the current will be lagging (some phase angle behind) the voltage. P = VI only works with scalar magnitudes when the waveforms are in phase, otherwise you have to solve with phasors (or integral transforms) because the phase angles are different. (see power factor: http://en.wikipedia.org/wiki/Power_factor).
Member of Technical Staff
Poles up, zeroes down.

johngineer

Posts: 105
Joined: Fri Aug 14, 2009 6:05 pm
Location: Brooklyn, NY, USA

Re: Current sensing for robot arm with PWM control of hacked servo motors

Oh okay, average values in the code *before* printing, I get it. And yes I realize it's an inductive load, but my experience with these tiny motors is that inductance is so small and instantaneous it really doesn't add much into the equation, so P=IV is pretty accurate still.

samples and logs to SD at 40KHz

Will look for this! I much prefer to log data directly onto a computer because I don't need a mobile solution, but if for some reason logging to SD is faster than sending serial data then it could be an option.

Thanks!

dustynrobots

Posts: 29
Joined: Fri Jul 22, 2011 5:43 pm
Location: New York, NY

Re: Current sensing for robot arm with PWM control of hacked servo motors

my experience with these tiny motors is that inductance is so small and instantaneous it really doesn't add much into the equation, so P=IV is pretty accurate still.

That may be true after filtering. But the inductive spikes from commutation and PWM switching will give you some instantaneous samples well outside what a straight application of Ohm will predict.

Posts: 48662
Joined: Sat Feb 07, 2009 10:11 am

Re: Current sensing for robot arm with PWM control of hacked servo motors

Hardware filtering and viewing the output on an oscilloscope worked like a charm! Thanks for all the tips. I posted my progress here: http://www.dustynrobots.com/news/signal ... -robotics/

Still more to do but I'm encouraged by the results.

dustynrobots

Posts: 29
Joined: Fri Jul 22, 2011 5:43 pm
Location: New York, NY