drone-technolgy:pid-controller
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| drone-technolgy:pid-controller [2022/05/17 14:59] – ilgarrasulov001 | drone-technolgy:pid-controller [2023/01/05 14:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 70: | Line 70: | ||
| === Arduino code === | === Arduino code === | ||
| + | |||
| + | Built tool's limitations | ||
| {{: | {{: | ||
| - | { | + | <file c arduino_code.ino> |
| // importing libraries | // importing libraries | ||
| #include " | #include " | ||
| Line 91: | 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 122: | Line 124: | ||
| pinMode(MOTOR, | pinMode(MOTOR, | ||
| pinMode(PIN_POT, | pinMode(PIN_POT, | ||
| - | // set desired | + | // set desired |
| - | | + | |
| Serial.println(" | Serial.println(" | ||
| tme=millis(); | tme=millis(); | ||
| Line 129: | 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 | + | int rot_75 |
| - | // set desired | + | // set desired |
| - | | + | |
| - | 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 | ||
| - | | + | |
| // read input from serial monitor | // read input from serial monitor | ||
| Line 178: | Line 176: | ||
| tme=millis(); | tme=millis(); | ||
| - | // read current | + | // read current |
| - | current_yaw=mpu.getYaw(); | + | current_roll=mpu.getRoll(); |
| - | // error calculation | + | |
| - | // | + | // error calculation |
| - | if ( current_yaw*desired_yaw >=0){ | + | error=desired_roll-current_roll; |
| - | error=desired_yaw-current_yaw; | + | |
| - | } else { | + | |
| - | + | ||
| - | if(current_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 239: | Line 226: | ||
| // print variable values to Serial Monitor | // print variable values to Serial Monitor | ||
| void print_pid() { | void print_pid() { | ||
| - | Serial.print(" | + | Serial.print(" |
| - | Serial.println(current_yaw, 2); | + | Serial.println(current_roll, 2); |
| - | + | ||
| - | Serial.print(" | + | Serial.print(" |
| - | Serial.println(desired_yaw, 2); | + | Serial.println(desired_roll, 2); |
| Serial.print(" | Serial.print(" | ||
| Serial.println(abs(error), | Serial.println(abs(error), | ||
| Line 279: | Line 266: | ||
| Serial.write(byteData, | Serial.write(byteData, | ||
| } | } | ||
| - | } | + | </ |
| + | |||
| + | |||
| + | Link to the code in GitHub repository | ||
| + | [[https:// | ||
| === PID Tuning === | === PID Tuning === | ||
| Line 297: | Line 288: | ||
| The commonly accepted way of tuning is following: | The commonly accepted way of tuning is following: | ||
| - | First you start changing Kp coefficient, | + | First you start changing Kp coefficient, |
| + | |||
| + | 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, | ||
| + | |||
| + | To change kp, ki and kd in our experiment without changing the code, you can enter: < | ||
| + | * " | ||
| + | * " | ||
| + | * " | ||
| + | * " | ||
| + | |||
| + | Here the values should be adapted for your device. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | For more examples of PID controller see nice videos on Youtube in Resources section. | ||
| ==== Resources ==== | ==== Resources ==== | ||
| - | https:// | + | * PID controller on Wikipedia [[https:// |
| + | * PID controller code [[https:// | ||
| + | * PID Tuning [[https:// | ||
| + | * PID Tuning [[https:// | ||
drone-technolgy/pid-controller.1652792396.txt.gz · Last modified: 2023/01/05 14:38 (external edit)