Skip to content

Commit

Permalink
Add time synchronization with callback example. (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julián Bermúdez Ortega authored and BorjaOuterelo committed Jan 27, 2020
1 parent af365ce commit c0a00cd
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ if(UCLIENT_BUILD_EXAMPLES)
add_subdirectory(examples/Deployment)
add_subdirectory(examples/Discovery)
add_subdirectory(examples/TimeSync)
add_subdirectory(examples/TimeSyncWithCb)
endif()

###############################################################################
Expand Down
51 changes: 51 additions & 0 deletions examples/TimeSyncWithCb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2017-present Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 2.8.12)
if (${CMAKE_VERSION} VERSION_GREATER 3.0)
cmake_policy(SET CMP0048 NEW)
endif()

project(time-sync-with-cb)

if(NOT UCLIENT_BUILD_EXAMPLES)
find_package(microxrcedds_client REQUIRED)
endif()

if(NOT PROFILE_UDP_TRANSPORT)
message(WARNING "Can not compile example: The PROFILE_UDP_TRANSPORT must be enabled.")
else()
add_executable(${PROJECT_NAME} main.c)
if(MSVC OR MSVC_IDE)
target_compile_options(${PROJECT_NAME} PRIVATE /wd4996)
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
C_STANDARD 99
C_STANDARD_REQUIRED YES
)

target_link_libraries(${PROJECT_NAME} microxrcedds_client $<$<C_COMPILER_ID:GNU>:-Wl,--gc-section,--no-export-dynamic>)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION examples/uxr/client/${PROJECT_NAME}/${BIN_INSTALL_DIR}
)

install(DIRECTORY ${PROJECT_SOURCE_DIR}/
DESTINATION examples/uxr/client/${PROJECT_NAME}
FILES_MATCHING PATTERN "*.h"
PATTERN "*.c"
PATTERN "*.idl"
)
endif()
97 changes: 97 additions & 0 deletions examples/TimeSyncWithCb/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2017-present Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <uxr/client/client.h>

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif

int64_t avg_time_offset = 0;
size_t sample_id = 0;

void on_time(
uxrSession* session,
int64_t current_time,
int64_t transmit_timestamp,
int64_t received_timestamp,
int64_t originate_timestamp,
void* args)
{
(void) args;
int64_t current_time_offset = ((current_time - originate_timestamp) - (transmit_timestamp - received_timestamp)) / 2;
avg_time_offset = (avg_time_offset * sample_id + current_time_offset) / (sample_id + 1);
++sample_id;
session->time_offset = avg_time_offset;
}

int main(int args, char** argv)
{
// CLI
if(3 > args || 0 == atoi(argv[2]))
{
printf("usage: program [-h | --help] | ip port\n");
return 0;
}

char* ip = argv[1];
uint16_t port = (uint16_t)atoi(argv[2]);

// Transport
uxrUDPTransport transport;
uxrUDPPlatform udp_platform;
if(!uxr_init_udp_transport(&transport, &udp_platform, ip, port))
{
printf("Error at create transport.\n");
return 1;
}

// Session
uxrSession session;
uxr_init_session(&session, &transport.comm, 0xCCCCDDDD);
if(!uxr_create_session(&session))
{
printf("Error at create session.\n");
return 1;
}

// Set time-callback.
uxr_set_time_callback(&session, on_time, NULL);

// Synchronize with the Agent
bool synchronized = false;
do
{
synchronized = uxr_sync_session(&session, 1000);
#ifdef WIN32
printf("synchronized with time offset %-5I64d us\n", session.time_offset / 1000);
Sleep(1);
#else
printf("synchronized with time offset %-5" PRId64 "us\n", session.time_offset / 1000);
sleep(1);
#endif

} while (synchronized);

// Delete resources
uxr_delete_session(&session);
uxr_close_udp_transport(&transport);

return 0;
}

0 comments on commit c0a00cd

Please sign in to comment.