Skip to content

Commit

Permalink
add hako master for sample program
Browse files Browse the repository at this point in the history
  • Loading branch information
tmori committed Jan 13, 2024
1 parent d5f2990 commit 7410790
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(HAKO_CMD_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/cmd")
set(HAKO_LIB_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/lib")
set(HAKO_PROXY_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/proxy")
set(HAKO_INC_DIR "${PROJECT_SOURCE_DIR}/src/include")
set(HAKOC_SOURCE_DIR "${PROJECT_SOURCE_DIR}/src/hakoc/src")

if (DEFINED HAKO_CLIENT_OPTION_FILEPATH)
include(${HAKO_CLIENT_OPTION_FILEPATH})
Expand Down
4 changes: 3 additions & 1 deletion examples/hello_world/src/hello_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ int main(int argc, const char* argv[])
else {
my_callback.on_manual_timing_control = my_on_manual_timing_control;
}

hako_master_start(delta_time_usec, delta_time_usec);
int ret = hako_asset_register(asset_name, config_path, &my_callback, delta_time_usec);
if (ret != 0) {
printf("ERORR: hako_asset_register() returns %d.", ret);
return 1;
}
ret = hako_asset_start();
printf("INFO: hako_asset_start() returns %d\n", ret);

hako_master_stop();
return 0;
}
12 changes: 12 additions & 0 deletions src/assets/src/hako_asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ int hako_asset_usleep(hako_time_t sleep_time_usec) {
}
return EINTR;
}

int hako_master_start(hako_time_t delta_usec, hako_time_t max_delay_usec)
{
if (hako_master_impl_start(delta_usec, max_delay_usec)) {
return 0;
}
return EIO;
}
void hako_master_stop(void)
{
hako_master_impl_stop();
}
53 changes: 53 additions & 0 deletions src/assets/src/hako_asset_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,56 @@ hako_time_t hako_asset_impl_get_world_time()
{
return hako_asset_instance.hako_asset->get_worldtime();
}
std::shared_ptr<hako::IHakoMasterController> hako_master = nullptr;
static bool hako_master_cmd_stop = false;
static void* hako_master_impl_thread_run(void* arg)
{
std::cout << "INFO: hako_master thread start" << std::endl;
if (arg) {
//nothing to do
}
while (hako_master_cmd_stop == false) {
try {
hako_master->execute();
} catch (std::exception *e) {
std::cerr << "ERROR: hako_master.execute() Failed" << std::endl;
return nullptr;
}
}
hako_master = nullptr;
return nullptr;
}

bool hako_master_impl_start(hako_time_t delta_usec, hako_time_t max_delay_usec)
{
if (hako_master != nullptr) {
return false;
}
pthread_t thread;
try {
hako::init();
hako_master = hako::create_master();
if (hako_master == nullptr) {
std::cout << "INFO: hako::create_master() Failed" << std::endl;
return false;
}
hako_master->set_config_simtime(max_delay_usec, delta_usec);
} catch (std::exception *e) {
std::cout << "INFO: hako::create_master() Failed" << std::endl;
return false;
}

if (pthread_create(&thread, NULL, hako_master_impl_thread_run, nullptr) != 0) {
std::cerr << "ERROR: Failed to create hako_master_impl_thread_run thread!" << std::endl;
return false;
}
return true;
}
void hako_master_impl_stop(void)
{
hako_master_cmd_stop = true;
while (hako_master != nullptr) {
usleep(WAIT_TIME_USEC);
}
return;
}
6 changes: 6 additions & 0 deletions src/assets/src/hako_asset_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ extern hako_time_t hako_asset_impl_get_world_time();
extern bool hako_asset_impl_pdu_read(const char* robo_name, HakoPduChannelIdType lchannel, char* buffer, size_t buffer_len);
extern bool hako_asset_impl_pdu_write(const char* robo_name, HakoPduChannelIdType lchannel, const char* buffer, size_t buffer_len);

/*
* for master api for test
*/
extern bool hako_master_impl_start(hako_time_t delta_usec, hako_time_t max_delay_usec);
extern void hako_master_impl_stop(void);

#endif /* _HAKO_ASSET_IMPL_HPP_ */
2 changes: 2 additions & 0 deletions src/include/hako_asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern int hako_asset_pdu_read(const char *robo_name, HakoPduChannelIdType lchan
extern int hako_asset_pdu_write(const char *robo_name, HakoPduChannelIdType lchannel, const char *buffer, size_t buffer_len);
extern hako_time_t hako_asset_simulation_time(void);
extern int hako_asset_usleep(hako_time_t sleep_time_usec);
extern int hako_master_start(hako_time_t delta_usec, hako_time_t max_delay_usec);
extern void hako_master_stop(void);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 7410790

Please sign in to comment.