MHefny's Posts (25)

Sort by

PID Diagram for HefnyCopter - II Firmware

HefnyCopter2 implements full PID terms that is P, I & D. It uses the following approach.


My vision is to keep math as simple as possible and use superposition to accumulate different behaviors together based on my suggested framework.

Gyro values for balancing are in purple.

Accelerometer and accumulated Gyro are used together as a virtual angle device to get estimated Angle and implements the restoring function using a PID and are shown in green.

AccZ implements Positioning-Balancing and is shown in orange.

Sonar implements Positioning-Restoring and is shown in Red.


Please let me know how do you find this approach.


Read more…

Receiver Handling and Signal Lost Detection Explained

KK2 Board Connections

How to Measure PWM Signal of RX

As discussed in the user guide, HefnyCopter2 accepts two RX to be connected in parallel, the Primary RX is the RX2, and the secondary one is RX1. That means RX2 should be always available to enable Arming.


Because of this large number of input channels "9 channels"  Primary: THR,ALI,ELV,RUD,AUX  Secondary: THE,ALI,ELE,RUD input ports are reorganized.

This is translated to the following ports:


Ports Definition of RX2   Ports Definition of RX1


RX2 interrupts are all handled by PCINT2 except AUX that is handked by PCINT1 PortB-0


Now lets go through the code lines and check these technique:

Filename: Receiver.c


 Interrupt PCINT2


The primary RX signal is received by PCINT2 "PortC" pins 0,1,5,7 for Picth,Yaw,Roll,Coll respectivly. The interrupt is triggered when there is any change i.e. rising edge or falling edge on any of those pins.

Because of the nature of the RX you cannot get two concurrent rising edges, Because the data is originally sent as serial, each decoded signal is translated into a PWM on a certain channel on the RX and this does happen in parallel, so you get the rising edge of a channel then the falling edge then the rising edge of the next one and so on.

receiver code 2

The above code is replicated with each channel, first we check of there is change on a given pin, in this case the pitch pin. Then we check if it is a rising edge? if yes then we tack a snap shot of the 16-bit timer TCNT1 into RX-raw[1][RXChannel_ELE]

if no which is a falling edge, then we calculate the difference between TCNT1 now and the one stored in RX-raw[1][RXChannel_ELE]. The logic is implemented in CalculateSignalLength2 (RXChannel_ELE).


How to Detect that RX Signal is Lost


The below code shows how HefnyCopter can  detect the following status:

- TX is never detected..... no signal.

- TX was on and now it is off.


Based on the behavior of OrangeRx CH6 receiver it has the following behavior:


case1: When turning-on the RX and the TX is still off, you can detect PWM signals in all channels except THR.

receiver code 8 signals

The green signal is for the ALE and the red is for THR.


case2: If you turn on both RX & TX and wait for bind with RX then turn TX off again the THR signal will be the only valid signal. Please note here that THR value is not the one that was detecmined by THR stick in TX, that is why if you turn your TX while your quadcopter ius armed motors will spin and it may fly randomly. 


receiver code 9 signals TXTurnedOff

We can see from the above graph the red signal for THR is on and ELE green is off. this is what we get from RX channels when we turn off TX after being connected with RX.


The below code are very similar to the Picth code, infact the are identical in parts that measure PWM signal, the only difference is in the two lines in the falling edge condition in the "else" block of the "if".

The code saves a timestamp of when last time this signal was received, it uses TCNT1_X which is a 16-bit variable that ticks one time every TCNT1 overflow, which is a 16-bit timer.

Signal Lost Detection


  Clearing bit 5 in RX_Good means that RX can sense a signal i.e. is now connected to TX.


 The next snippet is used to detect when the TX is turned off.


  Signal Loss Detection


We clear bit 6 in RX_Good variable  which means that the TX signal exists. Please note that we can choose any other channel such as pitch, AUX or Rudder. It happens that we have selected ROLL here. So we use the THR with any other channel to make the function.


The below function is regularl called by the main loop to apply actions required by the pilot. Each time the function is called it makes sure that there is updates in RXn_LastValidSignal_timestamp otherwise it sets bits 5 & 6 for RX2 & bits 1 & 2 for RX1.


 Signal Lost Detection


if you want more information about the hardware you may find this link "What is Inside Spektrum DX7 Receiver" useful.

Read more…

HefnyCopter Firmware works in KKv2.1 as in the demo

can switch between X-Mode & Plus-Mode.

there is also HefnyCopter2 that works on KK2 it has more new features:

1- BuddyMode where u can train ur friend using another 2 TX at a time.

2- XBEE connection where u can connect your quad to PC.

HefnyCopter is released wheile HefnyCopter is still in final developing phase :)

Read more…