[136] | 1 | /****************************************************************************
|
---|
| 2 | * Copyright (C) 2011 - 2014 Bosch Sensortec GmbH
|
---|
| 3 | *
|
---|
| 4 | * Accelerometer.ino
|
---|
| 5 | * Date: 2014/09/09
|
---|
| 6 | * Revision: 3.0 $
|
---|
| 7 | *
|
---|
| 8 | * Usage: Example code to stream Accelerometer data
|
---|
| 9 | *
|
---|
| 10 | ****************************************************************************
|
---|
| 11 | /***************************************************************************
|
---|
| 12 | * License:
|
---|
| 13 | *
|
---|
| 14 | * Redistribution and use in source and binary forms, with or without
|
---|
| 15 | * modification, are permitted provided that the following conditions are met:
|
---|
| 16 | *
|
---|
| 17 | * Redistributions of source code must retain the above copyright
|
---|
| 18 | * notice, this list of conditions and the following disclaimer.
|
---|
| 19 | *
|
---|
| 20 | * Redistributions in binary form must reproduce the above copyright
|
---|
| 21 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 22 | * documentation and/or other materials provided with the distribution.
|
---|
| 23 | *
|
---|
| 24 | * Neither the name of the copyright holder nor the names of the
|
---|
| 25 | * contributors may be used to endorse or promote products derived from
|
---|
| 26 | * this software without specific prior written permission.
|
---|
| 27 | *
|
---|
| 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
| 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
| 30 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 31 | * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
---|
| 32 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 33 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 35 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 37 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
|
---|
| 38 | *
|
---|
| 39 | * The information provided is believed to be accurate and reliable.
|
---|
| 40 | * The copyright holder assumes no responsibility for the consequences of use
|
---|
| 41 | * of such information nor for any infringement of patents or
|
---|
| 42 | * other rights of third parties which may result from its use.
|
---|
| 43 | * No license is granted by implication or otherwise under any patent or
|
---|
| 44 | * patent rights of the copyright holder.
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | #include "NAxisMotion.h" //Contains the bridge code between the API and the Arduino Environment
|
---|
| 48 | #include <Wire.h>
|
---|
| 49 |
|
---|
| 50 | NAxisMotion mySensor; //Object that for the sensor
|
---|
| 51 | unsigned long lastStreamTime = 0; //To store the last streamed time stamp
|
---|
| 52 | const int streamPeriod = 40; //To stream at 25Hz without using additional timers (time period(ms) =1000/frequency(Hz))
|
---|
| 53 | bool updateSensorData = true; //Flag to update the sensor data. Default is true to perform the first read before the first stream
|
---|
| 54 |
|
---|
| 55 | void setup() //This code is executed once
|
---|
| 56 | {
|
---|
| 57 | //Peripheral Initialization
|
---|
| 58 | Serial.begin(115200); //Initialize the Serial Port to view information on the Serial Monitor
|
---|
| 59 | I2C.begin(); //Initialize I2C communication to the let the library communicate with the sensor.
|
---|
| 60 | //Sensor Initialization
|
---|
| 61 | mySensor.initSensor(); //The I2C Address can be changed here inside this function in the library
|
---|
| 62 | mySensor.setOperationMode(OPERATION_MODE_NDOF); //Can be configured to other operation modes as desired
|
---|
| 63 | mySensor.setUpdateMode(MANUAL); //The default is AUTO. Changing to manual requires calling the relevant update functions prior to calling the read functions
|
---|
| 64 | //Setting to MANUAL requires lesser reads to the sensor
|
---|
| 65 | mySensor.updateAccelConfig();
|
---|
| 66 | updateSensorData = true;
|
---|
| 67 | Serial.println();
|
---|
| 68 | Serial.println("Default accelerometer configuration settings...");
|
---|
| 69 | Serial.print("Range: ");
|
---|
| 70 | Serial.println(mySensor.readAccelRange());
|
---|
| 71 | Serial.print("Bandwidth: ");
|
---|
| 72 | Serial.println(mySensor.readAccelBandwidth());
|
---|
| 73 | Serial.print("Power Mode: ");
|
---|
| 74 | Serial.println(mySensor.readAccelPowerMode());
|
---|
| 75 | Serial.println("Streaming in ..."); //Countdown
|
---|
| 76 | Serial.print("3...");
|
---|
| 77 | delay(1000); //Wait for a second
|
---|
| 78 | Serial.print("2...");
|
---|
| 79 | delay(1000); //Wait for a second
|
---|
| 80 | Serial.println("1...");
|
---|
| 81 | delay(1000); //Wait for a second
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void loop() //This code is looped forever
|
---|
| 85 | {
|
---|
| 86 | if (updateSensorData) //Keep the updating of data as a separate task
|
---|
| 87 | {
|
---|
| 88 | mySensor.updateAccel(); //Update the Accelerometer data
|
---|
| 89 | mySensor.updateLinearAccel(); //Update the Linear Acceleration data
|
---|
| 90 | mySensor.updateGravAccel(); //Update the Gravity Acceleration data
|
---|
| 91 | mySensor.updateCalibStatus(); //Update the Calibration Status
|
---|
| 92 | updateSensorData = false;
|
---|
| 93 | }
|
---|
| 94 | if ((millis() - lastStreamTime) >= streamPeriod)
|
---|
| 95 | {
|
---|
| 96 | lastStreamTime = millis();
|
---|
| 97 |
|
---|
| 98 | Serial.print("Time: ");
|
---|
| 99 | Serial.print(lastStreamTime);
|
---|
| 100 | Serial.print("ms ");
|
---|
| 101 |
|
---|
| 102 | Serial.print(" aX: ");
|
---|
| 103 | Serial.print(mySensor.readAccelX()); //Accelerometer X-Axis data
|
---|
| 104 | Serial.print("m/s2 ");
|
---|
| 105 |
|
---|
| 106 | Serial.print(" aY: ");
|
---|
| 107 | Serial.print(mySensor.readAccelY()); //Accelerometer Y-Axis data
|
---|
| 108 | Serial.print("m/s2 ");
|
---|
| 109 |
|
---|
| 110 | Serial.print(" aZ: ");
|
---|
| 111 | Serial.print(mySensor.readAccelZ()); //Accelerometer Z-Axis data
|
---|
| 112 | Serial.print("m/s2 ");
|
---|
| 113 |
|
---|
| 114 | Serial.print(" lX: ");
|
---|
| 115 | Serial.print(mySensor.readLinearAccelX()); //Linear Acceleration X-Axis data
|
---|
| 116 | Serial.print("m/s2 ");
|
---|
| 117 |
|
---|
| 118 | Serial.print(" lY: ");
|
---|
| 119 | Serial.print(mySensor.readLinearAccelY()); //Linear Acceleration Y-Axis data
|
---|
| 120 | Serial.print("m/s2 ");
|
---|
| 121 |
|
---|
| 122 | Serial.print(" lZ: ");
|
---|
| 123 | Serial.print(mySensor.readLinearAccelZ()); //Linear Acceleration Z-Axis data
|
---|
| 124 | Serial.print("m/s2 ");
|
---|
| 125 |
|
---|
| 126 | Serial.print(" gX: ");
|
---|
| 127 | Serial.print(mySensor.readGravAccelX()); //Gravity Acceleration X-Axis data
|
---|
| 128 | Serial.print("m/s2 ");
|
---|
| 129 |
|
---|
| 130 | Serial.print(" gY: ");
|
---|
| 131 | Serial.print(mySensor.readGravAccelY()); //Gravity Acceleration Y-Axis data
|
---|
| 132 | Serial.print("m/s2 ");
|
---|
| 133 |
|
---|
| 134 | Serial.print(" gZ: ");
|
---|
| 135 | Serial.print(mySensor.readGravAccelZ()); //Gravity Acceleration Z-Axis data
|
---|
| 136 | Serial.print("m/s2 ");
|
---|
| 137 |
|
---|
| 138 | Serial.print(" C: ");
|
---|
| 139 | Serial.print(mySensor.readAccelCalibStatus()); //Accelerometer Calibration Status (0 - 3)
|
---|
| 140 |
|
---|
| 141 | Serial.println();
|
---|
| 142 |
|
---|
| 143 | updateSensorData = true;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|