Saturday, October 6, 2012

HefnyCopter2 Gyro Noise Filtering


HefnyCopter


HefnyCopter is a firmware specially designed for KK boards. There are two types of this firmware.

HefnyCopter


                This version targets the green and blue KK boards from HobbyKing. The main feature of this version is that it allows awitching your Quadcopter from X to PLUS mode without the need to reorient the board. The main concept of this feature explained in this topic “quadcopter-stabilization-control-system”.
                The hex & sourcecode is available here . Please make sure that you read the manual well before flying as POTS functions are not the same as the original firmware of KK boards.

HefnyCopter2


                This is a complete new version and goes in parallel with HefnyCopter, i.e. there are still updates on HefnyCopter so they are two parallel tracks.
                This firmware targets KK2 board. The board has LCD, Accelerometers, and Gyro. As well as a beter processor ATMega324. This means you have better capabilities here to help you to build a better firmware.
                HefnyCopter2 still has the feature of mode switching from X to Plus. But now you can connect your board via XBEE to your PC and receive telemetry data, you can work better on you stabilization algorithm and enhance your quadcopter stability.
                HefnyCopter2 also has the feature of supporting two TX, that means you can fly it with a friend and train him/her how to fly the quad.


How HefnyCopter2 Helps to Improve Stabilization



Because of the ability of sending data to PC using XBEE, I was able to visualize the data and try different approaches to filter out the noise.




The Windows application that connects to XBEE has nice GUI interface for realtime display, however the more useful part is the CSV file that it dumps for the received data. In the below figure we can see the received data. Columns in order are:

Gyro_X, Gyro_Y, GyroX, Acc_X, Acc_Y, Acc_Z, M1, M2, M3, M4
Raw Data as received from XBEE in csv format
Raw Data as received from XBEE in csv format

Please note that Acc_X represent the pitch so it goes with Gyro_Y, also Gyro_Y increase when quadcopter rotates forward, while Acc_X increase while quadcopter rotates backward. i.e. Acc_X is related to Gyro_Y and with negative sign. Same applied for Acc_Y & Gyro_Y.

Acc_Z represents the gravity effects that is why it is not 0, it is 100 which is equivalent to 9.8 m/s, Gyro_Z is used to detect YAW so it has nothing to do with Acc_Z.

Values ranges from 500 to -500, while motor ranges from 0 to 1000. To keep logged data at reasonable size data is only transmitted when quadcopter is armed and motors are on. that is why the minimum values of motors Ms is 100 here in the sheet as below this values quadcopter ignores TX signal and send Zero to motors.

Let now study Gyro_Y values and apply some math on it.

Gyro_Y data only
Gyro_Y Data received from XBEE

as we can see above this is Gyro_Y data, I added three more columns:

Move Avg
   
        This is a moving average column, which means each value is added to the total and then divide the total over two.
       Gyro_Y_Total = (Gyro_Y_Total + Gyro_Y ) /2

LPF

    LPF stands for Low-Pass-Filter, here i used a complementary filter to reduce noise. Complementary filter is not a complex math at all, at least if we focus on the formula not the derivation, and try to understand it from the simple view point. The formula is as follows:

     Gyro_Y_Total = (0.95 * Gyro_Y_Total) + (0.05 * Gyro_Y)

   Each time we take small part of the signal and sum it to the total. assuming that the signal has high frequency noise, so we need to smooth it by taking part of the signal and accumulate it, to avoid taking peaks that represents high frequency.

  Yes there are better ways to filter noise and at specific frequencies -as we can find in sound equalizer-, but computation is difficult and time consuming. If you are interested for more complex algorithms try Kalman Filter, or read topics about Fast Fourier Transform.

LPF2

    This is the same as LPF. I only changed the factors, and took double the amount in LPF.

     Gyro_Y_Total = (0.90 * Gyro_Y_Total) + (0.10 * Gyro_Y)


Gyro_Y under different filters
Gyro_Y under different filters


You can see clearly from above how complementary filter does remove noise from signal. There are two lines red & blue in the complementary filter the one with some noise is LPF2 and the red smoother one is LPF where we take only 0.05 of the signal value.

HefnyCopter2 now implements complementary filters for Gyro_X, Gyro_Y & Gyro_Z and the result was a much smooth flying.

I am still working on studying different behaviors and values received from my quadcopter. I believe there is a much yet to be discovered -at least for me-. 


Data Analysis Files can be downloaded from here.


Please follow up with us on HefnyCopter.net

Tuesday, September 4, 2012

QuadCopter Stabilization & Control System "X & Plus Configuration"


QuadCopter Balancing & Controlling Separation


This topic discusses how to separate between balancing quadcopter and controlling it.

First let us discuss the balancing control techniques in different quadcopter modes. Quadcopter can fly in (+) configuration and (X) configuration. The only difference between these modes is where the front of the quad is.

The following diagram illustrates the difference.

Quadcopter Configuration "X & Plus"

Plus (+) Configuration Control


In this configuration the control as follows:
// Pitch Control
M1 = M1 + RX(Elevator);
M4 = M4 - RX(Elevator);
// Roll Control
M2 = M2 + RX(Aileron);
M3 = M3 - RX(Aileron);

This is very simple control and is found in all quadcopter code. Sure there are some checks here and there to avoid motor stopping or saturating.  But the flying logic is always as above. There is also a scaling factor that is used to determine the sensitivity of the sticks and a Divide factor that limits the value range.


X Configuration Control


In this configuration the control as follows:
// Pitch Control
M1 = M1 + RX(Elevator) /2;
M3 = M3 + RX(Elevator) /2;
M4 = M4 - RX(Elevator) /2;
M2 = M2 - RX(Elevator) /2;

// Roll Control 
M1 = M1 + RX(Aileron)/2;
M2 = M2 + RX(Aileron) /2;
M3 = M3 - RX(Aileron) /2;
M4 = M4 - RX(Aileron) /2;


As we can see this is the same logic, we only assumes that M1 & M3 together acts as a single virtual motor in the front, and M2 & M4 together acts as a virtual motor in the rear. Other than that it is the very same logic.
Same considerations and factors applied in the PLUS configuration go here as well.

Stabilization System


Now let us see the stabilization system. The idea is simple, if the quad is falling to the right then speed up the right motor and slow the left one with the same amount and vice versa. Also if the quad is falling down from the front arm then speed up the M1 motor and slow down  M4. The rule is written as follows.

M1 = M1 + PitchAmount  *  PitchGain
M4 = M4 -  PitchAmount  *  PitchGain
M2 = M2 + RollAmount   *   RollGain
M3 = M3  - RollAmount   *   RollGain

This is the basic rule that should be found in all quadcopter programs. The main difference between the different firmware approaches is how to calculate PitchAmount and RollAmount. This can simply be read from a Gyro sensor and multiply it by a constant factor to adjust the range, or we can use PID approach or even Kalman filter and combine Gyro with Acc sensors to get the exact degree.
The Gain factor is used to determine the sensitivity. Other checks such as trimming are common in quadcopter code.

Do we need Different Stabilization for X-Configuration?


Well in almost all quadcopter programs you will find that stabilization and control calculation follows the same concept. i.e. if we use X-Quadcopter then we add the PitchAmount to both motors M1 & M2. Also RollAmount is added to motors M1 & M3. This is valid approach but in fact it is not necessary at all, also there is a major drawback here.
It is not necessary because the stabilization control of Plus configuration can stabilize quadcopter if there is a simultaneous picth & roll forces, that means if the Control System is Plus of X the Stabilization System can stabilize both using the same PLUS configuration.
The main drawback is that Stabilization control requires the orient the control board so that its onboard gyro and acc are in the right directions, so you cannot switch between PLUS configuration to X-Configuration without reorient the board. So if we can keep the Stabilization System and only change the Control System we don’t need to reorient the board as Control System has nothing to do with the board sensors it only change the signal values sent to different motors.

HefnyCopter has implemented this idea successfully as in the below video.



Also please read my other topic about Gyro Noise Filtering.

See newest article Quadcopter Flight-Control  Framework

Tuesday, July 31, 2012

My X-525 QuadCopter

This is my second trial of building a quadcopter. The first quadcopter -is the one we can see below- was totally homemade, the arms were too thin and U-shape so motors were twisting the bars and it was unstable.

early steps in building frame

My hand-made frame
My second quadcopter uses exactly the same motors and board, I decided to use Hobby King X-525 Quadcopter Frame.


Building the body was easy it took 40 min to assemble it. and another 4 hours in mounting motors and soldering connections.

rubber to reduce vibrations
Flying results was different, QuadCopter is much stable now.

X-525 Frame with folding bars
Still I need training, below was my first trial, now I fly it much better however still a beginner.



If you are interested in building your quad, below is a complete list of items you need to buy. Building a quadcopter is an interesting task, although it does not require as high skills as building a scale RC plane or a helicopter where mechanics are too complex. Quadcopter are very easy to balance as well, they mainly depends on their control board for balancing so minor mismatch in CG is not an issue at all.


Bill of Materials:
   
       - Quadcopter Frame Hobbyking X666 Glass Fiber Quadcopter Frame 666mm
   
       - Propellers you need two propellers CW & two propellers CCW. I tried both GWS 9x5 CW & CCW as well as SF 10x45 CW & CCW. if you are a newbie here, then you need to get at least extra 4 propeller of each type i.e. CW & CCW as you should expect that the first thing you will break here are propellers.

       - 4 x Brushless FC28-22

       - 4 x ESC HobbyKing 20A BlueSeries Brushless Speed Controller or Mystery 30A BEC Brushless Speed Controller (Blue Series)

       - Hobby King Multi-Rotor Control Board V3.0 (Atmega 328 PA) or the newer Hobbyking KK2.0 Multi-rotor LCD Flight Control Board with LCD and additional accelerometer sensors.

       - 1 x LIPO Battery Turnigy 2200mAh 3S 40C Lipo Pack . The quad flies for +10 min.

      - 1 x Receiver 6CH such as OrangeRx R610 Spektrum DSM2 6Ch 2.4Ghz Receiver (w/ Sat Port) it is fully compatible with Spektrum & JR yet it costs lest that 1/10 of the price of original receivers.



Other Misc. Stuff are:

       - 1 x 2mm Gold Connectors 10 pairs (20pc) These connectors are slim and very suitable for your FC 28-22 wires. You will need them to connect motor to ESC.

       - 1 x 10CM Male to Male Servo Lead (JR) 26AWG (10pcs/set) the set contains 10 pieces which are enough for the quadcopter . . . actually you will need four or five based on the control board you choose.

       - 1 x Hobby King Quadcopter Power Distribution Board This links your power wires from ESCs together to connect them to the battery. You can also use Single Male to 4 x 3.5mm Female adapter instead based on your quadcopter size.

      - 1 x Turnigy 5mm Heat Shrink Tube - BLACK (1mtr) This is important and easy to use as well. It keeps your connections tight and clean. you need a simple air drier to shrink them. It is great to isolate connectors and keep them together.

     - 1 x HobbyKing Programming card for BlueSeries Brushless Speed Controller This is important to avoid the hassle of programming your ESC using transmitters and magical beeps. The quad has four ESC, you need easy and accurate programming tool to make your life easier.
 
Extra Stuff:

      - 1 x On Board Lipoly Low Voltage Alarm (2s~4s) actually I would not say it is all extra, it is important if you plan to fly your quad high. . . you need an alarm to tell you that your battery is about to finish and you need to bring your quad down before it gets down by itself :)

    - LEDs exists in different types  RED - GREEN - BLUE they are great if you plan to fly quad at night



HefnyCopter Code

  The video below show my quad flying using my own Hefny Copter Firmware. The new feature in this firmware is that your ability to switch between X-Quad configuration and + Quad Configuration using your remote TX only. no need to reorient the board or recompile the code.





Wednesday, July 18, 2012

N-Channel Switch


What is N-Channel Switch



N-Channel Switch is a hardware device that is very much like multiplexer that takes input from two sources and based on a select signal it outputs on of these two signals.

General Function


Domains of Application

     This is a general purpose device, so it can be used in different fields; however it was specifically designed to target RC planes. 

     In UAV planes you may need to take-off and land using your own skills not autonomously using plane computer. You can easily and instantly take control from your plan. Same need when testing your UAV you need to make sure that if something went wrong you are still able to get it back again. The idea here is that this switch is completely isolated from your complex UAV logic, so if main processor hangs due to software bug you can still access this switch and get your plan back.

Another use of this switch is when trainer a new pilot on RC Planes. You no longer need to have the same type of transmitters; you can even use your FM together with his 2.4 GHz in a master slave combination.  All you need to do is to put two receivers on that plane, connect them to the switch and you can start your training session safely.


Features and Specification


  • Takes Throttle, Elevator, Rudder & Aileron input from two sources. This can be two receivers or a receiver and a UAV board.
  • Use PWM signal to switch from first input to the second. You can use gear, Aux1, Aux2 …etc. to make switching.
  • Two digital output ports can be used to turn lights or other devices on and off by using a control signal.
  • Failsafe Switching. In case one source stops sending valid data the switch automatically switches to the other source.



Pin Assignment

Input Ports

Output Ports

Circuit Diagram


Circuit Diagram

Source Code & Hex

HEX file is here
Also Source Code is here

Wednesday, June 20, 2012

Handmade Arduino

When I first read about Arduino, I liked how compact it is, and how much processing power I can get. Also I loved the library and the idea of standard pins. I decided to buy a Mini-Arduino to play with. It costs me 160 L.E. in Egypt, that is about 27 USD. I decided to buy an ATMega 328 chip and try to imitate the board. I bought a crystal and couple of ceramic capacitors and some resistors, and started to build up an identical circuit.


Circuit Diagram using Fritzing 






My Actual Circuit identical to the diagram


 [extra wires you can see connected to Arduino are for AVR programming]



The end result is using ATMEGA-326 alone with nothing connecting to it except a resistor in the reset pin.

to make sure that they run at the same speed I developed a simple application that blinks the LED on a certain rate, and I uploaded the application to both chips using AVR. LEDs started to blink on the same rate.


Application Source Code



The good side is that the handmade version costed me only 40 L.E. which 6.67 USD only.
Application can be downloaded from here.

Monday, June 11, 2012

What is Inside Spektrum DX7 Receiver


In this document I try to go through my findings and my expectation of board structure and behavior. 




Overview
My Tiger Trainer plane was completely destroyed in a crash. Even the muffler and the receiver were destroyed. I took this as an opportunity to know what is inside Receiver DX7, and how much is it different than much cheaper receivers such as Orange Receiver.



Boards StructureAfter some investigation I came out with the following outputs:
1- The receiver of DX7 consists of two main boards attached to each other as in figure 1

Figure 1: DX7 Receiver Board

The two boards are:

a. The smaller upper board is the actual receiving board and has two antenna attached to it – one of them was detached in my plane crash-.
b. The bottom bigger board is the main board. It takes receiver output and decodes it and sends output signals through output pins to servos.




Figure 2: Block diagram of the main board of DX7


Things to notice on this board:


1- The first thing I noticed is that the satellite external circuit is identical to the receiver circuit –smaller upper board- inside the DX7 body. To prove that I took the receiver out and solder satellite circuit wires to pins where the receiver board was connected. When I turned on the receiver and the transmitter they could connect to each other.


2- There is three receiver points on this board. One in the middle where the main board connects, one on the left where the satellite socket exists, and another one on the right that is active but cannot be used unless you take the cover and solder your wires there.

I expect this will give extra coverage because you will be able to a receiver board for each dimension and you will cover reliably 3D, so whatever the position of your plane the link will be very reliable.


Figure 3: Main Board, and pin assignment of each receiver.



Figure #3 shows how receivers are connected to CY8C27443 microcontroller. 


Datasheet for this microcontroller can be found here 


Output Pins:

Throttle:   Pin 26     P0[6]

Aileron: Pin 4 -    P0[1]
Elevator: Pin 1 -    P0[7]
Rudder: Pin 24 -  P0[0]
Gear: Pin 25 -  P0[2]
Aux 1: Pin 3 -    P0[3]
Aux 2:      Pin 6 –   P2[5] 

What is can we conclude here is that the microcontroller receive signals from multiple receivers and determine which signal is valid. Then it simply converts serial data to PWM signal for each output. I found interesting topic here discussing details of this point by connecting satellite to Arduino http://www.dogfight.no/2011/01/spectrum-receiver-satellite-to-arduino.html


Orange Receiver


Although Orange receiver looks much more simple than DX7, but infact we can see many commons here. There is only two ICs the receiver, we should expect three here, but it seems that the second IC performs the decoding function as well, because at the end of the day they are microcontrollers and it is a matter of software updates.


The regulator is another difference, we have only one capacitor and a small regulator which gives an indication of the maximum current they can stand.



Another interested part I noticed is the signals that you receive from channels. Signals are originally send and received in series, then these signals are demultiplexed into their original channels. This means we never get any overlap between any two PWM signals   if we measure them from RX output channels. Channels can never overlap.
Throttle - ALI"green-red"
The above figure shows how signals Throttle and ALI are adjacent signals, but they never over lap, as we can see in the next figure
No Signal Overlapping
The same characteristics appears between ELE & Rudder signals, if you choose THR & ELE or rudder you will notice that the distance between these to signals are far.


If you are interested in more details about signal handling in your firmware and how to protect your quadcopter from losing TX signal while armed please check this link Receiver Handling and Signal Lost Detection Explained


Saturday, April 7, 2012

Controlling your RC Plane from Computer using SoundCard


Controlling your RC Plane using your PC is surprisingly easy

The main idea is that RC Transmitter generates audio signals that are carried by the Radio signals using FM or spread spectrum. The receiver decode these signals and output audio signals PMM that servo understands and moves accordingly.

This link explains every thing in good details.

However the sample is for Linux, another person who helped to develop a window version in this link


What is New ?


Well the C# code above reads from a joystick and generates an audio signal. My application is basically depends on the SoundPlay.cs but allows you to control your plane from the screen directly.

I tested it on my Spektrum DX7, and an Orange RX and it worked great.

Main Things to Remember:

1- Remember to set your voice setting to 192000 Hz in your Windows settings as in the image below.
2- Set your TX to in the Trainer menu P-Link.
3- Change Windows sound volume till DX7 detects that it is connected to another Remote.


Then you can enjoy controlling your RC



You can download source code from here

and binaries



Thanks for the guys who wrote the original linux and windows version.