lis3l06al.c File Reference


Detailed Description

Accelerometer example driver for AVR32 UC3.

This file provides an example for the ADC + Accelerometer on AVR32 UC3 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file lis3l06al.c.

#include "board.h"
#include "gpio.h"
#include "adc.h"
#include "lis3l06al.h"
#include "math.h"

Go to the source code of this file.

Defines

#define DEG_2_RAD(deg)   ((double)(deg)*3.14/180)

Functions

unsigned long acc_get_m_x (void)
 Return measured value of X sensor.
unsigned long acc_get_m_y (void)
 Return measured value of Y sensor.
unsigned long acc_get_m_z (void)
 Return measured value of Z sensor.
void acc_init (void)
 Initialise Accelerometer driver. Mandatory to call.
void acc_update (void)
 Get accelerometer measure (3x) and process results.
signed long is_acc_abs_angle_x (unsigned long abs_ang)
 Test if absolute value of X angle has reached the specified limit.
signed long is_acc_abs_angle_y (unsigned long abs_ang)
 Test if absolute value of Y angle has reached the specified limit.
Bool is_acc_down ()
 Test if sensor have detected 'down' orientation.
Bool is_acc_left ()
 Test if sensor have detected 'left' orientation.
Bool is_acc_meuh (char stop)
 Detect a topdown to up transition.
Bool is_acc_right ()
 Test if sensor have detected 'right' orientation.
Bool is_acc_slow ()
 Test acceleration about 1g +-10%.
Bool is_acc_topdown ()
 Test if sensor have detected 'topdown' orientation.
Bool is_acc_up ()
 Test if sensor have detected 'up' orientation.

Variables

avr32_acc_t acc


Define Documentation

#define DEG_2_RAD ( deg   )     ((double)(deg)*3.14/180)

Definition at line 264 of file lis3l06al.c.

Referenced by is_acc_abs_angle_x(), and is_acc_abs_angle_y().


Function Documentation

unsigned long acc_get_m_x ( void   ) 

Return measured value of X sensor.

Returns:
measured value of X sensor.

Definition at line 285 of file lis3l06al.c.

References avr32_acc_t::m, and xyz_t::x.

Referenced by print_mouse().

00286 {
00287    return acc.m.x;
00288 }

unsigned long acc_get_m_y ( void   ) 

Return measured value of Y sensor.

Returns:
measured value of Y sensor.

Definition at line 290 of file lis3l06al.c.

References avr32_acc_t::m, and xyz_t::y.

Referenced by print_mouse().

00291 {
00292    return acc.m.y;
00293 }

unsigned long acc_get_m_z ( void   ) 

Return measured value of Z sensor.

Returns:
measured value of Z sensor.

Definition at line 295 of file lis3l06al.c.

References avr32_acc_t::m, and xyz_t::z.

Referenced by print_mouse().

00296 {
00297    return acc.m.z;
00298 }

void acc_init ( void   ) 

Initialise Accelerometer driver. Mandatory to call.

Definition at line 213 of file lis3l06al.c.

Referenced by main().

00214 {
00215   volatile avr32_adc_t *adc = &AVR32_ADC;
00216   static const gpio_map_t ADC_GPIO_MAP =
00217   {
00218     {AVR32_ADC_AD_3_PIN,  AVR32_ADC_AD_3_FUNCTION}, // ADC channel 3
00219     {AVR32_ADC_AD_1_PIN,  AVR32_ADC_AD_1_FUNCTION}, // ADC channel 1
00220     {AVR32_ADC_AD_2_PIN,  AVR32_ADC_AD_2_FUNCTION}  // ADC channel 2
00221   };
00222 
00223   // enable GPIO pins for ADC
00224   gpio_enable_module(ADC_GPIO_MAP,
00225                      sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));
00226 
00227   // configure ADC
00228   adc_configure(adc);
00229 }

void acc_update ( void   ) 

Get accelerometer measure (3x) and process results.

Definition at line 231 of file lis3l06al.c.

References avr32_acc_t::ag, avr32_acc_t::ak, avr32_acc_t::ak2, avr32_acc_t::g, is_acc_slow(), avr32_acc_t::k, avr32_acc_t::m, avr32_acc_t::s, xyz_t::x, xyz_t::y, and xyz_t::z.

Referenced by main().

00232 {
00233   volatile avr32_adc_t *adc = &AVR32_ADC;
00234 
00235   // start + get from adc
00236   acc_get_value(adc, &acc.m) ;
00237 
00238   // ak  = acceleration = m - k
00239   xyz_diff( acc.m , acc.k, &acc.ak ) ;
00240   acc.ak2 = xyz_sumsq( acc.ak ) ;
00241 
00242   if ( is_acc_slow() ) {
00243      // update g for next move
00244      acc.g.x = acc.m.x ;
00245      acc.g.y = acc.m.y ;
00246      acc.g.z = acc.m.z ;
00247   }
00248   else {
00249      xyz_diff( acc.m , acc.g, &acc.ag ) ;
00250      xyz_round0( acc.ag , 16, &acc.ag ) ;
00251      xyz_add( acc.s , acc.ag, &acc.s ) ;
00252      //xyz_round0( acc.s , 256, &acc.s ) ;
00253      //print_dbg("m = "); print_xyz( acc.m ) ;
00254      //print_dbg("k = "); print_xyz( acc.k ) ;
00255      //print_dbg("g = "); print_xyz( acc.g ) ;
00256      //print_dbg("ag = "); print_xyz( acc.ag ) ;
00257      //print_dbg("s = "); print_xyz( acc.s ) ;
00258      //print_dbg("\r\n");
00260      //print_dbg("s2 = 0x"); print_dbg_hex(acc.s2); print_dbg("\r\n");
00261   }
00262 }

signed long is_acc_abs_angle_x ( unsigned long  abs_ang  ) 

Test if absolute value of X angle has reached the specified limit.

Returns:
Bool 1 if limit has been reached in one sens, -1 if limit has been reached in the opposite sens, 0 if limit has not been reached.

Definition at line 265 of file lis3l06al.c.

References ACC_1G, avr32_acc_t::ak, DEG_2_RAD, and xyz_t::x.

Referenced by print_angles().

00266 {
00267   if( abs(acc.ak.x) > (int)(sin(DEG_2_RAD(abs_ang)) * ACC_1G) )
00268   {
00269      if ( acc.ak.x > 0 ) return 1;
00270      else                return -1;
00271   }
00272   return 0;
00273 }

signed long is_acc_abs_angle_y ( unsigned long  abs_ang  ) 

Test if absolute value of Y angle has reached the specified limit.

Returns:
Bool 1 if limit has been reached in one sens, -1 if limit has been reached in the opposite sens, 0 if limit has not been reached.

Definition at line 275 of file lis3l06al.c.

References ACC_1G, avr32_acc_t::ak, DEG_2_RAD, and xyz_t::y.

Referenced by print_angles().

00276 {
00277   if( abs(acc.ak.y) > (int)(sin(DEG_2_RAD(abs_ang)) * ACC_1G) )
00278   {
00279      if ( acc.ak.y > 0 ) return 1;
00280      else                return -1;
00281   }
00282   return 0;
00283 }

Bool is_acc_down ( void   ) 

Test if sensor have detected 'down' orientation.

This function uses an software hysteresis.

Returns:
Bool TRUE if down is detected, FALSE if down is not detected.

Definition at line 189 of file lis3l06al.c.

References ACC_TRIG_Y1, avr32_acc_t::down, avr32_acc_t::m, avr32_acc_t::pos, and xyz_t::y.

Referenced by print_mouse().

00190 {
00191   if      ( (acc.pos.down==0) && (acc.m.y > (int)(1.01*ACC_TRIG_Y1)) ) acc.pos.down = 1 ;
00192   else if ( (acc.pos.down==1) && (acc.m.y < (int)(0.99*ACC_TRIG_Y1)) ) acc.pos.down = 0 ;
00193   return acc.pos.down ;
00194 }

Bool is_acc_left ( void   ) 

Test if sensor have detected 'left' orientation.

This function uses an software hysteresis.

Returns:
Bool TRUE if left is detected, FALSE if left is not detected.

Definition at line 170 of file lis3l06al.c.

References ACC_TRIG_X1, avr32_acc_t::left, avr32_acc_t::m, avr32_acc_t::pos, and xyz_t::x.

Referenced by print_mouse().

00171 {
00172   if      ( (acc.pos.left==0) && (acc.m.x > (int)(1.01*ACC_TRIG_X1)) ) acc.pos.left = 1 ;
00173   else if ( (acc.pos.left==1) && (acc.m.x < (int)(0.99*ACC_TRIG_X1)) ) acc.pos.left = 0 ;
00174   return acc.pos.left ;
00175 }

Bool is_acc_meuh ( char  stop  ) 

Detect a topdown to up transition.

This function uses an software hysteresis.

Returns:
Bool TRUE if topdown to up transition is detected, FALSE if topdown to up transition is not detected.

Definition at line 203 of file lis3l06al.c.

References ACC_TRIG_Z_BOT, ACC_TRIG_Z_TOP, avr32_acc_t::m, and xyz_t::z.

Referenced by main(), and print_mouse().

00204 {
00205   static char topdown = 0 ;
00206   static char meuh = 0 ;
00207   if ( stop )  meuh = 0 ;
00208   if      ( (topdown==0) && (acc.m.z > ACC_TRIG_Z_BOT) ) topdown = 1 ;
00209   else if ( (topdown==1) && (acc.m.z < ACC_TRIG_Z_TOP) ) { topdown = 0 ; meuh = 1 ; }
00210   return meuh ;
00211 }

Bool is_acc_right ( void   ) 

Test if sensor have detected 'right' orientation.

This function uses an software hysteresis.

Returns:
Bool TRUE if right is detected, FALSE if right is not detected.

Definition at line 176 of file lis3l06al.c.

References ACC_TRIG_X0, avr32_acc_t::m, avr32_acc_t::pos, avr32_acc_t::right, and xyz_t::x.

Referenced by print_mouse().

00177 {
00178   if      ( (acc.pos.right==0) && (acc.m.x < (int)(0.99*ACC_TRIG_X0)) ) acc.pos.right = 1 ;
00179   else if ( (acc.pos.right==1) && (acc.m.x > (int)(1.01*ACC_TRIG_X0)) ) acc.pos.right = 0 ;
00180   return acc.pos.right ;
00181 }

Bool is_acc_slow ( void   ) 

Test acceleration about 1g +-10%.

Gravity is always present! This is used to decide slow / fast behavior

Returns:
Bool TRUE if slow motion is detected, FALSE if fast motion is detected.

Definition at line 147 of file lis3l06al.c.

References ACC_QUIET_HI, ACC_QUIET_LO, and avr32_acc_t::ak2.

Referenced by acc_update(), and main().

00148 {
00149   return ( (acc.ak2 > ACC_QUIET_LO) && (acc.ak2 < ACC_QUIET_HI) ) ;
00150 }

Bool is_acc_topdown ( void   ) 

Test if sensor have detected 'topdown' orientation.

This function uses an software hysteresis.

Returns:
Bool TRUE if topdown is detected, FALSE if topdown is not detected.

Definition at line 196 of file lis3l06al.c.

References ACC_TRIG_Z_BOT, avr32_acc_t::m, avr32_acc_t::pos, avr32_acc_t::topdown, and xyz_t::z.

Referenced by print_mouse().

00197 {
00198   if      ( (acc.pos.topdown==0) && (acc.m.z > (int)(1.01*ACC_TRIG_Z_BOT)) ) acc.pos.topdown = 1 ;
00199   else if ( (acc.pos.topdown==1) && (acc.m.z < (int)(0.99*ACC_TRIG_Z_BOT)) ) acc.pos.topdown = 0 ;
00200   return acc.pos.topdown ;
00201 }

Bool is_acc_up ( void   ) 

Test if sensor have detected 'up' orientation.

This function uses an software hysteresis.

Returns:
Bool TRUE if up is detected, FALSE if up is not detected.

Definition at line 183 of file lis3l06al.c.

References ACC_TRIG_Y0, avr32_acc_t::m, avr32_acc_t::pos, avr32_acc_t::up, and xyz_t::y.

Referenced by print_mouse().

00184 {
00185   if      ( (acc.pos.up==0) && (acc.m.y < (int)(0.99*ACC_TRIG_Y0)) ) acc.pos.up = 1 ;
00186   else if ( (acc.pos.up==1) && (acc.m.y > (int)(1.01*ACC_TRIG_Y0)) ) acc.pos.up = 0 ;
00187   return acc.pos.up ;
00188 }


Variable Documentation

Initial value:

 {
  
  .m = { .x=0 , .y=0 , .z=0 } ,

  
  .pos.left    = 0,
  .pos.right   = 0,
  .pos.up      = 0,
  .pos.down    = 0,
  .pos.topdown = 0,

  
  .k = { .x=ACC_ZERO_X , .y=ACC_ZERO_Y , .z=ACC_ZERO_Z } ,  
  .ak = { .x=0 , .y=0 , .z=0 } ,
  .ak2 = 0 ,

  
  .g = { .x=ACC_ZERO_X , .y=ACC_ZERO_Y , .z=ACC_MIN } ,  
  .ag = { .x=0 , .y=0 , .z=0 } ,
  .ag2 = 0 ,
  .s = { .x=0 , .y=0 , .z=0 } ,
  .s2 = 0
}

Definition at line 65 of file lis3l06al.c.


Generated on Fri Feb 19 02:23:25 2010 for AVR32 UC3 - lis3l06al accelerometer driver by  doxygen 1.5.5