Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_LSM9DS1.h>
  4. #include <Adafruit_Sensor.h> // not used in this demo but required!
  5.  
  6. // i2c
  7. Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
  8.  
  9.  
  10. #include <Arduino.h>
  11. #include <SPI.h>
  12. #include "Adafruit_BLE.h"
  13. #include "Adafruit_BluefruitLE_SPI.h"
  14. #include "Adafruit_BluefruitLE_UART.h"
  15. #include "Adafruit_BLEGatt.h"
  16.  
  17. #include "BluefruitConfig.h"
  18.  
  19.  
  20. // Create the bluefruit object
  21. /* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
  22. Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
  23.  
  24. Adafruit_BLEGatt gatt(ble);
  25.  
  26. // A small helper
  27. void error(const __FlashStringHelper*err) {
  28. Serial.println(err);
  29. while (1);
  30. }
  31.  
  32. /* The service information */
  33.  
  34. int32_t htsServiceId;
  35. int32_t htsMeasureCharId;
  36.  
  37. void setupSensor() //IMU sensor
  38. {
  39. // 1.) Set the accelerometer range
  40. lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G);
  41. //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_4G);
  42. //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_8G);
  43. //lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_16G);
  44.  
  45. // 2.) Set the magnetometer sensitivity
  46. lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
  47. //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_8GAUSS);
  48. //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_12GAUSS);
  49. //lsm.setupMag(lsm.LSM9DS1_MAGGAIN_16GAUSS);
  50.  
  51. // 3.) Setup the gyroscope
  52. lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
  53. //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_500DPS);
  54. //lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_2000DPS);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. uint8_t measurement [12] = { 0 }; // Laver et array med 12 pladser - Kaldt measurement til bluetooth del
  63.  
  64.  
  65. int B[10]; //x
  66. int A[10]; //y(x)
  67. int runde = 1;
  68. float gennemsnit = 0;
  69. float degrees = 0;
  70. float lastDegrees = 0;
  71.  
  72.  
  73.  
  74. void setup()
  75. {
  76. Serial.begin(115200);
  77.  
  78. while (!Serial) {
  79. delay(1); // will pause Zero, Leonardo, etc until serial console opens
  80. }
  81.  
  82.  
  83. // Try to initialise and warn if we couldn't detect the chip
  84. if (!lsm.begin())
  85. {
  86. Serial.println("Oops ... unable to initialize the LSM9DS1. Check your wiring!");
  87. while (1);
  88. }
  89. Serial.println("Found LSM9DS1 9DOF");
  90.  
  91. // helper to just set the default scaling we want, see above!
  92. setupSensor();
  93.  
  94. /* Initialise the Bluetooth module */
  95. Serial.print(F("Initialising the Bluefruit LE module: "));
  96.  
  97. if ( !ble.begin(VERBOSE_MODE) )
  98. {
  99. error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  100. }
  101. Serial.println( F("OK!") );
  102.  
  103. /* Perform a factory reset to make sure everything is in a known state */
  104. Serial.println(F("Performing a factory reset: "));
  105. if (! ble.factoryReset() ) {
  106. error(F("Couldn't factory reset"));
  107. }
  108.  
  109. /* Disable command echo from Bluefruit */
  110. ble.echo(false);
  111.  
  112. Serial.println("Requesting Bluefruit info:");
  113. /* Print Bluefruit information */
  114. ble.info();
  115.  
  116. // this line is particularly required for Flora, but is a good idea
  117. // anyways for the super long lines ahead!
  118. // ble.setInterCharWriteDelay(5); // 5 ms
  119.  
  120. Serial.println(F("Adding the Health Thermometer Service definition (UUID = 0x1809): "));
  121. htsServiceId = gatt.addService(0x1809);
  122. if (htsServiceId == 0) {
  123. error(F("Could not add Thermometer service"));
  124. }
  125.  
  126. Serial.println(F("Adding the Temperature Measurement characteristic (UUID = 0x2A1C): "));
  127. htsMeasureCharId = gatt.addCharacteristic(0x2A1C, GATT_CHARS_PROPERTIES_INDICATE, 12, 12, BLE_DATATYPE_BYTEARRAY);
  128. if (htsMeasureCharId == 0) {
  129. error(F("Could not add Temperature characteristic"));
  130. }
  131.  
  132. /* Add the Health Thermometer Service to the advertising data (needed for Nordic apps to detect the service) */
  133. Serial.print(F("Adding Health Thermometer Service UUID to the advertising payload: "));
  134. uint8_t advdata[] { 0x02, 0x01, 0x06, 0x05, 0x02, 0x09, 0x18, 0x0a, 0x18 };
  135. ble.setAdvData( advdata, sizeof(advdata) );
  136.  
  137. /* Reset the device for the new service setting changes to take effect */
  138. Serial.print(F("Performing a SW reset (service changes require a reset): "));
  139. ble.reset();
  140.  
  141. Serial.println();
  142.  
  143.  
  144. KalibrerNul(); //første runde og kalibreringsdata af 0-grader er slut
  145. }
  146. /** Send randomized heart rate data continuously **/
  147.  
  148.  
  149. void loop() {
  150.  
  151. lsm.read(); /* ask it to read in the data */
  152. char data = Serial.read();
  153. Serial.print("runde \t");
  154. Serial.println(runde);
  155.  
  156.  
  157. if (data = 'k') { // hvis 'k' => Kalibre360
  158. Kalibrer360();
  159. }
  160.  
  161.  
  162. /* Get a new sensor event */
  163. sensors_event_t a, m, g, temp;
  164. lsm.getEvent(&a, &m, &g, &temp);
  165.  
  166.  
  167. int gyroData_y = lsm.gyroData.y; //skriv y
  168. degrees = gyroData_y - gennemsnit;
  169.  
  170. Serial.print (gyroData_y);
  171. Serial.print(" - ");
  172. Serial.print (gennemsnit);
  173. Serial.print(" = ");
  174. Serial.println(degrees);
  175. //Serial.println(lsm.gyroData.z);
  176.  
  177. degrees += lastDegrees;
  178.  
  179.  
  180.  
  181. lastDegrees = degrees;
  182. DataToBluetooth();
  183.  
  184. delay(100);
  185. runde++;
  186.  
  187. }
  188.  
  189.  
  190. void Kalibrer360() {
  191.  
  192. }
  193.  
  194.  
  195. void KalibrerNul() {
  196. Serial.println("n; \t x[n-1]; \t y= x[n-1]+n \t y[n-1] \t integreret");
  197. for ( int i = 0 ; i < 10; i++ ) {
  198. lsm.read(); //lsm (sensoer) læser data
  199. B[i] = lsm.gyroData.y; //læser w_acc om y-akse
  200. Serial.println(B[i]); //n
  201. /*Serial.print("\t ");
  202. int x = B[i - 1]; //x[n-1]
  203. Serial.print(x); // + B[runde - 1]+ B[runde - 2]+ B[runde - 3]+ B[runde - 4]+ B[runde - 5]+ B[runde - 6]+ B[runde - 7]+ B[runde - 8]); //data nuvæBende væBdi sammenlægges med davæBende væBdi
  204. Serial.print("\t\t ");
  205. int y = B[i] + B[i - 1];
  206. A[i] = y; // gem y på plads i (i Array A)
  207. Serial.print(y);//y=x[n-1]+n
  208. Serial.print("\t\t ");
  209. Serial.print(A[i - 1]); //y[n-1]
  210. int z = A[i - 1];
  211. Serial.print("\t\t ");
  212. Serial.println(y + z); //Y(n)= x[n]+y[n-1] = integreret
  213. int sum = B[9] + B[9 - 1]+ A[9 - 1];
  214. Gns = sum/10;*/
  215. }
  216. Serial.print("\t Gns; ");
  217.  
  218. float sum = 0; //for at kunne læse sum efter for-loop
  219. for ( int i = 0 ; i < 10; i++ ) {
  220. sum += B[i]; //+ B[2] + B[3] + B[4] + B[5] + B[6] + B[7] + B[8] + B[9] + B[10]) / 10);
  221. }
  222. gennemsnit = sum / 10;
  223. Serial.println (gennemsnit);
  224. }
  225.  
  226. void DataToBluetooth() {
  227. measurement[1] = ((unsigned int)lsm.accelData.x) / 256;
  228. measurement[0] = ((unsigned int)lsm.accelData.x) & 255;
  229. measurement[3] = ((unsigned int)lsm.accelData.y) / 256;
  230. measurement[2] = ((unsigned int)lsm.accelData.y) & 255;
  231. measurement[5] = ((unsigned int)lsm.accelData.z) / 256;
  232. measurement[4] = ((unsigned int)lsm.accelData.z) & 255;
  233.  
  234. measurement[7] = ((unsigned int)lsm.gyroData.x) / 256;
  235. measurement[6] = ((unsigned int)lsm.gyroData.x) & 255;
  236. //measurement[9] = ((unsigned int)lsm.gyroData.y) / 256;
  237. //measurement[8] = ((unsigned int)lsm.gyroData.y) & 255;
  238. measurement[9] = ((unsigned int)degrees) / 256;
  239. measurement[8] = ((unsigned int)degrees) & 255;
  240.  
  241.  
  242. measurement[11] = ((unsigned int)lsm.gyroData.z) / 256;
  243. measurement[10] = ((unsigned int)lsm.gyroData.z) & 255; //et array på 12 byte, som overføres til Inventor
  244.  
  245. // TODO temperature is not correct due to Bluetooth use IEEE-11073 format
  246. gatt.setChar(htsMeasureCharId, measurement, 12); //send 12 bytes bliver sat ind, som arg(measurement).. Send 12 elemneter(bytes) i arrayet afsted via bluetooth med krakteristik af htsMeasureCharId
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement