Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 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. float sizeOfB = 100;
  65.  
  66. int B[100]; //x
  67. int A[10]; //y(x)
  68. int runde = 1;
  69. float gennemsnit = 0;
  70. float degrees = 0;
  71. float lastDegrees = 0;
  72.  
  73. float lastDegreesMinus1 = 0;
  74. float degressNu = 0;
  75.  
  76.  
  77. void setup()
  78. {
  79. Serial.begin(115200);
  80.  
  81. while (!Serial) {
  82. delay(1); // will pause Zero, Leonardo, etc until serial console opens
  83. }
  84.  
  85.  
  86. // Try to initialise and warn if we couldn't detect the chip
  87. if (!lsm.begin())
  88. {
  89. Serial.println("Oops ... unable to initialize the LSM9DS1. Check your wiring!");
  90. while (1);
  91. }
  92. Serial.println("Found LSM9DS1 9DOF");
  93.  
  94. // helper to just set the default scaling we want, see above!
  95. setupSensor();
  96.  
  97. /* Initialise the Bluetooth module */
  98. Serial.print(F("Initialising the Bluefruit LE module: "));
  99.  
  100. if ( !ble.begin(VERBOSE_MODE) )
  101. {
  102. error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
  103. }
  104. Serial.println( F("OK!") );
  105.  
  106. /* Perform a factory reset to make sure everything is in a known state */
  107. Serial.println(F("Performing a factory reset: "));
  108. if (! ble.factoryReset() ) {
  109. error(F("Couldn't factory reset"));
  110. }
  111.  
  112. /* Disable command echo from Bluefruit */
  113. ble.echo(false);
  114.  
  115. Serial.println("Requesting Bluefruit info:");
  116. /* Print Bluefruit information */
  117. ble.info();
  118.  
  119. // this line is particularly required for Flora, but is a good idea
  120. // anyways for the super long lines ahead!
  121. // ble.setInterCharWriteDelay(5); // 5 ms
  122.  
  123. Serial.println(F("Adding the Health Thermometer Service definition (UUID = 0x1809): "));
  124. htsServiceId = gatt.addService(0x1809);
  125. if (htsServiceId == 0) {
  126. error(F("Could not add Thermometer service"));
  127. }
  128.  
  129. Serial.println(F("Adding the Temperature Measurement characteristic (UUID = 0x2A1C): "));
  130. htsMeasureCharId = gatt.addCharacteristic(0x2A1C, GATT_CHARS_PROPERTIES_INDICATE, 12, 12, BLE_DATATYPE_BYTEARRAY);
  131. if (htsMeasureCharId == 0) {
  132. error(F("Could not add Temperature characteristic"));
  133. }
  134.  
  135. /* Add the Health Thermometer Service to the advertising data (needed for Nordic apps to detect the service) */
  136. Serial.print(F("Adding Health Thermometer Service UUID to the advertising payload: "));
  137. uint8_t advdata[] { 0x02, 0x01, 0x06, 0x05, 0x02, 0x09, 0x18, 0x0a, 0x18 };
  138. ble.setAdvData( advdata, sizeof(advdata) );
  139.  
  140. /* Reset the device for the new service setting changes to take effect */
  141. Serial.print(F("Performing a SW reset (service changes require a reset): "));
  142. ble.reset();
  143.  
  144. Serial.println();
  145.  
  146. KalibrerNul(); //første runde og kalibreringsdata af 0-grader er slut
  147. }
  148. /** Send randomized heart rate data continuously **/
  149.  
  150.  
  151. void loop() {
  152.  
  153. lsm.read(); /* ask it to read in the data */
  154. char data = Serial.read();
  155. Serial.print("runde \t");
  156. Serial.println(runde);
  157.  
  158.  
  159. if (data = 'k') { // hvis 'k' => Kalibre360
  160. Kalibrer360();
  161. }
  162.  
  163.  
  164. /* Get a new sensor event */
  165. sensors_event_t a, m, g, temp;
  166. lsm.getEvent(&a, &m, &g, &temp);
  167.  
  168.  
  169. int gyroData_y = lsm.gyroData.y; //skriv y
  170. degrees = gyroData_y - gennemsnit;
  171.  
  172. Serial.print("gyroData_y:");
  173. Serial.print (gyroData_y);
  174. Serial.print(" - gennemsnit");
  175. Serial.print (gennemsnit);
  176. Serial.print(" = degrees");
  177. Serial.println(degrees);
  178. //Serial.println(lsm.gyroData.z);
  179.  
  180.  
  181. lastDegreesMinus1 = degrees + lastDegrees;
  182.  
  183. Serial.print("degrees:");
  184. Serial.print (degrees);
  185. Serial.print(" + lastDegrees");
  186. Serial.print (lastDegrees);
  187. Serial.print(" = lastDegreesMinus1");
  188. Serial.println(lastDegreesMinus1);
  189.  
  190.  
  191. degressNu = degrees + lastDegreesMinus1;
  192.  
  193. Serial.print("degrees:");
  194. Serial.print (degrees);
  195. Serial.print(" + lastDegreesMinus1:");
  196. Serial.print (lastDegreesMinus1);
  197. Serial.print(" = degressNu:");
  198. Serial.println(degressNu);
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. lastDegreesMinus1 = lastDegrees;
  209. Serial.print("lastDegreesMinus1:");
  210. Serial.println(lastDegreesMinus1);
  211.  
  212.  
  213. lastDegrees = degrees;
  214. Serial.print("lastDegrees:");
  215. Serial.println(lastDegrees);
  216.  
  217.  
  218. DataToBluetooth();
  219.  
  220. delay(100);
  221. runde++;
  222.  
  223. }
  224.  
  225.  
  226. void Kalibrer360() {
  227.  
  228. }
  229.  
  230.  
  231. void KalibrerNul() {
  232. Serial.println("n; \t x[n-1]; \t y= x[n-1]+n \t y[n-1] \t integreret");
  233. for ( int i = 0 ; i < sizeOfB; i++ ) {
  234. lsm.read(); //lsm (sensoer) læser data
  235. B[i] = lsm.gyroData.y; //læser w_acc om y-akse
  236. Serial.println(B[i]); //n
  237. /*Serial.print("\t ");
  238. int x = B[i - 1]; //x[n-1]
  239. 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
  240. Serial.print("\t\t ");
  241. int y = B[i] + B[i - 1];
  242. A[i] = y; // gem y på plads i (i Array A)
  243. Serial.print(y);//y=x[n-1]+n
  244. Serial.print("\t\t ");
  245. Serial.print(A[i - 1]); //y[n-1]
  246. int z = A[i - 1];
  247. Serial.print("\t\t ");
  248. Serial.println(y + z); //Y(n)= x[n]+y[n-1] = integreret
  249. int sum = B[9] + B[9 - 1]+ A[9 - 1];
  250. Gns = sum/10;*/
  251. }
  252. Serial.print("\t Gns; ");
  253.  
  254. float sum = 0; //for at kunne læse sum efter for-loop
  255. for ( int i = 0 ; i < sizeOfB; i++ ) {
  256. sum += B[i]; //+ B[2] + B[3] + B[4] + B[5] + B[6] + B[7] + B[8] + B[9] + B[10]) / 10);
  257. }
  258. gennemsnit = sum / sizeOfB;
  259. Serial.println (gennemsnit);
  260. }
  261.  
  262. void DataToBluetooth() {
  263. measurement[1] = ((unsigned int)lsm.accelData.x) / 256;
  264. measurement[0] = ((unsigned int)lsm.accelData.x) & 255;
  265. measurement[3] = ((unsigned int)lsm.accelData.y) / 256;
  266. measurement[2] = ((unsigned int)lsm.accelData.y) & 255;
  267. measurement[5] = ((unsigned int)lsm.accelData.z) / 256;
  268. measurement[4] = ((unsigned int)lsm.accelData.z) & 255;
  269.  
  270. measurement[7] = ((unsigned int)lsm.gyroData.x) / 256;
  271. measurement[6] = ((unsigned int)lsm.gyroData.x) & 255;
  272. //measurement[9] = ((unsigned int)lsm.gyroData.y) / 256;
  273. //measurement[8] = ((unsigned int)lsm.gyroData.y) & 255;
  274. measurement[9] = ((unsigned int)degrees) / 256;
  275. measurement[8] = ((unsigned int)degrees) & 255;
  276. measurement[11] = ((unsigned int)lsm.gyroData.z) / 256;
  277. measurement[10] = ((unsigned int)lsm.gyroData.z) & 255; //et array på 12 byte, som overføres til Inventor
  278.  
  279. // TODO temperature is not correct due to Bluetooth use IEEE-11073 format
  280. 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
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement