2

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 ;
Jonas
  • 121,568
  • 97
  • 310
  • 388
james
  • 153
  • 1
  • 13
  • I know nothing on NCX, but those 'integral' 'intergral' similar but not identical words... Also the 'int error' is calculated from two float's. Precision is lost, because of rounding or ceilng. – Ripi2 Dec 03 '16 at 01:28
  • 1
    This is not a PID problem. PID only considers that you are getting a finite error over a finite period of time. When you loose the line, (theoretically) the robot goes blind and doesn't know which direction to take. One way to solve it maybe by creating another conditional loop that in-case of no line acquired for the last 'n' loops, the robot keeps on moving in the direction it was last moving in; untill there is some line. This will, however, lead the robot to go on infinitely, unless you are also using obstacle detection (like an ultrasonic sensor in the front). – Atif Anwer Dec 03 '16 at 03:07
  • 1
    @AtifAnwer in case of the Line is lost is more usual to move along sin wave with increasing amplitude to not miss the Line after gap .... – Spektre Dec 03 '16 at 10:29
  • @Ripi2 i'm aware of this . thanks however :) – james Dec 03 '16 at 11:40
  • @AtifAnwer it's sounds to me like the PID last error part but with the correction as the condition in another while loop , and yes i'm having a obstacle sensor . – james Dec 03 '16 at 11:46
  • +1 to the suggestion by @Spektre . Also. Yes, the loop will keep track of the last error and if it grows infinitely or does not reduce at all, then moving in the increasing sine-wave pattern (instead of a straight line) until a line is reached will be the best bet to re-track the line. – Atif Anwer Dec 04 '16 at 02:04
  • Thanks @AtifAnwer , can you please help with translating this into pseudo-code , because i'm not really sure if i got the sine-wave pattern part – james Dec 04 '16 at 12:16

1 Answers1

1

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

Atif Anwer
  • 450
  • 5
  • 13