I'm learning to work with servo motors. The transform function in motion.h
template<typename T> void transform()
seems to be a key part of generating smooth motions, but I have some trouble understanding the code. For example, what's dutyAng where the cos comes from.
It would be great if there were some resources explaining the formulas. I'd appreciate it if anyone could give a hint.
Hi Timothy,
Thank you so much for your detailed response. This answers my questions perfectly and I really want to give it a 10 out of 3. It's so nice to have some people to break down the code for me.
After reading this, I tried to understand how the angle updates are done. Here's my understanding,
For convenience, let's call the target angle $b$ and the current angle $a$, in
diff[i - offset] = currentAng[i] - target[i - offset] * angleDataRatio;
we compute the difference as $a-b$. In the following code
for (int s = 0; s <= steps; s++) { for (byte i = offset; i < DOF; i++) { float dutyAng = (target[i - offset] * angleDataRatio + (steps == 0 ? 0 : (1 + cos(M_PI * s / steps)) / 2 * diff[i - offset])); calibratedPWM(i, dutyAng); } }
the angle updates are done in $steps$ steps, and each update computes dutyAng as
$$
b + (1 + cos(\pi*s / steps)) / 2 * (a-b)
= (1 - cos(\pi s / steps))/2 * b + (1 + cos(\pi s / steps))/2 * a
$$
which is a weighted average of $a$ and $b$. However, the transition from $a$ to $b$ is not equally spaced. I assume this is a trick to make the motion more life-like.
Please correct me if it's not correct or something is missing.
And thank you again, Timothy.
(PS: It would be great if the forum can add support for LaTeX)
Best,
Jiarui