Go Down

Topic: Problem with transmitting IMU/GPS/Ultrasonic data over Nrf24L01 (Read 5 times) previous topic - next topic

PeterH

Code: [Select]

struct StructBuff_t {
  char GPS[10]; 
  float imuRoll;
  float imuPitch;
  float imuYaw;
  unsigned int AP;
}
...
radio.setPayloadSize(12 * sizeof(byte));
...
radio.write((byte *)&StructBuff, sizeof(StructBuff));


I think you'll find that sizeof(StructBuff) is a lot bigger than 12 * sizeof(byte). Your payload size needs to be at least as big as your actual message size. If I were you I'd leave it at the default, which is the maximum supported size (32).

Also suggest you zero out the receive buffer before the receive, so you know that what you're looking at came over the radio link and you aren't being misled by uninitialised or historical data.
I only provide help via the forum - please do not contact me for private consultancy.

Cyric

am i not pointing to StructBuff correctly from radio.read?
Code: [Select]

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(8,9);
//---------------------

struct StructBuff_t {
  char GPS[100]; 
  float imuRoll;
  float imuPitch;
  float imuYaw;
  unsigned int AP;
  //  char SenBuffersAll[28];
}
StructBuff;
//---------------------
void setup(void){
  Serial.begin(57600);
  printf_begin();
  printf("\n\rRadio Setup\n\r");
  radio.begin();
  radio.setDataRate(RF24_2MBPS);
  radio.setCRCLength(RF24_CRC_16);
  radio.setPayloadSize(32);
  radio.setChannel(2);
  radio.setAutoAck(true);
  radio.openReadingPipe(1,0xF0F0F0F0D2LL);
  radio.openWritingPipe(0xF0F0F0F0E1LL);
  radio.startListening();
  radio.printDetails();
}
void loop(void){
  if ( radio.available() )
  {
    radio.startListening();
    bool ok = radio.read(&StructBuff,sizeof(StructBuff));
    if (ok)
      displayStructBuff(); 
    else 
      printf("failed.\n\r");
    delay(10);
  }
}



void displayStructBuff(){
  //  Serial.print("!"); 
  //  Serial.print("ANG:"); 
  Serial.print(StructBuff.imuRoll);
  Serial.print(",");
  Serial.print(StructBuff.imuPitch);
  Serial.print(",");
  Serial.print(StructBuff.imuYaw);
  Serial.print(",");
  Serial.print(StructBuff.AP);
  Serial.println();
  Serial.println(StructBuff.GPS);
}

output is
Code: [Select]

0.00,0.00,0.00,0

Cyric

I removed the GPS and AP out and just tied to trans a smaller struct. befor it was all 0.00 stuff but now i have some stuff coming across. it's it a packet size issue?
i stopped pre defining a packet size and do it with sizeof in the write/read call
Code: [Select]

printf_begin();
  printf("\n\rRadio Setup\n\r");
  radio.begin();
  radio.setDataRate(RF24_2MBPS);
  radio.setCRCLength(RF24_CRC_16);
// radio.setPayloadSize(32);
  radio.setChannel(2);
  radio.setAutoAck(true);
  radio.openReadingPipe(1,0xF0F0F0F0E1LL);
  radio.openWritingPipe(0xF0F0F0F0D2LL);
  radio.startListening();
  radio.printDetails();

Code: [Select]

radio.write((byte *)&StructBuff, sizeof(StructBuff)); TX side
radio.read((byte *)&StructBuff, sizeof(StructBuff)); RX side

Code: [Select]

typedef struct {
  float imuRoll;
  float imuPitch;
  float imuYaw;
}strbuff_t;
strbuff_t StructBuff;

now my output is like this
TX
Code: [Select]

Ok..
-0.00,-0.00,0.01,
Ok..
-0.25,-0.00,0.00,
Ok..
-0.10,-0.03,0.00,
Ok..
-0.24,0.13,0.00,
Ok..
-0.18,0.18,-0.00,
Ok..
-0.17,0.30,-0.00,
Ok..
-0.14,0.33,-1.96,
Ok..
-0.06,0.23,-3.70,
Ok..
-0.11,0.23,-5.43,



RX Side
Code: [Select]

0.00,0.00,0.00,
0.00,0.00,0.00,
0.00,0.00,0.00,
-1.50,-1.50,-1.50,
0.00,0.00,0.00,
0.19,0.19,0.19,
0.00,0.00,0.00,
0.00,0.00,0.00,
-0.00,-0.00,-0.00,

Cyric

this is prob a silly question but what is (byte *) doing for the function readio.----?

PaulS

Quote
it's it a packet size issue?

Quite likely.

Quote
this is prob a silly question but what is (byte *) doing for the function readio.----?

The radio.write() and radio.read() methods expect the first argument to be of type byte array. You struct instance is a collection of bytes, but it is not an array of bytes. So, you need to lie to the function, and tell it that what you have is an array of bytes. That is done with a cast - the (byte *) part.

Of course, a cast only works when it is reasonable to interpret data of one type (struct) as though it was another type (array of bytes).

Go Up