1

I want to measure the time elapsed between my two using rx/tx module in microseconds. I made my codes and I notice that something wrong in my output. I hope you can help me.

Both A(device 1) and B(device 2) are responsible for measuring a time delay accurately using a local clock.

  • If the time A sends the signal is TSA,
  • the time B receives the signal is TRB,
  • the time B replies to is TSB,
  • the time A receives the signal back is TRA
  • such that TSA < TRB <TSB < TRA,
  • then A measures TA = TRA -TSA and
  • B measures TB = TSB - TRB.

The TOF can be estimated by combining these two measurements:

  • Total time elapsed = (TA-TB)/2

TRANSMITTER code

#include <VirtualWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

const int transmit_pin = 12;
const int receive_pin = 11;
char *c;

unsigned long received, sends, elapsed;

void setup() { 
    Serial.println();
    Serial.begin(9600); // Debugging only

    //transmitter settings
    pinMode(13, OUTPUT)
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_set_tx_pin(12);
    vw_setup(1000); // speed of data transfer Kbps

    //receiver settings
    Serial.println();
    Serial.begin(9600); // Debugging only
    vw_set_rx_pin(11);
    vw_rx_start(); 
}

void loop() {
    //Transmitter
    digitalWrite(13, 1);
    c = "1";
    vw_send((uint8_t *)c, strlen(c));
    vw_wait_tx(); //Wait until the whole message is go
    delay(1000); // for debounce
    sends=micros();

    //Receiver
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) { // Non-blocking
        for(int i = 0;i < buflen;i++) {
            if(buf[i] == '2') { 
                digitalWrite(13, 0);
                delay(1000); // for debounce
                received=micros();
                elapsed=(received-sends);

                Serial.print(sends);
                Serial.println(" TRANSMITTED TIME");
                Serial.print(received);
                Serial.println(" RECEIVED TIME");
                Serial.print(elapsed);
                Serial.println(" microseconds elapsed");
            }
        }  
    }
}

RECEIVER code

#include <VirtualWire.h>
const int receive_pin = 11;
const int transmit_pin = 12;

char *chars;

unsigned long received, sends;


void setup() {
    Serial.println();
    Serial.begin(9600); // Debugging only
    //transmitter settings

    vw_set_ptt_inverted(true); // Required for DR3100
    vw_set_tx_pin(12);
    vw_setup(1000); // speed of data transfer Kbps

    //receiver settings
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_set_rx_pin(11);
    vw_setup(1000); // Bits per sec
    pinMode(13, OUTPUT);
    vw_rx_start(); // Start the receiver PLL running
}

void loop() {
    //Receiver
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    digitalWrite(13, 1);
    if (vw_get_message(buf, &buflen)) { // Non-blocking
        for(int i = 0;i < buflen;i++) {
            if(buf[i] == '1') { 
                received=micros();

                //Transmitter
                chars = "2";
                vw_send((uint8_t *)chars, strlen(chars));
                vw_wait_tx(); // Wait until the whole message is gone
                digitalWrite(13, 0);
                delay(1000);
                sends=micros();

                Serial.print(received);
                Serial.println(" RECEIVED TIME");
                Serial.print(sends);
                Serial.println(" TRANSMTTED TIME");   
            }
        }
    }
}//End for Loop

Transmitter output

Received time should be more larger as you can see in my formula.

transmitter output

Receiver output

receiver output

I hope you can help me what is the problem on my program.

Community
  • 1
  • 1
Rafael
  • 11
  • 3

1 Answers1

0

reading your code:

delay(1000);
sends=micros();                        // timestamp <SEND>

uint8_t buf[VW_MAX_MESSAGE_LEN];       // time to process is ε
uint8_t buflen = VW_MAX_MESSAGE_LEN;   // time to process is ε
if (vw_get_message(buf, &buflen)) {    // time to process is ε
    for(int i = 0;i < buflen;i++) {    // time to process is ε
        if(buf[i] == '2') {            // time to process is ε
            digitalWrite(13, 0);       // time to process is ε
            delay(1000);               // time to process is 1000000µs
            received=micros();         // timestamp <RECV>
            elapsed=(received-sends);

/* That means that in the following statements, all you'll see is
   that the difference between received and sends is 1000000µs + a few ε µs
   which is totally in line with your shown results */

            Serial.print(sends);       
            Serial.println(" TRANSMITTED TIME");
            Serial.print(received);
            Serial.println(" RECEIVED TIME");
            Serial.print(elapsed);
            Serial.println(" microseconds elapsed");
        }
    }  
}

So as a conclusion, I believe that you forget a few factors in your formula:

  1. you're also measuring processing time (the sum of all εµs)
  2. you're measuring the sleep times (the 1000000µs delays)

Which is not really representing the actual rx/tx but also what you're doing in your code, that adds up to the delay. So you're formula is not really wrong, it's just that your code is not implementing it.

So what's happening is that the ping echo time for your message is way faster than 1000032µs, thus what you're measuring is just your program's loop speed between the <SEND> timestamp and the <RECV> timestamp: which is 1000000µs of wait, and 32µs of processing.

zmo
  • 24,463
  • 4
  • 54
  • 90