Sunday, June 19, 2016

Tx_BNO055 Arduino Code Success

This code transmits the 3 position values and 3 acceleration values from a remote end device xbee in API 2 mode connected to an Arduino and sends it to a coordinator xbee connected to an explorer shield.  The data is read by XCTU.

Coordinator:
DL:  anything
MY:  1234
CE:  Coordinator(1)
AP:  API-2

End Device:
DL:  1234 (note it sends to coordinator
MY:  5678
CE: End Device(0)
AP: API-2

This is what success looks like!  Notice the 24 bytes of data to the right of the screen!  Question is whether we can convert them back into floats and get the correct values.






This code uses Andrew Rapp's xbee.h library as well as Adafruit's libraries.

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 * XBee-Arduino is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XBee-Arduino is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XBee-Arduino.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <XBee.h>

XBee xbee = XBee();
unsigned long start = millis();

// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

// 16-bit addressing for Series 1: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x1234, payload, sizeof(payload));
TxStatusResponse txStatus = TxStatusResponse();
//initialize BNo055
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 BNO055 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() {
  union flintbyte { //this is a C construct to convert float to 4 bytes
float ff; //to gain access to bytes of float
unsigned int ii; //to gain access to bytes of int
unsigned char cc[4]; //as many as needed to cover the largest other element
} fib;


   // 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
//      pin5 = analogRead(5);
//      payload[0] = pin5 >> 8 & 0xff;
//      payload[1] = pin5 & 0xff;
   
        imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
        imu::Vector<3> acc = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
        /*This way works!
        x=(int)(euler.x()*100);
        payload[0] = x >> 8 & 0xff;
        payload[1] = x & 0xff;
        Serial.print(euler.x());
        Serial.print("   ");
        Serial.print(pin5);
        Serial.print("   ");
        Serial.print(payload[0],HEX);
        Serial.print("   ");
        Serial.println(payload[1],HEX);
         xbee.send(tx);
         */
     
         fib.ff=euler.x();
         payload[3] = fib.cc[3];
         payload[2] = fib.cc[2];
         payload[1] = fib.cc[2];
         payload[0] = fib.cc[0];
         fib.ff=euler.y();
         payload[7] = fib.cc[3];
         payload[6] = fib.cc[2];
         payload[5] = fib.cc[2];
         payload[4] = fib.cc[0];      
         fib.ff=euler.z();
         payload[11] = fib.cc[3];
         payload[10] = fib.cc[2];
         payload[9] = fib.cc[2];
         payload[8] = fib.cc[0];
         fib.ff=acc.x();
         payload[15] = fib.cc[3];
         payload[14] = fib.cc[2];
         payload[13] = fib.cc[2];
         payload[12] = fib.cc[0];
          fib.ff=acc.y();
         payload[19] = fib.cc[3];
         payload[18] = fib.cc[2];
         payload[17] = fib.cc[2];
         payload[16] = fib.cc[0];
         fib.ff=acc.z();
         payload[23] = fib.cc[3];
         payload[22] = fib.cc[2];
         payload[21] = fib.cc[2];
         payload[20] = fib.cc[0];        
//         Serial.print(fib.ff);Serial.print('=');
//         Serial.print(fib.cc[3],HEX);Serial.print(','); //print the bytes, 3F,9D,70,A4
//         Serial.print(fib.cc[2],HEX);Serial.print(',');
//         Serial.print(fib.cc[1],HEX);Serial.print(',');
//         Serial.println(fib.cc[0],HEX);
//         fib.cc[0] ^= 1; //toggle LSB of mantissa in IEEE754
//         Serial.println(fib.ff);Serial.print(',');
//         //Serial.println((fib.ff-1.23)*1000000.0); //difference is 1.2e-7
         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

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

No comments:

Post a Comment