xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Data Structures | Macros | Functions | Variables
Orientation Calculation

Calculate Orientation using the Kalman-Filter, Accelerometer and Gyroscope. More...

Files

file  orientation.h
 Orientation API Header.
 
file  orientation.c
 Orientation API Implementation.
 

Data Structures

struct  Angles
 Can store orientation in Euler Space. More...
 

Macros

#define TODEG(x)   ((x * 180) / M_PI)
 Convert Radians to Degrees. More...
 

Functions

Error orientationInit (void)
 Initializes the Orientation API. More...
 
Error orientationTask (void)
 Calculate the current orientation. More...
 
void zeroOrientation (void)
 Sets the current orientation to zero. More...
 

Variables

Angles orientation
 Current Aircraft orientation. More...
 
Angles orientation = {.pitch = 0, .roll = 0, .yaw = 0}
 Current Aircraft orientation. More...
 
Angles orientationError = {.pitch = 0, .roll = 0, .yaw = 0}
 Current Aircraft orientation offset. More...
 
Kalman pitchData
 Kalman-State for Pitch Angle. More...
 
Kalman rollData
 Kalman-State for Roll Angle. More...
 

Detailed Description

Calculate Orientation using the Kalman-Filter, Accelerometer and Gyroscope.

Macro Definition Documentation

#define TODEG (   x)    ((x * 180) / M_PI)

Convert Radians to Degrees.

Definition at line 55 of file orientation.c.

Referenced by orientationTask().

Function Documentation

Error orientationInit ( void  )

Initializes the Orientation API.

Also initializes the Accelerometer, Gyroscope and Magnetometer. I2C should already be initialized!

Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR, ARGUMENT_ERROR or SUCCESS.
Examples:
hardwareTest.c, and uartFlight.c.

Definition at line 73 of file orientation.c.

References accInit(), CHECKERROR, complementaryInit(), gyroInit(), kalmanInit(), r250DPS, r4G, and SUCCESS.

73  {
74  Error e = accInit(r4G);
75  CHECKERROR(e);
76  e = gyroInit(r250DPS);
77  CHECKERROR(e);
78 
79 #if ORIENTATION_FILTER == FILTER_KALMAN
82 #elif ORIENTATION_FILTER == FILTER_COMPLEMENTARY
85 #endif
86 
87  return SUCCESS;
88 }
Error orientationTask ( void  )

Calculate the current orientation.

It will be stored in the global orientation Struct.

Returns
TWI_NO_ANSWER, TWI_WRITE_ERROR, ARGUMENT_ERROR or SUCCESS.
Examples:
uartFlight.c.

Definition at line 90 of file orientation.c.

References accRead(), CHECKERROR, complementaryExecute(), ERROR, getSystemTime(), gyroRead(), kalmanInnovate(), orientation, Angles::pitch, Angles::roll, SUCCESS, TODEG, Angles::vPitch, Angles::vRoll, Angles::vYaw, Vector3f::x, xySelfReset(), Vector3f::y, Angles::yaw, and Vector3f::z.

90  {
91  Vector3f g, a;
92  Error e = accRead(&a); // Read Accelerometer
93  CHECKERROR(e);
94  e = gyroRead(&g); // Read Gyroscope
95  CHECKERROR(e);
96 
97  // Calculate Pitch & Roll from Accelerometer Data
98  double roll = atan(a.x / hypot(a.y, a.z));
99  double pitch = atan(a.y / hypot(a.x, a.z));
100  roll = TODEG(roll);
101  pitch = TODEG(pitch); // As Degree, not radians!
102 
103  // Filter Roll and Pitch with Gyroscope Data from the corresponding axis
104 #if ORIENTATION_FILTER == FILTER_KALMAN
105  kalmanInnovate(&pitchData, pitch, g.x);
106  kalmanInnovate(&rollData, roll, g.y);
109 #elif ORIENTATION_FILTER == FILTER_COMPLEMENTARY
110  complementaryExecute(&pitchData, pitch, g.x);
111  complementaryExecute(&rollData, roll, g.y);
112  orientation.roll = rollData.angle;
113  orientation.pitch = pitchData.angle;
114 #endif
115 
116  // Zero Offset for angles
120 
121  // Store Angle Speeds
122  orientation.vRoll = g.y;
123  orientation.vPitch = g.x;
124  orientation.vYaw = g.z;
125 
126  // Self-Reset if data is garbage and we just came up
127  if (getSystemTime() < 2000) {
128  if (isnan(orientation.roll) || isnan(orientation.pitch) || isnan(orientation.yaw)) {
129  xySelfReset();
130  return ERROR;
131  }
132  }
133 
134  return SUCCESS;
135 }
void zeroOrientation ( void  )

Sets the current orientation to zero.

Examples:
uartFlight.c.

Definition at line 137 of file orientation.c.

References orientation, Angles::pitch, Angles::roll, and Angles::yaw.

Variable Documentation

Angles orientation

Current Aircraft orientation.

Examples:
uartFlight.c.

Definition at line 58 of file orientation.c.

Referenced by orientationTask(), pidTask(), and zeroOrientation().

Angles orientation = {.pitch = 0, .roll = 0, .yaw = 0}

Current Aircraft orientation.

Definition at line 58 of file orientation.c.

Referenced by orientationTask(), pidTask(), and zeroOrientation().

Angles orientationError = {.pitch = 0, .roll = 0, .yaw = 0}

Current Aircraft orientation offset.

Definition at line 61 of file orientation.c.

Kalman pitchData

Kalman-State for Pitch Angle.

Definition at line 64 of file orientation.c.

Kalman rollData

Kalman-State for Roll Angle.

Definition at line 65 of file orientation.c.