tomojitakasu / RTKLIB

2.54k stars 1.62k forks source link

Calling gen_rtcm3() correclty #384

Open DavidKelleySCSC opened 6 years ago

DavidKelleySCSC commented 6 years ago

Takasu-san and others:

I have a coding problem where I have a stream of RTCM3.x messages that need to be as small as possible for L1 only users, so I am removing the L2 content (1004->1002). In doing this I have noticed more than a few base stations that send a 1006 message when 1005 would do (the ant height is set to zero), so I want to convert those as well (saves another 2 bytes).

My question to the group is how to best call gen_rtcm3() at such times. I want to use RTKLIB here because is fairly lightweight and the rtcm_t structure is already loaded with all the state data that is needed (our in house encoder is much less well suited to this). Ideally the sw would decode a stream of messages and replace the 1006 with a 1006 every time it occurs. But in doing this I do not want to disturb the state of the rtcm_t structure that will also be called on to decode the next message and is maintaining a navigation filter for some QC needs. If I have to keep two full copies of rtcm_t, the value in using RTKLIB is diminished.

I call the encoder with the below fragment one the last byte of the 1006 message has been decoded. Before and after are calls to input_rtcm3() which calls decode_rtcm3() to fill out rtcm_t each time. In other words the rtcm_t 'state' is updated and correct before the call. And as far as I can tell, exporting a message from the state does not change the state in any way.

> // Generate new msg
> int sync = 0;
> int type = 1005;
> if (rtcm->sta.hgt > 0) type = 1006;
> // replaces rtcm->buff and messageLen with new one
> gen_rtcm3(rtcm, type, sync);

And on return rtcm->buff now holds the encoded message. I also put the values of rtcm->nbyte, nbit & len back to their former state when done, but this does not seem to be needed. And I only will make this call on decoded message boundaries.

This seems to work just fine, but I remain uneasy that calling both decode_rtcm3() and gen_rtcm3() on the same rtcm_t structure might have some subtle effect I am not aware of. Do others see any issues with this approach?

jjxtra commented 6 years ago

Looks great, I think 1006 works fine with height 0. Most rtklib code seems to reset the state of the rtcm struct, such as the decode and encode methods so re-use is fine.

Here's my code:

rtcm rtcmBase; sta_t sta; // Fill in sta with your station params

init_rtcm(&rtcmBase); rtcmBase.sta = sta; rtcmBase.staid = sta.stationId; gen_rtcm3(&rtcmBase, 1006, 0); write(port, rtcmBase.buff, rtcmBase.nbyte);