CanHaptics Lab 4: Controlled actuation using PID

Ahmed Anwar
5 min readApr 2, 2021

Lab 4: Code Repository

1. Run the code and try out the P controller.

How does it feel? What happens when you change the target position? What does changing the P parameter do? Do you notice any problems?

In this section, I tweaked the value of the proportional gain while keeping all else constant. My first impression was the system was very unstable and often the knob of the end effector would go flying off the haply due to rapid oscillations of the end effector. A presentable example can be seen below when I used a P value of 0.05.

p = 0.05

After my first interaction with the system, I realized that the system is very sensitive to the value of proportional gain so I started gradually decreasing the value of P and experienced stability in the system when the value of P was 0.01 or 0.02.

p = 0.01 & p = 0.02

Few issues I faced in this part were that I had to use my hand most of the times to stabilize the end effector as it was not reaching the target. Using my hand I experienced a greater force when the value of P was greater and vice versa. Also when the value of p was increased from 0.01 to 0.02, the speed of the end effector increased but the accuracy did not so I deduced at the end that there is a tradeoff between accuracy, stability and accuracy by tweaking proportional gain alone.

2. Add the D component to your controller.

How does this change the behavior of the Haply? Are there any problems?

After adding the D component the system attained stability. In addition, in my experience, the system was not that much responsive to the values of D so I was generous in increasing the value of D. However, I was skeptical about the value of P that I had to use in this step. Therefore, I started off with P = 0.01 but to my surprise, increasing it to 0.04 did not adversely affected the stability as well.

P = 0.04, D=0.99
P = 0.04, D=1.17

After trying different combinations of values of P and I, I achieved the greatest stability, speed and accuracy when p = 0.04 and = 1.17.

3. Add the I component to your controller.

How does this change the behavior of the Haply? Can you create a stable system that reaches the target?

Adding the Integral component was the final piece of the puzzle in the system. After using it, I would refer to it as autonomous missile that could correct its trajectory and eventually reach the target with very high accuracy. Although, it did take some time to correct itself.

P = 0.01, I = 0.02, D = 0.99

4. Implement path tracking

What happens when you old the handle ? How is it affected by the PID parameters?

In this part, I added the circular path following functionality in the system. To insert a circular moving target in the system, I utilized an online resource that provided an interactive processing environment to test the circular path. I had to play around with the radius and speed parameters to create a presentable path. The following code was added to the Setup function.

Code for moving in circular path
P = 0.06, I = 0.02, D = 0.99

After implementing a circular moving target, I had to tweak the system parameters. Initially I had started with the parameter configuration of my stable system, but I discovered that the end effector was very slow and was not keeping up with the moving target so it was constantly adjusting itself. Therefore, I increased the value of P to 0.06 which allowed the end effector to keep up with the moving target. Finally, when I placed my hand on the end effector, I was able to experience some resistance and when I released it the end effector corrected its path and started moving in the circle.

5. Play with the controller update rates, and with introducing delays.

Hint: use the “looptime” parameter. How does this change the system? What happens if you sample faster or slower? What happens if it’s random (this would require some coding)?

In this part, I tweaked the looptime parameter. The default value was 500 which was working fine. Using values greater than 500, caused the system to be unstable as it was moving too quickly. On the other hand using value of 250 did not significantly affect the stability of the system. However with value of 250, I felt that was a lag in the system. Finally, I also experimented using random looptime using the following code. When the looptime was random, as expected with larger values the system faced instability and with lower it got more stable.

Unstable system with > 500 looptime value


I really enjoyed working in this lab. I also added talcum powder on the base to reduce friction as I remembered that back in the days we used to add powder while playing carrom board. This significantly reduced the friction especially in the path following section.

This lab was my first introduction with the PID control loop. Moving the haply autonomously on a circular path was very fascinating to me and I can imagine more complex path following mechanisms that can be developed using the same codebase.

One downside, I feel is that there was lack of visual aid to diagnose the system. It was mostly by trial and error so I felt that having visual aid in the form of graphs or numbers might be helpful in tuning the system.