0

I'm trying out The Arduino ultimate GPS breakout, where I want to get the Longitude and Latitude from the GPS. Then I want to send those two variables wireless via RF. like in the image below: enter image description here

I use a library for the RF-module named panstamp to be able to send the Longitude and Latitude from Arduino 1, and receiving them in Arduino 2. like in the code below:

Transmitting:

    void send_data() {
    CCPACKET data;
    data.length=2;

    float lon=26.533255;
    float lat=27.533463;


    data.data[0]=lon;
    data.data[1]=lat;
    if(cc1101.sendData(data)){
    Serial.println(data.data[0]);
    Serial.println(data.data[1]);

    Serial.println(" sent ok ");
    return true;
    }else{
    Serial.println("sent failed ");
    return false;
    }

 }

Receiving:

void loop(){
        float j = 0;
        lon = packet.data[j];
          Serial.print(lon);
          Serial.print(" ");
        float k = 1;
        lat = packet.data[k];
          Serial.print(lat);
          Serial.println(".");
}

It works perfectly when transmitting and receiving :)

The problem is when I receive those two variables I just receive lon 26.00 and lat 27.00 but not lon 26.533255 lat 27.533463 as I expected.

There are some bugs with the data type I assume. I investigated the panstamp library to find something to change the type but without success.

Here is the header file for CCPACKET:

#ifndef _CCPACKET_H
#define _CCPACKET_H

#include "Arduino.h"

/**
 * Buffer and data lengths
 */
#define CC1101_BUFFER_LEN        64
#define CC1101_DATA_LEN          CC1101_BUFFER_LEN - 3

/**
 * Class: CCPACKET
 * 
 * Description:
 * CC1101 data packet class
 */
class CCPACKET
{
  public:
    /**
     * Data length
     */
    byte length;

    /**
     * Data buffer
     */
    byte data[CC1101_DATA_LEN];

    /**
     * CRC OK flag
     */
    boolean crc_ok;

    /**
     * Received Strength Signal Indication
     */
    byte rssi;

    /**
     * Link Quality Index
     */
    byte lqi;
};

#endif

and the source code for send data/ receive data:

boolean CC1101::sendData(CCPACKET packet)
{
  byte marcState;
  bool res = false;

  // Declare to be in Tx state. This will avoid receiving packets whilst
  // transmitting
  rfState = RFSTATE_TX;

  // Enter RX state
  setRxState();

  // Check that the RX state has been entered
  while (((marcState = readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x0D)
  {
    if (marcState == 0x11)        // RX_OVERFLOW
      flushRxFifo();              // flush receive queue
  }

  delayMicroseconds(500);

  // Set data length at the first position of the TX FIFO
  writeReg(CC1101_TXFIFO,  packet.length);
  // Write data into the TX FIFO
  writeBurstReg(CC1101_TXFIFO, packet.data, packet.length);

  // CCA enabled: will enter TX state only if the channel is clear
  setTxState();

  // Check that TX state is being entered (state = RXTX_SETTLING)
  marcState = readStatusReg(CC1101_MARCSTATE) & 0x1F;
  if((marcState != 0x13) && (marcState != 0x14) && (marcState != 0x15))
  {
    setIdleState();       // Enter IDLE state
    flushTxFifo();        // Flush Tx FIFO
    setRxState();         // Back to RX state

    // Declare to be in Rx state
    rfState = RFSTATE_RX;
    return false;
  }

  // Wait for the sync word to be transmitted
  wait_GDO0_high();

  // Wait until the end of the packet transmission
  wait_GDO0_low();

  // Check that the TX FIFO is empty
  if((readStatusReg(CC1101_TXBYTES) & 0x7F) == 0)
    res = true;

  setIdleState();       // Enter IDLE state
  flushTxFifo();        // Flush Tx FIFO

  // Enter back into RX state
  setRxState();

  // Declare to be in Rx state
  rfState = RFSTATE_RX;

  return res;
}


byte CC1101::receiveData(CCPACKET * packet)
{
  byte val;
  byte rxBytes = readStatusReg(CC1101_RXBYTES);

  // Any byte waiting to be read and no overflow?
  if (rxBytes & 0x7F && !(rxBytes & 0x80))
  {
    // Read data length
    packet->length = readConfigReg(CC1101_RXFIFO);
    // If packet is too long
    if (packet->length > CC1101_DATA_LEN)
      packet->length = 0;   // Discard packet
    else
    {
      // Read data packet
      readBurstReg(packet->data, CC1101_RXFIFO, packet->length);
      // Read RSSI
      packet->rssi = readConfigReg(CC1101_RXFIFO);
      // Read LQI and CRC_OK
      val = readConfigReg(CC1101_RXFIFO);
      packet->lqi = val & 0x7F;
      packet->crc_ok = bitRead(val, 7);
    }
  }
  else
    packet->length = 0;

  setIdleState();       // Enter IDLE state
  flushRxFifo();        // Flush Rx FIFO
  //cmdStrobe(CC1101_SCAL);

  // Back to RX state
  setRxState();

  return packet->length;
}

Please someone help me :)

The link to the Panstamp library: PanStamp Library

eerorika
  • 232,697
  • 12
  • 197
  • 326
AdiT
  • 539
  • 5
  • 18
  • Why can't you receive doubles? You are losing precision, that's the problem? Also, you have pointed out that the library code might have bug in it – ha9u63a7 Oct 28 '14 at 11:12
  • That is the problem that I'm losing precision. But what can i do to receive the doubles or floats? That is my question. – AdiT Oct 28 '14 at 11:17
  • Probably this assignment is not working correctly, data.data[0]=lon; data.data[1]=lat, here you are converting the float to byte type – Abhishek Chandel Oct 28 '14 at 11:30
  • All you need (if allowed by your scope of work) is to change `byte data[...]` to `Double data[...]` or `float data[...]`. In this way you are preserving information that you transmit and receive. – ha9u63a7 Oct 28 '14 at 11:32
  • You mean to change it in the header file? – AdiT Oct 28 '14 at 11:39

3 Answers3

1

As far as I see it, you lost your presicion here:

float lon=26.533255;
float lat=27.533463;
data.data[0]=lon;
data.data[1]=lat;

because data is an array of bytes according to this:

/**
 * Data buffer
 */
byte data[CC1101_DATA_LEN];

You need to bufferise data correctly.

HighPredator
  • 790
  • 4
  • 21
  • Hmm but how to buffer them correctly? As I mention I tried out but without success. Do you have any idea that will help me? – AdiT Oct 28 '14 at 11:31
  • @AdiT, take a look at this: http://stackoverflow.com/questions/24420246/c-function-to-convert-float-to-byte-array – HighPredator Oct 28 '14 at 11:34
0

float lon=26.533255; byte *p = (byte *)&lon;

for (int i = 0; i < sizeof(lon); i++){ data.data[i]= p[i]; }

do like this if it works proceed the same with lat or make a function like floattobyte and use.

Nagaraju Badaeni
  • 890
  • 8
  • 14
0

HighPredator is right!

From the panstamp lib we see that the CCPACKET::data field is a uint8_t array: https://github.com/panStamp/panstamp/wiki/CCPACKET#data

Basically when you write:

float lon=26.533255;
float lat=27.533463;
data.data[0]=lon;
data.data[1]=lat;

The compiler is essentially doing:

data.data[0]=uint8_t(lon); // So 26.533255f just becomes 26
data.data[1]=uint8_t(lat); // So 27.533463just becomes 27

You need to understand the float type, which is 4-bytes long and so you need to make your packet 8 bytes long and transmit the raw bytes like this:

data.length = 8;
data.data[0] = ((uint8_t*)(&lon))[0]; // Transfer first byte of the float
data.data[1] = ((uint8_t*)(&lon))[1];
data.data[2] = ((uint8_t*)(&lon))[2];
data.data[3] = ((uint8_t*)(&lon))[3]; // Transfer last byte of the float

data.data[4] = ((uint8_t*)(&lat))[0]; // Transfer first byte of the float
data.data[5] = ((uint8_t*)(&lat))[1];
data.data[6] = ((uint8_t*)(&lat))[2];
data.data[7] = ((uint8_t*)(&lat))[3]; // Transfer last byte of the float

On the receiving end, you can recompose the floats like this:

float lon, lat;
((uint8_t*)(&lon))[0] = data.data[0]; // Place first byte
((uint8_t*)(&lon))[1] = data.data[1];
((uint8_t*)(&lon))[2] = data.data[2];
((uint8_t*)(&lon))[3] = data.data[3]; // Place last byte

((uint8_t*)(&lat))[0] = data.data[4]; // Place first byte
((uint8_t*)(&lat))[1] = data.data[5];
((uint8_t*)(&lat))[2] = data.data[6];
((uint8_t*)(&lat))[3] = data.data[7]; // Place last byte

Hope that helps.

Patapom
  • 64
  • 2