【发布时间】:2015-08-19 19:35:35
【问题描述】:
我正在尝试设置两个 Arduino mircos,一个作为接收器,一个作为控制器,作为我媒体中心的无线遥控器。
控制器向接收器发送按钮组合。然后接收者按下相应的键盘组合。
我将 RF24 library from ManiacBug 与 Arduino micros 和 NRF24L01+ 收发器一起使用。
我想将控制器设置为写入模式,将接收器设置为侦听模式。由于通信是单向的,因此没有太多理由切换模式。 (我可以添加一个自动确认来仔细检查数据包,但这就是重点。)
我的问题是,如果我将它们各自保持在各自的模式下,通信就会非常不一致并且经常失败。
控制器的代码是
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(9,10);
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
// button
const unsigned short nButtons = 2;
const unsigned short buttonPin[] = {6,7};
unsigned curr,prev;
unsigned short getButtons(const unsigned short nButtons,
const unsigned short * const buttonPin);
unsigned short getButtons(const unsigned short nButtons,
const unsigned short * const buttonPin){
// write each button to a bit
unsigned short i,buttons = 0,state;
for(i = 0; i < nButtons; ++i){
state = digitalRead(buttonPin[i]);
if(state == HIGH){
buttons |= (1u << i);
}
}
return buttons;
}
void setup(void){
Serial.begin(57600);
printf_begin();
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(sizeof(unsigned short));
radio.openWritingPipe(pipes[0]);
unsigned short i;
for(i = 0; i < nButtons; ++i)
pinMode(buttonPin[i],INPUT);
curr = prev = 0;
}
void loop(void){
curr = getButtons(nButtons,buttonPin);
if(curr != prev){
printf("Buttons: %u\n\r",curr);
bool ok = radio.write( &curr, sizeof(unsigned short) );
prev = curr;
if (ok)
printf("sent.\n\r");
else
printf("failed.\n\r");
// have to cycle listening otherwise communication fails
radio.startListening();
radio.stopListening();
}
}
而接收者是
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(9,10);
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
const unsigned short nButtons = 2;
const unsigned short keys[] = {216, // left
215}; // right
const char* name[] = {"LEFT","RIGHT"};
unsigned short curr = 0,prev = 0;
void keyMap(const unsigned short curr, const unsigned short prev,
const unsigned short nButtons){
// map the button changes to keyboard commands
unsigned short i,change;
change = curr ^ prev;
for(i = 0; i < nButtons; ++i){
if((change & (1u << i)) && (curr & (1u << i))){
printf("Press %s\n\r",name[i]);
Keyboard.press(keys[i]);
}
else if(change & (1u << i)){
printf("Release %s\n\r",name[i]);
Keyboard.release(keys[i]);
}
}
}
void setup(void){
Serial.begin(57600);
printf_begin();
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// optionally, reduce the payload size. seems to
// improve reliability
radio.setPayloadSize(sizeof(unsigned short));
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
Keyboard.begin();
curr = prev = 0;
}
void loop(void){
// if there is data ready
if ( radio.available() ){
// Dump the payloads until we've gotten everything
unsigned short got_buttons;
bool done = false;
while (!done){
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_buttons, sizeof(unsigned short) );
curr = got_buttons;
keyMap(curr,prev,nButtons);
prev = curr;
printf("Got payload %u.\n\r",got_buttons);
}
}
}
我遇到的问题在于控制器代码。我必须有radio.startListening(); radio.stopListening();,否则几乎每次传输都会失败。如果您删除这两个语句,那么只有在您快速按下按钮时才能进行通信。
我打开了这两种方法的源代码,发现它们都调用了flush_tx() 和flush_rx()。难道是缓冲区被填满了?我对这种东西不太熟悉,所以不知道如何调试。
如果您对我为什么会出现这种行为有任何见解,或者如果您对调试有任何建议,我会非常感兴趣!
【问题讨论】: