Contiki-NG
tsch-adaptive-timesync.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, SICS Swedish ICT.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * TSCH adaptive time synchronization
36  * \author
37  * Atis Elsts <atis.elsts@sics.se>
38  *
39  */
40 
41 /**
42  * \addtogroup tsch
43  * @{
44 */
45 
46 #include "net/mac/tsch/tsch.h"
47 #include <stdio.h>
48 
49 #if TSCH_ADAPTIVE_TIMESYNC
50 
51 /* Estimated drift of the time-source neighbor. Can be negative.
52  * Units used: ppm multiplied by 256. */
53 static int32_t drift_ppm;
54 /* Ticks compensated locally since the last timesync time */
55 static int32_t compensated_ticks;
56 /* Number of already recorded timesync history entries */
57 static uint8_t timesync_entry_count;
58 /* Since last learning of the drift; may be more than time since last timesync */
59 static uint32_t asn_since_last_learning;
60 
61 /* Units in which drift is stored: ppm * 256 */
62 #define TSCH_DRIFT_UNIT (1000L * 1000 * 256)
63 
64 /*---------------------------------------------------------------------------*/
65 long int
67 {
68  return (long int)drift_ppm / 256;
69 }
70 /*---------------------------------------------------------------------------*/
71 /* Add a value to a moving average estimator */
72 static int32_t
73 timesync_entry_add(int32_t val)
74 {
75 #define NUM_TIMESYNC_ENTRIES 8
76  static int32_t buffer[NUM_TIMESYNC_ENTRIES];
77  static uint8_t pos;
78  int i;
79  if(timesync_entry_count == 0) {
80  pos = 0;
81  }
82  buffer[pos] = val;
83  if(timesync_entry_count < NUM_TIMESYNC_ENTRIES) {
84  timesync_entry_count++;
85  } else {
86  /* We now have accurate drift compensation.
87  * Increase keep-alive timeout. */
88  tsch_set_ka_timeout(TSCH_MAX_KEEPALIVE_TIMEOUT);
89  }
90  pos = (pos + 1) % NUM_TIMESYNC_ENTRIES;
91 
92  val = 0;
93  for(i = 0; i < timesync_entry_count; ++i) {
94  val += buffer[i];
95  }
96  return val / timesync_entry_count;
97 }
98 /*---------------------------------------------------------------------------*/
99 /* Learn the neighbor drift rate at ppm */
100 static void
101 timesync_learn_drift_ticks(uint32_t time_delta_asn, int32_t drift_ticks)
102 {
103  /* should fit in a 32-bit integer */
104  int32_t time_delta_ticks = time_delta_asn * tsch_timing[tsch_ts_timeslot_length];
105  int32_t real_drift_ticks = drift_ticks + compensated_ticks;
106  int32_t last_drift_ppm = (int32_t)(((int64_t)real_drift_ticks * TSCH_DRIFT_UNIT) / time_delta_ticks);
107 
108  drift_ppm = timesync_entry_add(last_drift_ppm);
109 
110  TSCH_LOG_ADD(tsch_log_message,
111  snprintf(log->message, sizeof(log->message),
112  "drift %ld", tsch_adaptive_timesync_get_drift_ppm()));
113 }
114 /*---------------------------------------------------------------------------*/
115 /* Either reset or update the neighbor's drift */
116 void
117 tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction)
118 {
119  /* Account the drift if either this is a new timesource,
120  * or the timedelta is not too small, as smaller timedelta
121  * means proportionally larger measurement error. */
122  if(last_timesource_neighbor != n) {
124  drift_ppm = 0;
125  timesync_entry_count = 0;
126  compensated_ticks = 0;
127  asn_since_last_learning = 0;
128  } else {
129  asn_since_last_learning += time_delta_asn;
130  if(asn_since_last_learning >= 4 * TSCH_SLOTS_PER_SECOND) {
131  timesync_learn_drift_ticks(asn_since_last_learning, drift_correction);
132  compensated_ticks = 0;
133  asn_since_last_learning = 0;
134  } else {
135  /* Too small timedelta, do not recalculate the drift to avoid introducing error. instead account for the corrected ticks */
136  compensated_ticks += drift_correction;
137  }
138  }
139 }
140 /*---------------------------------------------------------------------------*/
141 /* Error-accumulation free compensation algorithm */
142 static int32_t
143 compensate_internal(uint32_t time_delta_usec, int32_t drift_ppm, int32_t *remainder, int16_t *tick_conversion_error)
144 {
145  int64_t d = (int64_t)time_delta_usec * drift_ppm + *remainder;
146  int32_t amount = d / TSCH_DRIFT_UNIT;
147  int32_t amount_ticks;
148 
149  *remainder = (int32_t)(d - amount * TSCH_DRIFT_UNIT);
150 
151  amount += *tick_conversion_error;
152  amount_ticks = US_TO_RTIMERTICKS(amount);
153  *tick_conversion_error = amount - RTIMERTICKS_TO_US(amount_ticks);
154 
155  if(ABS(amount_ticks) > RTIMER_ARCH_SECOND / 128) {
156  TSCH_LOG_ADD(tsch_log_message,
157  snprintf(log->message, sizeof(log->message),
158  "!too big compensation %ld delta %ld", (long int)amount_ticks, (long int)time_delta_usec));
159  amount_ticks = (amount_ticks > 0 ? RTIMER_ARCH_SECOND : -RTIMER_ARCH_SECOND) / 128;
160  }
161 
162  return amount_ticks;
163 }
164 /*---------------------------------------------------------------------------*/
165 /* Do the compensation step before scheduling a new timeslot */
166 int32_t
167 tsch_timesync_adaptive_compensate(rtimer_clock_t time_delta_ticks)
168 {
169  int32_t result = 0;
170  uint32_t time_delta_usec = RTIMERTICKS_TO_US_64(time_delta_ticks);
171 
172  /* compensate, but not if the neighbor is not known */
173  if(drift_ppm && last_timesource_neighbor != NULL) {
174  static int32_t remainder;
175  static int16_t tick_conversion_error;
176  result = compensate_internal(time_delta_usec, drift_ppm,
177  &remainder, &tick_conversion_error);
178  compensated_ticks += result;
179  }
180 
181  if(TSCH_BASE_DRIFT_PPM) {
182  static int32_t base_drift_remainder;
183  static int16_t base_drift_tick_conversion_error;
184  result += compensate_internal(time_delta_usec, 256L * TSCH_BASE_DRIFT_PPM,
185  &base_drift_remainder, &base_drift_tick_conversion_error);
186  }
187 
188  return result;
189 }
190 /*---------------------------------------------------------------------------*/
191 #else /* TSCH_ADAPTIVE_TIMESYNC */
192 /*---------------------------------------------------------------------------*/
193 void
194 tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction)
195 {
196 }
197 /*---------------------------------------------------------------------------*/
198 int32_t
199 tsch_timesync_adaptive_compensate(rtimer_clock_t delta_ticks)
200 {
201  return 0;
202 }
203 /*---------------------------------------------------------------------------*/
204 #endif /* TSCH_ADAPTIVE_TIMESYNC */
205 /** @} */
#define TSCH_LOG_ADD(log_type, init_code)
Use this macro to add a log to the queue (will be printed out later, after leaving interrupt context)...
Definition: tsch-log.h:140
struct tsch_neighbor * last_timesource_neighbor
The neighbor last used as our time source.
TSCH neighbor information.
Definition: tsch-types.h:109
void tsch_set_ka_timeout(uint32_t timeout)
Set the desynchronization timeout after which a node sends a unicasst keep-alive (KA) to its time sou...
Definition: tsch.c:199
void tsch_timesync_update(struct tsch_neighbor *n, uint16_t time_delta_asn, int32_t drift_correction)
Updates timesync information for a given neighbor.
int32_t tsch_timesync_adaptive_compensate(rtimer_clock_t delta_ticks)
Computes time compensation for a given point in the future.
Main API declarations for TSCH.
long int tsch_adaptive_timesync_get_drift_ppm(void)
Gives the estimated clock drift w.r.t.