-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_sync.py
More file actions
38 lines (26 loc) · 887 Bytes
/
clock_sync.py
File metadata and controls
38 lines (26 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# encoding: utf-8
import subprocess
import datetime
import time
from dronekit import connect
NEED_GPS_COUNT = 3
gps_count = 0
timesynced = False
vehicle = connect('127.0.0.1:14550', wait_ready=True)
@vehicle.on_message('GPS_RAW_INT')
def on_gps_raw_int(self, name, message):
global gps_count
gps_count = message.satellites_visible
@vehicle.on_message('SYSTEM_TIME')
def on_system_time(self, name, message):
global gps_count, timesynced, NEED_GPS_COUNT
if gps_count > NEED_GPS_COUNT and not timesynced:
sec = message.time_unix_usec / 1e6
ds = datetime.datetime.fromtimestamp(sec)
# Sync time
subprocess.call(
['sudo', 'date', '-s', '{:}'.format(ds.strftime('%Y/%m/%d %H:%M:%S'))])
timesynced = True
while not timesynced:
time.sleep(0.5)
print('System time is synced! {}'.format(datetime.datetime.now()))