Author Topic: AGC always selects maximum gain  (Read 5656 times)

joelucid

  • Hero Member
  • *****
  • Posts: 867
AGC always selects maximum gain
« on: April 02, 2016, 02:13:54 AM »
I've done some tests this morning with AGC (automatic gain control). As reminder: the RFM69 library configures the RFM69HW for AGC with AUTORXRESTART. That means that at the beginning of each packet reception the receiver tunes the gain of the LNA to obtain the optimal sensitivity/ linearity trade-off.

So i wondered what gain the radio would select. I added some code to the irq handler to record the result and found that: AGC ALWAYS selects maximum gain. Even when I put a Moteino with RFM69HW and maximum tx power right next to the receiver.

I then switched on sensitivity boost which presumable cranks up the amplifier even higher. Still: always maximum gain.

If this is a result of the standard radio settings (FDev etc) this could be pretty significant: Switching off AGC halves the time required for RSSI detection and makes listen mode much more power efficient.

Anybody looked at this before or has feedback?

Joe

TomWS

  • Hero Member
  • *****
  • Posts: 1926
Re: AGC always selects maximum gain
« Reply #1 on: April 02, 2016, 09:11:02 AM »
I've done some tests this morning with AGC (automatic gain control). As reminder: the RFM69 library configures the RFM69HW for AGC with AUTORXRESTART. That means that at the beginning of each packet reception the receiver tunes the gain of the LNA to obtain the optimal sensitivity/ linearity trade-off.

So i wondered what gain the radio would select. I added some code to the irq handler to record the result and found that: AGC ALWAYS selects maximum gain. Even when I put a Moteino with RFM69HW and maximum tx power right next to the receiver.

I then switched on sensitivity boost which presumable cranks up the amplifier even higher. Still: always maximum gain.

If this is a result of the standard radio settings (FDev etc) this could be pretty significant: Switching off AGC halves the time required for RSSI detection and makes listen mode much more power efficient.

Anybody looked at this before or has feedback?

Joe
Do you mean the gain is set to maximum initially and then adjusts or that it remains at max?  And, how do you know?  The register only reads the setting to enable auto, I didn't think you could read the actual internal setting.

The reason I ask is, when I first started playing with Moteino and was working on the ATC code, I purposefully disabled AGC so that the ATC could be tested at reasonably close range.  There was a clear difference between AGC off with fixed gain and AGC on with varying TX levels (ie, the receiver with AGC on maintained a relatively constant signal level despite the varying input).

Tom

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #2 on: April 02, 2016, 09:48:42 AM »
Quote
Do you mean the gain is set to maximum initially and then adjusts or that it remains at max?  And, how do you know?  The register only reads the setting to enable auto, I didn't think you could read the actual internal setting.

I read out register 0x18 which contains the current gain as programmed or selected by AGC in bits 3-5. That always reads 8 - or maximum gain. Strange. Actually I got a 0x10 once, showing that it is possible to get other settings. But a rfm69hw at full power doesn't seem to be enough to trigger it regularly.

Joe

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #3 on: April 02, 2016, 04:16:00 PM »
@joelucid: Are you able to receive your measurement of the gain setting in less time than Tdelay after a packet is received?  It appears as though after a packet is received, and after Tdelay amount of time, the AGC resets the gain to G1 (which matches your report of 0x18 being 8 ) and presumably leaves it at G1 until measuring RSSI on the next packet.  At least that part of the AGC's behavior makes sense.  If you haven't already, you might want to test using a low bitrate just as an experiment to give yourself more time to query the gain setting (obviously use a nearby transmitter set to a high Tx for the experiment) and see if the gain is still stuck at G1 immediately after the packet is received. 

[Edit: Maybe it's even possible (?) to measure the gain sometime after RSSI goes high but before the packet is completely received if using the faster bitrates.  Not sure if it's ever discussed in the datasheet, but exactly what RFM69 operations can be done is parallel with the RFM69's other operations is unclear to me.  I do get the impression that at least some amount of parallelism is possible though.]
« Last Edit: April 02, 2016, 05:38:17 PM by WhiteHare »

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #4 on: April 02, 2016, 05:54:25 PM »
From the spec:

"Packet mode: if AutoRxRestartOn = 0, the LNA gain will remain the same for the reception of the following packet. If AutoRxRestartOn = 1, after the controller has emptied the FIFO the receiver will re-enter the WAIT mode described above, after a delay of InterPacketRxDelay, allowing for the distant transmitter to ramp down, hence avoiding a false RSSI detection."

I measure in the Irq handler before emptying the Fifo. So the radio hasn't even started the countdown to a new wait period yet.

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #5 on: April 02, 2016, 06:10:25 PM »
You're right. 

Could it be that the RFM69 resets the gain to G1 the instant it finishes receiving a packet?  That might make more sense anyway, in case maybe it needs some settle time.  Anyhow, if it's inevitable, why should it wait?

[Edit: This would be for the case where AutoRxRestartOn = 1.  For AutoRxRestartOn = 0, then the gain wouldn't change at all unless Rx was restarted or goes into WAIT mode].

[Edit2: I'm just thinking it probably wants to start a new cycle with max gain, so that if there is a faint pre-amble, it will still hear it, and if the RSSI isn't faint, it can turn down the gain at that point.  Well, that's the hypothesis anyway.]
« Last Edit: April 02, 2016, 07:04:59 PM by WhiteHare »

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #6 on: April 04, 2016, 05:49:28 AM »
I think I figured this one out. Ironically in a different context: getting AFC to work.

Here is how AFC and AGC are supposed to work:

1.) The radio is set to RX.
2.) The receiver waits until RSSI exceeds RSSIThresh, indicating an arriving packet.
3.) The receiver sets the gain for the amplifier during reception of preamble
4.) The receiver performs AFC during reception of preamble
5.) Actual package reception.

Here's what actually happens with the RFM69 lib:

1.) Radio is set to RX, RSSIThresh of -110
2.) Radio immediately detects RSSI higher than RSSIThresh and thinks it sees a packet
3.) Radio tunes the LNA assuming the background noise is a signal - ergo it ramps the receiver all the way up
4.) If you try to do AFC the receiver will detune itself by trying to narrow in on the noise
5.) Actual reception with suboptimal settings.

What should be done:

RSSIThresh needs to be set higher than the noise floor. Additionally periodically there should be a restart of AGC and AFC in case a noise activation has occurred.

Joe

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #7 on: April 04, 2016, 07:49:01 AM »
Thanks!  That's a clearer explanation than anything else that I've read to date.

It appears that the RFM69 puts the burden of setting  a proper RSSI threshhold entirely on the users shoulders.  In contrast, for instance, my WiFi device doesn't ask me to do that.  It appears that my WiFi device is constantly measuring RSSI and arrives at its own conclusion as to what the noise floor is.  So, for the RFM69, how to pick the best RSSI threshhold setting would be an interesting thread topic. 
« Last Edit: April 04, 2016, 07:52:38 AM by WhiteHare »

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #8 on: April 04, 2016, 09:28:08 AM »
Quote
So, for the RFM69, how to pick the best RSSI threshhold setting would be an interesting thread topic.

Yeah - same problem as with listen mode where we've understood for some time that selecting the right RSSIThresh is critical. This isn't a static choice: changes a lot whether the Mote is hooked up to a Pi, to a FTDI adaptor, just battery etc. Changes when you touch the board etc.

Also same solution: start with a conservative thresh. Write code to handle noise triggered rx cycles and restarts rx after timeout. If you get no noise triggers lower the threshold. If you get many, raise it.

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #9 on: April 04, 2016, 01:43:17 PM »
I've implemented a prototype for dynamic RSSIthresh management and I've got to say the results show crystal clear how important this topic is:

I'm running at 1200 baud, FDev 1260 hz, RxBW: 2600 hz, AFC on. I'm starting with an RSSIThresh of 220 (corresponding to -110 db).

One Moteino I plug via FTDI adapter into my MacBook's USB. The other only connects to a 9V battery. RSSIThresh for the Mac-bound Moteino converges to 189. On the solo Moteino to 240. That's 25 dBm which you lose if you don't dynamically adjust RSSIThresh.

Can't wait to try this out on a ESP8266 connected RFM69HW.

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #10 on: April 05, 2016, 02:07:15 AM »
Quote
Can't wait to try this out on a ESP8266 connected RFM69HW.

ESP8266 + RFM69HW on the macbook: around the same 189. Same combination on batteries around 204. That's including wifi activity which seems to cause only little interference.

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #11 on: April 05, 2016, 07:51:49 AM »
Quote
Can't wait to try this out on a ESP8266 connected RFM69HW.

ESP8266 + RFM69HW on the macbook: around the same 189. Same combination on batteries around 204. That's including wifi activity which seems to cause only little interference.

Is that a typo?  i.e. Did you mean 240 (i.e. same as solo Moteino) instead of 204?


joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #12 on: April 05, 2016, 08:32:15 AM »
Quote
Is that a typo?  i.e. Did you mean 240 (i.e. same as solo Moteino) instead of 204?

No. An ESP8266 (NodeMCU 1.0 in this case) + RFM69HW is much worse than a Moteino in this respect.

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #13 on: April 05, 2016, 08:38:45 AM »
Here's a Moteino hooked up to the old Pi2 (the first two columns) compared to a battery powered moteino 3 meters apart. Notice how badly the Pi performs with its regular RSSI impairments - even though its Threshold isn't too bad.

!SRSSI:-45, STHRESH:212, GRSSI:-44, GTHRESH:239
!SRSSI:-52, STHRESH:212, GRSSI:-44, GTHRESH:238
!SRSSI:-62, STHRESH:211, GRSSI:-44, GTHRESH:238
!SRSSI:-44, STHRESH:211, GRSSI:-44, GTHRESH:238
!SRSSI:-45, STHRESH:212, GRSSI:-44, GTHRESH:237
!SRSSI:-45, STHRESH:212, GRSSI:-45, GTHRESH:237
!SRSSI:-45, STHRESH:212, GRSSI:-45, GTHRESH:237
!SRSSI:-44, STHRESH:210, GRSSI:-44, GTHRESH:237
!SRSSI:-62, STHRESH:210, GRSSI:-45, GTHRESH:237
!SRSSI:-62, STHRESH:211, GRSSI:-44, GTHRESH:237
!SRSSI:-45, STHRESH:210, GRSSI:-44, GTHRESH:238
!SRSSI:-44, STHRESH:210, GRSSI:-44, GTHRESH:239
!SRSSI:-44, STHRESH:211, GRSSI:-44, GTHRESH:238
!SRSSI:-45, STHRESH:210, GRSSI:-44, GTHRESH:237
!SRSSI:-44, STHRESH:210, GRSSI:-45, GTHRESH:237
!SRSSI:-49, STHRESH:209, GRSSI:-44, GTHRESH:238
!SRSSI:-62, STHRESH:209, GRSSI:-44, GTHRESH:238
!SRSSI:-62, STHRESH:210, GRSSI:-44, GTHRESH:238

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #14 on: April 05, 2016, 09:50:55 AM »
At the risk of jumping to conclusions, it looks as though what's aggravating your Moteino-Pi combo must be something that's conducted rather than RF.  Right?  Before now, we never really knew.  I would take that as good news, because if you wanted to you could maybe mitigate against conducted EMI by severing the connection.  So,worst case, maybe optocouplers and some kind of isolated power supply.  Best case: some kind of filter(s) and/or ferrite bead, and/or decoupling capacitors (if you aren't already using them).
« Last Edit: April 05, 2016, 09:58:38 AM by WhiteHare »

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #15 on: April 05, 2016, 06:18:06 PM »
Quote
must be something that's conducted rather than RF.

That's the conclusion I already arrived at due to my earlier measurements. But it got more puzzling after this data: It's something that does not cause the noise floor to increase significantly (when compared to a simple usb power source). Yet it significantly decreases received signal strength periodically.

I've pretty much given up on the Pi. Not only after helping a friend reinstall the 3rd one after corrupted flash. I think the ESP8266 gw will be much simpler and more reliable. This datapoint was posted just for completeness sake.


joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #16 on: April 06, 2016, 11:01:04 AM »
I did some tests on the noise structure of a moteino hooked up via FTDI cable to a MacBook using RFM69 standard settings. I measured RSSI over the course of a second and recorded which values appeared how often. The histogram is pretty interesting (first column -RSSI * 2, second column Count):

Code: [Select]
131 ->   1
135 ->   1
147 ->  25
148 ->  35
149 ->  57
150 ->  89
151 ->  29
152 ->  26
153 ->  36
154 ->  14
155 ->  22
156 ->  40
157 ->  19
158 ->  19
159 ->  60
160 ->  48
161 ->  69
162 -> 114
163 ->  49
164 ->  58
165 -> 164
166 -> 105
167 -> 120
168 -> 324
169 -> 186
170 -> 200
171 -> 435
172 -> 208
173 -> 164
174 -> 231
175 ->  77
176 ->  87
177 -> 193
178 -> 142
179 -> 174
180 -> 437
181 -> 312
182 -> 442
183 -> 1000
184 -> 500
185 -> 436
186 -> 551
187 -> 150
188 -> 137
189 -> 256
190 -> 120
191 -> 109
192 -> 272
193 -> 133
194 -> 127
195 -> 236
196 ->  95
197 ->  76
198 ->  83
199 ->  21
200 ->  21
201 ->  20
202 ->  11
203 ->   3
204 ->   4
205 ->   1
206 ->   2
207 ->   1
209 ->   1
217 ->   1
218 ->   2
221 ->   2
235 ->   1
252 ->   1
253 ->   1

Note how broad the range is. If one uses the upper boundary (~147 = -73 dBm) as floor one loses a lot of dynamic range. Also note just how noisy this is. On a battery node I get an upper boundary of ~207!

If you want to use AGC/AFC you still need to set a fairly low RSSIThresh so you preserve dynamic range. When you get a noise RSSI detection that mistakenly starts RX you need to notice and restart RX. That can be done with RXTIMEOUT2.

It gets pretty complicated and nobody has really noticed any pain from always running the amp at max gain. So unless AFC is necessary it might be ok to keep the current behavior.

There's seems to be an opportunity here to create a low noise Moteino that doesn't have these issues. Or at least a low noise gateway.

Joe

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #17 on: April 06, 2016, 02:28:10 PM »
I wonder how much it even matters.  The gateway won't be battery powered, and the nodes will mostly be listening, but on batteries.  It might increase their transmits, but....

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #18 on: April 06, 2016, 03:40:00 PM »
Quote
I wonder how much it even matters.

The case where it matters is a sensor installed in a difficult location. When it sends updates the gateway won't be able to take full advantage of the receiver's sensitivity exactly because it is AC powered and suffers from noise.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6461
  • Country: us
    • LowPowerLab
Re: AGC always selects maximum gain
« Reply #19 on: April 06, 2016, 04:28:38 PM »
Quote
I wonder how much it even matters.
... it is AC powered and suffers from noise.
What noise do you mean?

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #20 on: April 06, 2016, 04:41:05 PM »
Quote
What noise do you mean?

If you just take RSSI readings during RX while nobody is sending anything you can measure the background noise. In this thread I've documented how the background noise is significantly higher in AC powered Moteinos than in battery powered ones.

The worst results were when powered via FTDI adapter from a macbook. There's a difference of 30 dBm between the top of the noise floor of a battery powered Moteino and one powered from the mac (that's 3x the difference between W and HW).

Since these readings are higher than your RSSI threshold the AGC is performed while no signal is being sent, resulting in erroneous results. That's not too bad since it causes the amp to always be set to max gain which seems to be ok even for strong signals.

But the noise they represent will also cause bit errors when trying to receive weak signals. Say if you're trying to rx a -110 dBm signal with -80 dBm of noise at the receiver.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6461
  • Country: us
    • LowPowerLab
Re: AGC always selects maximum gain
« Reply #21 on: April 07, 2016, 08:11:31 AM »
That to me sounds like a very noisy environment either caused by a poor power supply or massive EMI from your macbook.
50-60hz AC should not cause that type of noise all by itself. For instance in my SwitchMotes I don't see what you report at all and they sit in electrical boxes with a AC-DC supply (SwitchMotePSU) right underneath. They work great.

joelucid

  • Hero Member
  • *****
  • Posts: 867
Re: AGC always selects maximum gain
« Reply #22 on: April 07, 2016, 08:17:56 AM »
Quote
That to me sounds like a very noisy environment either caused by a poor power supply or massive EMI from your macbook.

Yeah the mac is by far the worst culprit here. All other supplies I've tested are much better. Also you wouldn't notice this with your settings necessarily. At least the order of magnitude I get from non Mac power supplies. This is important for long range scenarios where you want to receive down to -115 dBm or so. That so far I can only make work with batteries. With a normal power supply you should be able to go down to around -100 dBm.

But a common mode choke, a ferrite bead and smaller decoupling caps might make it work on AC as well. 

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #23 on: April 07, 2016, 10:19:44 AM »
As a quick and easy fix for the mac book (or similar) scenario, I wonder whether a USB isolator might help.  No kidding, there used to be one called "The Decrapifier", though I'm not sure whether it's still manufactured or not.  One of the RTL SDR vendors (the one who makes/sells a modded version with a 1ppm crystal) recommended it. 
« Last Edit: April 07, 2016, 10:34:40 AM by WhiteHare »

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: AGC always selects maximum gain
« Reply #24 on: April 07, 2016, 03:31:02 PM »
Potentially yet another instance of the cycling 20db RSSI phenomena:  https://lowpowerlab.com/forum/index.php/topic,814.0.html
With benefit of hindsight, it's unfortunate he wasn't more detailed about his test conditions.