Vertical Stabilization using MPU6050 and Servo?

Hello,

A friend and I have developed an interest in model rocketery. We’re currently working on a flight computer to take some sensors up and log data but we also wanted to add vertical stabilization to assist the rocket to fly vertical.

I’ve mocked up the hardware on a breadboard using a MPU-6050 break out board from eBay together with a Nano. At the minute, I’m using the I2Cdev library and this sample code:

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Servo.h"

MPU6050 mpu;

int16_t ax, ay, az;
int16_t gx, gy, gz;

Servo myservo;

int val;
int prevVal;

void setup() 
{
    Wire.begin();
    mpu.initialize();
    myservo.attach(9);
    myservo.write(90); //Set Servo to 90 Degrees (Rockets Point Upward!)
}

void loop() 
{
    mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    
    
    val = map(gy, -28000, 28000, 0, 180);
    
    
    
    if (val != prevVal)
    {
        myservo.write(val);
        prevVal = val;
    }
    
    delay(5);
}

The problem is that while this code does stabilize, it will only stabilize the rocket on whatever angle it’s been altered / knocked / drifted too and not back to vertical.

As I see it, the fins should gimbal and remain almost vertical no matter what angle the gyro is tilted to so as to return the rocket back on vertical course.

I’m sorry if my terminology is simplistic, I’m really only an amateur but could someone point me in the right direction. My maths is not the best so I’m not really too sure of how best to break down what I need to do. I’m not looking for someone to do it all for me, just point me in the right direction.

Many thanks,

M.

Forgot to say that I’ve only got one servo connected at the minute. I only own one atm but have two more on order. I’m going to be using 5g micro servers.

The rate gyro tells you how fast the axis is rotating but not how far it has rotated. To calculate how far you have to take the rate and multiply by time. That will tell you how far it has rotated in the last interval. Then you have to add all of those values together to get the total rotation.

The gyroscope won't read 0 when not rotating. That value is the zero offset. You have to take a long term average of the value you get when the gyroscope is not rotating and subtract that offset from each sample, otherwise your accumulated value will drift quickly.

Balancing robot code might give you some insight into how to use the gyro and accel to maintain vertical.

But you switch it on before launch or after? It should stabilize to the position it was before launch.

@bambamduino

Maybe you should be using the values from the accelerometer instead of the gyro, something like this:

    const float radToDeg = 180.0 / 3.1415926535897932384626433832795;
    float zScaleOffset = 1;

    float angleY = atan((float)ay / (az * zScaleOffset)) * radToDeg;  // calculate for y axis
    float angleX = atan((float)ax / (az * zScaleOffset)) * radToDeg;  // calculate for X axis

This will tell you the angle by which your rocket deviates from the vertical. However, if your rocket accelerates faster than 16g (the max limit of the mpu6050), then you'll need to think of something else.

To see an example of this sort of code in action, please see my youtube video below:

http://youtu.be/7nm1sERTW4g

Cheers Hacktron

Vertical is defined by gravity only if the rocket is stationary, on the launch pad for example.

jremington: Vertical is defined by gravity only if the rocket is stationary, on the launch pad for example.

Not according to Sir Isaac Newton.

Accelerometers measure acceleration. A low altitude rocket flying at constant velocity will give the same readout for each axis of the mpu6050, as one in the same orientation that is stationary on the launch pad.

As long as the rocket acceleration doesn't exceed 16g, it should be OK.

If the rocket were in freefall, each axis would give a readout of zero, making it useless for determining orientation.

Hackatron: Not according to Sir Isaac Newton.

It's actually Einstein we should listen to for this application.

Hackatron: Accelerometers measure acceleration. A low altitude rocket flying at constant velocity will give the same readout for each axis of the mpu6050, as one in the same orientation that is stationary on the launch pad.

This would only be true if we could be sure the rocket weren't changing altitude.

There are lots of scenarios where the rocket could experience 1g acceleration without the rocket hovering (or flying at constant altitude). The rocket could be accelerating to the side and downward (falling) with the combined acceleration of 1g.

Hackatron: As long as the rocket acceleration doesn't exceed 16g, it should be OK.

If the rocket were in freefall, each axis would give a readout of zero, making it useless for determining orientation.

The accelerometer will pretty much be useless for determining orientation the entire flight.

There's no way to distinguish acceleration from the rocket changing velocity from acceleration from gravity. This is Einstein's General Relativity at work.

This is why the gyros of the sensor are also needed.

As jremington suggests, the Arduino should establish the vertical direction prior to launch while correctly oriented on the launch pad. When the robot is launched, the gyros (and the smarts inside) of the MPU6050 should be able to keep track of the rocket's orientation.

I've played with MPU6050 sensors a bit and I was amazed how well they worked. The sensor does an extremely good job of integrating the gyro readings to produce a heading.

@bambamduino, use jremington's suggestion and add some way of indicating to the Arduino when it should record the rocket's orientation. Compare the current orientation against this target orientation and make the needed corrections. You may need some sort of PID algorithm.

@DuaneDegn

Speaking of Einstein, I think he is reported to have said something along the lines of "Any intelligent fool can make things bigger and more complex... it takes a touch of genius to go in the opposite direction" :)

I don't think bambamduino's rocket is intended to fly like an aeroplane. If it were, then gyro and accelerometer values would need to be combined. Since it is only flying in a general upwards direction and is not going in to orbit, accelerometer values and Newtonian laws of motion are sufficient.

Combining the accelerometer values for each axis is useless, as you say, which is why we need to inspect the values of the individual axis (as shown in my code example above, and as demonstrated in my youtube video - link above).

Regards Hackatron

Hackatron: I don't think bambamduino's rocket is intended to fly like an aeroplane. If it were, then gyro and accelerometer values would need to be combined. Since it is only flying in a general upwards direction and is not going in to orbit, accelerometer values and Newtonian laws of motion are sufficient.

You won't be able to distinguish gravity from acceleration. A rocket flying straight up and a rocket flying sideways (and curving toward the ground) can have identical accelerometer readings.

Accelerometers are great for determining orientation of stationary objects but once the object starts to move (accelerate) you need combine the accelerometer data with data from a gyro in order to determine orientation. There's a reason people don't make balancing bots or quadcopter controllers with just accelerometers. It's because accelerometers can't tell the difference between acceleration from gravity or acceleration from the change in velocity.

Since it is only flying in a general upwards direction and is not going in to orbit, accelerometer values and Newtonian laws of motion are sufficient.

What utter nonsense. Misbehaving rockets always go in directions other than straight up, and I've seen them turn and head straight down.

An accelerometer measures accelerations along internally defined axes. Without an external reference, there is absolutely no way to determine the orientation of the accelerometer itself.

By Newton's laws, it is completely impossible to use an accelerometer alone to determine velocity and position relative to an external coordinate system.

It is therefore completely impossible to predict the path of a rocket by accelerometer measurements alone.

@jremington, @DuaneDegn

Thank you, I'm already familiar with fusion of gyro and acceleration data and its use. I'm currently developing wearable tech with these devices for sports purposes. I don't actually disagree with the fundamental physics of what you are stating.

My disagreement is how bambamduino is being asked to approach the topic, who has already stated is not a rocket scientist, nor a mathematician.

@bambamduino

If your requirement is to correct the flight path of your rocket and recover from flying like the Blue Angels or Red Arrows, then you will need to follow the advice of the other forum members about fusing the readings from the gyro and accelerometer.

If you're satisfied that your rockets are reasonably linear in flight, then try the using the acceleration data.

Either way, let us know how you get on.

Hackatron

Hackatron:
I don’t actually disagree with the fundamental physics of what you are stating.

Then why say what was said in the quote below?

Hackatron:
If you’re satisfied that your rockets are reasonably linear in flight, then try the using the acceleration data.

This would be a waste of time. The acceleration data will be useless for determining the rocket’s orientation once it is under power. There isn’t a way to separate acceleration from gravity from the acceleration of the rocket. The acceleration from gravity can be detected when the object is not allowed to move. If an object is falling the acceleration from gravity is not detected by the sensor. When the rocket varies from its straight up flight, the accelerometer will not detect this as a sideways acceleration since the rocket will be moved (aka accelerated, aka fall) with the gravitational force. While the rocket is under power, this falling motion will go undetected by the accelerometer.

Let’s do a thought experiment.

Let’s say we have a quadcopter from which we can drop the rocket. Let’s also say we can start the rocket remotely.

Now I think you’ll agree when the rocket is falling from the quadcopter with the rocket motor off, there isn’t a way to use the accelerometer to determine orientation.1 Right?

When the falling rocket’s engine is fired, all2 the acceleration sensed by the accelerometer will be from the rocket’s motor. Right? Is there any way to determine the orientation of the rocket from the accelerometer at this point? I think not.

Hopefully you’ll agree a rocket which was initially falling but now under power isn’t any different than a rocket which was launched from the ground.

Once the ground isn’t pushing on the rocket anymore, it’s essentially falling with respect to the acceleration from gravity.

Footnotes

  1. Let’s ignore the acceleration from air resistance. I suppose it may be possible to gain some insight about the orientation of the rocket based on a knowledge of rocket’s aerodynamics but let’s not worry about this.

  2. Nearly all.

My disagreement is how bambamduino is being asked to approach the topic, who has already stated is not a rocket scientist, nor a mathematician.

Please explain why your response in such a case is to provide misinformation.

This rocket is not flying in a vacuum. It has aerodynamic drag. If the rocket is rotated by some small amount then it will experience a small side force (called lift, but lift may act downwards) that can be measured by an accelerometer. A restoring torque needs to be generated by the rocket to reduce the lift to zero and align the rocket thrust with the flight path.

You know those little rigid fins on the back of rockets and arrows? That's what they do.

MorganS: This rocket is not flying in a vacuum. It has aerodynamic drag. If the rocket is rotated by some small amount then it will experience a small side force (called lift, but lift may act downwards) that can be measured by an accelerometer. A restoring torque needs to be generated by the rocket to reduce the lift to zero and align the rocket thrust with the flight path.

You know those little rigid fins on the back of rockets and arrows? That's what they do.

There is nothing special about being sideways for lift to occur. Lift can occur in all orientations even vertical.

A restoring torque needs to be generated by the rocket to reduce the lift to zero and align the rocket thrust with the flight path.

How would you propose to separate the acceleration due to "lift" from all the other accelerations experienced by a rocket?

jremington: How would you propose to separate the acceleration due to "lift" from all the other accelerations experienced by a rocket?

You're asking me or the other guy who thinks accelerometers will work?

I think tail fins will work.

How would you propose to separate the acceleration due to "lift" from all the other accelerations experienced by a rocket?

Won't the acceleration that needs to be corrected be horizontal whilst the acceleration produced by the rocket will be vertical ?

MorganS: I think tail fins will work.

They won't be enough to ensure the robot flies straight. The fins help the rocket fly straight through air but if that air is moving (aka wind) the fins won't be enough to keep the rocket vertical.

UKHeliBob: Won't the acceleration that needs to be corrected be horizontal whilst the acceleration produced by the rocket will be vertical ?

An accelerometer only detects acceleration from gravity when something is holding it in place so the gravitational acceleration doesn't cause it to move.

In free fall the accelerometer reads zero because it's moving with the gravitational acceleration. When a rocket starts to move off from vertical gravity will cause the rocket to fall towards earth (cause the rocket to curve to the side). As with free fall, then the rocket is moving with gravity, the accelerometer won't be able to detect it.

Basically the horizontal acceleration wont be detected because the rocket will move with the acceleration. The acceleration vector measured by the accelerometer can be completely aligned with the rocket's axis while the rocket is in flying in any orientation.