I am working in a robot that has 2 motors and the speed of each is not the same. So, I use fork barrier sensors to get the number of rotations of each motor, control the speed and then use it to make the robot move for specific distance. I tried PI controller (P =8, I=0.2); the error compensates after about 2 sec to a limit of about 4 to 7 difference between left motor encoder and right motor encoder 'robot still drifts'. I used PID controller (P=1.5, I=0.2, D=0.004) but I cannot reach a satisfying result with it also.

Kindly could you please help me solve this problem?

Thanks in advance

Code: Select all

```
class PIController:
def __init__(self,pk=0,ik=0):
self.proportional_constant = pk
self.integral_constant = ik
self.integral_sums = 0
def get_value(self,error): ##PI Controller
self.integral_sums += error
p = self.self.proportional_constant*error
i = self.integral_constant*self.integral_sums
return p + i
```

Code: Select all

```
import utime
class PIDController:
def __init__(self,pk=0,ik=0,dk=0):
self.proportional_constant = pk
self.integral_constant = ik
self.derivative_constant = dk
self.current_time = utime.ticks_ms()
self.previous_time = 0
self.integral_sums = 0
self.last_error = 0
self.cum_error = 0.0
def get_value(self,error):
self.current_time = utime.ticks_ms()
elapsed_time = (self.current_time - self.previous_time)/1000 #in sec
error_rate = (error - self.last_error)/elapsed_time
self.cum_error += (error * elapsed_time)
p = self.proportional_constant * error
d = self.derivative_constant * error_rate
i = self.integral_constant * self.cum_error
self.last_error = error
self.previous_time = self.current_time
return (p + i + d)
def reset(self):
self.current_time = 0
self.previous_time = 0
self.integral_sums = 0
self.last_error = 0
self.cum_error = 0.0
self.sample_time = 0.00
```