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 15:02] ilgarrasulov001drone-technolgy:pid-controller [2022/05/18 15:59] (current) ilgarrasulov001
Line 75: Line 75:
 {{:drone-technolgy:arm_radian_scheme.png|}} {{:drone-technolgy:arm_radian_scheme.png|}}
  
 +<file c arduino_code.ino>
 // importing libraries // importing libraries
 #include "MPU9250.h" // sensor library #include "MPU9250.h" // sensor library
Line 92: 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 123: 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 130: 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 179: 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 240: 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 280: Line 266:
   Serial.write(byteData, 4);   Serial.write(byteData, 4);
 } }
 +</file>
  
 +
 +Link to the code in GitHub repository
 +[[https://github.com/eligosoftware/pid_arm_control/blob/main/sketch_apr29a_new_idea.ino]]
  
 === PID Tuning === === PID Tuning ===
Line 298: Line 288:
 The commonly accepted way of tuning is following: The commonly accepted way of tuning is following:
  
-First you start changing Kp coefficient, and Ki Kd are set to 0. P value is proportional to error, and this leads to the oscilation of the system. To decrease the oscilations we can decrease Kp. If we want to react faster to changes, we need to take into account also the speed of changes, or Derivative controller D. Remember its formula +First you start changing Kp coefficient, and Ki Kd are set to 0. P value is proportional to error, and this leads to the oscilation of the system. To decrease the oscilations we can decrease Kp. If we want to react faster to changes, we need to take into account also the speed of changes, or Derivative controller D. Remember its formula: <br> 
 + 
 +D = Kd*de/dt 
 + 
 +$$ D=K_{d}*\frac{\mathrm{d}e(t)}{\mathrm{d}t} $$ 
 + 
 +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|}} 
 + 
 +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]] 
 +  * PID controller code [[https://github.com/eligosoftware/pid_arm_control]] 
 +  * 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.1652792568.txt.gz · Last modified: 2022/05/17 15:02 by ilgarrasulov001