Contiki-Inga 3.x
gyro-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * Gyroscope sensor implementation
33  * \author
34  * Georg von Zengen
35  * Enrico Joerns <e.joerns@tu-bs.de>
36  */
37 
38 #include "contiki.h"
39 #include "lib/sensors.h"
40 #include "l3g4200d.h"
41 #include "gyro-sensor.h"
42 const struct sensors_sensor gyro_sensor;
43 static uint8_t initialized = 0;
44 static uint8_t ready = 0;
45 #define raw_to_dps(raw) TODO
46 static angle_data_t gyro_data;
47 /*---------------------------------------------------------------------------*/
48 static void
49 cond_update_gyro_data(int ch)
50 {
51  /* bit positions set to one indicate that data is obsolete and a new readout will
52  * needs to be performed. */
53  static uint8_t gyro_data_obsolete_vec = 0xFF;
54  /* A real readout is performed only when the channels obsolete flag is already
55  * set. I.e. the channels value has been used already.
56  * Then all obsolete flags are reset to zero. */
57  if (gyro_data_obsolete_vec & (1 << ch)) {
58  gyro_data = l3g4200d_get_angle();
59  gyro_data_obsolete_vec = 0x00;
60  }
61  /* set obsolete flag for current channel */
62  gyro_data_obsolete_vec |= (1 << ch);
63 }
64 /*---------------------------------------------------------------------------*/
65 static int
66 value(int type)
67 {
68  switch (type) {
69  case GYRO_X:
70  cond_update_gyro_data(GYRO_X);
71  return l3g4200d_raw_to_dps(gyro_data.x);
72 
73  case GYRO_Y:
74  cond_update_gyro_data(GYRO_Y);
75  return l3g4200d_raw_to_dps(gyro_data.y);
76 
77  case GYRO_Z:
78  cond_update_gyro_data(GYRO_Z);
79  return l3g4200d_raw_to_dps(gyro_data.z);
80 
81  case GYRO_X_RAW:
82  cond_update_gyro_data(GYRO_X_RAW);
83  return gyro_data.x;
84 
85  case GYRO_Y_RAW:
86  cond_update_gyro_data(GYRO_Y_RAW);
87  return gyro_data.y;
88 
89  case GYRO_Z_RAW:
90  cond_update_gyro_data(GYRO_Z_RAW);
91  return gyro_data.z;
92 
93  case GYRO_TEMP:
94  return l3g4200d_get_temp();
95  }
96  return 0;
97 }
98 /*---------------------------------------------------------------------------*/
99 static int
100 status(int type)
101 {
102  switch (type) {
103  case SENSORS_ACTIVE:
104  return initialized;
105  case SENSORS_READY:
106  return ready;
107  }
108  return 0;
109 }
110 /*---------------------------------------------------------------------------*/
111 static int
112 configure(int type, int c)
113 {
114  uint8_t value = 0;
115  switch (type) {
116 
117  case SENSORS_HW_INIT:
118  if (l3g4200d_available()) {
119  ready = 1;
120  return 1;
121  } else {
122  ready = 0;
123  return 0;
124  }
125  break;
126 
127  case SENSORS_ACTIVE:
128  if (c) {
129  if (l3g4200d_init() == 0) {
130  initialized = 1;
131  return 1;
132  }
133  } else {
134  if (l3g4200d_deinit() == 0) {
135  initialized = 0;
136  return 1;
137  }
138  }
139  break;
140 
142  switch (c) {
143  case GYRO_250DPS:
144  value = L3G4200D_250DPS;
145  break;
146  case GYRO_500DPS:
147  value = L3G4200D_500DPS;
148  break;
149  case GYRO_2000DPS:
150  value = L3G4200D_2000DPS;
151  break;
152  default:
153  // invalid value
154  return 0;
155  }
156  return (l3g4200d_set_dps(value) == 0) ? 1 : 0;
157 
158  case GYRO_CONF_DATA_RATE:
159  if (c <= GYRO_100HZ) {
160  value = L3G4200D_ODR_100HZ;
161  } else if (c <= GYRO_200HZ) {
162  value = L3G4200D_ODR_200HZ;
163  } else if (c <= GYRO_400HZ) {
164  value = L3G4200D_ODR_400HZ;
165  } else if (c <= GYRO_800HZ) {
166  value = L3G4200D_ODR_800HZ;
167  } else {
168  // invalid value
169  return 0;
170  }
171  return (l3g4200d_set_data_rate(value) == 0) ? 1 : 0;
172 
173  }
174  return 0;
175 }
176 /*---------------------------------------------------------------------------*/
177 SENSORS_SENSOR(gyro_sensor, GYRO_SENSOR, value, configure, status);