时间戳与外部授时
Android Java SDK:时间戳与外部授时
时间戳与外部授时
外部授时
调用 Glove.syncTime(double referenceTimeMs),以外部时间源提供的绝对 Unix 毫秒时间同步整个 Glove 域。参数支持小数毫秒,必须为有限数值;秒或纳秒值不能直接传入。
TimeSync.run(context, transport) 在启动前授时,并在后台运行期间每秒重新读取时间源、再次授时。无参重载使用 System::currentTimeMillis;需要自定义时间源时传入 DoubleSupplier,例如变量名为 externalTimeMs 时调用 glove.syncTime(externalTimeMs.getAsDouble())。使用本机系统时间时也可以写成 () -> (double) System.currentTimeMillis()。
import android.content.Context;
import com.qnbot.sdk.Sdk;
import com.qnbot.sdk.SdkConfig;
import com.qnbot.sdk.android.SerialTransport;
import com.qnbot.sdk.glove.Glove;
import com.qnbot.sdk.glove.GloveConfig;
import com.qnbot.sdk.glove.GloveDevice;
import java.util.function.DoubleSupplier;
/**
* Runs in the background while resynchronizing Glove time every second.
*
* <p>Pass the host {@link Context}, an authorized transport and an external Unix-millisecond time
* source. The SDK auto-discovers the only glove; pose and status subscriptions show the
* synchronized timestamps. Interrupt the worker to stop and join the runner.
*/
public final class TimeSync {
private TimeSync() {}
/** Resynchronizes from the host clock. */
public static void run(Context context, SerialTransport transport) throws InterruptedException {
run(context, transport, System::currentTimeMillis);
}
public static void run(Context context, SerialTransport transport,
DoubleSupplier referenceTimeMillis) throws InterruptedException {
SdkConfig config = SdkConfig.builder().device(GloveConfig.builder().build()).build();
try (Sdk sdk = new Sdk(config, context, transport); Glove glove = sdk.glove()) {
GloveDevice device = glove.device();
double reference = referenceTimeMillis.getAsDouble();
glove.syncTime(reference);
System.out.println("reference_time_ms=" + reference);
device.pose().subscribe(sample
-> System.out.println("pose sequence=" + sample.sequence
+ " source_time_ms=" + sample.sourceTime + " observed_at_ms=" + sample.observedAt));
device.status().subscribe(sample
-> System.out.println("status source_time_ms=" + sample.sourceTime
+ " observed_at_ms=" + sample.observedAt + " connected=" + sample.value.connected));
glove.start();
glove.runBackground();
try {
while (true) {
Thread.sleep(1_000);
reference = referenceTimeMillis.getAsDouble();
glove.syncTime(reference);
System.out.println("reference_time_ms=" + reference);
}
} finally {
glove.requestStop();
glove.join();
}
}
}
}在 Glove 域保持打开时,可在启动前授时,也可在运行中定期再次授时。重新授时更新所有 Glove 来源的后续源时间,不重启设备或订阅,也不改写已经交付的数据。
授时影响 SDK 分配的姿态、IMU、状态及新建采集/标定结果的源时间。外部帧显式提供的 timestampMs 仍优先使用原值。Sample.sourceTime 使用源时间轴,Sample.observedAt 继续使用 SDK 相对单调时间;二者单位均为毫秒,但不能直接相减推导延迟。Exo 时间和运行调度不受影响。
NaN 或无穷参数抛出 InvalidArgumentException,保留原有授时;Glove 域或 SDK 已关闭时抛出 ClosedHandleException。该方法同步 SDK 处理数据所用的时间,不向设备发送硬件授时命令。
时间戳
用 sourceTime 对齐使用同一参考时钟的来源,用 observedAt 的差值计算 SDK 观察到的样本间隔。