How to implement velocity control for RC servo on Particle Photon

In my project I am using a Photon to control a servo via packages sent over the network. Thus, both the package handling and the servo control have to run in the main control loop. However, I would like to control the servo using not only position commands, but also via velocity commands e.g. move to position X at velocity V.

To do this, I was trying to measure the time passed during a loop iteration, and then update the position by Velocity*delta_time until the position is reached.

Since, I am not an expert on micro controller programming, I wanted to ask first if there is a preferred way of doing this on a Photon?

How are you measuring the speed and position of your servo?
If you had a digital rotary encoder of some kind it would probably be better to use interrupts for the timing.

This is open to discussion :wink:

1 Like

How are you measuring the speed and position of your servo?

My Servos provide potentiometer position feedback that I read over an analog pin. (I would have to take a derivative to find the speed)

Is there example code of using interrupts to time a servo that that you could recommend?

Now, I did some research, and saw that if I were to for example update a rotary encoder, I might want to use interrupts to ensure the encoder value gets updated in a timely fashion. However, since my feedback is from a potentiometer, I can read the up-to-date value at any point in time.

I just learned the difference between external and internal interrupts thanks to the lovely people over at SparkFun. From this information, I gather that what I would need to ensure a timed operation of my actuator, I would require a Time Interrupt or as it’s done in the Photon, a software timer.

So my updated question is as follows, since my servo only allows position control, how do best create the illusion of velocity control?

For precise timing you can use the SparkIntervalTimer library.

I just read on the GitHub of the SparkIntervalTimer library that for most applications it is preferable to use a variable to track the elapsed milliseconds.

I guess this is an ignorant question, but what is the design decision that would drive me to use interrupts over mere elapsed millisecond variable?

That’s mainly a question of how much accuracy your use-case demands.
A secondary factor may be how many other interrupts or low-level tweakery your application uses as this might impact the accuracy of the millis() counter and consequently your own timing using that.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.