Hi people,
i have made a drone, but using an Arduino as a flight controller. Whatever, I like to control my Drone using the PC and 2 nrf24l01, one connected to an Arduino (connected to PC) and the other one in Drone.
searching for it I found a tutorial to make you own transmitter and receiver in this video:
Then I take it as bases of my project.
I can send the information from the Arduino (transmitter) to the Arduino in drone, but I don't know how I can read the information and transform it into an output PPM signal.
I do something, but it doesn't work.
This is my transmitting code, that should read the values from each of the 4 channels and transmitting to the other nrf:
/*
A basic 4 channel transmitter using the nRF24L01 module.
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipeOut = 0xE8E8F0F0E1LL;
String inString = ""; // string to hold input
int cd,num,pcd,rpw,vall=0;
int val01,val2,val3,val4;
String ch ="";
RF24 radio(9, 10);
// The sizeof this struct should not exceed 32 bytes
struct MyData {
byte throttle;
byte yaw;
byte pitch;
byte roll;
};
MyData data;
void resetData()
{
data.throttle = 0;
data.yaw = 127;
data.pitch = 127;
data.roll = 127;
}
void setup()
{
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
resetData();
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// send an intro:
Serial.println("Ready");
}
void loop()
{
readint();
calcRPY();
cal();
}
void readint() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) { inString += (char)inChar; }
if (inChar == '\n') { cd=inString.toInt();
inString = "";
}}}
void calcRPY()
{
if (cd!=pcd)
if(cd>0 && cd<5) { rpw=cd; }
else if (cd>5 && cd<1025) { vall=cd; }
pcd=cd;
}
void cal()
{
switch (cd)
{
case 1:
val01=vall;
data.throttle = map( val01, 0, 1023, 1000, 2000 );Serial.print("throttle=" ); Serial.print(val01);Serial.print(" yaw=" ); Serial.print(val2);Serial.print(" pitch=" );Serial.print(val3); Serial.print(" roll=" );Serial.println(val4);
break;
case 2:
val2=vall;
data.yaw = map( val2, 0, 1023, 1000, 2000);Serial.print("throttle=" ); Serial.print(val01);Serial.print(" yaw=" ); Serial.print(val2);Serial.print(" pitch=" );Serial.print(val3); Serial.print(" roll=" );Serial.println(val4);
break;
case 3:
val3=vall;
data.pitch = map( val3, 0, 1023, 1000, 2000);Serial.print("throttle=" ); Serial.print(val01);Serial.print(" yaw=" ); Serial.print(val2);Serial.print(" pitch=" );Serial.print(val3); Serial.print(" roll=" );Serial.println(val4);
break;
case 4:
val4=vall;
data.roll = map( val4, 0, 1023, 1000, 2000 );Serial.print("throttle=" ); Serial.print(val01);Serial.print(" yaw=" ); Serial.print(val2);Serial.print(" pitch=" );Serial.print(val3); Serial.print(" roll=" );Serial.println(val4);
break;
}
radio.write(&data, sizeof(MyData));
}
And this is my Receiver code, but it doesn't work, actually, i want to know what should I do to white the
PPM information on PINS 5,4, 7 and 8 that I need to contro my drone.
/*
A basic receiver using the nRF24L01 module to receive 4 channels and convert them to PPM.
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
////////////////////// PPM CONFIGURATION//////////////////////////
#define channel_number 4 //set the number of channels
#define PPM_FrLen 27000 //set the PPM frame length in microseconds (1ms = 1000µs)
#define PPM_PulseLen 400 //set the pulse length
//////////////////////////////////////////////////////////////////
int ppm[channel_number];
const uint64_t pipeIn = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);
// The size of this struct should not exceed 32 bytes
struct MyData {
byte throttle;
byte yaw;
byte pitch;
byte roll;
};
MyData data;
void setPPMValuesFromData()
{
ppm[0] = map(data.throttle, 0, 255, 1000, 2000);
ppm[1] = map(data.yaw, 0, 255, 1000, 2000);
ppm[2] = map(data.pitch, 0, 255, 1000, 2000);
ppm[3] = map(data.roll, 0, 255, 1000, 2000);
ppm[4] = 1000;
ppm[5] = 1000;
}
void resetData()
{
// 'safe' values to use when no radio input is detected
data.throttle = 0;
data.yaw = 127;
data.pitch = 127;
data.roll = 127;
setPPMValuesFromData();
}
/**************************************************/
void setup()
{
resetData();
// Set up radio module
radio.begin();
radio.setDataRate(RF24_250KBPS); // Both endpoints must have this set the same
radio.setAutoAck(false);
radio.openReadingPipe(1,pipeIn);
radio.startListening();
}
/**************************************************/
unsigned long lastRecvTime = 0;
void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(MyData));
CH1=data.throttle
CH2=data.yaw
CH3=data.pitch
CH4=data.roll
lastRecvTime = millis();
}
}
/**************************************************/
void loop()
{
recvData();
Serial.print("CH1:"); Serial.print(rc_values[RC_CH1]); Serial.print("\t");
Serial.print("CH2:"); Serial.print(rc_values[RC_CH2]); Serial.print("\t");
Serial.print("CH3:"); Serial.print(rc_values[RC_CH3]); Serial.print("\t");
Serial.print("CH4:"); Serial.println(rc_values[RC_CH4]);
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
// signal lost?
resetData();
}
setPPMValuesFromData();
}
/**************************************************/
//#error This line is here to intentionally cause a compile error. Please make sure you set clockMultiplier below as appropriate, then delete this line.
#define clockMultiplier 1 // set this to 2 if you are using a 16MHz Arduino, leave as 1 for an 8MHz arduino
thanks, everyone.
I pretend poste here the project when it's finished.