First, since steppers are great at positioning (there is no need for a position feedback), you should certainly limit their movement as you've said yourself. I am not sure how the motor shaft is engineered right now, but if it was fixed to the motor, letting it continue spinning would risk damaging the equipment.

Next, 200ms transport delay in your sensor will probably be too slow, otherwise you will need to slow things down a lot in order to slow down the ball itself. Similar to what Rocket Surgeon said, you should simplify the image processing algorithm to calculate the path **only once**, and then quickly calculate only the position of the ball in each frame. If you want to skip this step quickly, find a red ball instead of this one, and then check only the red component in your RGB picture, until you've found a better algorithm.

For the PID control, start with the fact that you actually need two separate PID controllers, one for the east-west motor, the other one for the north-south one. If you have two exact motors, their parameters must be equal.

For a PID controller to act, it needs to know the **error**: difference between the desired position, and the actual position of the ball. X and Y components of this **offset** will be the inputs for two PID controllers (one for each motor). To get the error, you need to have the **desired** position on your path first: a **trajectory**.

To get the trajectory, you need to process the image and get the **path**, as well as its starting and ending point. I am not sure if your algorithm is capable of distinguishing the path from the rest of the board right now, but if not, note that this is an algorithm of its own to handle before continuing. Again, you can skip this part by manually entering the junction points, if you are eager to see some results quickly. In any case, you should be able to define the setpoint speed, and have your software move the **desired** coordinate position over the path, from start towards the end. Obviously, you will start with a low desired speed.

So, before starting with control, you should go through the following checklist first:

- Simplify your image processing algorithm to get faster response
- Create an algorithm which creates a trajectory over your path using a predefined speed
- In each frame:
- Calculate the difference between the trajectory and the ball position
- Pass the delta-X component to the east-west PID, pass the delta-Y to the north-south PID

It may turn out that it is better to create the trajectory **one segment at a time**, and continue with the next segment when that ball ends the previous one. Otherwise, you will need to take care that the ball doesn't overshoot the desired trajectory (which may be hard to accomplish)

Since you changed your question, here is my second answer:

You have your coefficients \$K_p\$, \$K_i\$ and \$K_p\$. The transfer function is \$D(z) = K_p + K_i \frac{T}{2}\left[ \frac{z+1}{z-1}\right] + K_d \left[\frac{z-1}{Tz}\right] =\frac{a_0 + a_1 z^{-1} + a_2 z^{-2}}{1 + b_1 z^{-1} + b_2 z^{-2}}\$.
When you compare the two sides of the equation you will find can calculate everything you need. I assume you already have determined \$K_p\$, \$K_i\$, \$K_p\$ and \$T\$ either by measurement or from the requirements.

Calculate the coefficients

- \$a_0 = K_p + \frac{K_iT}{2} + \frac{K_d}{T}\$
- \$a_1 = -K_p + \frac{K_iT}{2} - \frac{2K_d}{T}\$
- \$a_2 = \frac{K_d}{T}\$
- \$b_1 = -1\$
- \$b_2 = 0\$

Negative exponents for z basically mean a shift. The output is
$$y[n] = x[n]a_0 + x[n-1]a_1 + x[n-2]a_2 + y[n-1]$$

The index \$n\$ is the current value, \$n-1\$ the previous one and so on.

## Best Answer

I have 3 points to share :

1-I will tell you why people mess with D term interms of sign , PID= P + I + D but , D = kd* (error -olderror) , and in position control if you are approaching the set point , always olderror is < error so D term will be negative , and thus it decreases the output and prevent the overshoot.

2-Regarding time , donot divide or use time . since you are using embedded microcontroller use just make a time constant and cancel it from all equations : for example use a timer interrupt to generate 1ms time constant and call your function inside .

3-Why you are using PID for velocity control , 90% of the time PI is enough in speed control , since D here is more like accelaration control .

Hope that helps