Monday, 2 March 2020

Custom Flight Controller Part 4.1: Assembly and Tuning PID

This is the final part of the project. In this part, with all the software and hardware ready,  I will document how I assemble all parts together and present my final quadcopter build.

Assembling quadcopter components is relatively easy and there are a lot of blog posts online elaborating the detailed process. In this post, I will simply document one challenge I faced. 

During assembly, I had to enforce the correct rotation direction of each motor by soldering the wires onto the ESC but there is no way of knowing which way motor will rotate with respect to a specific wire connection other than actually running it. However, since I write my own flight control software, I do not have to have a fixed motor rotation direction. That means it's OK for me to have front left motor spinning either clockwise or anti-clockwise. All I have to do is to compensate it in software later on. Therefore, what I needed to do was simply make sure that the the spinning direction of front left motor is the same as the spinning direction of back right motor and their spinning direction is opposite to that of motors on the front right motor and back left. To achieve that, I just needed to swap one wire connection for one pair of motor as elaborated in the picture below. With this trick, there is no need to actually connect the battery and run the motor. 
The wire connection for motor 2 and 4 is different from wire connection for motor 1 and 3.


Tuning PID


After assembling the quadcopter, it is time for the final but arguably most painful part of the whole project, tuning PID. There are a lot of guides about how to tune PID for quadcopter. However, most of them uses betaflight flight software which only has one set of PID. In my case, my control structure is more similar to Pixhawk 4's and thus I followed their guide mainly. Also, being able to adjust PID value using transmitter as documented in one of the previous post helped enormously.

As suggested, you have to tune inner loop attitude rate PID first. To do that, I added a macro defined flag UAV_CMD_ATT_RATE in my top-level UAV_Defines.h header file. When this flag is set, the flight control software will execute attitude control only and the data from transmitter will be assumed to set desired attitude rate. In another word, the outer attitude control loop is completed omitted in this mode. In existing flight control software, this is often referred to as Acro Mode.

The effect of different value of PID in this mode is clear. If P is too high, the quadcopter will start to oscillate, but if P is too low, the quadcopter will not respond to your command. D value will help prevent oscillation caused by large P but if D value is too high, it will lead to oscillation as well. My steps of tuning attitude rate PID is as follows:
  1. Set pitch and roll rate PID to all zero.
  2. Slowly increase yaw rate P value if needed so that the quadcopter's yaw is relatively stable.
  3. Slowly increase pitch and roll P value until the quadcopter starts to respond to user command or the quadcopter starts to oscillate.
  4. If the quadcopter starts to oscillate before it responds to user command, increase D until the oscillation disappears. 
  5. Repeat step 3 and 4 until the quadcopter is responsive enough.
  6. Increase yaw rate P value a little bit. Make sure it does not cause oscillation.

After the inner loop is tuned, I disabled the flag and put the flight control software back to attitude control mode, and proceed to tune attitude PID in this mode without changing the value of inner loop PID. The PID value is adjusted in similar fashion. This time, with a good P and D value, the quadcopter should be able to hover in air, pitch, roll, yaw in response to your command.

Finally, the quadcopter is ready to fly.




No comments:

Post a Comment