How to Calculate Stepper Motor RPM from Encoder Pulses? - TwinCAT Beckhoff

Code:
(* ********* Main routine ************** *)
IF ulint64_currTimestamp100ns > ulint64_prevTimestamp100ns THEN
lreal_rpm = (60e7 / 1024.0) / ULINT_TO_REAL(ulint64_currTimestamp100ns - ulint64_prevTimestamp100ns);
END_IF;
ulint64_prevTimestamp100ns = ulint64_currTimestamp100ns;
Is lreal_rpm PID PV?
How often should the Main routine run?
How often should the PID procedure be run?
 
The scan cycle is 1 ms, and the resolution of the system time is in multiples of 100 ns. ...
1) From the Beckhoff documentation, when the posted code converts the 100ns SYSTEMTIME to string format, the resolution drops to 1ms (see here), so the time has lost resolution, to 1ms, when it converts back to LREAL for the rate calculation.
2) Since the fbGetSystemTime call is issued in the 1ms scan cycle, it is retrieving the current time of the first 1ms scan cycle after the rising edge, not the current time of the rising edge itself, so even if the code was refactored to use a time format that maintained the 100ns resolution, the resolution of the measurement would still be 1ms.

The only way to get (near) 100ns time resolution between two consecutive rising edges would be with an interrupt routine triggered by the rising edge of the input of interest; even then the actual time resolution woudl be dependent on the input hardware's capability i.e. response time.

Another approach would be to sample the time of many consecutive rising edges, and perform a least-squares fit of those data to a line: the slope of that line would be the mean interval per pulse. Even at the scan cycle time resolution of 1ms, the multiple samples would in effect "beat down" the sampling alias noise and yield a resolution better than 1ms; IIRC, the noise reduction is something like the reciprocal of the square root of the number of samples in the fit, but don't quote me on that. This approach also applies a time filter to the measurement, which means this approach pays for the reduced noise by responding more slowly to changes in rate, which would affect the PID parameters used to tune the system.
 
Is lreal_rpm PID PV?
How often should the Main routine run?
How often should the PID procedure be run?
hahaha I assume those queries are for OP.

After a dozen and a half posts, do we ever have enough information in any thread on this forum to be able to answer to answer questions like that?

That said, I think the answer to the first question is yes, but that is because we got lucky and the information appears to be in Post #1, although @Manglemender's last post calls into question if that is what should be the PV.
 
I may be missing something here but Stepper motors are synchronous so if you ask it to do x steps/second then that is what they will do unless you hit pull out torque?

...
I'm not quite sure why you would need a PID loop to control the velocity of a synchronous device but, as others have said, don't execute a PID loop faster than the input variable can change or you will process the same error more than once which will result in over-integrating which, in turn, will give you an inherently unstable system.

I 2nd this. If you are worried, get a gearbox with a larger reduction. You can go as slow as you like. You must have some idea of the speed you want the load the object at. Design to that and use the stepper for what it is designed for.
 

Similar Topics

Good morning, I have a question. I don't know much about ST yet I would like to calculate the average number of products per hour. how do I do...
Replies
22
Views
2,967
Can someone help me with this? I'm no good at SCL - virtually everything I've done so far has been ladder logic. The return value from the...
Replies
13
Views
1,107
I want to calculate the energy consumed from instantaneous power. Can this be done using the TOT block with timebase set to Hour?
Replies
2
Views
692
Hi everyone, I have to calculate the length in millimeters of a glass that enters a transport (I don't know the speed yet) through a barrier...
Replies
15
Views
3,465
I have a pump with ON off status in PLC micrologix 1400 my question is how I can calculate working hours base on ON/off status How I can use the...
Replies
5
Views
2,425
Back
Top Bottom