1
0
mirror of https://github.com/peterantypas/maiana.git synced 2025-05-17 16:00:08 -07:00

Overhaul of NF detector

This commit is contained in:
Peter Antypas 2020-11-02 09:22:45 -08:00
parent db56ae58a9
commit 0d3b8dcad9
4 changed files with 80 additions and 59 deletions

View File

@ -23,8 +23,6 @@
#include "AISChannels.h" #include "AISChannels.h"
#include "Events.hpp" #include "Events.hpp"
#include <map>
using namespace std; using namespace std;
@ -33,35 +31,35 @@ class NoiseFloorDetector : public EventConsumer
public: public:
static NoiseFloorDetector &instance(); static NoiseFloorDetector &instance();
// Called directly by each receiver to report every RSSI reading at every SOTDMA slot // Called directly by each receiver to report every RSSI reading at every SOTDMA slot
void report(VHFChannel channel, uint8_t rssi); void report(char channel, uint8_t rssi);
void reset(); void reset();
// Returns the current noise floor of the channel, 0xff if unknown // Returns the current noise floor of the channel, 0xff if unknown
uint8_t getNoiseFloor(VHFChannel channelIndex); uint8_t getNoiseFloor(char channel);
void processEvent(const Event &e); void processEvent(const Event &e);
private: private:
typedef struct
{
uint8_t reading;
} Reading;
// TODO: Use a circular buffer instead, no need for STL here //typedef vector<uint8_t> ChannelReadings;
typedef vector<Reading> ChannelReadings;
typedef map<VHFChannel, ChannelReadings> ChannelData;
//ChannelReadings mChannelASamples;
//ChannelReadings mChannelBSamples;
uint8_t mChannelACurrent;
uint8_t mChannelBCurrent;
uint8_t mAFloor;
uint8_t mBFloor;
private: private:
NoiseFloorDetector(); NoiseFloorDetector();
void processSample(ChannelReadings &window, uint8_t rssi); //void processSample(ChannelReadings &window, uint8_t rssi);
uint8_t medianValue(ChannelReadings &window); //uint8_t medianValue(ChannelReadings &window);
void dump(); void dump();
private: private:
uint32_t mTicks; uint32_t mTicks;
ChannelData mData;
}; };

View File

@ -15,16 +15,12 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/> along with this program. If not, see <https://www.gnu.org/licenses/>
*/ */
#include "NoiseFloorDetector.hpp" #include "NoiseFloorDetector.hpp"
#include "EventQueue.hpp" #include "EventQueue.hpp"
#include <algorithm>
#include "AISChannels.h" #include "AISChannels.h"
#include "_assert.h"
#include "printf_serial.h"
#include "RadioManager.hpp"
#define WINDOW_SIZE 10 #define WINDOW_SIZE 10
@ -38,34 +34,45 @@ NoiseFloorDetector &NoiseFloorDetector::instance()
NoiseFloorDetector::NoiseFloorDetector() NoiseFloorDetector::NoiseFloorDetector()
: mTicks(0xffffffff) : mTicks(0xffffffff)
{ {
EventQueue::instance().addObserver(this, CLOCK_EVENT|RSSI_SAMPLE_EVENT); //mChannelASamples.reserve(WINDOW_SIZE);
//mChannelBSamples.reserve(WINDOW_SIZE);
mChannelACurrent = 0xff;
mChannelBCurrent = 0xff;
mAFloor = 0xff;
mBFloor = 0xff;
EventQueue::instance().addObserver(this, CLOCK_EVENT);
} }
void NoiseFloorDetector::report(VHFChannel channel, uint8_t rssi) void NoiseFloorDetector::report(char channel, uint8_t rssi)
{ {
if ( rssi < 0x20 ) // Not realistic, likely a bug if ( rssi < 0x20 ) // Not realistic, likely a bug
return; return;
if ( mData.find(channel) == mData.end() ) #if 0
processSample(channel == 'A' ? mChannelASamples : mChannelBSamples, rssi);
if ( channel == 'A' )
mChannelACurrent = medianValue(mChannelASamples);
else
mChannelBCurrent = medianValue(mChannelBSamples);
#endif
if ( channel == 'A' )
{ {
ChannelReadings r; mAFloor = min(mAFloor, rssi);
r.reserve(WINDOW_SIZE*2); }
mData[channel] = r; else
{
mBFloor = min(mBFloor, rssi);
} }
ChannelReadings &window = mData[channel];
processSample(window, rssi);
} }
uint8_t NoiseFloorDetector::getNoiseFloor(VHFChannel channel) uint8_t NoiseFloorDetector::getNoiseFloor(char channel)
{ {
if ( mData.find(channel) == mData.end() ) return channel == 'A' ? mChannelACurrent : mChannelBCurrent;
return 0xff;
return medianValue(mData[channel]);
} }
void NoiseFloorDetector::processEvent(const Event &e) void NoiseFloorDetector::processEvent(const Event &e)
{ {
switch(e.type) switch(e.type)
@ -80,14 +87,17 @@ void NoiseFloorDetector::processEvent(const Event &e)
++mTicks; ++mTicks;
} }
if ( mTicks == 30 ) if ( mTicks == 10 )
{ {
//DBG("Event pool utilization = %d, max = %d\r\n", EventPool::instance().utilization(), EventPool::instance().maxUtilization()); //DBG("Event pool utilization = %d, max = %d\r\n", EventPool::instance().utilization(), EventPool::instance().maxUtilization());
mChannelACurrent = mAFloor;
mChannelBCurrent = mBFloor;
dump(); dump();
reset(); reset();
mTicks = 0; mTicks = 0;
} }
break; break;
#if 0
case RSSI_SAMPLE_EVENT: case RSSI_SAMPLE_EVENT:
{ {
report(e.rssiSample.channel, e.rssiSample.rssi); report(e.rssiSample.channel, e.rssiSample.rssi);
@ -95,11 +105,13 @@ void NoiseFloorDetector::processEvent(const Event &e)
RadioManager::instance().noiseFloorUpdated(e.rssiSample.channel, rssi); RadioManager::instance().noiseFloorUpdated(e.rssiSample.channel, rssi);
} }
break; break;
#endif
default: default:
break; break;
} }
} }
#if 0
void NoiseFloorDetector::processSample(ChannelReadings &window, uint8_t rssi) void NoiseFloorDetector::processSample(ChannelReadings &window, uint8_t rssi)
{ {
while ( window.size() >= WINDOW_SIZE ) while ( window.size() >= WINDOW_SIZE )
@ -107,50 +119,51 @@ void NoiseFloorDetector::processSample(ChannelReadings &window, uint8_t rssi)
if ( window.empty() ) if ( window.empty() )
{ {
Reading r; window.push_back(rssi);
r.reading = rssi;
window.push_back(r);
return; return;
} }
// Insert the reading at the start if it qualifies // Insert the reading at the start if it qualifies
for ( ChannelReadings::iterator i = window.begin(); i != window.end(); ++i ) for ( ChannelReadings::iterator i = window.begin(); i != window.end(); ++i )
{ {
if ( rssi <= i->reading ) if ( rssi <= *i )
{ {
Reading r; window.insert(i, rssi);
r.reading = rssi;
window.insert(i, r);
break; break;
} }
} }
} }
uint8_t NoiseFloorDetector::medianValue(ChannelReadings &window) uint8_t NoiseFloorDetector::medianValue(ChannelReadings &window)
{ {
if ( window.empty() ) if ( window.size() < WINDOW_SIZE )
return 0xff; return 0xff;
return window[window.size()/2].reading; return window[window.size()/2];
} }
#endif
void NoiseFloorDetector::dump() void NoiseFloorDetector::dump()
{ {
Event e(PROPR_NMEA_SENTENCE); Event e(PROPR_NMEA_SENTENCE);
for ( ChannelData::iterator cIt = mData.begin(); cIt != mData.end(); ++cIt )
{ sprintf(e.nmeaBuffer.sentence, "$PAINF,A,0x%.2x*", mChannelACurrent);
uint8_t value = medianValue(cIt->second); Utils::completeNMEA(e.nmeaBuffer.sentence);
//DBG("[Channel %d noise floor: 0x%.2x]\r\n", AIS_CHANNELS[cIt->first].itu, value); EventQueue::instance().push(e);
sprintf(e.nmeaBuffer.sentence, "$PAINF,%c,0x%.2x*", AIS_CHANNELS[cIt->first].designation, value);
Utils::completeNMEA(e.nmeaBuffer.sentence); sprintf(e.nmeaBuffer.sentence, "$PAINF,B,0x%.2x*", mChannelBCurrent);
EventQueue::instance().push(e); Utils::completeNMEA(e.nmeaBuffer.sentence);
} EventQueue::instance().push(e);
} }
void NoiseFloorDetector::reset() void NoiseFloorDetector::reset()
{ {
mData.clear(); //mChannelASamples.clear();
//mChannelBSamples.clear();
mAFloor = 0xff;
mBFloor = 0xff;
} }

View File

@ -109,13 +109,14 @@ void Receiver::onBitClock()
uint8_t bit = HAL_GPIO_ReadPin(mDataPort, mDataPin); uint8_t bit = HAL_GPIO_ReadPin(mDataPort, mDataPin);
processNRZIBit(bit); processNRZIBit(bit);
++mSlotBitNumber; #if 1
#if 0 if ( mSlotBitNumber != 0xffff && mSlotBitNumber++ == CCA_SLOT_BIT - 1 )
if ( (mSlotBitNumber != 0xffff) && (mSlotBitNumber++ == CCA_SLOT_BIT) )
{ {
uint8_t rssi = reportRSSI(); uint8_t rssi = reportRSSI();
mRXPacket.setRSSI(rssi); mRXPacket.setRSSI(rssi);
} }
#else
++mSlotBitNumber;
#endif #endif
} }
@ -257,10 +258,18 @@ void Receiver::pushPacket()
uint8_t Receiver::reportRSSI() uint8_t Receiver::reportRSSI()
{ {
uint8_t rssi = readRSSI(); uint8_t rssi = readRSSI();
#if 0
Event e(RSSI_SAMPLE_EVENT); Event e(RSSI_SAMPLE_EVENT);
e.rssiSample.channel = mChannel; e.rssiSample.channel = mChannel;
e.rssiSample.rssi = rssi; e.rssiSample.rssi = rssi;
EventQueue::instance().push(e); EventQueue::instance().push(e);
#endif
char channel = AIS_CHANNELS[mChannel].designation;
NoiseFloorDetector::instance().report(channel, rssi);
return rssi; return rssi;
} }

View File

@ -231,8 +231,9 @@ void Transceiver::onBitClock()
} }
else if ( mUTC && mSlotBitNumber == CCA_SLOT_BIT && mTXPacket->channel() == mChannel ) else if ( mUTC && mSlotBitNumber == CCA_SLOT_BIT && mTXPacket->channel() == mChannel )
{ {
uint8_t rssi = readRSSI(); int rssi = mRXPacket.rssi();
if ( rssi <= bsp_noise_floor() + TX_CCA_HEADROOM ) int nf = NoiseFloorDetector::instance().getNoiseFloor(AIS_CHANNELS[mChannel].designation);
if ( rssi <= nf + TX_CCA_HEADROOM )
{ {
startTransmitting(); startTransmitting();
} }