CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

implement pwm current limit
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

implement pwm current limit
PostPosted: Sat Sep 13, 2014 3:15 am     Reply with quote

I have a 16F886 with CCP1 and CCP2 pins set as PWM output.
Each pwm pin is driving a H bridge. The bottom rail of the H bridge is connected to GND trough a shunt resistor. The voltage on the positive side of the shunt resistor is connected to the analog input.
While the current is continuous in the load, the current in the shunt resistor is positive during the T-on (increasing) and is zero during the T-off. That is the reason why I want to sample only during the T-on.
The purpose is to lower the Pwm duty cycle if the current in the bridge exceed a set value.
The analog pins I used are AN13 and AN4.

How should I deal with this problem?
Would it be sufficient to read the pwm pin with a polling and if found high make many quick adc readings? (and average them or take the max)
Here is a short code on how I have configured the micro-controller.

Code:
SETUP_SPI(SPI_SS_DISABLED);
   //timer0
   setup_timer_0(RTCC_DIV_256);
   enable_interrupts(int_RTCC);
   //timer1
   setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_8 ); //gives around 4 Hz interrupt
   set_timer1(0);
   enable_interrupts(int_TIMER1);
   //timer2
   setup_timer_2(T2_DIV_BY_16, 255, 16); //setup_timer_2 (mode, period, postscale)
   //   The cycle time will be (1/clock)*4*t2divBy*(period+1)=9.6 kHz
   //setup_timer_2 (T2_DIV_BY_16,255, 16);        //65.28 ms, 15.3Hz
   enable_interrupts(int_TIMER2);
   //setup ADC
   SETUP_ADC_PORTS(sAN0 | sAN1 |sAN2 | sAN4 | sAN13 | VSS_VDD );
   setup_adc( ADC_CLOCK_DIV_8 );
   //setup PWM
   SETUP_CCP1(CCP_PWM);   // Configure CCP1 as a PWM
   SETUP_CCP2(CCP_PWM);   // Configure CCP1 as a PWM
   //serial port interrupt
   enable_interrupts(INT_RDA);
   //global interrupt
   enable_interrupts(global);

Code:
 set_pwm1_duty(200);
Ttelmah



Joined: 11 Mar 2010
Posts: 19550

View user's profile Send private message

PostPosted: Sat Sep 13, 2014 4:03 am     Reply with quote

Problem is the delays in any software approach. This is why more complex chips have an input, that can trigger hardware limiting.

Now, on a software approach, you could (for instance), add a peak level detector circuit, with a slow decay, on the current sense, and then reading the voltage from this, would allow you to know the peak current being generated, and adjust to keep this below a level. Your mention of averaging, suggests you circuit is noisy (often a problem round PWM's), so you want to ignore some spikes, but this then increases delays....

I'd always be looking to respond as quickly as possible. Another idea, would be to go with the same peak level circuit, feeding into a comparator, and then setting the voltage on the other input of this, would allow you to have an interrupt that says 'overcurrent', and allow a much faster response.

On your code, the line:

SETUP_SPI(SPI_SS_DISABLED);

Is _wrong_. This says 'enable the SPI, but disable slave select'....
It's a common fault generated by people using the 'wizard'. The correct syntax is:

SETUP_SPI(FALSE);

Then, normally, the PWM, goes 'on' at the start of it's cycle. So the count resets, and immediately the power drive goes 'on', for the pulse period. Now this is useful, since the timer driving the PWM, has the ability to interrupt at this point (yours is set to potentially interrupt every 16cycles of the PWM at this point). So sampling at this point, then gives a reading of the worst case current. Consider if you could alter your design to use this?.
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Sep 13, 2014 2:23 pm     Reply with quote

Depending on the load you probably don't need subsecond current monitoring.Without knowing the 'details' it's hard to decide but I've checked heavy loads (1HP motors) at a 1Hz rate for years, never had any overheating issues.

While it might be nice to check current every pulse, it might not really be necessary in the overall scheme of things.

Just food for thought.

jay
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Sep 15, 2014 9:07 am     Reply with quote

You don't reveal enough info to really solve it.

How long can the bridge circuit load survive with a 100% - ie
bridge full on - condition?

Are there sudden load changes you are trying to correct for?
Quantified in the time domain please ?

What sort of overshoot are you trying to manage ultimately
by duty cycle change?
V out or P out ?
If there is a LC buck circuit involved -
how did you calculate your values?

How quickly must you increase pwm duty and recover from
UNDER current
when you HAVE done a compensation to lower it previously?

There seems to be MUCH more info needed
before you have have a workable approach to an answer.

take heart, this sort of problem can be challenging
to the most experienced designer too, as it is NOT simply a PIC
programming problem at it's heart.
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

PostPosted: Mon Sep 15, 2014 1:53 pm     Reply with quote

True, let me add some more info.
I am using a L298 dual H bridge rated 2A (each), to control a 0.5A 24V dc motor of a satellite dish.
The transistor bridge can handle a repetitive 2.5A (at 80% duty cycle, ton=10ms) and 3A 100 ms non repetitive.
Datasheet: http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00000240.pdf

I want to preserve the integrity of the H bridge, in case the motor has a short circuit or a mechanical overload.
What I would like to do is to decrease the duty cycle so that the current is regulated to 2A average value on the load. If this is too difficult for my current hardware, I would be happy if the L298 does not blow up.

As of your comments it seems that it would be more reliable to take the voltage on the shunt resistor, compare it to a threshold voltage and with the output of the op-amp turn down the "enable" pin of the L298 (notifying the PIC eventually). With a r-c network the L298 could be re-enabled.
This could be an improvement of the design (or the peak level detection circuit suggested by Ttelmah), but for the moment I have a board where the shunt voltage goes directly to the ADC input. I would be happy if I can turn off the bridge within a decent time of the overcurrent, and re-enable in 20 ms.

The 24V comes from a 4.5A or 3A 24v chinese switching power suppy with current protection.

In the PIC 16F886 I am using the internal oscillator, which is 8 MHz. Tosc is therefore 0.125 us.
The prescaler of timer 2 is set to 16, the overflow value (period) to 255 and the postscale to 16.
The cycle time will be (1/clock)*4*t2div*(period+1), which is (1/8M)*4*16*(255+1)=2048 us.
2.048 ms is 488 Hz.
Mhh, I did some wrong calculation here....I thought I am using something like 15 kHz switching frequency Shocked
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 16, 2014 8:54 am     Reply with quote

now we are getting somewhere.
can you post a schematic of the bridge and load - with pic drive connections?

it may turn out that external hardware current sensing -foldback drive limiting - is the better answer with as you say -

Quote:
turn down the "enable" pin of the L298 (notifying the PIC eventually).


let me also say that if it is RMS current that is the issue - a well chosen PTTC protector with "notification" could do the trick too - as then all the PIC has to do is
*kill PWM
* wait a bit for PTTC to reset
* continue

further afield in clever circuit design - the right PTTC could replace the sense resistor

BUT
instantaneous "cycle by cycle" sensing could work too.

care post the schemo you have now ?
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

PostPosted: Tue Sep 16, 2014 2:28 pm     Reply with quote

Schematic: http://www.tiikoni.com/tis/view/?id=524af13
The original one was kind of a spiderweb.

Then the 6 inputs of the L298 are directly connected to 6 output of the pic:
In1 - RC2/CCP2
In2 - RB2
In3 - RB1
In4 - RC1/CCP2
EnableA - RB4
EnableB - RC3

The circuit has also the RS232 in place.

I never heard about PTTC. I think I prefer the shunt thought (with the idea of send the value of the current via serial port)
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 16, 2014 10:18 pm     Reply with quote

values of RSA RSB ?

you said"control a 0.5A 24V dc motor "
else where you say 2 A limit BUT
whats your measured stall current for the 24V motor?

( the measured stall current IS your worst case- a
"shorted" motor is VERY unlikely, and a FUSE should
be used for that extreme possibility .....)
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 4:28 pm     Reply with quote

Have you attempted to measure the rate at which current builds up into a stalled motor?
Might answer some of your questions.
Or give you an idea of the scale of your design problem.

Mike
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

PostPosted: Thu Sep 18, 2014 2:24 pm     Reply with quote

Good point! I didn't think about the rate of rise and value of the short circuit current. I will try to make some measurements. Thanks also about the difference on using the fuse.

The shunt resistor is 0.1 ohm, 1%, 1W. It gives 0.2V at 2A.
The voltage resolution is 5/1024= 4.8828 mV, ....200/4.88=41 intervals, therefore the current resolution is 48.78 mA...which is quite ok for the purpose.

The motor is rated 4A, but I normally measure 0.5 to 0.8 A. The H bridge is rated 2A.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Sep 19, 2014 5:48 am     Reply with quote

"shunt resistor is 0.1 ohm, 1%, 1W. It gives 0.2V at 2A. "

an RC integrator op amp (rail to rail) with a gain of 25 and
a 10 msec time constant, bridging the .1 ohm sense
would nicely scale to all 10 bits of your A/D in the pic
over the 0-2a range you desire.
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

PostPosted: Sat Sep 20, 2014 7:17 am     Reply with quote

Well, I did some tests this morning with a digital oscilloscope.
First of all I changed to 20 kHz pwm frequency (otherwise the motor current has an eccessive ripple). It really was 480 Hz.
Code:
 setup_timer_2(T2_DIV_BY_1, 100, 16);


At 24V supply the current with blocked rotor is 3A. The initial slope of rise is 1.5 A/ms. From this, the characteristics of the motors are: R=8 ohm, L=16 mH, tau=0.128 s.
The free run current is 300 mA with an inrush current of about 1.5A.

So, I have about 1 ms (20 switching periods) to shut off the pwm once I detect I>2A.
In the oscilloscope I realized that the current in the shunt is raising during the Ton (obvious) and that is decreasing (NOT ZERO) during the Toff. I can't see why (shouldn't it be zero?). However, this is good for sampling with the ADC.

In the future design I will put a 2A fuse, or the op-amp to scale the shunt voltage, or a comparator to disable the L298 enable pin.

With the current hw on the sw side I intend to sample at maximum rate (in the main program loop) when the motor is On, and reset the max every 5-10 switching periods. Any advice is welcome. Thank you
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Sep 22, 2014 1:37 am     Reply with quote

webgiorgio wrote:

.
.
I realized that the current in the shunt is raising during the Ton (obvious) and that is decreasing (NOT ZERO) during the Toff. I can't see why (shouldn't it be zero?).
.
.

The motor has inductance.
The current rise rate determined by the applied voltage and the inductance.
Same happens to the rate of fall during the off period.
You need to look at what happens to the applied voltage during the off period.

Mike

PS If you have difficulty seeing what happens with a motor, replace it with a simple inductor.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Sep 24, 2014 7:16 am     Reply with quote

here is a way that MAY work to get instantaneous protection as you rise above 2 amps output.

1- change sense resistor to .2 ohms
2- connect 6.8k resistor to the enable input pin, PULLED HI TO +5V
3- add 2n3904 transistor.
EMITTER->gnd /LOW SIDE of sense resistor
BASE-> hi side sense resistor thru a 220 ohm resistor
COLLECTOR->enable pin ( pulled high by resistor from step 1 )

if it's too touchy on a cycle by cycle basis -
add a select-by-experiment averaging capacitor
from COLLECTOR to ground to keep glitches from interfering
where T=RC starts with 6.8k and your capacitor value.

this would also create a hiccup sort of safe "retry", w/o
PIC intervention, while hopefully respecting the safe area of the bridge

as you approach or go beyond about 1.8 amps load,
the enable will be pulled low in an instant fold back sort of way -
no pic involved. no duty cycle change.
then i would READ the COLLECTOR volts by PIC to KNOW what is going on in the new hardware based protector.

might just work 4 u
webgiorgio



Joined: 02 Oct 2009
Posts: 123
Location: Denmark

View user's profile Send private message

PostPosted: Mon Sep 29, 2014 2:45 pm     Reply with quote

Mike, note that the shunt is not along the motor (where the current is continuous) but from the bottom rail of the H bridge to GND.

My try sampling the shunt voltage "randomly" did not work. I will stop wasting time on it and go for the hardware solution suggested by asmboy.
The schematic should be like this:



I know this is not an hardware forum....
If R=0.2 ohm, when I=2A there are 0.4V. But Vbesat of the 2N3904 is 0.85V. Shouldn't I use a higher resistor?
Why 220 ohm on the base? (I think that could be 1-10 k as well)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group