1

我想使用 rx/tx 模块测量我的两个我做了我的代码,我注意到我的输出有问题。我希望你能帮助我。

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

  • 如果时间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

发射器代码

#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

发射机输出

正如您在我的公式中看到的那样,收到的时间应该更大。

发射机输出

接收器输出

接收器输出

我希望你能帮助我我的程序有什么问题。

4

1 回答 1

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 延迟)

这并不能真正代表实际的 rx/tx,而是您在代码中所做的事情,这会增加延迟。所以你的公式并不是真的错,只是你的代码没有实现它。

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

于 2015-03-08T13:29:14.810 回答