Cruisers Forum
 


Join CruisersForum Today

Reply
 
Thread Tools Rate Thread Display Modes
Old 25-08-2016, 22:25   #1
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
How do I set system time from GPS

I am running OpenCPN on Ubuntu 14.04 on a Nvidia TK1 ARM board.

I have NMEA0183 over USB from a Vesper XB8000. With GPS. AIS and Instrument data all in the same feed.

This system has no real-time clock, so, time and date is not set when not connected to the internet.

What is the easiest way to set system time from the NMEA0183 DATA and still have the data fed to OpenCPN ?

Thanks,
JM.
__________________

__________________
NahanniV is offline   Reply With Quote
Old 26-08-2016, 01:59   #2
bcn
Registered User

Join Date: May 2011
Location: underway whenever possible
Boat: Rangeboat 39
Posts: 2,759
Re: How do I set system time from GPS

here two links how to use a GPS for NTP:

https://blog.logentries.com/2015/07/...ource-to-ntpd/
https://www.lammertbies.nl/comm/info/GPS-time.html

Remember that for a precise time setting you will need a PPS signal from your GPS in order to sync the start of the second - or one will have to live with an error of +/- 1 second. Looks not important at a first moment but can be critical if software depends on timestamps. Some safe connections will not allow clients "from the future" for example...
__________________

__________________
bcn is online now   Reply With Quote
Old 26-08-2016, 03:15   #3
Registered User

Join Date: Jul 2010
Location: Monastir, Tunisia
Boat: Amel Sharki
Posts: 1,774
Re: How do I set system time from GPS

The Logbook Plugin is able to use either UTC time, local time (both from the machine sys time) or GPS time. Perhaps it is usefull to implement this into OpenCPN also.

Gerhard
__________________
CarCode is offline   Reply With Quote
Old 26-08-2016, 06:51   #4
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Quote:
Originally Posted by CarCode View Post
The Logbook Plugin is able to use either UTC time, local time (both from the machine sys time) or GPS time. Perhaps it is usefull to implement this into OpenCPN also.

Gerhard
Yes, I think it would be useful.

Thanks,
JM.
__________________
NahanniV is offline   Reply With Quote
Old 26-08-2016, 21:05   #5
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Quote:
Originally Posted by bcn View Post
here two links how to use a GPS for NTP:

https://blog.logentries.com/2015/07/...ource-to-ntpd/
https://www.lammertbies.nl/comm/info/GPS-time.html

Remember that for a precise time setting you will need a PPS signal from your GPS in order to sync the start of the second - or one will have to live with an error of +/- 1 second. Looks not important at a first moment but can be critical if software depends on timestamps. Some safe connections will not allow clients "from the future" for example...
Tried to follow these instructions, but I seem to run into a problem with the fact that I have two way traffic on the usb port connected to the XB8000 (OpenCPN is sending nav data to my instruments).
__________________
NahanniV is offline   Reply With Quote
Old 31-08-2016, 17:33   #6
Registered User
 
boat_alexandra's Avatar

Join Date: Aug 2009
Location: Masachusetts
Boat: bristol 27
Posts: 2,803
Re: How do I set system time from GPS

There is code in opencpn to set the hardware clock based on gps time.

Unfortunately it has a not so "clever" guard to avoid setting the clock if there is more than 60 seconds of error. So I hack my opencpn to remove this clause and it's working to set the system clock.
__________________
boat_alexandra is offline   Reply With Quote
Old 31-08-2016, 20:43   #7
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Quote:
Originally Posted by boat_alexandra View Post
There is code in opencpn to set the hardware clock based on gps time.

Unfortunately it has a not so "clever" guard to avoid setting the clock if there is more than 60 seconds of error. So I hack my opencpn to remove this clause and it's working to set the system clock.
Can you help me implement this hack ?
I am already building from source, but can't find the code you are talking about; I was a programmer in a former life, but not C++.

What NMEA sentances is it using for time ?

Thanks,
JM.
__________________
NahanniV is offline   Reply With Quote
Old 31-08-2016, 21:23   #8
Registered User
 
boat_alexandra's Avatar

Join Date: Aug 2009
Location: Masachusetts
Boat: bristol 27
Posts: 2,803
Re: How do I set system time from GPS

chart1.cpp:9492
Code:
        //          Correct system time if necessary                                                    
        //      Only set the time if wrong by more than 1 minute, and less than 2 hours                 
        //      This should eliminate bogus times which may come from faulty GPS units                  
                                                                                                        
        if( ( abs( b ) > 60 ) && ( abs( b ) < ( 2 * 60 * 60 ) ) ) {
you have to have SetSystemTime=1 in the config as well
__________________
boat_alexandra is offline   Reply With Quote
Old 31-08-2016, 23:59   #9
Registered User

Join Date: Nov 2012
Location: Orust Sweden
Boat: Najad 34
Posts: 1,967
Re: How do I set system time from GPS

The code Sean refers to is already in master and is active if "ocpnUPDATE_SYSTEM_TIME" is set:
Code:
//          If defined, update the system time using GPS receiver data.
//          Time update is applied if the system time and GPS time differ
//          by more than one minute, and only once per session.
//          On Linux, this option requires root privileges, obtained by sudo.
//          Thus, the following line is required in etc/sudoers:
//
//              nav ALL=NOPASSWD:/bin/date -s *
//
//          Where "nav" is the user's user name.
//
//          Also, the opencpn configuration file must contain the key
//          [Settings]
//              SetSystemTime=1
//          For security, this option is not available on the "Options" dialog
#define ocpnUPDATE_SYSTEM_TIME
//      Use the fix time to update the local system clock, only once per session
But, beware, if "SetSystemTime" is set and you're playing a VDR file while OCPN is starting up your system time will be set to VDR file's GPS time - or?
__________________
Hakan is offline   Reply With Quote
Old 01-09-2016, 00:22   #10
bcn
Registered User

Join Date: May 2011
Location: underway whenever possible
Boat: Rangeboat 39
Posts: 2,759
Re: How do I set system time from GPS

Does one want to get the time updated 10x second from your 10hz GPS???

"why do I need a fuse in my electrical system? It works without well...."

There are reasons given in the code why the update shall be only valid under certain conditions.
Hakan has given a very valid example.

Or: you have a system that draws back-ups at a given hour. Or other actions. Your GPS goes wrong, what?

What might be handy would be something like:
If Systemtime is different from GPS time > 1s ask the user if the Systemtime should be corrected to GPS time.
Ask this once and only at program start and if SetSystemTime is enabled.

Throwing out the guard is just a bad hack. Not a good idea
__________________
bcn is online now   Reply With Quote
Old 01-09-2016, 07:50   #11
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Quote:
Originally Posted by boat_alexandra View Post
chart1.cpp:9492
Code:
        //          Correct system time if necessary                                                    
        //      Only set the time if wrong by more than 1 minute, and less than 2 hours                 
        //      This should eliminate bogus times which may come from faulty GPS units                  
                                                                                                        
        if( ( abs( b ) > 60 ) && ( abs( b ) < ( 2 * 60 * 60 ) ) ) {
you have to have SetSystemTime=1 in the config as well
Thanks, That is exactly what I need, but have not got it working yet:

Hacked code:
Code:
//#ifdef ocpnUPDATE_SYSTEM_TIME
//      Use the fix time to update the local system clock, only once per session
//    if( ( sfixtime.Len() ) && s_bSetSystemTime && ( m_bTimeIsSet == false ) ) {
      if( ( sfixtime.Len() ) && ( m_bTimeIsSet == false ) ) {
        wxDateTime Fix_Time;

        if( 6 == sfixtime.Len() )                   // perfectly recognised format?
                {
            wxString a;
            long b;
            int hr = 0;
            int min = 0;
            int sec = 0;

            a = sfixtime.Mid( 0, 2 );
            if( a.ToLong( &b ) ) hr = b;
            a = sfixtime.Mid( 2, 2 );
            if( a.ToLong( &b ) ) min = b;
            a = sfixtime.Mid( 4, 2 );
            if( a.ToLong( &b ) ) sec = b;

            Fix_Time.Set( hr, min, sec );
        }
        wxString fix_time_format = Fix_Time.Format( _T("%Y-%m-%dT%H:%M:%S") ); // this should show as LOCAL

        //          Compare the server (fix) time to the current system time
        wxDateTime sdt;
        sdt.SetToCurrent();
        wxDateTime cwxft = Fix_Time;                  // take a copy
        wxTimeSpan ts;
        ts = cwxft.Subtract( sdt );

        int b = ( ts.GetSeconds() ).ToLong();

        //          Correct system time if necessary
        //      Only set the time if wrong by more than 1 minute, and less than 2 hours
        //      This should eliminate bogus times which may come from faulty GPS units

//        if( ( abs( b ) > 60 ) && ( abs( b ) < ( 2 * 60 * 60 ) ) ) {

#ifdef __WXMSW__
            //      Fix up the fix_time to convert to GMT
            Fix_Time = Fix_Time.ToGMT();

            //    Code snippet following borrowed from wxDateCtrl, MSW

            const wxDateTime::Tm tm( Fix_Time.GetTm() );

            SYSTEMTIME stm;
            stm.wYear = (WXWORD) tm.year;
            stm.wMonth = (WXWORD) ( tm.mon - wxDateTime::Jan + 1 );
            stm.wDay = tm.mday;

            stm.wDayOfWeek = 0;
            stm.wHour = Fix_Time.GetHour();
            stm.wMinute = tm.min;
            stm.wSecond = tm.sec;
            stm.wMilliseconds = 0;

            ::SetSystemTime( &stm );            // in GMT

#else

            //      This contortion sets the system date/time on POSIX host
            //      Requires the following line in /etc/sudoers
            //          nav ALL=NOPASSWD:/bin/date -s *

            wxString msg;
            msg.Printf(_T("Setting system time, delta t is %d seconds"), b);
            wxLogMessage(msg);

            wxString sdate(Fix_Time.Format(_T("%D")));
            sdate.Prepend(_T("sudo /bin/date -s \""));

            wxString stime(Fix_Time.Format(_T("%T")));
            stime.Prepend(_T(" "));
            sdate.Append(stime);
            sdate.Append(_T("\""));

            msg.Printf(_T("Linux command is:"));
            msg += sdate;
            wxLogMessage(msg);
            wxExecute(sdate, wxEXEC_ASYNC);

#endif      //__WXMSW__
            m_bTimeIsSet = true;

//        }           // if needs correction
    }               // if valid time

//#endif            //ocpnUPDATE_SYSTEM_TIME
}
Log file shows time change:
Code:
19:03:12 EST: Setting system time, delta t is -1511828681 seconds
I tried adding "nav ALL=NOPASSWD:/bin/date -s *" in etc/sudoers and then in etc/sudoers.d/sudoers

but my time is not changing, what am I doing wrong ?

Thanks,
JM.

EDIT: I also tried changing "nav" to my username
__________________
NahanniV is offline   Reply With Quote
Old 01-09-2016, 08:19   #12
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Quote:
Originally Posted by bcn View Post
Does one want to get the time updated 10x second from your 10hz GPS???

"why do I need a fuse in my electrical system? It works without well...."

There are reasons given in the code why the update shall be only valid under certain conditions.
Hakan has given a very valid example.

Or: you have a system that draws back-ups at a given hour. Or other actions. Your GPS goes wrong, what?

What might be handy would be something like:
If Systemtime is different from GPS time > 1s ask the user if the Systemtime should be corrected to GPS time.
Ask this once and only at program start and if SetSystemTime is enabled.

Throwing out the guard is just a bad hack. Not a good idea
This is only setting the time once per OpenCPN session, so your first concern is not valid.

I agree with your Idea of asking.

My use of this is on an ARM based system with no real time clock and frequently no internet.

Cheers,
JM.
__________________
NahanniV is offline   Reply With Quote
Old 01-09-2016, 08:28   #13
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Sorry, there are two pertinent log messages:
Code:
19:00:52 EST: Setting system time, delta t is -1511828541 seconds
19:00:52 EST: Linux command is:sudo /bin/date -s "D T"
It seems like the command is not being formatted correctly?
__________________
NahanniV is offline   Reply With Quote
Old 01-09-2016, 20:43   #14
Registered User
 
NahanniV's Avatar

Join Date: Mar 2011
Location: On the Boat
Boat: Finnsailer 38
Posts: 1,143
Re: How do I set system time from GPS

Can anyone tell me why this code is not working ?
__________________

__________________
NahanniV is offline   Reply With Quote
Reply

Tags
gps

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GPS Time for accurate Computer Time rgleason OpenCPN 2 09-03-2016 16:03
[SOLD] Garmin set - 18"HD radar with 740 GPS and all wiring. mdvick Classifieds Archive 21 25-02-2016 15:57
set up GPS with Windows 10 and Opencpn mewstone OpenCPN 7 05-01-2016 08:48
How to Set Up AIS System Sinkers Marine Electronics 1 25-07-2010 01:20
Nav GPS Set Up for Amateur Bareboat Skipper sydneyspitfire Navigation 0 09-02-2010 02:48



Copyright 2002- Social Knowledge, LLC All Rights Reserved.

All times are GMT -7. The time now is 09:10.


Google+
Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Social Knowledge Networks
Powered by vBulletin® Version 3.8.8 Beta 1
Copyright ©2000 - 2017, vBulletin Solutions, Inc.

ShowCase vBulletin Plugins by Drive Thru Online, Inc.