Time comparison between normal TX-RX mode and Auto Ack payloads

If you have read the library properly then you know how the transceivers and the functions that have been developed work i.e what happens in a normal mode where we switch between Rx and Tx mode and how the Auto ack mode works. Anyways let me tell you once again in brief – In the normal mode where we switch between Tx and Rx modes the time interval between sending a message and receiving a message from Rx node would be greater than the same process where the Tx receives the message from Rx an an acknowledgement packet. This happens because of the fact that in the switching mode most of the time is taken in changing the contents of registers to change the mode from Tx to Rx or Rx to Tx , probably some other things like pushing the message in the buffers too and then the mode finally changes and the message is transmitted. But the Enhanced Shockburst protocol automatically sets the PTX in receive mode after it sends a message and the PRX in transmit mode after it receives a message (without the involvement of the MCU). Hence if you send a reply using the auto acknowledge mode the time interval will be significantly less.

NOTE : After Enhanced Shockburst sets the PTX in receive mode it will set it back in PTX mode after the reception of an ack payload and not in Rx mode. The same applies to PRX.

So we have predicted that when we run our transceivers in normal switching mode then the time elapsed between a transmission and a reception would be more than the time gap between transmission and reception using Ack payloads. Time to actually prove this –

Lets first see the time gap in the normal switching mode –

Here we send some command signals from the TX by entering some character in the serial monitor. As soon as the Rx receives the character it sends back a character (which actually represents sensor readings) to Tx. We then see the time gap between the Tx sending the character and receiving the character from Rx.

Tx –

#include<SPI.h>
#include"nRf24L01.h"
#include"RF24.h"
#include"printf.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
bool go_to_tx_mode = false;
char input[32] = ""; float time;
void setup() {
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
  radio.printDetails();
  // put your setup code here, to run once:

}

void loop() {
  if (go_to_tx_mode)
  {
    radio.stopListening();
    bool ok = radio.write(input, 32); time = micros();
    if (ok)
    {
      printf("ME : %s\n", input);
      go_to_tx_mode = false;
      radio.startListening();
    }
    else
      printf("could not write....\n");
  }
  if (radio.available())
  {
    int payload;
    bool done = false;
    while (!done)
    {
      done = radio.read( &payload , sizeof(payload));
      Serial.println(micros() - time);
      printf("HIM : %d\n", payload);
    }

  }
  if (Serial.available())
  {
    int len = Serial.readBytesUntil('\n', input, 31);
    input[len] = '\0';
    go_to_tx_mode = true;
  }

}

Rx –

#include<SPI.h>
#include"nRf24L01.h"
#include"RF24.h"
#include"printf.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
bool go_to_tx_mode = false;
int input = 2;
void setup() {
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
  radio.printDetails();
  // put your setup code here, to run once:

}

void loop() {
  if (go_to_tx_mode)
  {
    radio.stopListening();
    bool ok = radio.write(&input, sizeof(input));
    if (ok)
    {
      printf("ME : %d\n", input);
      go_to_tx_mode = false;
      radio.startListening();
    }
    else
      printf("could not write....\n");
  }
  if (radio.available())
  {
    char payload[32] = "";
    bool done = false;
    while (!done)
    {
      done = radio.read( payload , 32 );
      printf("HIM : %s\n", payload);
      go_to_tx_mode = true;
    }

  }
  input += 2;
  if (input >= 1000)
    input = 2;
}

The results are as follows –

NORMAL TX-RX MODE

normal time 1normal time 2normal time 3

Now lets see the Auto ack mode –

Tx –

#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
int rec[1] = {5};
float time;
bool go_to_tx_mode = false;
char input[32] = "";
RF24 radio(9, 10);
const uint64_t pipe[1] = {0xF0F0F0F0E1LL};

void setup()
{
  Serial.begin(57600);
  radio.begin();
  delay(100);
  radio.setAutoAck(true);
  radio.enableAckPayload();
  radio.enableDynamicPayloads();
  radio.stopListening();
  radio.openWritingPipe(pipe[0]);
  radio.setRetries(15, 15);
}

void loop() {

  if (go_to_tx_mode) {
    if (radio.write(input, sizeof(input)))
    {
      time = micros();
      go_to_tx_mode = false;
      Serial.print( input );
      Serial.println("...tx success");
      if (radio.isAckPayloadAvailable())
      {
        radio.read(rec, sizeof(int));
        Serial.print(micros() - time);
        Serial.print(".......received ack payload is : ");
        Serial.println(rec[0]);
      }
      else
      {
        go_to_tx_mode = false; //doing this completely shuts down the transmitter if an ack payload is not received !!
        Serial.println("status has become false so stop here....");
      }
    }
  }
  if (Serial.available())
  {
    int len = Serial.readBytesUntil('\n', input, 31);
    input[len] = '\0';
    go_to_tx_mode = true;
  }
}

Rx –

#include<SPI.h>
#include<nRF24L01.h>
#include<RF24.h>
const uint64_t pipe[1] = {0xF0F0F0F0E1LL};
RF24 radio(9, 10);
int rec[1] = {2};
char red[32];

void setup()
{
  Serial.begin(57600);
  radio.begin();
  delay(100);
  radio.setAutoAck(true);
  radio.enableAckPayload();
  radio.enableDynamicPayloads();
  radio.openReadingPipe(1, pipe[0]);
  radio.startListening();
  radio.setRetries(15, 15);
}
void loop() {
  if ( radio.available() ) {
    radio.writeAckPayload( 1, rec, sizeof(int) );
    radio.read( red, sizeof(red) );
    rec[0] += 2;
    Serial.print("character got is : ");
    Serial.println(red);
  }
}

The results are as follows –

FOR ACK PAYLOADS

20141208T143155

So we can clearly notice that the time gap in normal Tx -Rx mode is of the order of milliseconds (around 1.05 to 1.1 milliseconds) whereas the time gap in Auto Ack mode is of the order of microseconds ( 176 microseconds).

This time difference according to me is huge (more than 6 times !!!). So if you want to make a very agile and quick response system then you must definitely go for Auto Ack packets.

If you send some larger control signals then the time gap increases correspondingly due to obvious reasons but it still stays in few hundreds of microseconds .

Advertisements

About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in Arduino, nRF24L01+, Wireless, wireless communication and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s