Skip to content

Commit

Permalink
sbf: fix issue with automatic base config in QGC
Browse files Browse the repository at this point in the history
The automatic base configuration would sometimes print weird port names
as well as reconnect every few seconds to the receiver.
  • Loading branch information
flyingthingsintothings authored and julianoes committed May 8, 2024
1 parent 5810dac commit 0b79ec4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
49 changes: 35 additions & 14 deletions src/sbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,29 @@ GPSDriverSBF::~GPSDriverSBF()

int GPSDriverSBF::configure(unsigned &baudrate, const GPSConfig &config)
{
char buf[GPS_READ_BUFFER_SIZE];
char msg[MSG_SIZE];

_configured = false;

setBaudrate(SBF_TX_CFG_PRT_BAUDRATE);
baudrate = SBF_TX_CFG_PRT_BAUDRATE;
_output_mode = config.output_mode;

// Make sure we can send commands to the receiver
sendMessage(SBF_CONFIG_FORCE_INPUT);

char buf[GPS_READ_BUFFER_SIZE];
char com_port[5] {};
// Disable previous output for now so we can detect the COM port
for (int i = 1; i <= 2; i++) {
snprintf(msg, sizeof(msg), SBF_CONFIG_DISABLE_OUTPUT, "COM", i);
sendMessageAndWaitForAck(msg, SBF_CONFIG_TIMEOUT);
}
for (int i = 1; i <= 4; i++) {
snprintf(msg, sizeof(msg), SBF_CONFIG_DISABLE_OUTPUT, "USB", i);
sendMessageAndWaitForAck(msg, SBF_CONFIG_TIMEOUT);
}

char com_port[5] {};
size_t offset = 1;
bool response_detected = false;
gps_abstime time_started = gps_absolute_time();
Expand Down Expand Up @@ -134,7 +146,6 @@ int GPSDriverSBF::configure(unsigned &baudrate, const GPSConfig &config)
}

// Delete all sbf outputs on current COM port to remove clutter data
char msg[MSG_SIZE];
snprintf(msg, sizeof(msg), SBF_CONFIG_RESET, com_port);

if (!sendMessageAndWaitForAck(msg, SBF_CONFIG_TIMEOUT)) {
Expand Down Expand Up @@ -185,12 +196,13 @@ int GPSDriverSBF::configure(unsigned &baudrate, const GPSConfig &config)
}

sendMessageAndWaitForAck(msg, SBF_CONFIG_TIMEOUT);

snprintf(msg, sizeof(msg), SBF_CONFIG, com_port);

sendMessageAndWaitForAck(msg, SBF_CONFIG_TIMEOUT);
}

// Output a set of SBF blocks on a given connection at a regular interval.
int i = 0;
snprintf(msg, sizeof(msg), SBF_CONFIG, com_port);

do {
++i;

Expand Down Expand Up @@ -282,21 +294,25 @@ bool GPSDriverSBF::sendMessageAndWaitForAck(const char *msg, const int timeout)
return found_response;
}

// -1 = error, 0 = no message handled, 1 = message handled, 2 = sat info message handled
// return value:
// 0b1111_1111 = an error occurred
// 0b0000_0000 = no message handled (not set up yet)
// 0b0000_0001 = message handled
// 0b0000_0010 = sat info message handled
int GPSDriverSBF::receive(unsigned timeout)
{
int handled = 0;
gps_abstime time_started;
uint8_t buf[GPS_READ_BUFFER_SIZE];

// Do not receive messages until we're configured
if (!_configured) {
gps_usleep(timeout * 1000);
return 0;
}

uint8_t buf[GPS_READ_BUFFER_SIZE];

// timeout additional to read timeout
gps_abstime time_started = gps_absolute_time();

int handled = 0;
// Timeout after not receiving a complete message for a certain time
time_started = gps_absolute_time();

while (true) {
// Wait for only SBF_PACKET_TIMEOUT if something already received.
Expand Down Expand Up @@ -329,7 +345,10 @@ int GPSDriverSBF::receive(unsigned timeout)
}
}

// 0 = decoding, 1 = message handled, 2 = sat info message handled
// return value:
// 0b0000_0000 = still decoding
// 0b0000_0001 = message handled
// 0b0000_0010 = sat info message handled
int GPSDriverSBF::parseChar(const uint8_t b)
{
int ret = 0;
Expand Down Expand Up @@ -388,9 +407,11 @@ int GPSDriverSBF::parseChar(const uint8_t b)

case SBF_DECODE_RTCM3:
if (_rtcm_parsing->addByte(b)) {
// Complete message received
SBF_DEBUG("got RTCM message with length %i", (int) _rtcm_parsing->messageLength());
gotRTCMMessage(_rtcm_parsing->message(), _rtcm_parsing->messageLength());
decodeInit();
ret |= 1;
}

break;
Expand Down
14 changes: 3 additions & 11 deletions src/sbf.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,11 @@

#define SBF_CONFIG "setSBFOutput, Stream1, %s, PVTGeodetic+VelCovGeodetic+DOP+AttEuler+AttCovEuler, msec100\n"

#define SBF_CONFIG_DISABLE_OUTPUT "setDataInOut,%s%d,,-RTCMv3\n"

#define SBF_CONFIG_RTCM "" \
"setDataInOut, USB1, Auto, RTCMv3+SBF\n" \
"setPVTMode, Rover, All, auto\n" \
"setSatelliteTracking, All\n" \
"setSatelliteUsage, All\n" \
"setElevationMask, All, 10\n" \
"setReceiverDynamics, Moderate, Automotive\n" \
"setSBFOutput, Stream1, DSK1, Support, msec100\n" \
"setSBFOutput, Stream2, USB1, DOP+VelCovGeodetic, sec1\n" \
"setSBFOutput, Stream3, USB1, PVTGeodetic, msec200\n" \
"setFileNaming, DSK1, Incremental\n" \
"setFileNaming, DSK1, , 'px4rtcm'\n"
"setDataInOut, USB1, Auto, RTCMv3\n" \
"setPVTMode, Static, All, auto\n"

#define SBF_CONFIG_RTCM_STATIC1 "" \
"setReceiverDynamics, Low, Static\n"
Expand Down

0 comments on commit 0b79ec4

Please sign in to comment.