betaflight / betaflight

Open Source Flight Controller Firmware
GNU General Public License v3.0
8.06k stars 2.87k forks source link

M10 ValSet support, GPS connection stability #12799

Closed freasy closed 9 months ago

freasy commented 1 year ago

Marked ready to merge; extensively bench tested, has been flight tested. All known functional issues have been addressed.

This PR is @freasy's work, primarily. With @ZzyzxTek's help, I've done much of the comment handling, some refactoring, and a lot of testing and documentation. However it was @freasy who single-handedly started this project. He set in place all the massive changes needed to properly support the new ValGetSet type M10 UBlox commands, to identify the type of module we are working with and configure it appropriately, and essentially all the underpinnings that make this PR work so well.

Users should now expect a reliable and solid hardware connection from nearly all modern GPS modules, leading to smoother and better GPS rescue control.

UBLox is strongly recommended. NMEA 'sort of'l works, but simply isn't well supported both in our software and in the modules themselves.

The recommended baud rate is 57600. There is little to be gained by using 115200 with the recommended 10hz data rate. Lower baud rates reduce the CPU time required for parsing the incoming data and are more resistant to electrical noise.

The recommended data rate is 10Hz for GPS Rescue purposes. 5Hz or less is recommended if the GPS is only for fixes on the radio in case the quad is lost. If an M8 module is set at 20Hz, it will revert to its own internal default rate, which could be very slow. 20Hz requires 115200 and M9 or higher, and is not recommended as it may be susceptible to electrical interference.

Data Rate Baud Rate CPU cost Comments
20hz 11500 Highest M10 only; test carefully!
10hz 38400 or higher Medium 57600 and 10hz is recommended for GPS Rescue
5hz 19200 or higher Medium For GPS Rescue or general use
1-2Hz 9600 or higher Leasts Too slow for smooth GPS rescue, OK for simple position/speed info

The recommended PID loop rate while using GPS is 8k4k. 8k8k may simply not have enough CPU time available in the loop to support higher baud rates. GPS code requires almost as much time as the Rx or OSD code. It is quite expensive. Check the tasks CLI command and the percentage CPU usage when using 8k8k.

ToDo's:

Note 1: NMEA may work, but with the following caveats:

Note 2: The GPS 'AutoBaud' switch, in Configurator, has been disabled in this PR. It now achieves nothing, either way; leave it off.

Note 3: When more than about 25 sats are part of the Nav solution, some UBLox modules randomly drop Nav packets. This appears in the debug as sample intervals of 200 and occasionally 300ms. This seems to be a problem with some GPS hardware modules, it's not a Betaflight problem.

Update 2023-08-12

Update 2023-08-08

Update 2023-08-04

Update 2023-08-01

Update 2023-07-29

Update 2023-07-26

Update 2023-07-25

Update 2023-07-22

Update 2023-07-22

Update 2023-07_20

Update 2023_07_19 : lots of changes from @ctzsnooze and @ZzyzxTek:

@freasy earlier extensive work included:

The debug mode is GPS_CONNECTION, and can be viewed with this Blackbox Log viewer PR, and 'live' in the sensors tab. It currently displays:

Debug While connecting While receiving data Comments
0 GPS din model GPS dyn model 1 at the start (acquire model ID), 7 once 3D fix arrives (flight model ID)
1 GPS Nav data interval GPS Nav data interval GPS module interval between nav data packets
2 time since last Nav data time since last Nav data each GPS iteration, 100 or 500Hz, resets on new Nav data
3 Baud rate GPS data calculated, interval FC time interval between Nav data output
4 gpsData.state gpsData.state main State * 100 + sub-State, eg 413 is CONFIGURE at step 13
5 executeTimeUs executeTimeUs CPU time required for code to run
6 gpsData.ackState gpsData.ackState 0 = idle, 1 = waiting, 2 = ack, 3 = mack
7 serialRxBytesWaiting serialRxBytesWaiting incoming serial buffer size in bytes

For testing:

  1. Set the GPS communication rate in the ports tab to the default of 57600, and the gps_update_rate to 10hz

  2. Set the GPS tab AutoConfig to ON and choose Ublox. Auto-baud does nothing, ignore it.

  3. Go to cli and type:

    set debug_mode = GPS_CONNECTION
    save
  4. Power cycle....

  5. Check that the GPS indicator light at the top of the Configurator screen comes on quickly

  6. Check that satellites register, and the map updates

  7. Use to the CLI 'status' command to confirm that the GPS is 'connected' at the intended baud rate..

  8. In the sensors tab, include 'debug', check the baud rate, current nav packet interval (100 for 10hz), flight model value (7 is normal), etc

  9. confirm normal behaviour in the OSD

  10. Go for a fly, make a log, check it all works, try a rescue!

Known Issues:

Feedback would be gratefully appreciated!

blckmn commented 1 year ago

AUTOMERGE: (FAIL)

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets WARNING: It may be unstable. Use only for testing! See: https://www.youtube.com/watch?v=I1uN9CN30gw for instructions for unified targets!

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets WARNING: It may be unstable. Use only for testing! See: https://www.youtube.com/watch?v=I1uN9CN30gw for instructions for unified targets!

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets WARNING: It may be unstable. Use only for testing! See: https://www.youtube.com/watch?v=I1uN9CN30gw for instructions for unified targets!

jsk2084 commented 1 year ago

gps_info

MON-VER acquired: true

swVersion: ROM SPG 5.10 (7b202e) hwVersion: 000A0000 (M10) extension: FWVER=SPG 5.10 PROTVER=34.10 GPS;GLO;GAL;BDS SBAS;QZSS

haslinghuis commented 1 year ago

@freasy can we have this on one line:

GPS;GLO;GAL;BDS SBAS;QZSS

freasy commented 1 year ago

@freasy can we have this on one line:

GPS;GLO;GAL;BDS SBAS;QZSS

No, this are 2 lines in the unit, which have no id or anything, so a line merge would be fuzzy :/ i would like to leave it like that as it is the raw response from the unit

KarateBrot commented 1 year ago

The PR adds an absolute freight train of additional flash. Can you try to make the code more memory efficient? For example I can see at least one function which is not needed (ubloxUTCStandardConfigToUnitValue()). The file structure also might need some work. We often split up init code if the c-file gets too bloated, so rather than gps_config.h and .c, just make a single gps_init.c and put all GPS init code in there. Also, splitting up NMEA and Ublox code and putting them into their respective files might be a good idea.

Does gps_info work for NMEA as well? (We support NMEA and Ublox) My suggestion is to focus on one goal at a time, either ublox M10 support OR the new GPS info command.

SteveCEvans commented 1 year ago

gps_info

MON-VER acquired: true

swVersion: ROM SPG 5.10 (7b202e) hwVersion: 000A0000 (M10) extension: FWVER=SPG 5.10 PROTVER=34.10 GPS;GLO;GAL;BDS SBAS;QZSS

The swVersion, hwVersion and extension strings seem surplus to requirements and a waste of FLASH. Also, why report 000A0000 when M10 is all we need.

ctzsnooze commented 1 year ago

Rather than moving gps.c and gps.h into a new directory, maybe simplify the PR by leaving them where they are, and adding gps_init.c and gps_init.h into the same io directory.

freasy commented 1 year ago

@SteveCEvans if there is a version string, that is unknown to the parse function, i want to display it

ZzyzxTek commented 1 year ago

What does adding the UTC standard settings fix for us? Previously the standard was set to 0 in UBX-CFG-NAV5, which corresponds to AUTO. It seems like in the new code it also defaults to AUTO. Does AUTO break something with M10s and it needs to be set to one of the other explicit standards?

freasy commented 1 year ago

What does adding the UTC standard settings fix for us? Previously the standard was set to 0 in UBX-CFG-NAV5, which corresponds to AUTO. It seems like in the new code it also defaults to AUTO. Does AUTO break something with M10s and it needs to be set to one of the other explicit standards?

It speeds up the sat find and fix if you set your corresponding time zone here

ZzyzxTek commented 1 year ago

It speeds up the sat find and fix if you set your corresponding time zone here

But is that broken for M10s? Not trying to be difficult, but expanding the scope of this PR to other improvements makes it hard to see what exactly we're doing to address the M10 problems.

The new code still sets it to AUTO by default. Does the user need to know to set it to their local UTC standard?

freasy commented 1 year ago

No auto is fine and it just expands the options to configure the unit

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

freasy commented 1 year ago

@ledvinap thank you for all your reviews and input! That really helps alot! Are you on the bf discord? Would help in some cases :)

ledvinap commented 1 year ago

@freasy : You can contact me on Gmail

freasy commented 1 year ago

@freasy : You can contact me on Gmail

i cant find it :(

ledvinap commented 1 year ago
@gmail.com ;-)
github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

ctzsnooze commented 1 year ago

Tested with an M8 unit that is configured in the GPS hardware to link up at 115200 baud.

When set in Ports tab to manual, connection is directly at 115200 in status window = good!

When set in Ports tab to AUTO, the connection can transiently be 9600 (and show not configured), but then, and most of the time, shows 57600 (set to57600)

I think this is a Configurator error, since I notice that if I save AUTO in Configurator, then exit and come back the value gets saved as a manual value of 57600.

@haslinghuis, once this PR is finalised, can we get Configurator to trigger this GPS auto-connect code when the GPS UART in the Ports tab is set to Auto?

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

ZzyzxTek commented 1 year ago

When set in Ports tab to manual, connection is directly at 115200 in status window = good!

When set in Ports tab to AUTO, the connection can transiently be 9600 (and show not configured), but then, and most of the time, shows 57600 (set to57600)

I think this is a Configurator error, since I notice that if I save AUTO in Configurator, then exit and come back the value gets saved as a manual value of 57600.

Setting AUTO on the Ports tab does not actually set anything to AUTO, it sets the port baud to 57600. This seems intentional.

I mentioned this in the IMU dev channel, I'll copy it here for doc...

In Configurator, when setting the GPS port to AUTO, Configurator maps it to 57600 (port.js):

            let gpsBaudrate = $(portConfig).find('.gps_baudrate').val();
            if (gpsBaudrate === 'AUTO') {
                gpsBaudrate = '57600';
            }

In CLI, when entering a serial config with GPS baud rate of 0 (AUTO), it rejects any baud not 9600 to 115200 (doesn't map it to anything) (cli.c):

        case 1:
            if (baudRateIndex < BAUD_9600 || baudRateIndex > BAUD_115200) {
                continue;
            }
            portConfig.gps_baudrateIndex = baudRateIndex;
            break;

In MSP, it takes whatever is submitted, no validation (msp.c):

            portConfig->gps_baudrateIndex = sbufReadU8(src);

So, it seems at some point the decision was made not to let users set it to AUTO. The doc page for Serial under GPS Baud Rate says:

Also has a boolean AUTOBAUD. It is recommended to use a fixed baudrate. Configure GPS baudrate according to device documentation.

The doc page for GPS doesn't say anything about support for AUTO. The default baud rate for a GPS serial port is 57600.

haslinghuis commented 1 year ago

Flashed MambaG4 and set port to 115200 -> does not detect GPS (icon stays grey) and in GPS tabs true / false and sat count are being toggled constantly.

Setting speed to 57600 everything seems to work.

# gps_info
# MON-VER acquired: true
swVersion: 
 FW=5.10, PROTO=34.10
hwVersion: 
 M10 (000A0000)
extension:
github-actions[bot] commented 1 year ago

Do you want to test this code? Here you have an automated build: Assets

WARNING: It may be unstable. Use only for testing!

FrenzyIncarnate commented 1 year ago

gps_info

MON-VER acquired: true

swVersion: FW=5.10, PROTO=34.10 hwVersion: M10 (000A0000) extension: This is the result I got. Is this ok? I dont see extensions as I saw at some of the commenters

FrenzyIncarnate commented 1 year ago

Just installed 4.5 zulu with this pr. Previously it was loosing sats after takeoff . Did a small flight outside at 11pm :) it did not lose any . Good work @freasy . Attached the log file LOG00001.zip

TheIsotopes commented 11 months ago

@freasy tested on latest master. The extension from gps_info is empty. I seem to remember that something was listed there.

using: MATEK GNSS SAM-M10Q

# gps_info
# MON-VER acquired: true
swVersion: 
 FW=5.10, PROTO=34.10
hwVersion: 
 M10 (000A0000)
extension: 
TheIsotopes commented 11 months ago

imo all looks good.

Screenshot 2023-06-27 121626

ctzsnooze commented 10 months ago

@adam-ah please join us on discord - ping me there

adam-ah commented 10 months ago

@adam-ah please join us on discord - ping me there

@ctzsnooze I don't use discord - am I missing out on much? :)

ctzsnooze commented 10 months ago

@adam-ah yes ;-)

adam-ah commented 10 months ago

Wow, first of all, thank you so much for all the hard work you've put into this PR since May 12! Your dedication is truly commendable, and I'm excited to see the progress made so far.

However, as much as I appreciate the effort, I must admit that the current size of the PR has become quite challenging to review meaningfully. There are hundreds of unanswered comments and new commits, making it a bit overwhelming to handle in one go.

To ensure that everyone can give your work the attention it deserves, I would really suggest considering splitting this PR into three smaller ones, as discussed earlier. This way, it will be easier for us to review and thoroughly test each portion both on unit tests and actual hardware.

Again, I want to thank the team for the efforts, and I'm looking forward to your understanding in breaking down the PR to help us proceed with the review and merge process more efficiently. Let's collaborate on this, and I'm confident we'll get it done in no time! 🙏😊

haslinghuis commented 10 months ago

@ctzsnooze please apply following diff. GPS Signal Strength is updated in configurator.

mark@pc:~/dev/pr/firmware/test/betaflight$ git diff
diff --git a/src/main/drivers/stm32/serial_uart_stm32f4xx.c b/src/main/drivers/stm32/serial_uart_stm32f4xx.c
index 1f5dbf5b7..d24ba47c7 100644
--- a/src/main/drivers/stm32/serial_uart_stm32f4xx.c
+++ b/src/main/drivers/stm32/serial_uart_stm32f4xx.c
@@ -392,7 +392,7 @@ void uartIrqHandler(uartPort_t *s)
             USART_ITConfig(s->USARTx, USART_IT_TXE, DISABLE);

             // Switch TX to an input with pullup so it's state can be monitored
-            uartTxMonitor(s);
+            // uartTxMonitor(s);
         }
     }

diff --git a/src/main/io/gps.c b/src/main/io/gps.c
index f4539c1cb..fccb08450 100644
--- a/src/main/io/gps.c
+++ b/src/main/io/gps.c
@@ -1357,6 +1357,7 @@ void gpsUpdate(timeUs_t currentTimeUs)
         GPS_update &= ~GPS_MSP_UPDATE;
     }

+    DEBUG_SET(DEBUG_GPS_UNIT_CONNECTION, 4, gpsData.state);
     DEBUG_SET(DEBUG_GPS_UNIT_CONNECTION, 7, gpsData.satInfoRequired);

     switch (gpsData.state) {
@@ -1391,14 +1392,18 @@ void gpsUpdate(timeUs_t currentTimeUs)

 #ifdef USE_GPS_UBLOX
             } else {
-                if (gpsConfig()->autoConfig == GPS_AUTOCONFIG_ON) {
-                    if (isConfiguratorConnected()) {
-                        if (gpsData.satInfoRequired) {
-                            requestSatelliteInfo();
-                            gpsData.satInfoRequired = false;
-                        }
+                if (isConfiguratorConnected()) {
+                    DEBUG_SET(DEBUG_GPS_UNIT_CONNECTION, 1, gpsPackageCounter);
+                    gpsPackageCounter++;
+                    // set the condition for requesting new satellite info while connected to Configurator
+                    // We only should call this once, so we set the flag and call it once
+                    if (!gpsData.satInfoRequired) {
+                        gpsData.satInfoRequired = true;
+                        gpsPackageCounter = 0;
+                        requestSatelliteInfo();
                     }
                 }
+                // THIS HAS NOTHING TO DO WITH RECEIVING DATA
                 if (gpsData.ubloxUsingFlightModel == false && STATE(GPS_FIX)) {
                     ubloxSendNAV5Message(gpsConfig()->gps_ublox_flight_model);
                     gpsData.ubloxUsingFlightModel = true;
@@ -1448,20 +1453,10 @@ static void gpsNewData(uint16_t c)
         return;
     }

-    DEBUG_SET(DEBUG_GPS_UNIT_CONNECTION, 1, gpsPackageCounter);
-    gpsPackageCounter++;
-    if (gpsData.state == GPS_STATE_RECEIVING_DATA) {
-        // count the number of packets received in one second
-        if (cmp32(millis(), gpsData.lastNavMessage) >= 1000) {
-            // set the condition for requesting new satellite info while connected to Configurator
-            gpsData.satInfoRequired = true;
-            gpsPackageCounter = 0;
-        }
-        // new speed and position data received, and successfully parsed.  Other commands ignored.
-        gpsData.lastLastNavMessage = gpsData.lastNavMessage; // used only for a delta time in dashboard.c
-        gpsData.lastNavMessage = millis();
-        sensorsSet(SENSOR_GPS);
-    }
+    // new speed and position data received, and successfully parsed.  Other commands ignored.
+    gpsData.lastLastNavMessage = gpsData.lastNavMessage; // used only for a delta time in dashboard.c
+    gpsData.lastNavMessage = millis();
+    sensorsSet(SENSOR_GPS);

     GPS_update ^= GPS_DIRECT_TICK;

BTFL_BLACKBOX_LOG_20230721_214450_FOSSF4ELRS.zip

Full 16MB bench log: BTFL_BLACKBOX_LOG_20230722_012121_FOSSF4ELRS.zip

adam-ah commented 10 months ago

What incredible work being poured into this project! It's great to see how much the community will benefit from these efforts.

However, I must admit that when it comes to gps.c, I have some concerns. The code's complexity, length, and difficulty to read and test give me a bit of unease.

It feels like we might be throwing a ticking time bomb into a room (release), not only hoping the occupants (pilots) won't be harmed too much by any shrapnel (bugs), but that the room (codebase) stays well-organized after the explosion (code maintainability).

Breaking down the code into separate files, such as (gps.c, gps_init.c, gps_ublox.c, and others), and refactoring the monolithic methods would be immensely helpful, if I may suggest. Also, whenever possible, simplifying nested structures would make the code much more approachable (both readability, maintainability, and testability).

I understand that everyone's efforts are invaluable especially as people are spending their free time on this, and I'm truly grateful for everyone's dedication to this project.

ZzyzxTek commented 10 months ago

Yes, we all share your concerns about the state of the code in the GPS driver files. This code has been around in this condition for a long time, it was not written for this PR. And for the most part, has worked well enough for the users of GPS.

The intent of this PR was only to address a couple of immediate issues:

That's what was embarked on, along the way a few other things crept in:

This ended up being a bigger PR with the above than I think some of us expected or wanted. un!t did a huge amount of work to get it started and mostly finished up before his new baby arrived (congrats!). ctzsnooze and I have been working to get it finished up to where it's releasable. It's what we've got right now, maybe not everything we wish we had.

GPS Rescue testing has run into the connect/reconnect issues, as have some users. There are a lot of users adopting the new M10 modules, most are unaware of the risks of them not being fully supported. We feel it would be very valuable to get these changes released as soon as we can (with hopefully a 4.4 retrofit). Breaking up this PR and especially embarking on larger refactorings now would delay that. Keep in mind we have a non-trivial testing effort in front of us to make sure this works across a variety of modules.

There is an active discussion on Discord about larger refactoring or rewrite plans. You are encouraged to join that. I think you'll find the folks there understand the principles of good design & coding and want to apply them to the GPS module.

Perhaps as a suggestion, we can focus the review comments for this PR on what's minimally necessary to get it releasable? If there are changes made for this PR that smell, let's clean them up, but perhaps limit the clean up to that?

What incredible work being poured into this project! It's great to see how much the community will benefit from these efforts.

However, I must admit that when it comes to gps.c, I have some concerns. The code's complexity, length, and difficulty to read and test give me a bit of unease.

It feels like we might be throwing a ticking time bomb into a room (release), not only hoping the occupants (pilots) won't be harmed too much by any shrapnel (bugs), but that the room (codebase) stays well-organized after the explosion (code maintainability).

Breaking down the code into separate files, such as (gps.c, gps_init.c, gps_ublox.c, and others), and refactoring the monolithic methods would be immensely helpful, if I may suggest. Also, whenever possible, simplifying nested structures would make the code much more approachable (both readability, maintainability, and testability).

I understand that everyone's efforts are invaluable especially as people are spending their free time on this, and I'm truly grateful for everyone's dedication to this project.

adam-ah commented 10 months ago

@ZzyzxTek, thanks for taking the time to fill in the history or this PR.

I'll do my best to address these as objectively as possible, and apologies if any of these are contradictory, I truly appreciate all the effort on BF

That's what was embarked on, along the way a few other things crept in:

It is more important to focus on what matters now: quality code that is readable, testable, maintainable, and bug-free. We (hoomans) are easily distracted by emotions and history, but it is our role to make the right decisions - what matters is where we are at now.

GPS Rescue testing has run into the connect/reconnect issues, as have some users.

Breaking up this PR and especially embarking on larger refactorings now would delay that. Keep in mind we have a non-trivial testing effort in front of us to make sure this works across a variety of modules.

If we can't review and test the code properly, it leads to endless changes without meaningful review and testing. Delaying this PR is necessary due to numerous unaddressed comments and proposed additions to already lengthy files.

Splitting up the PR would actually make it easier to release the fixes and would give us time to mull over the less critical parts. Given that the PR started in May, the argument has never been stronger to split up this PR.

I do hear your argument and desire to help users. I have had these discussion countless times in professional contexts, and it is never an easy one.

folks there understand the principles of good design & coding and want to apply them to the GPS module.

It would be good to see these being applied more.

This code has been around in this condition for a long time, it was not written for this PR.

While in isolation it might be true,

Perhaps as a suggestion, we can focus the review comments for this PR on what's minimally necessary to get it releasable?

If I summarise your arguments, I see the key points as follow

If I understood you correctly, I'm afraid I cannot support this, for the following reasons

As for the above, I still support my previous suggestions, in line with @haslinghuis

In any case, thanks again for all the hard work!

ctzsnooze commented 10 months ago

@adam-ah

If you would re-write the code from scratch, test it, and make a PR - that would be fabulous. We all agree, and we all already knew, that the code requires a total refactor. So we are 100% on the same wavelength there.

But you won't. And at present, no-body will.

So we have a situation where M10 units require manual re-programming for them to work, and in fact a lot of M8 units also don't work.

Now you may not understand the code, and I agree that it is extremely tough to follow. But since working on it, we have at least 3 people who understand it very, very well. And those people are slowly, and effectively, solving the issues, one by one. This has been a team effort, involving changes to the debugging tools, to Configurator, and to the code.

This is the current situation:

There are a lot more improvements like that. And with each improvement, our understanding of the code improves, and it gets a little bit more transparent, better commented, and easier to follow.

So I am going to be blunt, this PR will continue its incremental improvements and will be merged, fundamentally in the current form and structure, no matter how strongly I agree that it is in need of a massive re-write.

I would be grateful if you would take the time to actually test the PR, use the blackbox debug tool provided, and help make the code better by providing clear, tested, code improvement suggestions. Otherwise, I accept your views, and indeed agree, and hope that you agree that you don't need to keep re-stating the same views over and over.

adam-ah commented 10 months ago

So I am going to be blunt, this PR will continue its incremental improvements and will be merged, fundamentally in the current form and structure, no matter how strongly I agree that it is in need of a massive re-write.

Considering that many of these changes have already been implemented elsewhere with improved structure and readability, I'm not entirely convinced of the necessity for this level of arrogance. @ctzsnooze https://github.com/iNavFlight/inav/blob/a528a6dfcac275666b8d22dcebfa6d756be8985a/src/main/io/gps_ublox.c

In any case, I understand that feedback can be challenging to handle at times. I'll leave it at that and wish you the best of luck!

ctzsnooze commented 10 months ago

@adam-ah I too very much appreciate how nicely GPS connections are handled in iNav. And I agreed with you that this code requires refactoring, extensively, or rewriting in full. However that is not the purpose of this PR. It is important, right now, to focus on what is achievable and necessary. Right now the most useful thing you could do, if you want to assist with code development in a productive and positive way, is to test the code and provide solutions to any issues that you identify. Later on we will look at refactoring, at which time your PR's will be duly considered, if you provide any.

freasy commented 10 months ago

So I am going to be blunt, this PR will continue its incremental improvements and will be merged, fundamentally in the current form and structure, no matter how strongly I agree that it is in need of a massive re-write.

Considering that many of these changes have already been implemented elsewhere with improved structure and readability, I'm not entirely convinced of the necessity for this level of arrogance. @ctzsnooze https://github.com/iNavFlight/inav/blob/a528a6dfcac275666b8d22dcebfa6d756be8985a/src/main/io/gps_ublox.c

In any case, I understand that feedback can be challenging to handle at times. I'll leave it at that and wish you the best of luck!

Hey, even most of the work here is from me, I was absent, because my second child arrived. That's why I handed this PR over to @ctzsnooze

In discord I already stated, that the whole gps code will be deleted and rewritten at some point. Because fixing this mess is useless hours. On the other hand this PR brings stuff to the table, we don't want to miss, thus the continued work here.

I very much appreciate your contribution! If possible, could you join discord, I am more familiar with communication there?

When I rewrite the whole gps code, I need as much help and brain power as needed, if you want to join then, I would be happy :)

ctzsnooze commented 10 months ago

This is a log image showing nav packet loss at 115200 when Configurator is connected and we get enough sats for a 3D fix. At the point we change to flight model 7 (red debug 0 trace) we start getting dropouts (debug 1 and 3, and the yellow timer climbing higher). The dropouts occur every 500ms, which is the interval at which sat info is requested.

Screen Shot 2023-07-24 at 12 44 40

Here is the identical circumstance when the board is booted up without Configurator connected. There is no dropout problem, because sat info is not requested after booting up without Configurator being connected.

Screen Shot 2023-07-24 at 12 26 26

It's also clear that GPS modules 'remember' the number of sats recently acquired, and when hot-swapped, the NAV-SAT commands are as big as the max number of possible satellites recently identified. So while a module may connect and show sats reliably when first cold connected, because the number of sats, and the size of the NAV-SAT data messages are small, once it acquires enough sats to start failing, it will always fail after a hot swap. This explains some of the randomness with 115200 baud under the current code; the module may show sats early on, then stubbornly refuse to, yet work perfectly when cold-connected the next day.

haslinghuis commented 10 months ago

BTFL_BLACKBOX_LOG_COMMIT59.zip

70 minutes on 115K2 without loosing gpx fix. Gps signal data is hardly updated.

16:50 After powering off for 10 minutes or so with cold start it took 10 minutes to gain a fix.

haslinghuis commented 10 months ago

BTFL_BLACKBOX_LOG_20230725_163058_FOSSF4ELRS(256 buffer).zip

Maybe we need 256+128 bytes buffer but latest tests are showing much better results!!!

Really like to implement @adam-ah suggestions after this gets done

https://github.com/betaflight/betaflight/blob/293e14e8bd4b87019780ed0598cb7cfaa636415a/src/main/io/gps.c#L1535-L1545

ctzsnooze commented 10 months ago
this image is outdated; it shows earlier versions of the code failing to speed up the task at the start of a packet and also at the end. This has been fixed in later commits. This image shows first a NAV-PVT message (first red line), and then a NAV-SAT message (second red line), being cleared from the serial buffer at 1kHz. This was at 115200 baud, a 10hz update rate, a 256byte serial buffer, and about 30 sats in view. ![Screen Shot 2023-07-26 at 10 58 03](https://github.com/betaflight/betaflight/assets/11737748/e4551539-37de-4338-8b3b-95ac85859a6c) Note that the peak serial buffer value while parsing this particular NAV-SAT packet was 147bytes, which would have overloaded the old 128 byte buffer, leading to loss of this packet. If the baud rate was 57600, the number of bytes entering the buffer in a given time period would be halved, and the old buffer would have been OK - but the downside would have been that clearing the buffer would take twice as long. It looks like 34800 baud could lead to the NAV-PVT buffer being incompletely cleared before the NAV-SAT data started arriving, which could also lead to loss of the NAV-SAT packet. Note that in-flight, no SAT messages are sent. The FC only has to decode NAV-PVT messages. It is likely that 115200 will have more than enough buffer space in-flight for NAV-PVT messages only, and that 57600 will also be fine. If the user wishes to experiment with higher than 10hz update rates, higher baud rates will result in the buffer being cleared faster. I anticipate that for reliable 20hz update rates in-flight, 57600 may be OK, but that 115200 would be a better bet. At this point I don't recommend 20hz update rates.
ctzsnooze commented 10 months ago

@haslinghuis - despite the slow log sample rate, your log shows that sometimes there is sufficient time between NAV-PVT and NAV-SAT messages for them to be distinct, and both kinds of message would reliably get through in these cases.

Screen Shot 2023-07-26 at 11 54 21

At other times, the two appear to coincide. However, the NAV-PVT message appears to always be handled, since the time interval between received NAV packets steps up when that happens, and appears to happen normally.

Screen Shot 2023-07-26 at 12 03 48