### 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:

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:

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.

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.

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.

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.

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.

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.