I have been working on a self balancing robot and there are quite a few parameters and one of them is loop execution time (t_loop henceforth). To my understanding, the smaller t_loop, the better it is for the control algorithm. But, since the motors also need to be given sufficient time to react, we cannot make it very small. Also, making t_loop large so that motors have sufficient time to react will make the system unstable. So there has to be some optimum value. I started with 5 milliseconds and started increasing it. At 5 ms the robot is quiet jerky (but it manages to balance). Between 10 and 20 ms, the vibrations become less. The question is how to find the optimum value of t_loop or control loop execution time.
The answer is not a trivial one. Because,
- the system dynamics depends on multiple sub-system dynamics (including software, sensors, and actuators (motors in this case))
- finding the optimum value for t_loop, requires finding the best possible controller for all (i.e. several) t_loop values.
It's worth considering the following approach:
- assume that the motors have the slowest dynamics (hence they are the bottle-neck for the problem)
- the bandwidth of the motors will define the frequency at which they can be successfully commanded (you can utilize an open-loop test code, e.g. a code that runs sine waves for the motors in different frequencies, and the decide when the motors are not able to keep up with the commands)
- the frequency that the motors can receive commands is the control frequency (fm). It is best practice to use a 10*fm frequency to control the motor in this case. ref: https://controlguru.com/sample-time-is-a-fundamental-design-and-tuning-specification/
Further than this, you will need to refer to modern-control literature for the following terms:
- system bandwidth,
- crossover frequency,
- nyquist theorem
But, I think a deeper dive into control theory is not necessary at this moment.
A normal game engine at the desktop PC runs with around 40 frames per second. This is equal to 1000/40=25msec of waiting time after a tick. Fasten up the system brings no advantage but it will only increase the CPU consumption. Different from the framerate the motor control has to be executed. A motor needs some time to speed up and stop, especially in embedded systems. What is used in robotics operating systems like VXworks is a concept called realtime control. This means, that the operating systems provides threads. A thread is dropped from the main program and runs in the background until the task is done.
For example, the motor needs 1 second to stop. This means, the task in which the stop signal is send to the motor has a length of 1 second. The motor stop task is activated at timeframe 1000 and runs until the timeframe 1040 (1 second * 40 fps = 40 ticks). .During that time the main program doesn't stop but it runs also. The concept of threading is used at desktop PCs heavenly for creating GUI applications but it is useful for controlling robots as well.