A constant current measure of a dc motor with ATmega328

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
I would like to implement a constant current measure of a dc motor (9V 600mA) using a l298 and the sense pin in the low side of each h-bridge. The problem is the oscillation of tension in shunt, caused by drive PWM. I'm appling a frequency of 62 KHz, the maximum of a Atmega328 and using a RC filter, but the time constat is big. Maybe try a stable dc signal is the wrong way to do it, but i don't know how to do it on another way. I think that maybe it's better to use a interrupt of PWM and in some moment after do the lecture in adc. But I think that this is not the best idea.

Hope that it is clear, my english is not so good.

Last Edited: Tue. Sep 16, 2014 - 03:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rcmurakami wrote:
and using a RC filter, but the time constat is big.
That's how I've done it.  Yes the time constant is relatively large.  Is there a specific reason that's not suitable for you?  How much temporal resolution do you need?  Is this just for monitoring?  Or do you have something more sophisticated in mind like PID?

 

BTW, the 328's ADC isn't especially suited to measuring the very low voltages you will see across the sense resistor with any kind of useful resolution because there is only a gain of 1x available.  You can use the 1.1V internal reference, but you're still likely to see much lower voltages, perhaps a few dozen millivolts at the very greatest, with any sensible resistor selection.

 

The usual approach is to use an op-amp.  Here's a product that does just that:

http://www.microbot.it/en/produc...

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have in mind implement a pid speed, I want estime a speed measuring the current. And my big problem is the oscillation in signal, because this make me calculate wrong speeds. I know about ADC resolution and 1.1V really isn't great. I think use a opamp or a sensor current monitor.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd suggest to download AN893 doc., "Low-Cost Bidirectional Brushed DC Motor Control Using the PIC16F684". Though it's written for PIC, but you may take a concept, there are different way to control a motor, and one is sensorless :

"Low-cost RPM measurement can be performed with a BDC motor by measuring the back EMF voltage from the motor (see Figure 3). The BDC RPM is directly proportional to the back EMF voltage."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rcmurakami wrote:
I have in mind implement a pid speed, I want estime a speed measuring the current.
That will only be reliable if the load on the motor is constant.  As load changes, the speed/current curve with change as well.

 

If you want to measure speed, measure it.  Use an optical encoder, a hall-effect sensor, or the technique mentioned by @rcmurakami.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks zteshnik for application note, I knew only of the an894. joeymorin I can't measure speed because I can't use a encoder in my project in this moment, but I can estimate when the loads increase using computational vision. I can see if the robot run less ou is stop. My project is a robot soccer.

 

I will do some tests today, because maybe the high frequency of PWM is disturbing my final signal.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rcmurakami wrote:
maybe the high frequency of PWM is disturbing my final signal.
The wildly changing ADC results are inevitable with PWM drive of a DC motor.  Even if you drive at full power i.e. 100% duty i.e. no pulses, you will see wide variation on the ADC due to the make-break nature of a commutated DC motor.

 

The solution is to do the RC filtering you're doing, and/or digital filtering in the app.  That could be as simple as averaging a number of samples together, or a sliding window average, or a more sophisticated FIR or IIR filter.

 

Whichever way you go, it shouldn't be a deal-breaker for PID, you'll just have to select the appropriate sample rate for the input to the PID, and the correct gain coefficients for P, I, and D.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am with difficult to do the signal amplifier. How the better configuration of amp op in this case? How I can combine the RC filter with the amp op? I am measuring the current after the load...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rcmurakami wrote:
I am with difficult to do the signal amplifier. How the better configuration of amp op in this case? How I can combine the RC filter with the amp op? I am measuring the current after the load...
Have a look at how the Arduino Motor shield does it:

http://arduino.cc/en/uploads/Mai...

 

A very simple LM358 op-amp circuit with a gain of 11.  The input is taken from the sense L298's sense resistor.  The sense resistor is 0.15 ohm (What value is your sense resistor?).  At 1 A the voltage across the sense resistor will be 150 mV.  With a gain of 11, the output of the op-amp will be 1.65 V.

 

Place an RC filter on the output of the op-amp with whatever TC you need for your app.  A longer TC will smooth out the chop and spikes you're seeing, but a shorter TC can be more responsive for PID.  You can do filtering in software instead of or in addition to the RC filter.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks! My sense resistor is 1 ohm, because in the moment I don't have a lower. The power supply of lm358 is 5V and 0V? I am testing the same configuration but don't work...but in my tests I used the 741

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At 1 ohm, a 600 mA load will show a 0.6 V drop across the sense resistor.  With the 1.1 V internal reference, this will give you a good range for the ADC without an op-amp, where 600 mA will be represented by 55% of full scale.

 

However the resistor will also dissipate 360 mW, which is a bit much.  Is your 1 ohm resistor rated for 1/2 W?  I would find a lower value resistor.  A 0.15 ohm resistor will dissipate only 90 mW

 

With that 1 ohm sense resistor you don't want much gain on the op-amp.  If you stick with the 1.1 V reference I would probably go for unity gain in order to keep some headroom.  If you go to Vcc=5V for the reference, I'd maybe use a gain of 4 or 5 at most for the same reason.

 

The LM741 isn't designed for use with a 0V/+5V supply.  The LM358 is, as you can tell from the schematic I already posted.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't will use the 1 ohm in the definitive project. I have a doubt, if I change the internal reference to 1.1V all peripherals will go work with this reference, including the I/O ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase the PWM frequency! The relationship (ignoring back EMF and such) is V = L dI/dt. V is constant (your supply voltage). L is approximately constant. So, if you make dt smaller by raising the frequency, then dI is proportionately smaller. This, in turn, decreases the ripple across the current sense resistor by the same proportion.

 

To do this, you MAY need to increase the CPU clock frequency.

 

The ONLY peripherals that use Vref are: (1) comparator, (2) adc, (3) brownout detector. I think that is all. General purpose IO has nothing to do with reference. 

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Mon. Sep 22, 2014 - 12:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Increase the PWM frequency!

Say what? The freq may already be to high for the inductance (as of yet unknown) of the motor. The pulse width must be long enough to let the motor current reach its rated level during on time or torque will suffer. Motor  inductance is a critical factor in determining the max pwm frequency. Otherwise operating voltage must be raised to unreasonable levels.

 

Some of my own experiments with a random drill motor and paper shredder motor at 12v required pwm freqs in the low audio range to achieve normal torque.

 

Rick

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, typical PWM frequency for cordless drill motor (commutated) speed controllers is 4 kHz.  These motors typically have stall currents in the neighbourhood of 10 or 20 amps.  I suspect the OP's motor running at 600 mA is a little less beefy.  While that doesn't directly imply a lower inductance, it nevertheless likely has a higher ideal PWM frequency.  The OP is currently driving at 62 kHz.  Probably a bit too high, but not horribly so.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Mon. Sep 22, 2014 - 03:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While that doesn't directly imply a lower inductance,

It tends to imply a higher inductance, thus requiring a much lower pwm freq and greater on time than he is using. The L/R tc must be low enough to allow current to rise to the desired level during pwm on time or the motor torque will be poor. Of course neither of us know the motor inductance, but the mentioned voltage/current does imply more inductance and resistance than the motors I mentioned. None of that may have anything to do with his current measurement problem, but it is an issue he will encounter.

 

Rick