Go Down

Topic: Arduino hardware pulse counters? (Read 6753 times) previous topic - next topic

kivig

Hi,
I'd like to intercept and count pulses sent to a stepper driver.
That's up to 400Khz (200-300 actually, but I'd like to be higher end proof) on 3 to 6 channels of pulse_pin + direction_pin.

I've read some tutorials on timers but they all concentrate on outpting, like PWM.
Are the hardware timers capable of up/down-counting depending on direction flag?

Assuming this will take most of Atmega2560 performace resources, is there some way of transmitting gathered information to another Arduino? I mean if timers are taken, will anything else still work?

Peter_n

#1
Jan 21, 2015, 12:03 pm Last Edit: Jan 21, 2015, 12:04 pm by Peter_n
Not every pin of the ATmega2560 is available : arduino.cc/en/Hacking/PinMapping2560

A timer can use an input pin, but not automatically decrement or increment.
The direction pin could trigger an interrupt to change the decrement/increment, but that might be too slow.

Also 3 to 6 channels is too much, I think. You need extra hardware that behaves like the hardware of a stepper driver. I have no idea what kind of hardware that could be. Making something with logic requires time to develop, perhaps there is a counter chip with I2C ? I don't know.

When timers are busy, the ATmega "cpu" has nothing to do. In a real situation, often interrupt are generated by the timers, and those interrupts can keep the "cpu" busy.

Robin2

I'd like to intercept and count pulses sent to a stepper driver.
If all you want to do is count pulses you would not need a timer. If you want to time the pulses, that is a different matter.

Perhaps if you explain what you are trying to achieve it would enable you to get better advice.

Can't you use the software that is creating the pulses to keep a count?

You have not said what you mean by "gathered information".

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

jurs

I'd like to intercept and count pulses sent to a stepper driver.
That's up to 400Khz (200-300 actually, but I'd like to be higher end proof) on 3 to 6 channels of pulse_pin + direction_pin.
400 kHz is pretty fast.

As the overhead of interrupt-handling is about 4µs, and 400000*4µs = 1600000 µs= more microseconds than one second has, nothing will work at that speed, if it is based on interrupts.

Without using interrupts I only know about the possibility, to use one timer and pin set up for "counter with external source". So this would be a timer that counts up not from the controller frequency, but from an external frequency on the counter pin.

I don't know, if any of the Arduino boards support twor or more such counters, but UNO and MEGA support just one counter with external source, I think.

Sacha22

Arduino Uno's MCU has 2 independant timer, each one can have its own external clock.
But one is a 8-bit timer and the other is a 16-bit timer so for the first one, you'll have to deal with an over flow at about 1500 Hz: that should be OK to deal with that overflow with interrupts.

kivig

Making something with logic requires time to develop, perhaps there is a counter chip with I2C ? I don't know.
Hmm, guess I'll investigate if such sophisticated exist. Thanks.

I don't know, if any of the Arduino boards support twor or more such counters, but UNO and MEGA support just one counter with external source, I think.
That's disappointing, I thought there are 6 timers 4 being 16 bit on Mega.
As the overhead of interrupt-handling is about 4µs
That's some useful information. Thanks.


You have not said what you mean by "gathered information".
Send current pulse count of all channels to another Arduino.
Perhaps if you explain what you are trying to achieve it would enable you to get better advice.
Can't you use the software that is creating the pulses to keep a count?
I want to make a middle-man device between computer or another G-Code processor, that will compare pulse count to real positions via encoders and measure applied forces to issue stop if anything goes beyond limits with stepper motors or act as "servo processor" in case of DC motors.

--
So performance is a real issue then. Could it be solved with ArduinoDUE? Until now it was lying on a shelf as UNOs and Megas were enough, so picture with it's interrupts and timers is completely black for me :)

Sacha22

That's disappointing, I thought there are 6 timers 4 being 16 bit on Mega.That's some useful information. Thanks.
It has: http://www.atmel.com/Images/doc2549.pdf

I know it's a huge PDF with many pages, but that's actually your Bible as soon as you're trying to push things deeper than what the native Arduino's interface allows.
You always have to refer to your MCU's datasheet when it comes to knowing what your MCU is able to do, and how to do it ;)

Robin2

Send current pulse count of all channels to another Arduino.
You could not possibly send the count value after every single pulse at 400kHz.

Quote
I want to make a middle-man device between computer or another G-Code processor, that will compare pulse count to real positions via encoders and measure applied forces to issue stop if anything goes beyond limits with stepper motors or act as "servo processor" in case of DC motors.
It seems to me that this is a very sophisticated concept that would not be easy to implement even with a fast enough microprocessor.

For example, given that you can't send the count value after every pulse (if the pulses are fast) how can you decide when to send the count values so that they have some meaning for your "protection" system.

The normal arrangement would be for the computer generating the step pulses also to monitor the encoder pulses so that it can immediately see a discrepancy.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Peter_n

It would be a nice addition to the Bus Pirate : a stepper up/down sniffer.
http://dangerousprototypes.com/docs/Features_overview

kivig

#9
Jan 22, 2015, 01:35 am Last Edit: Jan 22, 2015, 01:36 am by kivig
It has: http://www.atmel.com/Images/doc2549.pdf
Thanks.

You could not possibly send the count value after every single pulse at 400kHz.
Of course not :)
For example, given that you can't send the count value after every pulse (if the pulses are fast) how can you decide when to send the count values so that they have some meaning for your "protection" system.
Well, polling at 100Hz would be good enough. Mechanical end will not be able to stop immediately anyway, so even a 50ms lag is not a huge difference for stop signal. Precision in total pulse count however is important. And yes, of course time tolerances are seriously smaller for "servo" idea, but then again - millisecond doesn't change much in the world where object has some mass and inertia :) So if I'll be able to push pulse polling to 400Hz it'll be more than enough.

The normal arrangement would be for the computer generating the step pulses also to monitor the encoder pulses so that it can immediately see a discrepancy.
Yes, but also normal arrangement would mean specialized software and limited flexibility. Here I'd be able to use - PC with any software, GRBL, USB G-Code interpreters etc.

Robin2

Of course not :)Well, polling at 100Hz would be good enough.
......
Yes, but also normal arrangement would mean specialized software and limited flexibility. Here I'd be able to use - PC with any software, GRBL, USB G-Code interpreters etc.
Maybe my mind is still in first gear this morning ...

Even if you count the number of pulses in 10 millisecs and send that number to the PC I can't figure out how you can use that number to decide whether the machine needs to be switched off.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kivig

Even if you count the number of pulses in 10 millisecs and send that number to the PC I can't figure out how you can use that number to decide whether the machine needs to be switched off.
Well the whole idea is:
The pulse count gets sent to another Arduino that monitors some other parameters also and sends a standard emergency stop button command if it notices a discrepancy that is so huge the time lag could not be at fault (probably machine is about to crash) or when particular axis stops for a few milliseconds it is able to see precisely if there is any deviation (workpiece is about to be compromised). In case computer doesn't respond to stop it was probably at fault and Arduino trips the power switch.

kivig

Ok, so timers can't go up/down, and from what I've been able to find it seems DUE has about the same interrupt overhead as Mega.
Guess I'll have to cut on expectation of 400 or even 200Khz to make it work at all.

So I'm thinking about feasibility of setting up an attiny85@8MHz per channel to count via interrupt and report through I2C which it seems to have.
Unfortunately I haven't had a lot to do with I2C so the question is - will interrupts loading processor on 40-80% affect ability to use I2C?

dlloyd

#13
Jan 22, 2015, 05:16 pm Last Edit: Jan 22, 2015, 07:04 pm by dlloyd
Quote
I'd like to intercept and count pulses sent to a stepper driver.
That's up to 400Khz (200-300 actually, but I'd like to be higher end proof) on 3 to 6 channels of pulse_pin + direction_pin.
Try researching the concept of PPM and how it relates to PWM for servo motors.

EDIT: Sorry, was thinking of servo motors.

Robin2

Well the whole idea is:
The pulse count gets sent to another Arduino that monitors some other parameters
Maybe you know exactly how to do this and you are just not telling us. But I have a sneaking suspicion that you don't.

What "other parameters" can another Arduino monitor that can usefully be compared with a pulse count?

If you can detect the pulses as they are sent to the motor and also detect the pulses from an encoder a simple AND logic gate (cost less than £1) could be used to detect a disparity between them - assuming, of course, that the number of step pulses per revolution is the same as the number of steps produced by the encoder. Even if there is a simple multiple between motor and encoder steps you could use logic gates to compare things and trip an alarm or shut-down. You won't have a pulse rate that causes problems for the speed of logic gates.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up