Hey guys, in the last post, I have shown you how to make the full connection for the drone circuit and also how to do a test in the MultiWii win GUI. If you have not yet watch that post, make sure to watch it HERE. Now, in this post, I will be sharing the MultiWii code also guide you on how to Tune the PID in order to get a stable flight.

part 5 thumnail

We have already made the connection in the previous post. In this post,  we shall continue by uploading the code to the Arduino. We will be using MultiWii code that I have modified to use the NRF24L01 as the receiver in a single Arduino and also configured it for the brushed motor. If you have followed everything like I did in the previous videos or posts, you do not need to change anything in the code. Just upload the code to the Arduino and everything should be working fine. Download the Multiwii code below.


Once you have downloaded the code, unzip it to a preferred folder. And then navigate to the folder and open the MultiWii.ino file using the Arduino IDE.

muiltiwii
  
Once you open the Arduino IDE, you will find the screen as above. You do not have to do anything in the code. Just select your Arduino board and upload the code to your Arduino. Once you have uploaded the code. Open the MultiWii Win GUI. You can download it by clicking on the link below.


Once downloaded, unzip the file to your preferred location. Then open the folder and double click on the MultiWiiWinGui.exe. You can watch that on the youtube video that you will find below. The interface should look exactly like below.

im 1
Now select the port of your arduino and make sure that the BAUD Rate is set to 115200 and click on connect. Make sure that the Arduino is connected before clicking on connect. Once it is successfully connected, you will see that the "connect" is now changed to "Disconnect"

im2
Now, navigate to real-time data, you will see the graph where you can see if the sensor is working, and also on the right side, you will see the data received from the transmitter. You can turn on your transmitter and play with the joystick to see if it is receiving the data. I am using a transmitter that I have already made in my previous post. You can follow the post and make it yourself. Check it HERE.
You can upload the transmitter code by copy and paste the code below or by downloading it.


Transmitter Code

// TheFScreations
//Subscribe to my youtube Channel: TheFScreations
//6CH RC transmitter using Arduino and NRF24L01
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipeOut = 0xE8E8F0F0E1LL; //Address should be same as receiver

RF24 radio(7, 8); // CE & CSN pin. (Change according to CE and CSN pin of NRF24L01 to Arduino

struct MyData {
  byte throttle;
  byte yaw;
  byte pitch;
  byte roll;
  byte AUX1;
  byte AUX2;
};


MyData data;

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

void setup()
{
  //Start everything up
  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(pipeOut);
  resetData();
}

int mapJoystickValues(int val, int lower, int middle, int upper, bool reverse)
{
  val = constrain(val, lower, upper);
  if ( val < middle )
    val = map(val, lower, middle, 0, 128);
  else
    val = map(val, middle, upper, 128, 255);
  return ( reverse ? 255 - val : val );
}

void loop()
{
  // change min, middle, and max of joystick value accordingly
  data.throttle = mapJoystickValues( analogRead(A0), 76, 450, 863, false );
  data.yaw      = mapJoystickValues( analogRead(A1),  152, 540, 918, true );
  data.pitch    = mapJoystickValues( analogRead(A2), 0, 508, 1023, false );
  data.roll     = mapJoystickValues( analogRead(A3), 0, 514, 1023, true );
  data.AUX1     = digitalRead(2); //The 2 toggle switches
  data.AUX2     = digitalRead(3);

  radio.write(&data, sizeof(MyData));
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

im 3

Place the drone on a flat surface and hit the Calibrate ACC button and click on 'OK' when prompted. Then wait for 5s. This will calibrate the sensor to take that position as reference.

im 4

Then click on RC control settings and click on the box to make the angle mode turn on when the AUX1 is low and Horizon mode is on when AUX 1 is high. Then click on Write settings. This will write the setting to the arduino and save it.

im 5

Then click on the parameters button. In this section, we need to tune the PID. First of all, put the P, I, and D values to 0 for Roll and Pitch at first. Then connect the battery to the Arduino. Put Throttle to minimum and YAW to the maximum to Arm the motor.
im 6

Then increase the Throttle, until the motors start spinning. Keep the speed low. Lift up the drone and move the drone. You should feel the drone is fighting back against the movement by spinning faster. This will make sure that the sensor is working fine. Once you confirm this, We can now proceed to the PID tuning.

im 7

The PID tuning process using the TRIAL and ERROR method is very time-consuming. It takes a lot of time to get the perfect value. But I recommend keeping the D value for pitch and ROLL to 20 and I to 0.010. and then increase the P-value by 0.1 every time unit you find the drone is becoming more stable. DON'T FORGET TO CLICK ON WRITE SETTING AFTER CHANGING THE VALUE.

im 8

Every time you change the P-value, disconnect the drone and check how the drone is performing. If you find the drone oscillating, connect it again to the MultiWii Win GUI and increase the value again.

im 9

After a lot of Trials and Errors, I found these values to give good stability for the drone. For Roll, P = 0.9, I = 0.010, D = 34. For Pitch, P = 1.1, I = 0.010, D = 37. The rest remains the same. Now this value works for me, but your PID value might be different, but close to my values.

im 10

This gives the drone its stability and everything is working fine. You can see the video on my YouTube channel about the PID and also how the drone is performing.

im 11

im 12

This is the last post for this project. In the next post and video, I will be starting a new project. So make sure to subscribe to my YouTube channel to make sure that you are up to date with the projects that I am working on. Watch the YouTube video below.