Xbee and arduino Due quadcopter

hello guys,

I have a big issue with a progect that I'm working on, I'm building a Quadcopter using 2 arduino Due(one on the quadcopter and another on the transmitter) and 2 Xbee (one on the quadcopter and another on the transmitter). As I'm building the quadcopter and the transmitter by my self I use two Xbee. Ok till now its working fine, but the problem is that when I do the readings from the xbee on the quadcopter, the loop speed goes down drastically, from 1Khz without Serial.read() to 50 Hz with the readings. How can I solve this problem?
The transmitter doen't have big problems as it just have to send the datas that is reads from the channel pots.
How can I make the reading faster without loosing data?
Here is the part of the reading in the receiver sketch :

while (  Serial.available() > 0) {

    t_pid_p_gain_rp = Serial.parseInt();
    t_pid_i_gain_rp = Serial.parseInt();
    t_pid_d_gain_rp = Serial.parseInt();
    t_pid_p_gain_yaw = Serial.parseInt();
    t_pid_i_gain_yaw = Serial.parseInt();
    t_pid_d_gain_yaw = Serial.parseInt();
    receiver_input_channel_3 = Serial.parseInt();
    receiver_input_channel_4 = Serial.parseInt();
    receiver_input_channel_2 = Serial.parseInt();
    receiver_input_channel_1 = Serial.parseInt();

    if (Serial.read() == '\n') 
     {
      t_pid_p_gain_rp = constrain(t_pid_p_gain_rp, 0, 9000);
      t_pid_i_gain_rp = constrain(t_pid_i_gain_rp, 0, 900);
      t_pid_d_gain_rp = constrain(t_pid_d_gain_rp, 0, 900);
      t_pid_p_gain_yaw = constrain(t_pid_p_gain_yaw, 0, 9000);
      t_pid_i_gain_yaw = constrain(t_pid_i_gain_yaw, 0, 900);
      t_pid_d_gain_yaw = constrain(t_pid_d_gain_yaw, 0, 900);
      receiver_input_channel_3 = constrain(receiver_input_channel_3, 1000, 2000);
      receiver_input_channel_4 = constrain(receiver_input_channel_4, 1000, 2000);
      receiver_input_channel_2 = constrain(receiver_input_channel_2, 1000, 2000);
      receiver_input_channel_1 = constrain(receiver_input_channel_1, 1000, 2000);
      }

    }

In my first model I was just sending the channels values and it was fast and responsive, but when I added also the PID parameters the whole skecth got slow, and also the response time got really slower. Does anyone have an idea to solve this problem?

Thanks

Serial operations are time consuming. First thing to try is increase the baud value to 250000 in the Serial.begin() statement and the monitor window.

wow it actually worked, I though that it wont work as i tried once by setting the baudrate to 19200, but there was a loss of data, now I'm running it with 38400, and work perfectly :slight_smile:
Thanks for the suggestion

Keep going up until it fails. Mine works at 250K to the monitor. I've tested a linked between Serial1 on a Mega through an FTDI adapter to a terminal program on Win7-64 at 2Mbps and it worked.

By the way is there any other way to get the data form the serial buffer in background? like a way to set a Serial interrupt routine that reads the buffer without disturbing the main sketch?

If you use the interrupt pin on the RF24 and a service routine, you should be able to get something. I've never seen it used and don't know what would trigger the event. You'll have to read the data sheet for that info.

anyway I made it, it flies now. Now i have to find a way of saving the PID parameters somewhere, otherwise every time i turnoff the Tx it loose the values, do you have any idea of how to di it?

There is an EEProm built into the Arduino. It may only be 256 bytes but that should be enough to hold your PID values. Find the library which includes EPromEverything. It has a function to read and write a structure in one shot. Put all your values into the structure and write it during setup(). Thereafter, comment out the write command but read the structure during setup() into the appropriate variables.

Here is the link to the EProm-Read/Write-Anything function. It's not a separate library but just a few procedures in a header file you create then use in your sketch. This should solve your problem.

PS
I've attached my copy of EEPromAnything.h

PPS
There is also the 24LC256_512 chip which is I2C addressable. It has much higher storage capacity and has an available Arduino library. I've used it before to store configuration data very similar to what you need.

EEPROMAnything.h (535 Bytes)

Arduino due doesn't have an eeprom, so im going to save the datas on a SD

The 24LC256 chip might be simpler but you'll need a 3v3 version or deal with the level shifting. Adafruit has a bi-directional level shifter that works nicely with I2C.

Hi New bie

I want to send the data from due board to laptop through xbee.And I am on start up.Can you please help