motor control pulse count ?

Hi all

Just a question as a Newbie to arduino . Is it possible to have one encoders step signal as an up counter say on interrupt 0 pin 2 and a second encoders step signal on interrupt 1 pin 3 , then compare the up counts of both signals , if there is a difference then pwm an output to a motor to bring the counters difference value together again ie a kind of pid. Control

I’m trying to achieve sync between a source step pulse and a feedback step pulse.

One encoder is turned mechanically the source or desired signal ,and the other encoder is the feedback or actual count.

Converting the steps to a frequency and measuring the frequency difference is not an option because the source and feedback could be the same frequency however they could be out of sync.

Its similar to stepper signals to a stepper driver to a stepper motor fitted with a feedback encoder, if the stepper motor missed some steps the feedback tells the driver, the driver issues more steps to push the motor to the desired position or volocity.

In our case a stepper system fails due to stall out torque loss with high speeds.

so we will use a dc motor as a replacement with pwm control.

My idea would be to measure the difference in the counts of the encoders , output to pwm to reduce the difference to matching, when they are then reset both the counters to zero and repeat.

I’m thinking that the count within an aduino will have a max count ceiling ?

Very long winded thank you so much for taking the time to read.

I’m not asking for someone to write the code ,just asking from people who are experienced could it be possible to do .

remote_mains: Just a question as a Newbie to arduino . Is it possible to have one encoders step signal as an up counter say on interrupt 0 pin 2 and a second encoders step signal on interrupt 1 pin 3 , then compare the up counts of both signals , if there is a difference then pwm an output to a motor to bring the counters difference value together again ie a kind of pid. Control

That sounds perfectly feasible. The only concern I would have is the number of encoder pulses per second - is an Arduino fast enough ?

How often do you want to compare the two counts ? I suspect it would not be practical to do so for every encoder pulse - but perhaps several times per second.

...R

You'd run a PID loop like that at more like 100 to 1000Hz, off a timer interrupt perhaps, and yes it would use a fair amount of CPU handling the floats, but it should cope. The encoder interrupts could fire at 50kHz and still be handled fine if only using ints/longs.

Thankyou Robin2 and markT

Sorry for the delay I been working away with no internet .

Your input is great .. I would be using speed at 3000rpm with a encoder 1000ppr

If I'm right that calculates to be 50.000ppr from one channel only from each encoder..into the arduino interupt..there will be no reverse run so the channel B on each encoder not used .

I Could I suppose half that to be a 500 ppr encoders instead of the 1000s I intended to use if the limitation of the arduino would be hindering .

As for the arduino doing its calculations ..can it not be continually monitoring .. or does it have to be input sampling or like screenshot at milli or micro second intervals

How quick would an arduino operate taking or reading the input sample..

Many thanks.

My mistake

It would ne 50.000 pulses per second into arduino 50hz

That’s 50khz

Just turned off predictive

3000rpm at 1000ppr is 50 revolutions per second, 50000 pulses per revolution, 200000 transistions per revolution, almost certainly too fast for interrupt handling

So agreed 50000 pulses per second . If using quadrature then yes 4 transitions if using both channels of the encoder . Calcs to 200.000

I get that . I hoped to use only one channel .and to detect on a rising edge only .is that then 50.000 detections / second or 50khz

Forgive my basic knowledge Is 50khz still too fast ?

Maybe I should lower the ppr to around 200 That then would be instead off 50.000 detections it would be 10.000 detections Pee rev or per second

And quadrature 40.000 transitions. Cound the arduino handle that kind of speed .

Earlier MarkT mention running at 100hz to 1000hz .. is that a more realistic operating window .

Then I may require some form of mechanical gearing .

If you don't measure all transitions you will lose lock when reversing direction or at stationary - however if that's not an issue you can just monitor one transition, and 50kHz is just about do-able.

volatile long count = 0L ;

void isr ()
{
  count ++ ;
}

void setup ()
{
  attachInterrupt (0, isr, RISING) ;
  ...
}

long read_count ()  // read the count atomically
{
  noInterrupts () ;
  long res = count ;
  interrupts () ;
  return res;
}

However you will find quite a lot of microseconds are taken up forwarding the interrupt to the isr via the attachInterrupt mechanism - you might get faster response by configuring a pin-change interrupt instead, even though that only allows you to trigger on CHANGE.