获取两个arduino之间的时间间隔(以微秒为单位)

获取两个arduino之间的时间间隔(以微秒为单位),arduino,distance,measurement,elapsedtime,time-measurement,Arduino,Distance,Measurement,Elapsedtime,Time Measurement,我想使用rx/tx模块以微秒为单位测量两个模块之间经过的时间。我编写了代码,我注意到输出中有错误。我希望你能帮助我 设备1和设备2均负责使用本地时钟准确测量时间延迟 如果A发送信号的时间是TSA, B接收信号的时间为TRB, B回复的时间是TSB, A接收回信号的时间为TRA 以便TSA

我想使用rx/tx模块以微秒为单位测量两个模块之间经过的时间。我编写了代码,我注意到输出中有错误。我希望你能帮助我

设备1和设备2均负责使用本地时钟准确测量时间延迟

如果A发送信号的时间是TSA, B接收信号的时间为TRB, B回复的时间是TSB, A接收回信号的时间为TRA 以便TSA
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");
        }
    }  
}
因此,作为结论,我相信您忘记了公式中的几个因素:

您还可以测量所有εµs之和的处理时间 您正在测量1000000µs延迟的睡眠时间 这并不是真正代表实际的接收/发送,而是你在代码中所做的,这增加了延迟。所以您认为公式并不是真的错,只是您的代码没有实现它


因此,消息的ping回显时间远快于1000032µs,因此,您所测量的只是程序在时间戳和时间戳之间的循环速度:即1000000µs的等待时间和32µs的处理时间。

为了测量经过的时间,我应该在程序中编辑什么?
#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
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");
        }
    }  
}