Advertisement
Guest User

experimental_TX

a guest
Jun 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.56 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BNO055.h>
  4. #include <utility/imumaths.h>
  5. #include <SPI.h>
  6. #include <RH_RF95.h>
  7.  
  8. /* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
  9.    which provides a common 'type' for sensor data and some helper functions.
  10.  
  11.    To use this driver you will also need to download the Adafruit_Sensor
  12.    library and include it in your libraries folder.
  13.  
  14.    You should also assign a unique ID to this sensor for use with
  15.    the Adafruit Sensor API so that you can identify this particular
  16.    sensor in any data logs, etc.  To assign a unique ID, simply
  17.    provide an appropriate value in the constructor below (12345
  18.    is used by default in this example).
  19.  
  20.    Connections
  21.    ===========
  22.    Connect SCL to analog 5
  23.    Connect SDA to analog 4
  24.    Connect VDD to 3-5V DC
  25.    Connect GROUND to common ground
  26.  
  27.    History
  28.    =======
  29.    2015/MAR/03  - First release (KTOWN)
  30.    2015/AUG/27  - Added calibration and system status helpers
  31. */
  32.  
  33. /* Set the delay between fresh samples */
  34. #define BNO055_SAMPLERATE_DELAY_MS (10)
  35. RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95
  36.  
  37. uint8_t calibData[11] = {-13,-25,-13,-2,0,0,-93,276,306,1000,716};
  38.  
  39. Adafruit_BNO055 bno = Adafruit_BNO055(55);
  40. int led = 9;
  41. /**************************************************************************/
  42. /*
  43.     Displays some basic information on this sensor from the unified
  44.     sensor API sensor_t type (see Adafruit_Sensor for more information)
  45. */
  46. /**************************************************************************/
  47. void displaySensorDetails(void)
  48. {
  49.   sensor_t sensor;
  50.   bno.getSensor(&sensor);
  51.   Serial.println("------------------------------------");
  52.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  53.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  54.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  55.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
  56.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
  57.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
  58.   Serial.println("------------------------------------");
  59.   Serial.println("");
  60.   delay(500);
  61. }
  62.  
  63. /**************************************************************************/
  64. /*
  65.     Display some basic info about the sensor status
  66. */
  67. /**************************************************************************/
  68. void displaySensorStatus(void)
  69. {
  70.   /* Get the system status values (mostly for debugging purposes) */
  71.   uint8_t system_status, self_test_results, system_error;
  72.   system_status = self_test_results = system_error = 0;
  73.   bno.getSystemStatus(&system_status, &self_test_results, &system_error);
  74.  
  75.   /* Display the results in the Serial Monitor */
  76.   Serial.println("");
  77.   Serial.print("System Status: 0x");
  78.   Serial.println(system_status, HEX);
  79.   Serial.print("Self Test:     0x");
  80.   Serial.println(self_test_results, HEX);
  81.   Serial.print("System Error:  0x");
  82.   Serial.println(system_error, HEX);
  83.   Serial.println("");
  84.   delay(500);
  85. }
  86.  
  87. /**************************************************************************/
  88. /*
  89.     Display sensor calibration status
  90. */
  91. /**************************************************************************/
  92. void displayCalStatus(void)
  93. {
  94.   /* Get the four calibration values (0..3) */
  95.   /* Any sensor data reporting 0 should be ignored, */
  96.   /* 3 means 'fully calibrated" */
  97.   uint8_t system, gyro, accel, mag;
  98.   system = gyro = accel = mag = 0;
  99.   bno.getCalibration(&system, &gyro, &accel, &mag);
  100.  
  101.   /* The data should be ignored until the system calibration is > 0 */
  102.   Serial.print("\t");
  103.   if (!system)
  104.   {
  105.     Serial.print("! ");
  106.   }
  107.  
  108.   /* Display the individual values */
  109.   Serial.print("Sys:");
  110.   Serial.print(system, DEC);
  111.   Serial.print(" G:");
  112.   Serial.print(gyro, DEC);
  113.   Serial.print(" A:");
  114.   Serial.print(accel, DEC);
  115.   Serial.print(" M:");
  116.   Serial.print(mag, DEC);
  117. }
  118.  
  119. /**************************************************************************/
  120. /*
  121.     dtostrf emulation for Feather M0
  122. */
  123. /**************************************************************************/
  124. char *dtostrf(double value, int width, unsigned int precision, char *result)
  125. {
  126.   int decpt, sign, reqd, pad;
  127.   const char *s, *e;
  128.   char *p;
  129.   s = fcvt(value, precision, &decpt, &sign);
  130.   if (precision == 0 && decpt == 0) {
  131.   s = (*s < '5') ? "0" : "1";
  132.     reqd = 1;
  133.   } else {
  134.     reqd = strlen(s);
  135.     if (reqd > decpt) reqd++;
  136.     if (decpt == 0) reqd++;
  137.   }
  138.   if (sign) reqd++;
  139.   p = result;
  140.   e = p + reqd;
  141.   pad = width - reqd;
  142.   if (pad > 0) {
  143.     e += pad;
  144.     while (pad-- > 0) *p++ = ' ';
  145.   }
  146.   if (sign) *p++ = '-';
  147.   if (decpt <= 0 && precision > 0) {
  148.     *p++ = '0';
  149.     *p++ = '.';
  150.     e++;
  151.     while ( decpt < 0 ) {
  152.       decpt++;
  153.       *p++ = '0';
  154.     }
  155.   }    
  156.   while (p < e) {
  157.     *p++ = *s++;
  158.     if (p == e) break;
  159.     if (--decpt == 0) *p++ = '.';
  160.   }
  161.   if (width < 0) {
  162.     pad = (reqd + width) * -1;
  163.     while (pad-- > 0) *p++ = ' ';
  164.   }
  165.   *p = 0;
  166.   return result;
  167. }
  168. /**************************************************************************/
  169. /*
  170.     Arduino setup function (automatically called at startup)
  171. */
  172. /**************************************************************************/
  173. void setup(void)
  174. {
  175.   Serial.begin(115200);
  176.   Serial.println("Orientation Sensor Test"); Serial.println("");
  177.  pinMode(led, OUTPUT);    
  178.  // pinMode(RFM95_RST, OUTPUT);
  179.   //digitalWrite(RFM95_RST, HIGH);
  180.  // Serial.begin(9600);
  181.   //while (!Serial) ; // Wait for serial port to be available
  182.   if (!rf95.init())
  183.     Serial.println("init failed");  
  184.   // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
  185.  
  186. rf95.setModemConfig(RH_RF95::Bw500Cr45Sf128);
  187. rf95.setFrequency(915.0);
  188. rf95.setTxPower(23, false);
  189.  
  190.   // The default transmitter power is 13dBm, using PA_BOOST.
  191.   // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
  192.   // you can set transmitter powers from 5 to 23 dBm:
  193. //  driver.setTxPower(23, false);
  194.   // If you are using Modtronix inAir4 or inAir9,or any other module which uses the
  195.   // transmitter RFO pins and not the PA_BOOST pins
  196.   // then you can configure the power transmitter power for -1 to 14 dBm and with useRFO true.
  197.   // Failure to do that will result in extremely low transmit powers.
  198. //  driver.setTxPower(14, true);
  199.  
  200.   /* Initialise the sensor */
  201.   if(!bno.begin())
  202.   {
  203.     /* There was a problem detecting the BNO055 ... check your connections */
  204.     Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
  205.     while(1);
  206.   }
  207.  
  208.   delay(1000);
  209.  
  210.   /* Display some basic information on this sensor */
  211.   displaySensorDetails();
  212.  
  213.   /* Optional: Display current status */
  214.   displaySensorStatus();
  215.   bno.setSensorOffsets(calibData);
  216.   bno.setExtCrystalUse(true);
  217. }
  218.  
  219. /**************************************************************************/
  220. /*
  221.     Arduino loop function, called once 'setup' is complete (your own code
  222.     should go here)
  223. */
  224. /**************************************************************************/
  225. void loop(void)
  226. {
  227.   /* Get a new sensor event */
  228.   sensors_event_t event;
  229.   bno.getEvent(&event);
  230.   imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
  231.   imu::Vector<3> acceler = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
  232.   imu::Vector<3> gyro = bno.getVector(Adafruit_BNO055::VECTOR_GYROSCOPE);
  233.  
  234.     uint8_t systemstatus, gyrostatus, accelstatus, mag;
  235.   systemstatus = gyrostatus = accelstatus = mag = 0;
  236.   bno.getCalibration(&systemstatus, &gyrostatus, &accelstatus, &mag);
  237.  
  238.   /* Display the floating point data */
  239.  
  240.   Serial.print(euler.x(), 4);
  241.   Serial.print(" ");
  242.   Serial.print(euler.y(), 4);
  243.   Serial.print(" ");
  244.   Serial.print(euler.z(), 4);
  245.   Serial.print(" ");
  246.   Serial.print(acceler.x(), 4);
  247.   Serial.print(" ");
  248.   Serial.print(acceler.y(), 4);
  249.   Serial.print(" ");
  250.   Serial.print(acceler.z(), 4);
  251.   Serial.print(" ");
  252.   Serial.print( gyro.x(), 4);
  253.   Serial.print(" ");
  254.   Serial.print( gyro.y(), 4);
  255.   Serial.print(" ");
  256.   Serial.println( gyro.z(), 4);
  257.  
  258.  
  259. //Character Array
  260. char xVal[9]; //9 is the maximum number of characters expected in an x-value (i.e. xxx.yyyy\0 = 9 chars)
  261.                 //(including the '.', '-' and invisible end-of-string indicator '\0' <- this is a special marker just counts as 1 char)
  262.   char yVal[10];//10 is the maximum number of characters expected in an x-value (i.e. -xxx.yyyy\0 = 10 chars)
  263.                 //(including the '.', '-' and invisible end-of-string indicator '\0' <- this is a special marker just counts as 1 char)
  264.   char zVal[10]; //same as y
  265.  
  266.   char axVal[10];
  267.   char ayVal[10];
  268.   char azVal[10];
  269.  
  270.   char gxVal[10];
  271.   char gyVal[10];
  272.   char gzVal[10];
  273.  
  274.   //Convert the floating points to strings.
  275.   dtostrf(euler.x(),3,4,xVal);
  276.   dtostrf(euler.y(),3,4,yVal);
  277.   dtostrf(euler.z(),3,4,zVal);
  278.  
  279.   dtostrf(acceler.x(),3,4,axVal);
  280.   dtostrf(acceler.y(),3,4,ayVal);
  281.   dtostrf(acceler.z(),3,4,azVal);
  282.  
  283.   dtostrf(gyro.x(),3,4,gxVal);
  284.   dtostrf(gyro.y(),3,4,gyVal);
  285.   dtostrf(gyro.z(),3,4,gzVal);
  286.  
  287.   char payload[RH_RF95_MAX_MESSAGE_LEN]; //This will hold what we'll actually transmit.  32 is the maximum number of characters expected in the this will is the string
  288.  
  289.   sprintf(payload,"%s:%s:%s:%s:%s:%s:%s:%s:%s:%d:%d:%d",xVal,yVal,zVal,axVal,ayVal,azVal,gxVal,gyVal,gzVal,systemstatus,accelstatus,gyrostatus);
  290.  
  291.  
  292.  
  293.   /* Optional: Display sensor status (debug only) */
  294.   //displaySensorStatus();
  295.  
  296.  
  297.   /* New line for the next sample */
  298.  //Serial.println(payload);
  299. // if (rf95.available())
  300.  // {
  301.   //  Serial.println("test");
  302.     // Should be a message for us now  
  303.     uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  304.     uint8_t len = sizeof(buf);
  305.    // if (rf95.recv(buf, &len))
  306.   //  {
  307.       digitalWrite(led, HIGH);
  308. //      RH_RF95::printBuffer("request: ", buf, len);
  309.    //   Serial.print("got request: ");
  310.    //   Serial.println((char*)buf);
  311.    //   Serial.print("RSSI: ");
  312.    //   Serial.println(rf95.lastRssi(), DEC);
  313.      
  314.       // Send a reply
  315.      // uint8_t data[] = "And hello back to you";
  316.  
  317.  
  318.      //Sends data over
  319.       rf95.send((uint8_t*)payload, sizeof(payload));
  320.       rf95.waitPacketSent();
  321.    //   Serial.println("Sent a reply");
  322.        digitalWrite(led, LOW);
  323.     //}
  324.     //else
  325.     //{
  326.       //Serial.println("recv failed");
  327.     //}
  328.   //}
  329.  
  330.   /* Wait the specified delay before requesting nex data */
  331.   delay(BNO055_SAMPLERATE_DELAY_MS);
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement