algorithmroboticspid-controllernxtnxc

implementing PID algorithm in line following robot


I'm working on a small project with NXT mindstorms set. My intention was to build a Robot that can follow a line very smoothly and as fast as possible. Therefore after a small research I found the PID algorithm and I was able to understand and implement the algorithm into a NXC code. The robot has just did everything right according to the algorithm but when the line is interrupted (gaps) the robot loses the line and can't get back to it. The thing is that when the gap is up to 9cm it is fine he can get back but in 10 he just loses the line. I'm using one light sensor. Is there any way that I can adjust the PID code to work with this Problem?

My Code:

    // kd ,ki kp are also defined 
    task main()
   {
    int error = 0;
    float previous_error = 0;
    float setpoint = 0;
    float actual_position = 0;
    int integral = 0;
    float derivative = 0;
    float speed=50;
    float lasterror = 0
    float correction = 0
    float fahrenA = 0
    float fahrenC = 0
    SetSensorLight(IN_2);

    SENSOR_TYPE_LIGHT_ACTIVE;


 while(true)
   { 
      actual_position  = LIGHTSENSOR;
      error = setpoit -  actual_position ;
      integral = error + intergral ;
      derivative = error - previous_error;
      correction = (kp * error )+ (ki * intergral) + (kd  * derivative          );
      turn = correction / 100;
      fahrenA = Tp + turn;
      fahrenC = Tp – turn; 
      OnFwd(OUT_A,fahrenA); 
      OnFwd(OUT_C,fahrenC);
      previous_error = error ;

Solution

  • By a sine-wave pattern, we mean that the robot may follow the following path to increase the chances of re-capturing the line after losing it. You can code the path using simple if-else and timers/tachometer readings. (Thanks to @Spektre for the suggestion!):

    enter image description here