current sence with BTN7960B with PWM

Hi
I have a MegaMoto shield that uses a Infineon BTN7960B chip. I'm using 2 of these shields as a Hbridge for my 2 drive motors.
I want to detect stall of the motors to temporary increase the voltage (PWM) to overcome hurdles (mole piles).
I used analog read on the sensing pin. But This came with wierd results. Plenty of 0's and very different values.
I've contacted MegaMoto and got a response (copied below) which does not really lead me to a solution. (Frankly I think they never used the sensor pin with anything else then PWM 255 in half bridge setup; and they don't care enough to spend any effort)
So it looks like I will have to dig into this myself but I have no clue where to start.
On the MegaMoto schematics (I think) I see that the IS pin of the BTN760B is a current source that has a resistor of 634 connected to the ground.
The resistor makes it a voltage (U=R*I) which can be sensed.
However as I PWM the BTN7960B there is sometimes a current and sometimes not. This explains the 0 reads.
I tried doing 20 reads; take the max and multiply withe the PWM rate (maxPWMRead * PWMValue/255) however this seems to be unreliable. I mean the left engine does it fine but the right engine does not notice it is stalled at all.
So I will have to go with the advice of Chris of Megamoto to use PWM related interrupts. I have already read about interrupt firing when a pin goes high or low but I have never read about PWM related interrupts. So a pointer explaining this to a dummy (don't tell me to read the data sheet without at east a link to a datasheet, please =()
Chris also mentions a sawtooth graph of the current. I know quite a bit about engines but PWM was not part of my training. Anyone has a reference to a doc explaining this?
The explanation on PWM registers is completely beyond me. Anyone knows a pointer to a more extensive explanation?
And finally: Chris states I need to read the value just before cut off. Is this at all possible with Arduino?

Best regards
Jantje

HI Jan,

Please read the ATMega datasheet on the timers and the interrupts available. You can see how to set up an interrupt routine in the Arduino documentation and examples. What you want to do is trigger on either the timer roll-over (1/2 of the on time) or at the second compare match when the PWM wave shuts off. The timer counts up from zero until it matches the value in the PWM register (compare #1), then it turns on the PWM. The current in the load starts to climb at that point. The roll-over point is when the timer reaches the max value and starts to count down again. The compare match when counting down is when the PWM shuts off. This will be the point of highest current. So this is when you want to trigger an A/D conversion. This will take some reading on your part but isn't really that hard if you follow the examples.

Regards,

Chris

On 5/26/2012 12:07 PM, Jan Baeyens wrote:

Chris
I don't think I fully understand and I surly don't know what to do. Any examples or documentation on this anywhere that can help me out.
I have tried reading the current 20 times in a row and take the max. This figure is probably the top of the sawtooth because it is way to much.
Best regards
Jan

On 26-May-12 21:02, Chris Baron wrote:

You are probably sensing when the PWM is off. Also the current reading is a sawtooth for an inductive load like a motor so where you sample on the wave will affect the reading.

You can use timer interrupts to trigger an A/D conversion at a common point in the PWM cycle.

Regards,

Chris

On 5/25/2012 7:42 AM, Jan Baeyens wrote:

Chris
Thanks for the quick answer.
I have been testing in the mean time without the jumper and found that I get "weird" readings. Something like:
correct reading
0
0
0
0
correct reading.
0
...

Could this be caused by the PWM? (I'm currently use 112 as PWM value)
Can I fix this with a capacity? If so what is the best value?
I also read that Arduino can not handle high impedance. Could this be the reason?
I'm basically clueless and need this to work.
Best regards
Jantje

PS some background on what I want to do:
I have 2 usages in mind
I'm building a mow robot for rough terrain I have a battery of 24 Volts; 2 (or 4) 12 volt drive engines and 1 (or 2) 24 Volt mow engines
I want to sense the current used by my drive system and in case I notice the drive engine is stalled (high current > 5 Amps) I want to increase the power for a short while to "get started" (Basically I want to put more than 12 V on a 12 Volt engine) If that didn't fix the stalling I want to shut down the engine; let it cool and drive backwards;
For the 24 volt 100 watt mow engine(s) I want to sense the current to save energy. I want to run the engine on a lower voltage. In case the grass is long the current goes up and I want to increase the voltage. When the current drops I want to go back to the original voltage.
I also want to use the mow current as a steering to the robot (Where is the grass long)

The first usage is written (till the shut down) but is unreliable because of the bad readings. Even with a filter I can not fix it as 4 times 0 in a row will influence the current to hard to have a reliable result.

PPS For your information: I buy shields because I have little to no electronics experience.

An update
I have been reading through the ATMEGA datasheet an through the http://code.google.com/p/arduino-pinchangeint.
From there it seems possible but there is no support for Mega in the pinchangeint library. So this is going to be a long trip.

I still have my doubts about reading the analog pin "correctly" during the interrupt. Has anyone some experience with this?

The more I read the more I would like to have a hardware solution. Can anyone help me with that?
I could remove the used resistor and add some components. As far as I know electronics a cap and a resistor would do. I have however no idea on which capacity or resistor value I need. Remember: I'm not looking for precision here. If I can get it right at +-0.1 Amp with amps higher than 2 Amps that is fine for me.
I prefer adding some hardware to reading analog pins during interrupts. After all there are 4 interrupts to catch on PWM pins. It is hard for me to believe this will not impact performance.

Any help is welcomed.
Best regards
Jantje

You probably solved your problem by now.
However a simple way is to tie both current sense resistors together so that whichever bridge is active you will always get a non zero voltage on the current sense resistors now paralleled.
The current sense voltage may vary a bit due to the motor inductance but you can average it.
The scaling of the current sense will be halved due to the parallelling.
The L298 works this way as the current sense resistor is shared by the two halves of each bridge.