Wednesday, April 18, 2018

Custom Arduino Shield in Fritzing

I use Fritzing to develop prototypes and design simple PCB's. Although there is vigorous discussion on the merits of Fritzing compared to other open source free tools, I find it meets my needs.

One thing that wasn't obvious (too me at least), was how to create your own custom Arduino shield without having to work out where to place all the pins so it would mount correctly on an Arduino.

Figure 1. Select Arduino Shield from shape - properties.


This turns out to be fairly straight forward when you know how. Here are the steps:

  1. Open Fritzing (download it if you don't already have a copy - there is a version for all the major OS's).
  2. Select the PCB tab. Tap anywhere on the blank PCB and then change the shape to Arduino Shield (Figure 1) in the properties shape field on the right hand side of the screen. The PCB should then change from a rectangle to the shape shown in Figure 1.
  3. In the parts section, select the Arduino group and drag an Arduino Uno part across to match the position of the PCB. This will give you the pins and their label. Job done!
  4. You can then design your custom shield in the breadboard or schematic tab and connect to the pins on the Arduino to connect to the correct pins on the shield.




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.

===================================================

Your
  Multicopter
    Flight
      Controller

YMFC-AL Setup Program

===================================================
For support and questions: www.brokking.net

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
OK!
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
OK!
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
OK!
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
Done!
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 Brokking.net 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!

TAKE CARE WHEN DOING THE FOLLOWING TESTS - THE DRONE CAN BE DANGEROUS WITH THE PROPELLERS INSTALLED!!! MAKE SURE YOU TAKE APPROPRIATE PRECAUTIONS, KEEP FINGERS CLEAR OF PROPS AND WEAR SAFETY GLASSES.

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!



Arduino Self Levelling Drone (Part 4)

Connecting Power to the Arduino




There are a number of different ways that you can power your Arduino. To select which one is most appropriate for our Drone we need to consider each option.



1 – USB Port: this is good for prototyping and where you have little or no external current requirements or voltage greater than 5 V. You can supply 5 V via the USB port and this enables the dual function of powering and programming the board. The current limit imposed by the PTC self-resettable fuse is 500 mA, but usually the fuse will tolerate up to almost twice that value before blowing. The LiPo battery will deliver over 12 V when fully charged (nominal is 11.6 V), so this option is not appropriate for the drone. Note also that if you are powering the Arduino via USB from a computer, there is a current limitation of on that port of 250 mA or 500 mA, (depending on the USB port type).

2 – The 2.1 mm DC JACK Port: You can connect between 6 and 20 V to this jack. The onboard Arduino regulator will provide 5 V for the board and external devices if required. Practically, you want to supply between 7 and 12 V. The lower limit allows for voltage loss via the protection diode and regulator, while the upper limit will prevent the regulator from running too hot. The jack needs to be wired centre positive. If you provide power via the DC jack then you can access this (less the voltage drop from the protection diode) from the Arduino Vin pin. This can be useful if you need voltages other than 5 V.



3 – Vin Arduino Pin: this pin can be used as either an input or output.

3a: input for external powering, note that it is NOT protected against reverse polarity. The Vin pin connects to the regulator’s input and after the DC JACK protection diode. Don't ever connect voltage to the DC jack and the Vin pin.

3b: output from which to draw the voltage applied to the JACK socket, less the protection diode’s voltage drop. This can be used to power small loads, requiring a voltage higher than 5 V and equal to the voltage applied to the DC JACK socket (less the diode drop).

In both cases the voltage negative is the Arduino GND pin.

The 3.3V and 5V Pins

The 5V pin is directly connected to the regulator’s output, and you can use this to power external loads. If no voltage is applied to the USB Port or to the DC JACK, then the 5 V pin could be used to power the Arduino but it is not recommended. In general, regulators do not like voltages being applied to their output, but this happens even when powering the Arduino from the USB port, therefore we assume that the board can cope with this. Nevertheless it is still dangerous since there is no protection (both the diode and the PTC fuse are up stream of this pin).

There is another regulator connected to the output of the 5V regulator which delivers 3.3V for use in external loads. It can not be used as a voltage input for the Arduino.

Conclusion

Given all the above, for the drone we will connect the LiPo battery via the DC jack. This gives us diode reverse polarity protection and can handle the 11.6V or so that we get from the battery (depending on the charge state). Using a 2.1 mm plug also makes it easy to connect and disconnect the Arduino power.



Use the wires that come from the Power Distribution Board and connect to the battery (red and green in the photo above). The plug is centre positive (i.e. connect the middle pin to the red/positive cable). Make sure you put heat-shrink on the cables before soldering.



Once you have wired the plug, check continuity using your multimeter. The LiPo battery dean plug makes an easy test point.



Finally you can insert the 2.1mm plug into the Arduino DC JACK. While you are there, also insert the Right Angle Headers (green in the photo below).



Wiring up the Arduino IO


MPU-6050




We will start with wiring up the MPU-6050 I2C connections. It is easier if you pre-tin the 90 degree Arduino headers and the wire. Hopefully you took note of which colour wire connects to the SDA and SCL connections on the MPU-6050 (see Part 2 in this tutorial). In our case:

BLUE    = SDA
WHITE = SCL

Leave enough slack in the wires so that you can pull out the 90 degree headers (to remove the Arduino) if required. Make sure you put the heat shrink on the cables BEFORE you solder the connection. You need a steady hand.



We will wire up the 5V and GND for the MPU-6050 shortly, we need to do a simple 5V wiring harness to include the receiver as well since we only have one 5V pin on the Arduino.

Indicating LED




Next up is the indicator LED which is wired to D12 on the Arduino. You can calculate the current limiting resistor for your LED using the LED Resistor Calculating site. You will need:

1. Source Voltage: 5V
2. LED Forward Voltage: 1.68V(I measured mine using an LCR meter but you can get it from the data sheet).
3. LED Forward Current: 20mA (nominal/typical from Data Sheet).



This gives a result of 180 ohms for my LED. If in doubt, just use a 330 ohm resistor. Note when wiring LED's that they are polarised.





Wire the positive of the LED to the current limiting resistor, then cover with heat shrink. The resistor end then gets soldered to D12 and the -ve end of the LED to GND. Refer to the photograph below.



Receiver Connections



Now we will do the receiver connections and the 5V bus. Use 5 servos cable for this, as we need the end connector to plug into the receiver. Use one servo cable as the power connection and the other 4 to connect the signal (white wire on my cable). In other words, remove the white wire from one servo cable and remove the red and black wires from the remainder.



The receiver should indicate which way around to plug the servo cables. Plug the power connection to BAT (double check that you have it the right way around) and then plug in the signal wires to channels 1 through 4. You can remove the plugs from the end which goes to the Arduino.



Cut the signal wires to length, strip and pre-tin as before. Have the heat shrink in place before soldering.



The connections are:

Arduino D8    to    Rx CH 1
Arduino D9    to    Rx CH 2
Arduino D10  to    Rx CH 3
Arduino D11  to    Rx CH 4



5V Wiring Harness



Construct a wiring harness to connect the 5V pin on the Arduino to the power on the receiver and MPU-6050.



Similarly, connect a GND wiring harness to connect one of the Arduino GND pins to the receiver, MPU-6050 and ESC BEC GND (brown wire - do this if they aren't already connected via the ESC to the GND on the Power Distribution Board). I connected the two back arm ESC GND's to this harness and the two front ones to the GND used for the voltage divider resistors. Refer to the photographs.



Voltage Divider




Note when wiring the voltage divider that the order of the resistors is important. The 1.5k resistor needs to connect to Vin and the 1k to GND.



The output voltage gets connected to A0 on the Arduino. To make things easier to keep track of, I marked the 1.5k resistor with liquid paper.



Wire the two front ESC GND (brown) wires to the GND side of the 1k Resistor, then cover the resistors in heat shrink.



Finally solder the resistors to Vin and GND and the wire to A0.



ESC Control Lines



The last connections to the Arduino are the four ESC control (orange) wires. The connections are as follows:

Arduino D4  to  ESC 1 (right front - CCW rotation)
Arduino D5  to  ESC 2 (right rear - CW rotation)
Arduino D6  to  ESC 3 (left rear - CCW rotation)
Arduino D7  to  ESC 4 (left front - CW rotation)

Pre-tin the Arduino pins and wires. Put the heats shrink in place before soldering and then make the connections shown above. You can cut off the ESC red 5V BEC wires if you want. These are not used. I'm right handed but I find it easier to hold the wire in place and solder with the left hand in this instance.


That concludes Part 4. In Part 5 we will download the firmware, and calibrate the sensors, ESC's and Receiver. You will need a charged LiPo so get it on the charger.


Monday, April 9, 2018

Arduino Self Levelling Drone (Part 3)

Bullet Connectors




My ESC's and motor wires didn't come with bullet connectors and you could just solder these wires together but if the motor is running the wrong way then swapping the wires becomes a lot harder. Bullet connectors also make it easier to dismantle the drone in the future or replace individual components.



It may help to pre-tin the leads and make sure that you have removed enough insulation so that you can fit the wire, soldering iron and solder in the back of the plug and socket. Make sure that you place the heat-shrink on the wire before you solder it to the plug or socket. I used a different colour of heat-shrink on each wire to make it easier to track what is plugged into what.

You need four hands to solder these things so I jury rigged a bullet plug holder using pliers and an elastic band.



You may find it easier to solder the bullet plug socket to the ESC's before you solder them to the power distribution board, but it doesn't make much difference.

Once you have done the ESC's you can move onto the motors.



Make sure that the motor leads are all the way into the bullet plugs and don't move anything while the solder hardens otherwise you could get a dry (high resistance) joint.




Lessons Learned


One thing that I would do different if I did this again, is to measure the length of wire required to fit neatly along the F450 frame arm and have just long enough to connect to the motor wires. I ended up leaving these wires too long which is not a problem functionally but it doesn't look as neat as it could. See the picture below.


Trim the ESC (blue) wires before attaching the bullet plugs or you will end up with this.

Also as I ended up replacing a couple of the ESC's, it would have been easier if I also had bullet connectors used for the connection to the Power Distribution Board rather than soldering them directly.

One of the burnt out ESC's.


Mount Motors on the Arms


We can now start the arm assembly. My motors come with a mounting bracket with holes that don't match up with the holes in the motor, and four very short screws. Luckily we don't need these. Use 4 of the long hex bolts which come with the F450 air frame.



Once again it feels like you need four hands but if you hold the motor and arm in place with one hand you can screw in the bolt using an appropriately sized allen key with the other. Get the first bolt just in then start on the bolt diagonally opposite. Then start the final two bolts.



Screw in each bolt only a little and then progressively tighten each bolt (similar to changing a tyre on a car). Eventually we will put loctite on these but for now just screw them in firmly (not too tight). Push the 3 motor wires through the gaps in the airframe arms. The ESC's are mounted on the underside of the arms.



You can now push the propeller holder onto the motor shaft. My motors came with collet prop adapters. 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 the propeller nut and washer is tightened down.

Connect Arms to the Power Distribution Board


Traditionally the two red arms are mounted at the front of the drone, so that you can tell which direction it is pointed.



Use the small hex bolts that came with the F450 air frame to mount each of the arms to the power distribution board. You will need a smaller size allen key for these bolts.



The easiest way is to turn the power distribution board over and connect the arms from below. Make sure that the power and servo leads from the ESC's pass through the gap in the legs and don't get crushed as you attach each arm.



After mounting the arms, plug the ESC wires into the motor wires using the banana plugs. Which wires plug into which doesn't matter at this stage.



We eventually want to add up with the situation shown in the table below, so I wired arms which were diagonally opposite the same.

Arduino  Location    Direction of rotation

D4right front  counter clockwise
D5right rearclockwise
D6left rearcounter clockwise
D7left frontclockwise

Finally, cable tie the ESC's to the drone arms. Try and place each ESC in a similar position on the arms to keep the drone balanced.



Attach the Top Board to the Arms


Turn the Power Distribution Board and arms over and mount the top Arduino Board using the same small hex bolts. Four are required for each arm, but the top board will block some of these holes, just use the ones you can access at this stage. Once again screw them in firmly but not too tight. We will need to pull this apart again to mount the battery, etc.



This concludes the hardware build of the drone. In part 4 we will wire up the rest of the electronics to the Arduino.