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.
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?
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.