Please I'm working on a quadcopter, and I have to made my own flight controller based on arduino uno.
First of all please how can I get a filtred angular velocity and linear acceleration from the MPU6050, is the following code give us the angular velocity in deg/sec and the linear velocity in m/s² ?

accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

and is this pid value in this following code is right ?

  accel_reading = ax;
  accel_corrected = accel_reading - accel_offset;
  accel_corrected = map(accel_corrected, -16800, 16800, -90, 90);
  accel_corrected = constrain(accel_corrected, -90, 90);
  accel_angle = (float)(accel_corrected * accel_scale);

  err = angle_setpoint - accel_angle  ;

  P = Kp * err ;
  I += (Ki * err) ;
  D = Kd * (err - errp);

  pid = P + I + D;

Thank you.

2 Answers 2


How can I convert PID correction values to an PWM brushless command?

you have to do that yourself. you have to figure out first how to correct a pid value directionally. for example, should you increase the duty cycle if pid is positive or negative?

after that, you will need to map the pid value to a range for your pwm dc.

basically, it is application / task specific, and you are in complete charge.


You convert PID outputs to process outputs by selecting appropriate Kp, Ki, and Kd constants -- they translate the error components into a process output. For example, if you want 10 degrees of error to give you a full-scale analogWrite() adjustment, set Kp=255.0/10. Ki and Kd will depend on the (unspecified) sampling interval. If the measurements are noisy, you can use the Ki term to do some filtering work, but if you filter it prior to the PID, it will reduce the performance of the PID.

Code-wise, you are missing some sort oferrp=err; code, you need more than acceleration in the x direction to calculate an angle, (atan2(az,ax) or some such, depending on orientation), and your PID scheme doesn't protect against common problems like out-of-range, integral windup, or inconsistent sampling interval. You should investigate the http://playground.arduino.cc/Code/PIDLibrary for a more robust implementation than you've half-coded above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.