Sensor Values are drifting

So I wrote a sketch using Dr Owens tutorial to see the sensor values the ardupilot 2.6 was outputting. When I upload the code to the ardupilot and run serial monitor, my pitch goes to -2.1 and stays there. My roll goes to -1.4 and stops there. My yaw is the worst. It just keeps incrementing towards negative. I do not move the APM at all. It is completely stationary when I initialise it. Could it be the code I'm using. I also took off the cover of the board and put it back on. Could that be it. Has anyone else had this problem before?

PS, I using the libraries Dr Owen posted on his "How to proram your own Autopilot" tutorial.

Also, my Pitch is confined to -88<P<88. 

I wasnt having any issues yesterday

My code is below


#include <AP_Common.h>
#include <AP_Math.h>
#include <AP_Param.h>
#include <AP_Progmem.h>
#include <AP_ADC.h>
#include <AP_InertialSensor.h>

#include <AP_HAL.h>
#include <AP_HAL_AVR.h>

// MPU6050 accel/gyro chip
AP_InertialSensor_MPU6000 ins;

const AP_HAL::HAL& hal = AP_HAL_AVR_APM2; // Hardware abstraction layer

void setup()

// Turn off Barometer to avoid bus collisions
hal.gpio->pinMode(40, GPIO_OUTPUT);
hal.gpio->write(40, 1);

// Turn on MPU6050 - quad must be kept still as gyros will calibrate

// initialise sensor fusion on MPU6050 chip (aka DigitalMotionProcessing/DMP)
hal.scheduler->suspend_timer_procs(); // stop bus collisions

// We're ready to go! Now over to loop()


void loop()

// Ask MPU6050 for orientation
float roll,pitch,yaw;
ins.quaternion.to_euler(&roll, &pitch, &yaw);
roll = ToDeg(roll) ;
pitch = ToDeg(pitch) ;
yaw = ToDeg(yaw) ;

// Ask MPU6050 for gyro data
Vector3f gyro = ins.get_gyro();
float gyroPitch = ToDeg(gyro.y), gyroRoll = ToDeg(gyro.x), gyroYaw = ToDeg(gyro.z);

PSTR("P:%4.1f R:%4.1f Y:%4.1f\n"),



You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • I am also curious about this. I ran a similar test on an APM yaw drift was about 0.45 degrees per second....this is using the Arducopter AP_InertialSensor libraries.

    I remember using a standalone MPU6050 with an Arduino once using the standard MPU6XXX libraries and I was getting much better results...I don't remember exactly, but I feel like it was around 1 degree per minute on the yaw if left alone on a desk and that was after doing the recommended calibrations.

    What bugs me though is the pitch being limited to +-88~90 degrees. I'm sure there is a reason for why it is the way it is, but I would rather have -180 to 180.

    I haven't tried looking in the libraries yet, but is there an easy way to modify the pitch range to -180 to 180?

  • Hello Ozy. I used your code and my values were not drifting. They were initially but stabilized later on. My pitch is varying from -90<P<90. I think yours will too if you orient the quad properly. It seems like it is less than 90 but if you rotate slightly in the yaw and roll directions, the pitch will also change.

This reply was deleted.


DIY Robocars via Twitter
3 hours ago
DIY Robocars via Twitter
DIY Robocars via Twitter
RT @f1tenth: What can we learn from autonomous racing? Actually, a lot! To get an idea what you can do in this field and where the research…
Oct 11
DIY Robocars via Twitter
Oct 11
DIY Robocars via Twitter
RT @a1k0n: It's aliiiiiive! Less than a week until @selfracingcars and I'm just now digging this bad boy out. Been a crazy few months. Hope…
Oct 10
DIY Robocars via Twitter
RT @OttawaAVGroup: Mark your calendars. CAV Canada is happening on Dec. 2nd, 10am-4pm EST. And guess what - admission is FREE! Get your ti…
Oct 6
DIY Robocars via Twitter
RT @f1tenth: Our new 1:10 scale 3D racetrack is here. We will implement it in the @SVLSimulator in the next weeks so everybody can use it.…
Oct 6
DIY Robocars via Twitter
RT @DAVGtech: Received sweet autonomous Ferrari's that we plan to demo at the @IndyAChallenge on our portable @donkey_car track. Let us kno…
Oct 6
DIY Robocars via Twitter
Sep 30
DIY Robocars via Twitter
Sep 9
DIY Robocars via Twitter
RT @chr1sa: We've got another virtual @DIYRobocars race tomorrow at 9:00am PT. Two dozen autonomous cars will compete, four at a time. Ther…
Sep 4
DIY Robocars via Twitter
Sep 1
DIY Robocars via Twitter
Aug 31
DIY Robocars via Twitter
Aug 31
DIY Robocars via Twitter
RT @ExplorerRobocar: Sometimes, I am really a big kid!! Having fun with my virtual RC car on the @diyrobocars track 😜😉, by using the « AR R…
Aug 24
DIY Robocars via Twitter
RT @grepLeigh: @donkey_car @unity3d @TensorFlow ~1050 episodes into training, my agent learned to erratically drive in the left lane. 🤖😆 ht…
Aug 24