How to make esp8266 based drone controller

How to make esp8266 based drone controller, you are excited from the title! well i am eager to show how i made this.

The search to make quadcopter without a flights controller ends here. I was kind making drone for my project

I was making a drone for my project I was thinking make it without flight controller as the flight controller goes to expensive so i was surfing the net all night without the flight controller to make it work and it was very disappointing that I didn’t do one. So I decided to make this blog so it’s easier for people who will be making a quadcopter without a flight controller.

Table of contents to make esp8266 based drone controller

  1. Intro
  2. How a flight controller works & steps
  3. Story & why selecting components
  4. MPU 6050 
  5. How PWM works  &  ESC control
  6. How PID works  &  Implement in codes
  7. Solve timing issues
  8. Communication Issues with RC
  9. WIFI UDP socket how works and implement
  10. Explain Anish Numerical system (Ascii) & implement
  11. Wi-Fi communication
  12. Flight Test
  13. Outro

Intro: For a long time I’ve dreamt about my own Flight Controller. Yes, I made my own Flight Controller including best of all the features I’ve upgraded till now. So, let’s break those limitations.

FC (flight controller ) working:

A FC mainly contains 4 segments. Input segment takes instructions from the user where to move the drone. And the Output segment sends PWM signal to the ESCs and control the motor speed in order to fly according to the instructions. The IMU simply measures the tilt angle in 3 axis. The processing unit collects all data and corrects the tilt angle using PID controller. We can also include peripherals devices like GPS, compass. But that would be for another video. 

Components

For the processing unit, a NodeMCU had laid arround since long so I’m gonna use that. *MEME* Actually this little device is way more powerful than I thought. It has WiFi, it runs on 80Mhz and it has inbuild floating poinfollt processing unit.So that’s quite awesome.

For the IMU I’m gonna use MPU6050. It comes with a gyroscope and an accelerometer sensor. Here I’m not using any Tx Rx ,I will use WiFi for communication and the reason.

wait a bit. For the rest of the drone I’m gonna use the drone which I’ve made 2 year back. So, I gathered all the things and started with the coding.

MPU6050

 

MPU6050

This sensor uses i2c to communicate. So I connected SDA & SCL to D1 and D2 of the NodeMCU. As described in the datasheet it has 3 modes for each sensor.

I’m gonna use 500 degree scale range of the gyroscope and 8g scale range for the accelerometer. To get the angles from it I have to configure these registers. The Wire library of arduino IDE will easily do this jobsThen it requests 14 bytes from the sensor and read the angles in series.

Make sure that you have defined the integers as 16 bit as the data is a 2’s compliment value. The accelerometer provides realtime angle But the gyroscope provides angular velocity. If I want to get angle from the gyroscope then I have to integrate the angular velocity in each loop.

So, I got angles from 2 different sensors. Well, the motors generates a lot of vibrations which will significantly affect the accelerometer. So I’ve used a complimentary filter to overcome this problem. Which uses both sensor data to generate a stable angle.

But the gyro sensors have little errors. To get it solved I places the drone level and read the data for 4000 times then took the average and I got gyro errors.

In this way I can calibrate once and use every time. Then I substracted it in each loop. So, now I get the perfect realtime angles.

PWM & ESC

Before getting into the ESCs we need to know about PWM. PWM is a method of producing pulse of HIGH & LOW state of a pin. The data transferred through it is the time between the rising edge and the falling edge of the pulse and it is refreshed periodically. Which is the frequency of the PWM signal. (how-brushless-motor-and-esc-work)

If you pull the signal pin of the ESC to  high and pull it down after 1000 microseconds then the throttle input to the ESC is 0% and if you increase that time upto 2000 microseconds then the Speed of the motor will increase accordingly. So, here after defining the pin as output in the loop section I set the pin to high and after 1000 microseconds I pulled it down.

If I increase the HIGH state time then the motor speed increases. Normally this range is 1000 to 2000 microseconds. I used direct register control to minimize the delay of setting the pin high and low. Then I used micros timer instead of the the delay function. Now whatever the value is, it will always be in range of 1000 to 2000 microseconds.

So after setting the pin HIGH I have to wait at least 1000 microseconds. And it’s a waste of time. So I put the MPU6050 code in that waiting portion. So, at first I set the pin HIGH and then read the angles data as it roughly takes  400 microseconds.

Then it waits until 1000 microseconds is over. Then pull down each of the 4 pin connected to esc according to the throttle to be send to the ESCs. In this portion we got 600 microseconds so I can put other codes here.

And still the refresh rate will be 2000 microseconds giving us a PWM frequency of 500Hz.

Have a look how brushless motor control works:

by how to mechatronics

PID

So, now we can control speed of individual motors. But to sync it with the angle in order to correct the tilt angles I’m gonna use PID controller. It uses three type of controller. The P controller changes it’s value proportional to the angle using this formula.

The more you tilt the drone the more it will increase and will reduce if the tilt angle decreases. But only with it the drone will be wobbly. Some thing like this. So we need some braking system. And here the D controller comes into place.

It only reacts to changes in the angle. But when the angle is close to 0 degrees then the P controller will not work for the D controller, as the P controller value will be very low compare to the D controller value  at that angle.

So we need another I controller which will intregate values untill the tilt angle is perfectly 0 degree. And the PID is the sum of those three  controller.

I created PID controller for each of the 3 axis of the drone, ROLL, PITCH and YAW. This calculation takes very short time so I put the code in that 600 microseconds waiting state while generating PWM signals for the ESCs.

WiFi

Now the drone is stable but I need something to communicate with it and control. I could use The commercial TX Rx but they use PWM . But I don’t wanna mess up with it again. Since the nodemcu has inbuilt WiFi, I can use it for communication.

Esp8266

I used UDP communication to communicate with the python script running on my Laptop. That script is very simple. Just some keys to increase and decrease throttle, control direction & changing PID constants values. And the whole data is 28 byte long causing a significant amount of delay in the loop for reading the character from the buffer array. So I have to compress the data.

Circuit Diagram to make esp8266 based drone controller

ASCII

Then I got something crazy. Ascii keys… Since the data only contains integers so they can be presented with some specific character. The WiFi_UDP library uses 7 bit character array as buffer. So, we can use 0 to 127 ascii value.

But what will happen to those number which are greater than 127. Well for that I divided the integer in two parts and presented it with two ascii keys then decoded it again in the arduino loop section.

But still I can’t send all data at once. So I used another character to define the data. Type1 contains roll, pitch, throttle and yaw values & type 2 contains PID constants. So, it will check the first byte and then read the data accordingly.

Also I want to stream angles and drone data to the python script. So, I used one loop for receiving data and the next one is for transmission & so on. And as you can see, it seriously reduces  the looptime, about 2200 microseconds maximum.

Tuning

Then I started the drone and configure the PID Tuning. Increase the D controller until it becomes wild. then same to the P const. and set the I controller to minimum values. And that gives me a very good result. Well this is a very simple PID controller. I will make an advance version of the PID controllers later in another video.

Flight

Check my flight test after i make esp8266 based drone controller

Outro

This is a very simple Flight Controller. I will upgrade it in upcoming videos. like – adding GPS. compass, autopilot, althold and a lot more things. And talking about this…  I have used Wi-Fi instead of transmitter, So, yes it can provide unlimited range. Don’t worry I’ll make a dedicated video on Tx communication.

I’ll upload the codes and make it opensource for all of you  if this video cross 1000 likes. So, like & share the video, subscribe for upcoming projects.

I hope you enjoyed the idea to make esp8266 based drone controller, you can check our other interesting iot projects 

And thanks for your support, see you very soon in the next video till then bye bye.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *