RemoteUptimeClockTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.time;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 
00021 import org.junit.Before;
00022 import org.junit.Test;
00023 import org.ros.time.RemoteUptimeClock.LocalUptimeProvider;
00024 
00025 import java.util.concurrent.Callable;
00026 
00030 public class RemoteUptimeClockTest {
00031 
00032   private double localUptime;
00033   private double remoteUptime;
00034   private double delta;
00035   private double drift;
00036   private double driftSensitivity;
00037   private double errorReductionCoefficientSensitivity;
00038   private int latencyOutlierFilterSampleSize;
00039   private double latencyOutlierFilterThreshold;
00040 
00041   @Before
00042   public void setup() {
00043     localUptime = 0;
00044     remoteUptime = 0;
00045   }
00046 
00047   private RemoteUptimeClock newRemoteUptimeClock() {
00048     RemoteUptimeClock remoteUptimeClock =
00049         new RemoteUptimeClock(new LocalUptimeProvider() {
00050           @Override
00051           public double getSeconds() {
00052             moveTimeForward(delta, drift);
00053             return localUptime;
00054           }
00055         }, new Callable<Double>() {
00056           @Override
00057           public Double call() throws Exception {
00058             return remoteUptime;
00059           }
00060         }, driftSensitivity, errorReductionCoefficientSensitivity, latencyOutlierFilterSampleSize,
00061             latencyOutlierFilterThreshold);
00062     return remoteUptimeClock;
00063   }
00064 
00065   private void moveTimeForward(double delta, double drift) {
00066     localUptime += delta;
00067     remoteUptime += delta / drift;
00068   }
00069 
00070   @Test
00071   public void testUnityDrift() throws Exception {
00072     delta = 7;
00073     drift = 1;
00074     driftSensitivity = 1;
00075     errorReductionCoefficientSensitivity = 1;
00076     latencyOutlierFilterSampleSize = 1;
00077     latencyOutlierFilterThreshold = 1.5;
00078     RemoteUptimeClock remoteUptimeClock = newRemoteUptimeClock();
00079     remoteUptimeClock.calibrate(10, 0);
00080     for (int i = 0; i < 10000; i++) {
00081       remoteUptimeClock.update();
00082       assertEquals(1, remoteUptimeClock.getDrift(), 1e-6);
00083     }
00084     assertEquals(0, remoteUptimeClock.getErrorReductionCoefficient(), 1e-9);
00085   }
00086 
00087   @Test
00088   public void testDrift() throws Exception {
00089     delta = 31;
00090     drift = 2;
00091     driftSensitivity = 1;
00092     errorReductionCoefficientSensitivity = 1;
00093     latencyOutlierFilterSampleSize = 1;
00094     latencyOutlierFilterThreshold = 1.5;
00095     RemoteUptimeClock remoteUptimeClock = newRemoteUptimeClock();
00096     remoteUptimeClock.calibrate(10, 0);
00097     for (int i = 0; i < 10000; i++) {
00098       remoteUptimeClock.update();
00099       assertEquals(2, remoteUptimeClock.getDrift(), 1e-6);
00100     }
00101     assertEquals(0, remoteUptimeClock.getErrorReductionCoefficient(), 1e-9);
00102   }
00103 
00104   @Test
00105   public void testConvergence() {
00106     delta = 7;
00107     drift = 2;
00108     driftSensitivity = 1;
00109     errorReductionCoefficientSensitivity = 1;
00110     latencyOutlierFilterSampleSize = 1;
00111     latencyOutlierFilterThreshold = 1.5;
00112     RemoteUptimeClock remoteUptimeClock = newRemoteUptimeClock();
00113     remoteUptimeClock.calibrate(10, 0);
00114     // Remote clock jumps.
00115     remoteUptime += 71;
00116     // We update less often.
00117     delta = 31;
00118     // Calibrated drift was wrong.
00119     drift = 6;
00120     for (int i = 0; i < 3; i++) {
00121       remoteUptimeClock.update();
00122     }
00123     assertEquals(6, remoteUptimeClock.getDrift(), 1e-6);
00124     for (int i = 0; i < 11; i++) {
00125       remoteUptimeClock.update();
00126       assertEquals(6, remoteUptimeClock.getDrift(), 1e-6);
00127     }
00128     assertEquals(0, remoteUptimeClock.getErrorReductionCoefficient(), 1e-9);
00129   }
00130 
00131   @Test
00132   public void testConvergenceSensitivity() {
00133     delta = 7;
00134     drift = 2;
00135     driftSensitivity = 0.5;
00136     errorReductionCoefficientSensitivity = 0.5;
00137     latencyOutlierFilterSampleSize = 1;
00138     latencyOutlierFilterThreshold = 1.5;
00139     RemoteUptimeClock remoteUptimeClock = newRemoteUptimeClock();
00140     remoteUptimeClock.calibrate(10, 0);
00141     // Calibrated drift was wrong.
00142     drift = 2.1;
00143     for (int i = 0; i < 11; i++) {
00144       remoteUptimeClock.update();
00145     }
00146     assertEquals(2.1, remoteUptimeClock.getDrift(), 1e-4);
00147     assertEquals(0, remoteUptimeClock.getErrorReductionCoefficient(), 1e-3);
00148   }
00149 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49