在几微秒内获得两个arduino之间的时间

时间:2015-03-08 12:53:15

标签: arduino distance measurement elapsedtime time-measurement

我想用微秒来衡量两个之间使用rx / tx模块的时间。我制作了我的代码,我注意到输出中有问题。我希望你能帮助我。

A(设备1)和B(设备2)都负责使用本地时钟准确测量时间延迟。

  • 如果时间A发送信号为TSA
  • B收到信号的时间为TRB
  • B回复的时间是TSB
  • A收到信号的时间为TRA
  • 这样TSA < TRB <TSB < TRA
  • 然后A衡量TA = TRA -TSA
  • B衡量TB = TSB - TRB

TOF 可以通过组合这两个测量来估算:

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

TRANSMITTER代码

#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");
            }
        }  
    }
}

收件人代码

#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 http://i58.tinypic.com/245xcag.jpg

接收器输出

receiver output http://i62.tinypic.com/23j5rfp.jpg

我希望你能帮助我解决我的计划中的问题。

1 个答案:

答案 0 :(得分:0)

阅读你的代码:

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");
        }
    }  
}

作为结论,我相信你忘记了公式中的一些因素:

  1. 您还在测量处理时间(所有εμs的总和)
  2. 您正在测量sleep次(1000000μs延迟)
  3. 这并不是真正代表实际的rx / tx,而是代表你在代码中所做的事情,这加起来就是延迟。所以你的公式并不是真的错,只是你的代码没有实现它。

    所发生的事情是,您的消息的ping回显时间比1000032μs快,因此您测量的只是您的程序在<SEND>时间戳和<RECV>时间戳之间的循环速度:这是1000000μs的等待和32μs的处理。