我想使用 rx/tx 模块测量我的两个arduino之间经过的时间(以微秒为单位)。我做了我的代码,我注意到我的输出有问题。我希望你能帮助我。
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
发射机输出
正如您在我的公式中看到的那样,收到的时间应该更大。
接收器输出
我希望你能帮助我我的程序有什么问题。