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
drone-technolgy:pid-controller [2022/05/17 17:55] 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>
  
drone-technolgy/pid-controller.1652802945.txt.gz · Last modified: 2022/05/17 17:55 by ilgarrasulov001