source: rtos_arduino/trunk/examples/NAxesMotion/rca_app.cpp@ 137

Last change on this file since 137 was 137, checked in by ertl-honda, 8 years ago

サンプルの追加.

File size: 8.1 KB
Line 
1#include "rca.h"
2
3/****************************************************************************
4* Copyright (C) 2011 - 2014 Bosch Sensortec GmbH
5*
6* Accelerometer.ino
7* Date: 2014/09/09
8* Revision: 3.0 $
9*
10* Usage: Example code to stream Accelerometer data
11*
12****************************************************************************
13/***************************************************************************
14* License:
15*
16* Redistribution and use in source and binary forms, with or without
17* modification, are permitted provided that the following conditions are met:
18*
19* Redistributions of source code must retain the above copyright
20* notice, this list of conditions and the following disclaimer.
21*
22* Redistributions in binary form must reproduce the above copyright
23* notice, this list of conditions and the following disclaimer in the
24* documentation and/or other materials provided with the distribution.
25*
26* Neither the name of the copyright holder nor the names of the
27* contributors may be used to endorse or promote products derived from
28* this software without specific prior written permission.
29*
30* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
34* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
40*
41* The information provided is believed to be accurate and reliable.
42* The copyright holder assumes no responsibility for the consequences of use
43* of such information nor for any infringement of patents or
44* other rights of third parties which may result from its use.
45* No license is granted by implication or otherwise under any patent or
46* patent rights of the copyright holder.
47*/
48
49#include "NAxisMotion.h" //Contains the bridge code between the API and the Arduino Environment
50#include <Wire.h>
51
52NAxisMotion mySensor; //Object that for the sensor
53unsigned long lastStreamTime = 0; //To store the last streamed time stamp
54const int streamPeriod = 20; //To stream at 25Hz without using additional timers (time period(ms) =1000/frequency(Hz))
55
56//bool updateSensorData = true; //Flag to update the sensor data. Default is true to perform the first read before the first stream
57
58void setup() //This code is executed once
59{
60 //Peripheral Initialization
61 Serial.begin(115200); //Initialize the Serial Port to view information on the Serial Monitor
62 I2C.begin(); //Initialize I2C communication to the let the library communicate with the sensor.
63 //Sensor Initialization
64 mySensor.initSensor(0x29); //The I2C Address can be changed here inside this function in the library
65 mySensor.setOperationMode(OPERATION_MODE_NDOF); //Can be configured to other operation modes as desired
66 mySensor.setUpdateMode(MANUAL); //The default is AUTO. Changing to manual requires calling the relevant update functions prior to calling the read functions
67}
68
69float g_eula_h;
70float g_eula_r;
71float g_eula_p;
72
73void loop() //This code is looped forever
74{
75 if ((millis() - lastStreamTime) >= streamPeriod)
76 {
77 lastStreamTime = millis();
78 mySensor.updateEuler(); //Update the Euler data into the structure of the object
79 mySensor.updateCalibStatus(); //Update the Calibration Status
80
81 g_eula_h = mySensor.readEulerHeading();
82 g_eula_r = mySensor.readEulerRoll();
83 g_eula_p = mySensor.readEulerPitch();
84#ifdef PRINT_SENSOR
85 Serial.print("Time: ");
86 Serial.print(lastStreamTime);
87 Serial.print("ms ");
88
89 Serial.print(" H: ");
90 Serial.print(g_eula_h); //Heading(h) 0 to 360 data
91 Serial.print("deg ");
92
93 Serial.print(" R: ");
94 Serial.print(g_eula_r); //Roll(y) 180 to -180 data
95 Serial.print("deg");
96
97 Serial.print(" P: ");
98 Serial.print(g_eula_p); //Pitch(x) 180 to -180 data
99 Serial.print("deg ");
100
101 Serial.print(" A: ");
102 Serial.print(mySensor.readAccelCalibStatus()); //Accelerometer Calibration Status (0 - 3)
103
104 Serial.print(" M: ");
105 Serial.print(mySensor.readMagCalibStatus()); //Magnetometer Calibration Status (0 - 3)
106
107 Serial.print(" G: ");
108 Serial.print(mySensor.readGyroCalibStatus()); //Gyroscope Calibration Status (0 - 3)
109
110 Serial.print(" S: ");
111 Serial.print(mySensor.readSystemCalibStatus()); //System Calibration Status (0 - 3)
112
113 Serial.println();
114#endif
115 }
116
117#if 0
118 if (updateSensorData) //Keep the updating of data as a separate task
119 {
120 mySensor.updateAccel(); //Update the Accelerometer data
121 mySensor.updateLinearAccel(); //Update the Linear Acceleration data
122 mySensor.updateGravAccel(); //Update the Gravity Acceleration data
123 mySensor.updateCalibStatus(); //Update the Calibration Status
124 updateSensorData = false;
125 }
126
127 if ((millis() - lastStreamTime) >= streamPeriod)
128 {
129 lastStreamTime = millis();
130 Serial.print("Time: ");
131 Serial.print(lastStreamTime);
132 Serial.print("ms ");
133
134 Serial.print(" aX: ");
135 Serial.print(mySensor.readAccelX()); //Accelerometer X-Axis data
136 Serial.print("m/s2 ");
137
138 Serial.print(" aY: ");
139 Serial.print(mySensor.readAccelY()); //Accelerometer Y-Axis data
140 Serial.print("m/s2 ");
141
142 Serial.print(" aZ: ");
143 Serial.print(mySensor.readAccelZ()); //Accelerometer Z-Axis data
144 Serial.print("m/s2 ");
145
146 Serial.print(" lX: ");
147 Serial.print(mySensor.readLinearAccelX()); //Linear Acceleration X-Axis data
148 Serial.print("m/s2 ");
149
150 Serial.print(" lY: ");
151 Serial.print(mySensor.readLinearAccelY()); //Linear Acceleration Y-Axis data
152 Serial.print("m/s2 ");
153
154 Serial.print(" lZ: ");
155 Serial.print(mySensor.readLinearAccelZ()); //Linear Acceleration Z-Axis data
156 Serial.print("m/s2 ");
157
158 Serial.print(" gX: ");
159 Serial.print(mySensor.readGravAccelX()); //Gravity Acceleration X-Axis data
160 Serial.print("m/s2 ");
161
162 Serial.print(" gY: ");
163 Serial.print(mySensor.readGravAccelY()); //Gravity Acceleration Y-Axis data
164 Serial.print("m/s2 ");
165
166 Serial.print(" gZ: ");
167 Serial.print(mySensor.readGravAccelZ()); //Gravity Acceleration Z-Axis data
168 Serial.print("m/s2 ");
169
170 Serial.print(" C: ");
171 Serial.print(mySensor.readAccelCalibStatus()); //Accelerometer Calibration Status (0 - 3)
172
173 Serial.println();
174 updateSensorData = true;
175 }
176#endif
177
178 delay(1);
179}
180
181bool processing_connected = false;
182
183void establishContact() {
184 while (!Serial5.available()) {
185 Serial5.print('A'); // send a capital A
186 delay(300);
187 }
188 Serial.println("Processing Task : Connect!");
189 processing_connected = true;
190}
191
192void task1_setup()
193{
194 Serial5.begin(115200);
195 Serial.print("Start!");
196}
197
198int inByte = 0;
199int last_connect = 0;
200
201#define TIMEOUT_MS 3000
202
203void task1_loop()
204{
205 if(!processing_connected){
206 establishContact();
207 }else{
208 if((millis() - last_connect) > TIMEOUT_MS){
209 processing_connected = false;
210 Serial.println("Processing Task : Disconnect!");
211 }
212 }
213
214 int16_t roll = (int16_t)g_eula_r;
215 int16_t pitch = (int16_t)g_eula_p;
216 int16_t heading;
217
218 if(g_eula_h <= 180){
219 heading = (int16_t)g_eula_h;
220 }
221 else{
222 heading = (int16_t)(g_eula_h - 360);
223 }
224
225 if (Serial5.available()){
226 inByte = Serial5.read();
227 last_connect = millis();
228
229 Serial5.write((uint8_t)(heading >> 8));
230 Serial5.write((uint8_t)heading);
231 Serial5.write((uint8_t)(roll >> 8));
232 Serial5.write((uint8_t)roll);
233 Serial5.write((uint8_t)(pitch >> 8));
234 Serial5.write((uint8_t)pitch);
235 }
236 delay(1);
237}
Note: See TracBrowser for help on using the repository browser.