MotorPositionController "busy"

Comments & issues
Supporting Windows 8 or newer
Post Reply
graeme
Fresh meat
Posts: 3
Joined: Wed Apr 16, 2025 3:23 am
Contact:

MotorPositionController "busy"

Post by graeme »

I have been fiddling with a number of different ways to control a motor on DCC1100, but I can't find a way to say that the motor is still "busy" - obviously I can compare the current position with the demand etc. but that is slightly messy as it will always seem to end up one or two counts off from the demand & stop there

Is there a good motif people use to say the movement is done, or is it the same kind of thing I am doing here? I also tried using the duty cycle but that does not quite go to zero at the end of the move.

I have not yet tuned the motor away from defaults, beyond inverting the P, I, D values...

Almost what I am looking for is:

Code: Select all

    mpc.setEngaged(True)
    mpc.setTargetPosition(target)
    while mpc.isBusy():
        time.sleep(0.1)
Or similar: it is the

Code: Select all

mpc.isBusy()
I am trying to resolve here.
User avatar
mparadis
Site Admin
Posts: 676
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: MotorPositionController "busy"

Post by mparadis »

Using the DutyCycle update event is the simplest way to check if the motor is still moving. If your dutycyle is still non-zero at the end of the move, there are a few things you can do:
  • Set a deadband- this creates a buffer zone on either side of the target position where the motor will stop moving, which will prevent the oscillations that are causing your DutyCycle to never settle to zero.
  • Tune your PID parameters- If you tune your parameters, you should be able to get the motor to approach the target gently enough that it does not overshoot, and your DutyCycle should settle to zero.
  • If the above two options fail, you can probably just check the value of DutyCycle and treat any movement beneath a certain threshold as counting as "stopped" (or do the same when checking target position against actual position).
graeme
Fresh meat
Posts: 3
Joined: Wed Apr 16, 2025 3:23 am
Contact:

Re: MotorPositionController "busy"

Post by graeme »

Thanks, this feels fairly similar to what I was doing - but I did stub my toe on the fact that the duty cycle starts at effectively zero, so when you set a demand position, immediately after the call the duty cycle is _still_ zero until things start moving - would need to add a short delay which felt untidy, or use the union of current position close to demand position _and_ duty cycle being zero-ish to determine if it was done.

I was just wondering if there is a more elegant way to do it than this, but it would appear not (not suggesting that there _should_ be, more that there could have been)
jdecoux
Engineering
Posts: 181
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: MotorPositionController "busy"

Post by jdecoux »

Unfortunately, as you have noticed, implementing an all-encompassing "isBusy" function on the motor position controllers is not as straightforward as checking any one set of conditions, as the motor being "busy" will mean different things in different systems.

For the greatest certainty the motor is done moving, I recommend checking the motor's returned position and velocity (velocity calculated from change in position over time) and decide they are close enough to the target position and zero, respectively.

For clarity of future readers of this post: checking if duty cycle is exactly zero, or even below a given threshold is not really a good solution here. Duty cycle is only a function of how hard the motor is being driven at a given moment, and could drop below a threshold for reasons other than arriving at a target position (such as external forces, or changing directions). You should trust the motor's current position and velocity as the actual indication that the motor has arrived.
graeme
Fresh meat
Posts: 3
Joined: Wed Apr 16, 2025 3:23 am
Contact:

Re: MotorPositionController "busy"

Post by graeme »

Thank you for the clarity, I will take a look at the motor velocity, position readbacks
Post Reply