User Tools

Site Tools


drone-technolgy:pid-controller

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
drone-technolgy:pid-controller [2022/05/17 17:42] ilgarrasulov001drone-technolgy:pid-controller [2022/05/18 15:59] (current) ilgarrasulov001
Line 93: Line 93:
 float error; float error;
 float ki_error_range=10; float ki_error_range=10;
-float desired_yaw=38.0;+float desired_roll=38.0;
 float pError=0.0; float pError=0.0;
-float current_yaw=0.0;+float current_roll=0.0;
 float PID_p, PID_i, PID_d, PID_total; float PID_p, PID_i, PID_d, PID_total;
 // time parameters for setting the frequency of reading sensor values // time parameters for setting the frequency of reading sensor values
Line 124: Line 124:
   pinMode(MOTOR, OUTPUT);   pinMode(MOTOR, OUTPUT);
   pinMode(PIN_POT, INPUT);   pinMode(PIN_POT, INPUT);
-  // set desired yaw to the value, read from potentiometer +  // set desired roll to the value, read from potentiometer 
-  set_desired_yaw();+  set_desired_roll();
   Serial.println("Setup finished");   Serial.println("Setup finished");
   tme=millis();    tme=millis(); 
Line 131: Line 131:
  
  
-void set_desired_yaw(){+void set_desired_roll(){ 
 +  // -55 lowest, 20 max, 0 center, total 75
   // read potentiometer value, range is [1024-10]   // read potentiometer value, range is [1024-10]
   int rot_1024= analogRead(PIN_POT);   int rot_1024= analogRead(PIN_POT);
-  // convert to 255 units system +  // convert to 75 units system 
-  int rot_255 255*(1024 - rot_1024)/1014; +  int rot_75 75*(1024 - rot_1024)/1014; 
-  // set desired yaw +  // set desired roll 
-  if (rot_255<=141){ +  desired_roll=rot_75-55;
-    desired_yaw=38+rot_255; +
-    } +
-    else { +
-      desired_yaw=-179+(rot_255-141)    +
-      }+
   }   }
  
 void loop() { void loop() {
   // set desired yaw in accordance to the last read from potentiometer   // set desired yaw in accordance to the last read from potentiometer
-  set_desired_yaw();  +  set_desired_roll();  
  
   // read input from serial monitor   // read input from serial monitor
Line 180: Line 176:
             tme=millis(); // set tme variable to current time in milliseconds             tme=millis(); // set tme variable to current time in milliseconds
  
-            // read current yaw angle +            // read current roll angle 
-             current_yaw=mpu.getYaw(); +             current_roll=mpu.getRoll();
-             // error calculation+
                            
-             // if current yaw and desired yaw have the same signs +             // error calculation  
-             if ( current_yaw*desired_yaw >=0){ +            error=desired_roll-current_roll;
-             error=desired_yaw-current_yaw; +
-              } else { +
- +
-                if(current_yaw> 0){ +
-                  error= 179  -current_yaw + 179 - abs(desired_yaw); +
-                } else{ +
-                    error= -179  -current_yaw -( 179 - abs(desired_yaw)); +
-                  }+
                                          
-                } 
-         
             // P calculation                 // P calculation    
             PID_p = kp * multiplier* error;             PID_p = kp * multiplier* error;
Line 241: Line 226:
 // print variable values to Serial Monitor // print variable values to Serial Monitor
 void print_pid() { void print_pid() {
-    Serial.print("Current Yaw: "); +    Serial.print("Current Roll: "); 
-    Serial.println(current_yaw, 2); +    Serial.println(current_roll, 2); 
- +     
-    Serial.print("Desired Yaw: "); +    Serial.print("Desired Roll: "); 
-    Serial.println(desired_yaw, 2);+    Serial.println(desired_roll, 2);
     Serial.print("Absolute error: ");     Serial.print("Absolute error: ");
     Serial.println(abs(error), 2);     Serial.println(abs(error), 2);
Line 281: Line 266:
   Serial.write(byteData, 4);   Serial.write(byteData, 4);
 } }
- 
 </file> </file>
  
Line 312: Line 296:
 Here, the smaller the period between measurements or the bigger the change in error, the bigger will be the final D component. Here, the smaller the period between measurements or the bigger the change in error, the bigger will be the final D component.
  
 +And finally, you can tune the I component. It affects the final PID value only when error is within the given range relative to desired yaw value. For example, if the error is too small for proportional P controller and the error didn't change since the last measurement, the PID will be zero without I controller.
 +
 +To change kp, ki and kd in our experiment without changing the code, you can enter: <br>
 +   * "kp=4" for P coefficient value
 +   * "ki=2" for I coefficient value
 +   * "kd=1" for D coefficient value 
 +   * "kier=20" for I controller range value 
 +
 +Here the values should be adapted for your device.
  
 {{:drone-technolgy:serialmonitor_input.png|}} {{:drone-technolgy:serialmonitor_input.png|}}
 +
 +For more examples of PID controller see nice videos on Youtube in Resources section.
  
  
 ==== Resources ==== ==== Resources ====
  
-  * [[https://en.wikipedia.org/wiki/PID_controller]] +  * PID controller on Wikipedia [[https://en.wikipedia.org/wiki/PID_controller]] 
-  * [[https://github.com/eligosoftware/pid_arm_control]] +  * PID controller code [[https://github.com/eligosoftware/pid_arm_control]] 
-  * [[https://www.youtube.com/watch?v=IB1Ir4oCP5k&ab_channel=RealPars]]+  * PID Tuning [[https://www.youtube.com/watch?v=IB1Ir4oCP5k&ab_channel=RealPars]] 
 +  * PID Tuning [[https://www.youtube.com/watch?v=JFTJ2SS4xyA&t=612s&ab_channel=Electronoobs]]
drone-technolgy/pid-controller.1652802173.txt.gz · Last modified: 2022/05/17 17:42 by ilgarrasulov001