[chronojump] Accelerometer bluetooth arduino code added



commit 558a318a97a9cbfe23f40698332e6b7b653a4a31
Author: Xavier Padullés <x padulles gmail com>
Date:   Mon Sep 9 15:45:35 2019 +0200

    Accelerometer bluetooth arduino code added

 arduino/accelerometer/accelerometer.ino | 284 ++++++++++++++++++++++++++++++++
 1 file changed, 284 insertions(+)
---
diff --git a/arduino/accelerometer/accelerometer.ino b/arduino/accelerometer/accelerometer.ino
new file mode 100644
index 00000000..4082af4c
--- /dev/null
+++ b/arduino/accelerometer/accelerometer.ino
@@ -0,0 +1,284 @@
+#include <Wire.h>
+#include <Adafruit_Sensor.h>
+#include <Adafruit_BNO055.h>
+#include <utility/imumaths.h>
+
+/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
+   which provides a common 'type' for sensor data and some helper functions.
+
+   To use this driver you will also need to download the Adafruit_Sensor
+   library and include it in your libraries folder.
+
+   You should also assign a unique ID to this sensor for use with
+   the Adafruit Sensor API so that you can identify this particular
+   sensor in any data logs, etc.  To assign a unique ID, simply
+   provide an appropriate value in the constructor below (12345
+   is used by default in this example).
+
+   Connections
+   ===========
+   Connect SCL to analog 5
+   Connect SDA to analog 4
+   Connect VDD to 3-5V DC
+   Connect GROUND to common ground
+
+   History
+   =======
+   2015/MAR/03  - First release (KTOWN)
+   2015/AUG/27  - Added calibration and system status helpers
+*/
+
+String version = "Accelerometer-0.1";
+
+unsigned long lastTime = 0;
+unsigned long currentTime = 0;
+unsigned long elapsedTime = 0;
+unsigned long totalTime = 0;
+
+
+//Wether the sensor has to capture or not
+boolean capturing = false;
+
+/* Set the delay between fresh samples */
+#define BNO055_SAMPLERATE_DELAY_MS (100)
+
+Adafruit_BNO055 bno = Adafruit_BNO055(55);
+
+/**************************************************************************/
+/*
+    Arduino setup function (automatically called at startup)
+*/
+/**************************************************************************/
+void setup(void)
+{
+  Serial.begin(115200, SERIAL_8N1);  // Inicializamos  el puerto serie
+  //Serial.println("Orientation Sensor Test"); Serial.println("");
+
+  /* Initialise the sensor */
+  if (!bno.begin())
+  {
+    /* There was a problem detecting the BNO055 ... check your connections */
+    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
+    while (1);
+  }
+
+  delay(1000);
+
+  /* Display some basic information on this sensor */
+  //displaySensorDetails();
+
+  /* Optional: Display current status */
+  //displaySensorStatus();
+
+  bno.setExtCrystalUse(true);
+
+}
+
+/**************************************************************************/
+/*
+    Arduino loop function, called once 'setup' is complete (your own code
+    should go here)
+*/
+/**************************************************************************/
+void loop(void)
+{
+  if (capturing)
+  {
+    //Mark of starting data frame 
+    Serial.write(0xFF);
+
+    //getting time
+    currentTime = micros();
+    
+    /* Get a new sensor event */
+    sensors_event_t orientationData, linearAccelData;
+    //bno.getEvent(&event);
+    bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER);
+    bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL);
+
+    
+
+    //Managing the timer overflow
+    if (currentTime > lastTime)      //No overflow
+    {
+      elapsedTime = currentTime - lastTime;
+    } else  if (currentTime <= lastTime)  //Overflow
+    {
+      elapsedTime = (4294967295 - lastTime) + currentTime; //Time from the last measure to the overflow 
event plus the currentTime
+    }
+    totalTime += elapsedTime;
+    lastTime = currentTime;
+    sendLong(totalTime);
+
+    sendFloat(orientationData.orientation.x);
+    sendFloat(orientationData.orientation.y);
+    sendFloat(orientationData.orientation.z);
+
+    sendFloat(orientationData.acceleration.x);
+    sendFloat(orientationData.acceleration.y);
+    sendFloat(orientationData.acceleration.z);
+
+    /* Optional: Display calibration status */
+    //displayCalStatus();
+
+    /* Optional: Display sensor status (debug only) */
+    //displaySensorStatus();
+
+    /* New line for the next sample */
+    //  Serial.println("");
+
+    /* Wait the specified delay before requesting nex data */
+    delay(BNO055_SAMPLERATE_DELAY_MS);
+  }
+}
+
+
+
+/**************************************************************************/
+/*
+    Displays some basic information on this sensor from the unified
+    sensor API sensor_t type (see Adafruit_Sensor for more information)
+*/
+/**************************************************************************/
+void displaySensorDetails(void)
+{
+  sensor_t sensor;
+  bno.getSensor(&sensor);
+  Serial.println("------------------------------------");
+  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
+  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
+  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
+  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
+  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
+  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
+  Serial.println("------------------------------------");
+  Serial.println("");
+  delay(500);
+}
+
+/**************************************************************************/
+/*
+    Display some basic info about the sensor status
+*/
+/**************************************************************************/
+void displaySensorStatus(void)
+{
+  /* Get the system status values (mostly for debugging purposes) */
+  uint8_t system_status, self_test_results, system_error;
+  system_status = self_test_results = system_error = 0;
+  bno.getSystemStatus(&system_status, &self_test_results, &system_error);
+
+  /* Display the results in the Serial Monitor */
+  Serial.println("");
+  Serial.print("System Status: 0x");
+  Serial.println(system_status, HEX);
+  Serial.print("Self Test:     0x");
+  Serial.println(self_test_results, HEX);
+  Serial.print("System Error:  0x");
+  Serial.println(system_error, HEX);
+  Serial.println("");
+  delay(500);
+}
+
+/**************************************************************************/
+/*
+    Display sensor calibration status
+*/
+/**************************************************************************/
+void displayCalStatus(void)
+{
+  /* Get the four calibration values (0..3) */
+  /* Any sensor data reporting 0 should be ignored, */
+  /* 3 means 'fully calibrated" */
+  uint8_t system, gyro, accel, mag;
+  system = gyro = accel = mag = 0;
+  bno.getCalibration(&system, &gyro, &accel, &mag);
+
+  /* The data should be ignored until the system calibration is > 0 */
+  Serial.print("");
+  if (!system)
+  {
+    Serial.print("! ");
+  }
+
+  /* Display the individual values */
+  Serial.print("Sys:");
+  Serial.print(system, DEC);
+  Serial.print(" Gyro:");
+  Serial.print(gyro, DEC);
+  Serial.print(" Accel:");
+  Serial.print(accel, DEC);
+  Serial.print(" Magn:");
+  Serial.println(mag, DEC);
+  Serial.println("------------------------------------");
+
+}
+
+void serialEvent()
+{
+  String inputString = Serial.readString();
+  String commandString = inputString.substring(0, inputString.lastIndexOf(":"));
+
+  if (commandString == "start_capture") {
+    start_capture();
+  } else if (commandString == "end_capture") {
+    end_capture();
+  } else if (commandString == "get_version") {
+    get_version();
+    } else if (commandString == "get_status") {
+      displaySensorDetails();
+      displayCalStatus();
+      displaySensorStatus();
+  } else {
+    Serial.println("Not a valid command");
+  }
+  inputString = "";
+}
+
+void start_capture()
+{
+  Serial.println("Starting capture...");
+  totalTime = 0;
+  lastTime = micros();
+  capturing = true;
+}
+
+void end_capture()
+{
+  capturing = false;
+  Serial.println("");
+  Serial.println("Capture ended");
+}
+void get_version()
+{
+  Serial.println(version);
+}
+
+void sendInt(int i) {
+  byte * b = (byte *) &i;
+  Serial.write(b[0]); //Least significant bit
+  Serial.write(b[1]); //Most significant bit
+  Serial.flush();
+  return;
+}
+
+void sendLong(long l) {
+  byte * b = (byte *) &l;
+  Serial.write(b[0]); //Least significant bit
+  Serial.write(b[1]);
+  Serial.write(b[2]);
+  Serial.write(b[3]); //Most significant bit
+  Serial.flush();
+  return;
+}
+
+
+void sendFloat(float f) {
+  byte * b = (byte *) &f;
+  Serial.write(b[0]); //Least significant bit
+  Serial.write(b[1]);
+  Serial.write(b[2]);
+  Serial.write(b[3]); //Most significant bit
+  Serial.flush();
+  return;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]