我的arduino代码有什么问题? (nRF24)

时间:2018-09-01 11:50:01

标签: arduino serial-port wifi

我正在尝试基于arduino构建无人机。现在,我正在尝试使用两个nRF24制作自己的发送器和接收器。老实说我没有2个arduino,所以现在我正在使用两个带有外部时钟的atmega328p,但我认为这可能不会影响这个问题。

基本上,此代码应从串行监视器输入两个数字,映射它们并通过两个NRF24发送信息。

现在我正在使用Arduino uno作为发送器,使用atmega328p作为接收器,使用atmega328p作为飞行控制器,它们应该接受PWM信号的输入。问题是飞行控制器根本不响应我发送的信号,但在测试时确实响应,所以我想问题出在两个模块之间。

发射器代码:

byte i = 0; //First value to be sent
byte b = 127; //Second value to be sent

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


const uint64_t pipeOut = 0xE8E8F0F0E1LL;//Pipe address

RF24 radio(8,9); // CE/CSN

struct MyData { // 6 Datas to be sent, we will have just the first two variable as test
  byte throttle; //All of the must be between 0 and 255, not specified in the code, but
  byte yaw;     //assumed by the programmer
  byte pitch;
  byte roll;
  byte AUX1;
  byte AUX2;
};

typedef struct MyData my_data;

my_data data;


void resetData() 
{
  //This are the start values of each channal
  // Throttle is 0 in order to stop the motors, 127 the middle

  data.throttle = 0;
  data.yaw = 127;
  data.pitch = 127;
  data.roll = 127;
  data.AUX1 = 0;
  data.AUX2 = 0;

}

void setup() {
  Serial.begin(9600);

  // Setting up the radio comunication

  radio.begin();
  radio.openWritingPipe(pipeOut);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();

  resetData(); //Resetting the data


}

void loop() {

  if(Serial.available()>0){

  int a = Serial.parseInt();
    i = a; //Taking in input the first value
  int c = Serial.parseInt();
    b=c; //Taking in input the second value after a space or a char

  Serial.print("Throttle = ");
  Serial.print(i);
  Serial.print(" Yaw = ");  
  Serial.println(b);

  }

  data.throttle = i; //Sending i as throttle value
  data.yaw      = b; //Sending b as yaw value
  data.pitch    = 127; //Fixed values, no need for the test
  data.roll     = 127;
  data.AUX1     = 0;
  data.AUX2     = 0;
  radio.write(&data, sizeof(MyData));


}

收件人代码:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>

int tempo;

int pwm_width_2 = 0; //Define widths of the pwm signals
int pwm_width_3 = 0;
int pwm_width_4 = 0;
int pwm_width_5 = 0;
int pwm_width_6 = 0;
int pwm_width_7 = 0;

Servo PWM2; //Using servo library to generate PWM signals
Servo PWM3;
Servo PWM4;
Servo PWM5;
Servo PWM6;
Servo PWM7;


struct MyData {
byte throttle;     //Like in the trasmitter
byte yaw;
byte pitch;
byte roll;
byte AUX1;
byte AUX2;
};


typedef struct MyData my_data;

my_data data;

const uint64_t pipeIn = 0xE8E8F0F0E1LL;     //Remember that this code is the same as in the transmitter

RF24 radio(9, 10); // CE/CSN

void resetData()
{
data.throttle = 0;
data.yaw = 127;
data.pitch = 127;
data.roll = 127;
data.AUX1 = 0;
data.AUX2 = 0;
}

/**************************************************/

void setup()
{

  PWM2.attach(2);  //Set the pins for each PWM signal
  PWM3.attach(3);
  PWM4.attach(4);
  PWM5.attach(5);
  PWM6.attach(6);
  PWM7.attach(9);

  //Configure the NRF24 module
  resetData();
  radio.begin();  
  radio.openReadingPipe(1,pipeIn);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);

  //we start the radio comunication
  radio.startListening();
}

/**************************************************/

unsigned long lastRecvTime = 0;

void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(MyData));
lastRecvTime = millis(); //here we receive the data
}
}

/**************************************************/

void loop()
{

recvData();

unsigned long now = millis();
//Here we check if we've lost signal, if we did we reset the values 
if ( now - lastRecvTime > 1000 ) {
// Signal lost?
resetData();
}

pwm_width_2 = map(data.throttle, 0, 255, 1000, 2000);     //PWM value on digital pin D2
pwm_width_3 = map(data.yaw,      0, 255, 1000, 2000);     //PWM value on digital pin D3
pwm_width_4 = map(data.pitch,    0, 255, 1000, 2000);     //PWM value on digital pin D4
pwm_width_5 = map(data.roll,     0, 255, 1000, 2000);     //PWM value on digital pin D5
pwm_width_6 = map(data.AUX1,     0, 255, 1000, 2000);     //PWM value on digital pin D6
pwm_width_7 = map(data.AUX2,     0, 255, 1000, 2000);     //PWM value on digital pin D7


//Now we write the PWM signal using the servo function
PWM2.writeMicroseconds(pwm_width_2);
PWM3.writeMicroseconds(pwm_width_3);
PWM4.writeMicroseconds(pwm_width_4);
PWM5.writeMicroseconds(pwm_width_5);
PWM6.writeMicroseconds(pwm_width_6);
PWM7.writeMicroseconds(pwm_width_7);

}

POST脚本:

这不是我的代码,我是从www.electronoobs.com获得的,并对其进行了调整以适合我的串行监视器通信,但它根本不起作用:(

0 个答案:

没有答案