Tag Archives: accelerometer

How to combine data from Gyro and Accel

Ok, so after some experiments on different approach in filtering the data,I’m now interested in how to mix data from GYRO and ACC in order to get optimized results.

As always, also this time I found out a very interesting article that describe exactly this type of approach: kalman filter vs complementary filter.

Here it is possible to find how to implement 2 type of complementary filters, and the kalman filter to solve our problem.If you are interested in the kalman filter I strongly recommand to have a look on it since it is really simple to be implemented.I have to say that is is written for Arduino, not in python, but I don’t think this can allarm us.

Personally I just implemented the first order complementary filter and it seems to solve both the noise and drift problem.

IMU_Fusion1

In the picture you can see the comparison between the returned angle from gyro, from acc,  from the complementary filter and also from the complementary filter  using the acc data filtered with kalman filter.

As you can see the gyro drift that is really evident in the right (the blue line) is completelty deleted .

IMU_Fusion2

Here it is the pic zoom of the left side, where the registered acc noise is clear (red line).Look as the complementary filter reduces it strongly.

So finally after this test I decide to implement the complementary filter on my sw.

You can  find it here as IMU_Test3.

Note that the complementary filter is included directly in the imu_test.py code in the getAngleCompl() .

Now I can say that the development related to the IMU can be frozen at this stage. Now I want to move this into a python object that runs in a parallel thread so  I can monitor the IMU data anytime. After that It’s time to investigate in the PID.

Tutorial: How to read data from IMU

In the previous post I described how to setup raspberry pi for connection with the IMU.

Now it is time to see how to read some data from the sensor.

First yuo need sw that manage the i2c interface.there are many examples.You can find one called adafruit_i2c.py on github.

Then it is necessary to have the code specific for the sensor,in my case a MPU6050.

I tryied for some days to build my own code, but I encountered problems related to unconsistent results: even if I did not move the sensor, the returned results were always different. I suspect it was a problem on how I formatted the values.

Finally, Thanks to the great job done by Hove in  his blog, I used his code and I’m now able to collect correct data from the sensor.

I did some minor modification and prepare this IMU_test files.

So I started some preliminary tests to verify which is the sensor behaviour.

I fixed the sensor on a bar ,horizontally, than turned the bar by a known angle ( 13 degrees,measured with my smartphone level) then move back to horizontal.

IMU_test1

I recorded on a file the sensor data : acceleration along axis (from ACCelerometer) and rotational speed (from GYRO scope). On excel sheet  I calculated the angle around x  respect the ACC and respect the GYRO:

  • rx ACC=DEGREES(ATAN2(accZ+9.8;accY))
  • rx(i) GYRO=wx(i) *dt+wx(i-1)

Below you can see the graph.

IMU

I underline in the picture the 2 tipical problems on the IMU :

  • the Gyro drift (you can see an angle of 1  degree while it reality it was 0)
  • the Accelerometer sensibility to noise.

So next  development step is to filter/reduce this 2 problems by combining the 2 sensors results.

IMU Sensor Installation

Since a week ago, I ‘ve been looking into my new IMU: Inertial Measurement Unit.

I bought  from e-bay a MPU6050, a sensor including:

  • a gyroscope that can return the angular speed around the 3  axis x,y,z.
  • an accelerometer  that can return the linear acceleration along the 3  axis x,y,z.

This sensor can communicate trou I2C interface.

Below the electrical drawing:

wiring_IMUIMU_conn

 

 

 

 

 

 

 

For the sw installation I fuond a clear tutorial on the Adafruit website ,so I just report here a memo for the necessary steps:

in sudo nano /etc/modules add:

  • i2c-bcm2708
  • i2c-dev

sudo apt-get install python-smbus

sudo apt-get install i2c-tools

in sudo nano /etc/modprobe.d/raspi-blacklist.conf comment:

  • #blacklist spi-bcm2708
  • #blacklist i2c-bcm2708

Now ,running the command sudo i2cdetect -y 1  yuo have to see the address 0x68, corresponding to the default sensor address.

SW Dev: Intro controller loop

So far the sw development included basic functionality of jogging going to directly modify the speed engine and leaving the user to their modulation.

At this point I want to introduce the concept of control in which the ‘operator indicates the’ desired angle and the controller, our rapberry pi, must decide the speed ‘of the engines.

control_loop

The theoretical relationship between the quadcopter angle (be it roll, pitch, yaw, or a combination of them) can be found on the page of the quadcopter. The balance that you get is unstable and just a minimal disturbance (such as a breath of wind) can cause this balance is lost.

In reality we must also consider asymmetries of construction (structure, propellers, etc..), Or non-constant behavior of the motors.

That translates into the need to know:
• what ‘s the instantaneous behavior of the aircraft
• what ‘s the real relationship between angles and speed engines as different from the theoretical

For the first point we must introduce something that can read the current state of the quadcopter, say a sensor. In most cases, a sensor mounted on board as a gyroscope or gyro+ accelerometer.

The gyro returns the angular velocity from which the integral angle is achieved. The accelerometer returns l’ acceleration in its components x, y, z

Thus, known the current angle and the  target angle,it  can be possible to calculate the error and decide a strategy to modulate the speed of the engines.

This strategy results in a control method called PID.

It is assumed that the relationship between two variables (in our case motor angular speed and angle) is approximated to three components:
• Proportional (P) = kp * Ei
• Integral (I) = Ki * sum (Ei) * t
• Derivative (D) = kd * (Ei-Ei-1) / t

Where Ei and ‘target error between w and w ith current instantly, you’ cycle time of the loop where the various k must be determined empirically.

The pid so returns the variation of the motor speed in order to minimize E.

A loop that reads from the sensor, calculates the  correction with pid,applies the motor w is the basis of the angle control loop

In the future posts I will dedicate a specific description of the following blocks: sensor, PID and  control loop with the purpose of defining the final structure of the program.