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 17:55] – ilgarrasulov001 | drone-technolgy:pid-controller [2023/01/05 14:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| 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, | pinMode(MOTOR, | ||
| pinMode(PIN_POT, | pinMode(PIN_POT, | ||
| - | // set desired | + | // set desired |
| - | | + | |
| Serial.println(" | Serial.println(" | ||
| 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 | + | 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 180: | 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 241: | 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 281: | Line 266: | ||
| Serial.write(byteData, | Serial.write(byteData, | ||
| } | } | ||
| - | |||
| </ | </ | ||
drone-technolgy/pid-controller.1652802945.txt.gz · Last modified: 2023/01/05 14:38 (external edit)