Sunday, June 19, 2016

Code for sending x value from BNo055 from end device to coordinator

This works!  I get an RX packet on XCTU.  But, converting from float to bytes isn't working.  Next thing to figure out.


#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <XBee.h>
//#include <SoftwareSerial.h>

XBee xbee = XBee();

unsigned long start = millis();

// allocate two bytes
uint8_t payload[] = { 0, 0 };

// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x1234, payload, sizeof(payload));

TxStatusResponse txStatus = TxStatusResponse();
Adafruit_BNO055 bno = Adafruit_BNO055();
int x = 0;

void setup() {
  Serial.begin(9600);
  xbee.setSerial(Serial); //MAKE SURE to move the UART switch to UART when running the program.  Switch to DLine when uploading program.
   /* 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);
  }
}

void loop() {
 
   // start transmitting after a startup delay.  Note: this will rollover to 0 eventually so not best way to handle
    if (millis() - start > 15000) {
      // break down 10-bit reading into two bytes and place in payload
     
        imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);

  //This calculation of multiplying by 100 and then converting to an int doesn't seem to work or I don't  
 //understand the results!
        x=(int)(euler.x()*100);
        payload[0] = x >> 8 & 0xff;
        payload[1] = x & 0xff;
     
        //Serial.print(x);
        Serial.print("   ");
        Serial.print(payload[0],HEX);
        Serial.print("   ");
        Serial.println(payload[1],HEX);
         xbee.send(tx);
    }
 
    // after sending a tx request, we expect a status response
    // wait up to 5 seconds for the status response
    if (xbee.readPacket(5000)) {
        // got a response!

        // should be a znet tx status            
    if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
      xbee.getResponse().getTxStatusResponse(txStatus);
   
      // get the delivery status, the fifth byte
           if (txStatus.getStatus() == SUCCESS) {
            // success.  time to celebrate
               // or flash status led

           }
        }    
    } else if (xbee.getResponse().isError()) {
      //nss.print("Error reading packet.  Error code: ");
      //nss.println(xbee.getResponse().getErrorCode());
      // or flash error led
    }
   
    delay(1000);
}

No comments:

Post a Comment