Wednesday, April 11, 2018

Arduino Self Levelling Drone (Part 5)

Loading the QuadCopter Firmware

The first step is to download the firmware and save it in your Arduino library directory. This zip file will include 3 Arduino sketches:

  1. YMFC-AL_setup.ino
  2. YMFC-AL_esc_calibrate.ino
  3. YMFC-AL_Flight_controller.ino

Download the setup sketch to your Arduino and open up the IDE serial monitor (Tools -> Serial Monitor) and follow the instructions. If you get any errors then the firmware author has an FAQ. DO NOT connect the battery at this stage.

The complete setup will look something like the following. Make sure that you assign the pitch, roll and yaw functions to different receiver channels. The usual setup is called mode 2 which assigns pitch to the right stick up/down, roll to the right left/right, throttle to the left stick up/down and yaw to the left stick left/right.

Make sure that you check the high, low and centre values for the four digital input channels from the receiver. The low should be close to 1000, the middle near 1500 and the high around 2000. I had to run setup a few times before the correct values were recorded.



YMFC-AL Setup Program

For support and questions:

Have fun!

System check
Checking I2C clock speed.
I2C clock speed is correctly set to 400kHz.

Transmitter setup
Checking for valid receiver signals.. OK

Place all sticks and subtrims in the center position within 10 seconds.
9 8 7 6 5 4 3 2 1  

Center positions stored.
Digital input 08 = 1492
Digital input 09 = 1492
Digital input 10 = 1516
Digital input 11 = 1492

Move the throttle stick to full throttle and back to center
Throttle is connected to digital input 10
Channel inverted = no

Move the roll stick to simulate left wing up and back to center
Roll is connected to digital input 8
Channel inverted = no

Move the pitch stick to simulate nose up and back to center
Pitch is connected to digital input 9
Channel inverted = yes

Move the yaw stick to simulate nose right and back to center
Yaw is connected to digital input 11
Channel inverted = no

Gently move all the sticks simultaneously to their extends
When ready put the sticks back in their center positions
Measuring endpoints....

High, low and center values found during setup
Digital input 08 values:1008 - 1492 - 1984
Digital input 09 values:1004 - 1492 - 1988
Digital input 10 values:1012 - 1516 - 1988
Digital input 11 values:1492 - 1492 - 1500
Move stick 'nose up' and back to center to continue

Gyro search
Searching for MPU-6050 on address 0x68/104
MPU-6050 found on address 0x68

Gyro register settings
Register 0x6B is set to:0
Register 0x1B is set to:1000

Gyro calibration
Don't move the quadcopter!! Calibration starts in 3 seconds
Calibrating the gyro, this will take +/- 8 seconds
Please wait....................
Axis 1 offset=-99.78
Axis 2 offset=-66.14
Axis 3 offset=-60.42

Gyro axes configuration
Lift the left side of the quadcopter to a 45 degree angle within 10 seconds
Angle detection = 1
Axis inverted = no
Put the quadcopter back in its original position
Move stick 'nose up' and back to center to continue

Lift the nose of the quadcopter to a 45 degree angle within 10 seconds
Angle detection = 2
Axis inverted = yes
Put the quadcopter back in its original position
Move stick 'nose up' and back to center to continue

Rotate the nose of the quadcopter 45 degree to the right within 10 seconds
Angle detection = 3
Axis inverted = yes
Put the quadcopter back in its original position
Move stick 'nose up' and back to center to continue

LED test
The LED should now be lit
Move stick 'nose up' and back to center to continue

Final setup check
Receiver channels ok
Gyro axes ok

Storing EEPROM information
Writing EEPROM
Verify EEPROM data
Verification done
Setup is finished.
You can now calibrate the esc's and upload the YMFC-AL code.

After the setup is completed all the settings are stored in the EEPROM of the Arduino.

Checks and Calibration

The following is reproduced from the web site of the firmwares author.

To make sure that everything is working correct it's necessary to run some basic checks. Remove the props, disconnect the flight battery and upload the ESC calibration program to the Arduino. Open the serial monitor at 57600baud.

Receiver input check

Send the letter 'r' to start the receiver monitor. Now move the sticks and see if the values on the screen correspond with the movements of the sticks.

All the channels should read 1000us till 2000us with a center position of 1500 (+/-8).

Gyro / accelerometer angle check

After the receiver check is completed send the letter 'a' to start the angle check.

Don't move the quadcopter because the gyro needs to calibrate itself. After the calibration the roll and pitch angles are shown. The yaw value is the output of the gyro and will go back to zero if the yaw rotation stops.

Check if the angles correspond with the movement of the quadcopter:

Nose up is positive pitch and nose down is negative pitch.
Left wing up is positive roll and left wing down is negative roll.
Nose right is positive yaw and nose left is negative yaw.

Calibrate the ESC's

Electronic speed controllers or ESC's for short are controlled with a 1000us till 2000us pulse. 1000us means off and 2000us means full throttle. To make sure that all the ESC's react the same way it's important to calibrate the 1000us and 2000us point. Without calibration the motors will perform different and the quadcopter doesnt fly well or might even crash.

Remove the props and upload the ESC calibration program to the Arduino. Disconnect the USB cable and follow the instructions in the manual to calibrate the ESC's.

In most cases ESC Calibration works as follows:
  1. Throttle to minimum and turn on the transmitter.
  2. Place the throttle stick in the upper position (full throttle).
  3. Connect the flight battery.
  4. Wait for your ESCs to emit a musical tone, the number of beeps indicates your battery’s cell count (i.e. 3 for 3S, 4 for 4S) and then an additional two beeps to indicate that the maximum throttle has been captured.
  5. Pull the transmitter’s throttle stick down to its minimum position.
  6. The ESCs should then emit a long tone indicating that the minimum throttle has been captured and that calibration is complete.
  7. f the long tone indicating successful calibration was heard, the ESCs are now “live” and if you raise the throttle a bit they should spin. Test that the motors spin by raising the throttle a bit and then lowering it again.
  8. Disconnect the flight battery

If possible check the manual of your specific ESC for the correct calibration procedure. The ones I purchased didn't come with any instructions but the above procedure worked.

It was at this stage that I smoked a couple of my ESC's and motors. I don't know what caused this but replacing them got everything working. I suspect that the cheap ESC's and motors may have some quality control issues. If you do buy the cheap ones that I purchased then I suggest getting an extra set. In retrospect I would probably buy better quality ESC's and motors to avoid the aggravation of having to re-solder everything. If smoke starts coming out of your ESC's or motors (or anything for that matter) disconnect the battery immediately! If smoke is just coming out of the motors, replace both the ESC and the motor, I burnt out an extra motor because I didn't do this.

Burnt out ESC - you can see where it melted through the heat shrink.

It is also possible to calibrate the ESC's individually but you need to do this before you cut off the servo plugs on the ESC control cables and solder them to the Arduino. For completeness the procedure for individual calibration is:

  1. Plug one of your ESC three-wire cables into the throttle channel of the RC receiver. (This is usually channel 3.)
  2. Turn on the transmitter and set throttle stick to maximum (full up).
  3. Connect the LiPo battery
  4. You will hear a musical tone then two beeps.
  5. After the two beeps, lower the throttle stick to full down.
  6. You will then hear a number of beeps (one for each battery cell you’re using) and finally a single long beep indicating the end points have been set and the ESC is calibrated.
  7. Disconnect battery. Repeat these steps for all ESCs.
  8. If it appears that the ESC’s did not calibrate then the throttle channel on the transmitter might need to be reversed.
  9. If you are still having trouble after trying these methods (for example, ESCs still beep continuously) try lowering your throttle trim 50%.

You shouldn't need to do individual calibration but it is available if the all at once method doesn't work.

Attach the Propellers

Attaching a collet style prop adapter is not obvious the first time you attempt it. This is probably one of the most common prop adapter you see people using. It has a collet that grabs the smooth motor shaft when propeller nut and washer is tightened down.

The first step is to slip the collet onto the motor shaft. This should be a tight fit, otherwise it is difficult to screw down the nut without everything spinning. Some people suggest putting Loctite on the shaft, this is probably not a bad idea but wait until you have performed all the tests below and are doing the final tighten up before flying. I tried it without Loctite but on the first flight the propeller spun off, luckily it was still on the ground at the time. So I went back and put Loctite on the motor shaft and the propeller nut. Make sure that none of it drips down into the motor. I used the blue Loctite so that there was some chance I could get it off when I needed to replace a propeller. The red Loctite requires heat to remove it, which is a problem if you have plastic propellers.

Next slip on the spacer which grabs the bottom of the propeller. Make sure that you put it the right way around.

Finally put on the propeller and screw down the prop nut to keep everything in place. Note the props are designed to be clock wise or counter clockwise. They normally come in pairs. Make sure you mount the correct prop on each motor (refer to Figure 1 below). You want the leading edge of the props to face in the direction of the rotation shown below. The leading edge is the highest edge on the prop.

You can use a drill bit or similar to tighten the prop nuts, these need to be secure but be careful not to strip the aluminium thread. Finger tight should work.

Check Rotation Direction and Balancing the Propellers

The next step is to check that the props are spinning in the right directions and that they are balanced. Adjacent props spin in the opposite direction to keep the drone from yawing. This provides a similar function to the tail rotor on a helicopter. The props need to be balanced to minimise vibration which will prevent the gyro and accelerometer from self levelling.

We will again use the ESC calibration software for these checks. For convenience we have reproduced the balancing instructions from the web site. You shouldn't need to reload the ESC calibration software.

Make sure that you hang onto the drone when you test with the propellors mounted. Learn from my mistake - even one propellor has enough lift to flip the drone off the table and onto the floor!


Mount the props on the motors and check if the counter clock wise and clock wise props are in the right position. Upload the ESC calibration program and open the Arduino serial monitor at 57600baud. Send '1' via the serial monitor and wait for the response "Test motor 1 (right front CCW.)".

The numbers that are printed on the screen represents the amount of vibration measured by the accelerometer. This is not a standardized value and should only be used to minimize the amount of vibration of your YMFC-AL quadcopter.

Hold the quadcopter firmly down, place the throttle in the lowest position and connect the flight battery. Now slowly increase the throttle until motor 1 starts to spin. Check the direction of rotation and that the prop produces upward thrust. If the motor rotates in the wrong direction you need to switch two of the three motor wires. Put the throttle in the lowest position to stop the motor.

Now hold the motor frame firmly in your hand and increase the throttle to half throttle. Check the numbers on the screen and also memorize the vibrations that you feel with you hand that is holding the motor frame.

Stop the motor and put a small piece of tape on one of the blades and run the test again. Check if the vibrations reduce. If nor try a piece of tape on the other blade. Keep doing this until the motor and prop run as smooth as possible. This can sometimes be a daunting task but the reward is a very stable flying quadcopter. So take your time and get it perfect!

When done with motor 1, send a '2' via the Arduino IDE and start the process again for motor / prop 2. And after that, send a '3' for motor number 3 and a '4' for motor 4. By sending a '5' all the motors will run together as a final test.

Flight Controller Software

We are finally ready to load the flight controller software. Disconnect the battery and upload the YMFC-AL_Flight_controller sketch to the Arduino. When done, disconnect the USB cable and find a spot to test your drone.

Always turn on your transmitter (with throttle in the minimum position) before connecting the flight battery. This is a good habit to get into.

Hold the quadcopter firmly in your hand and start/stop the motors with the following sequence:

Start = throttle down and yaw left
Stop = throttle down and yaw right

If you have flown any DJI drones this may be the opposite to what you are used to. We want to test the start/stop function before we do anything else.

Start the engines again and increase the throttle up to the point when it almost starts to become weightless. The quadcopter should now try to level itself. If you move the quad it should start to counteract the movement until it is level again. This is an important step, you need to make sure that the drone is balanced and able to self level before you try and fly it in the wild. If you don't it may be very difficult to fly - I learned this the hard way...

When the roll or pitch stick of the transmitter is moved the quadcopter should move in the same direction. If this is not the case redo the setup procedure and double check the settings.

Next up we will cover the procedure for the first flight. You need to become familiar with the rules for flying drones in your local area, particularly if you are going to be stupid enough to post a video on YouTube of you doing the wrong thing. CASA is watching!

No comments:

Post a Comment