fmas
Posts: 5
Joined: Tue Jun 19, 2018 11:42 pm

Measuring stepper motor distance travelled with PWM

Thu Jun 27, 2019 12:48 am

Hi team!

I'm working on a project in C++ with a stepper motor moving a platform back and forth. I need to be able to change the motor's speed and measure the exact distance the motor (and stage) has travelled. Currently, I can successfully step the motor and change the speed by changing the frequency of the PWM while keeping it at a constant 50% duty cycle, but I can't seem to find an accurate way to measure the distance travelled.

Prior to implementing PWM, I kept count of the number of steps since the motor was permanently set at 32 micro steps and thus kept a fixed step-distance, and this method was highly reproducible and accurate. However, now that PWM is constantly changing the stepping frequency this method isn't applicable anymore.

Does anyone have any ideas on how to overcome this? Thanks in advance!

danjperron
Posts: 3408
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Measuring stepper motor distance travelled with PWM

Thu Jun 27, 2019 1:09 pm

Well you could set a counter to your PWM pulse.

https://www.raspberrypi.org/forums/view ... hp?t=33113


I.M.O. you should add a microcomputer for that like an arduino or a pic. Use the PWM has a clock and the FWD/REV to count up or down.
The microcomputer could report the exact position using serial or I2C. A PIC12F1840 is under $2 CA and it could be programmed using the PI.

https://github.com/danjperron/burnLVP

For an I2c example using this PIC I do have a 10bit A/D converter. Make a counter instead of a A/D is way simpler.
https://github.com/danjperron/A2D_PIC_RPI


B.T.W. I'm sure that an Arduino code exit to set to a specific count with ramp for stepper motors.

fmas
Posts: 5
Joined: Tue Jun 19, 2018 11:42 pm

Re: Measuring stepper motor distance travelled with PWM

Tue Jul 23, 2019 11:43 pm

I have considered counting the pulses, however, the number of pulses still doesnt help me accurately measure the distance travelled since i don't know the distance per step. Previously, I was using the number of steps to determine distance travelled because the motor had a fixed step distance, but with PWM this is no longer feasible.

Would a microcontroller/chip help in measuring distance from the starting point?

Thanks for you help!

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Measuring stepper motor distance travelled with PWM

Wed Jul 24, 2019 12:16 am

Not knowing the exact physical setup of your drive I am making a few assumptions here, though hopefully the concept comes through well enough.

As you are using Stepper motors the thing to do is count steps. If you take the effective drive diameter of your drive wheels (assuming belt driven or similar), and the number of steps per revolution, you can fairly easily calculate the distance per step.

This is the advantage of using stepper motors, knowing the distance based on how many steps. I do not know how you are using PWM with steppers, and I work with stepper motors every day.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

Heater
Posts: 13694
Joined: Tue Jul 17, 2012 3:02 pm

Re: Measuring stepper motor distance travelled with PWM

Wed Jul 24, 2019 3:12 am

fmas,

It's not clear to me exactly what you are doing there. Can I try and summarize it like this:

1) You had been using a simple "bang-bang" scheme to step the motor. Such that counting the output pulses gave a direct measurement of the number of motor steps and hence distance traveled.

2) You are now using some kind of "micro-stepping" scheme, driven by continuously changing PWM signals. This clearly makes determining distance traveled from the raw driver output signal pulses difficult if not impossible.

Right so far?

So my question is: How are you generating that micro-stepping, continuously variable PWM drive signal?

I ask because if you are doing it with software running on the Raspberry Pi, somewhere in that software it knows how many complete steps and micro-steps it is commanding. It knows the movement distances it is commanding. You only have to find where that is in the code and make use of it.

If you are not doing it with software running on the Raspberry Pi then we will need to know how exactly before being able to answer your question.
Memory in C++ is a leaky abstraction .

danjperron
Posts: 3408
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Measuring stepper motor distance travelled with PWM

Wed Jul 24, 2019 3:22 am

Well microstepping with specific step using PWM is possible! I did it on a old post.

https://www.raspberrypi.org/forums/view ... =microstep

fmas
Posts: 5
Joined: Tue Jun 19, 2018 11:42 pm

Re: Measuring stepper motor distance travelled with PWM

Tue Aug 13, 2019 11:40 pm

Thank you all for your help!
Heater wrote:
Wed Jul 24, 2019 3:12 am

So my question is: How are you generating that micro-stepping, continuously variable PWM drive signal?

I ask because if you are doing it with software running on the Raspberry Pi, somewhere in that software it knows how many complete steps and micro-steps it is commanding. It knows the movement distances it is commanding. You only have to find where that is in the code and make use of it.
Heater,

I'm using the PWM pin on the pi (pin 12, or 1 in the wiringPi pinout) to pulse the step pin on the microcontroller. By keeping it constant at a 50% duty cycle, the step pin goes high and low evenly so the motor moves. Meanwhile, I'm changing the frequency of the PWM cycle to alter the motor's speed. I did try to use the software PWM library in wiringPi but it didn't work as well for me.

With counting steps, I'm just not sure if the motor is always travelling the same step distance as the frequency changes? Does anyone have any idea about this? Does it just revert back to its default microstepping size?

danjperron
Posts: 3408
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Measuring stepper motor distance travelled with PWM

Wed Aug 14, 2019 12:03 am

I did something like you want to do on a test. More or less I specify the number of microstep to do. Then it should be easy to implement a stop function and count the number of step done or check on the fly the number of step.

https://www.raspberrypi.org/forums/view ... it=stepper

Return to “C/C++”