Skip to content

Commit

Permalink
fix #31
Browse files Browse the repository at this point in the history
  • Loading branch information
tmori committed Jan 20, 2024
1 parent ab67f3e commit d5997f8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 47 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ APIのカテゴリとしては、その目的の応じて以下の3ケース
- `config_path`: アセットの設定ファイルへのパス。アセットの初期化や設定に必要な情報が含まれることを想定します。型: `const char*`
- `callback`: アセットのイベント処理を行うコールバック関数へのポインタ。このコールバックは、アセットの初期化、シミュレーションステップ、リセットなどのイベントが発生したときに呼び出されます。型: `hako_asset_callbacks_t*`
- `delta_usec`: シミュレーション時間のタイムステップをマイクロ秒単位で指定します。この値はシミュレーションが更新される頻度を決定します。型: `hako_time_t`
- `model`: 箱庭アセットが制御プログラムの場合は `HAKO_ASSET_MODEL_CONTROLLER`。プラントモデルの場合は `HAKO_ASSET_MODEL_PLANT`。 型: `HakoAssetModelType`

**戻り値**:
成功時は `0` を返します。失敗時は非 `0` のエラーコードを返します。
Expand Down Expand Up @@ -126,7 +127,7 @@ int main() {
const char* config_path = "/path/to/config.json";

// コールバック関数と時間ステップを指定してアセットを登録
int result = hako_asset_register(asset_name, config_path, callbacks, 1000000); // 1秒ごとに更新
int result = hako_asset_register(asset_name, config_path, callbacks, 1000000, HAKO_ASSET_MODEL_CONTROLLER); // 1秒ごとに更新

if (result != 0) {
// エラーハンドリング
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world/src/hello_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int argc, const char* argv[])
my_callback.on_manual_timing_control = my_on_manual_timing_control;
}
hako_conductor_start(delta_time_usec, delta_time_usec);
int ret = hako_asset_register(asset_name, config_path, &my_callback, delta_time_usec);
int ret = hako_asset_register(asset_name, config_path, &my_callback, delta_time_usec, HAKO_ASSET_MODEL_CONTROLLER);
if (ret != 0) {
printf("ERORR: hako_asset_register() returns %d.", ret);
return 1;
Expand Down
4 changes: 2 additions & 2 deletions src/assets/src/hako_asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static inline bool file_exists(const char* path) {
return file.good();
}

int hako_asset_register(const char *asset_name, const char *config_path, hako_asset_callbacks_t *callbacks, hako_time_t delta_usec) {
int hako_asset_register(const char *asset_name, const char *config_path, hako_asset_callbacks_t *callbacks, hako_time_t delta_usec, HakoAssetModelType model) {
if (asset_name == nullptr || *asset_name == '\0') {
std::cerr << "Error: Asset name is not set." << std::endl;
return EINVAL;
Expand All @@ -31,7 +31,7 @@ int hako_asset_register(const char *asset_name, const char *config_path, hako_as
return EINVAL;
}
hako_asset_impl_register_callback(callbacks);
if (!hako_asset_impl_init(asset_name, config_path, delta_usec)) {
if (!hako_asset_impl_init(asset_name, config_path, delta_usec, (model == HAKO_ASSET_MODEL_PLANT))) {
return EIO;
}
std::cout << "INFO: asset(" << asset_name << ") is registered." << std::endl;
Expand Down
97 changes: 56 additions & 41 deletions src/assets/src/hako_asset_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,33 @@ HakoAssetType hako_asset_instance;
/***********************
* simulation control
***********************/

static void hako_asset_impl_parse_robots(void)
static PduReader* create_reader(const nlohmann::json &reader_json)
{
PduReader* reader = new PduReader{
reader_json["type"],
reader_json["org_name"],
reader_json["name"],
reader_json["channel_id"],
reader_json["pdu_size"]
};
HAKO_ASSET_ASSERT(reader != nullptr);
return reader;
}
static PduWriter* create_writer(const nlohmann::json &writer_json)
{
PduWriter* writer = new PduWriter{
writer_json["type"],
writer_json["org_name"],
writer_json["name"],
writer_json["write_cycle"],
writer_json["channel_id"],
writer_json["pdu_size"],
writer_json["method_type"]
};
HAKO_ASSET_ASSERT(writer != nullptr);
return writer;
}
static void hako_asset_impl_parse_robots(bool is_plant)
{
const json& robots_json = hako_asset_instance.param["robots"];

Expand All @@ -16,35 +41,33 @@ static void hako_asset_impl_parse_robots(void)
// name を取得
robot->name = robot_json["name"];

// ロボット側が被制御対象のコンフィグファイルなので、READとWRITEは逆になることに注意
// is_plant: true プラントモデル
// is_plant: false 制御プログラム
// is_plantが false の場合は、ロボット側が被制御対象のコンフィグファイルなので、READとWRITEは逆になることに注意

// PduReaders を取得
if (robot_json.find("shm_pdu_writers") != robot_json.end()) {
const json& pdu_readers_json = robot_json["shm_pdu_writers"];
for (const auto& reader_json : pdu_readers_json) {
PduReader* reader = new PduReader{
reader_json["type"],
reader_json["org_name"],
reader_json["name"],
reader_json["channel_id"],
reader_json["pdu_size"]
};
robot->pdu_readers.push_back(*reader);
if (is_plant) {
robot->pdu_writers.push_back(*create_writer(reader_json));
}
else {
robot->pdu_readers.push_back(*create_reader(reader_json));
}
}
} else {
// nothing to do
}
if (robot_json.find("rpc_pdu_writers") != robot_json.end()) {
const json& pdu_readers_json = robot_json["rpc_pdu_writers"];
for (const auto& reader_json : pdu_readers_json) {
PduReader* reader = new PduReader{
reader_json["type"],
reader_json["org_name"],
reader_json["name"],
reader_json["channel_id"],
reader_json["pdu_size"]
};
robot->pdu_readers.push_back(*reader);
if (is_plant) {
robot->pdu_writers.push_back(*create_writer(reader_json));
}
else {
robot->pdu_readers.push_back(*create_reader(reader_json));
}
}
} else {
// nothing to do
Expand All @@ -54,33 +77,25 @@ static void hako_asset_impl_parse_robots(void)
if (robot_json.find("shm_pdu_readers") != robot_json.end()) {
const json& pdu_writers_json = robot_json["shm_pdu_readers"];
for (const auto& writer_json : pdu_writers_json) {
PduWriter* writer = new PduWriter{
writer_json["type"],
writer_json["org_name"],
writer_json["name"],
writer_json["write_cycle"],
writer_json["channel_id"],
writer_json["pdu_size"],
writer_json["method_type"]
};
robot->pdu_writers.push_back(*writer);
if (is_plant) {
robot->pdu_readers.push_back(*create_reader(writer_json));
}
else {
robot->pdu_writers.push_back(*create_writer(writer_json));
}
}
} else {
// nothing to do
}
if (robot_json.find("rpc_pdu_readers") != robot_json.end()) {
const json& pdu_writers_json = robot_json["rpc_pdu_readers"];
for (const auto& writer_json : pdu_writers_json) {
PduWriter* writer = new PduWriter{
writer_json["type"],
writer_json["org_name"],
writer_json["name"],
writer_json["write_cycle"],
writer_json["channel_id"],
writer_json["pdu_size"],
writer_json["method_type"]
};
robot->pdu_writers.push_back(*writer);
if (is_plant) {
robot->pdu_readers.push_back(*create_reader(writer_json));
}
else {
robot->pdu_writers.push_back(*create_writer(writer_json));
}
}
} else {
// nothing to do
Expand All @@ -89,7 +104,7 @@ static void hako_asset_impl_parse_robots(void)
}
}

bool hako_asset_impl_init(const char* asset_name, const char* config_path, hako_time_t delta_usec)
bool hako_asset_impl_init(const char* asset_name, const char* config_path, hako_time_t delta_usec, bool is_plant)
{
hako_asset_instance.is_initialized = false;
std::ifstream ifs(config_path);
Expand All @@ -104,7 +119,7 @@ bool hako_asset_impl_init(const char* asset_name, const char* config_path, hako_
hako_asset_instance.current_usec = 0;
try {
hako_asset_instance.param = json::parse(ifs);
hako_asset_impl_parse_robots();
hako_asset_impl_parse_robots(is_plant);
} catch (const json::exception& e) {
std::cerr << "JSON parsing error: " << e.what() << std::endl;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/assets/src/hako_asset_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extern HakoAssetType hako_asset_instance;
#define HAKO_ASSET_WAIT_TIME_USEC (1000 * 10)

extern bool hako_asset_impl_register_callback(const hako_asset_callbacks_t* callback);
extern bool hako_asset_impl_init(const char* asset_name, const char* config_path, hako_time_t delta_usec);
extern bool hako_asset_impl_init(const char* asset_name, const char* config_path, hako_time_t delta_usec, bool is_plant);
extern bool hako_asset_impl_wait_running(void);
extern HakoSimulationStateType hako_asset_impl_state();
extern bool hako_asset_impl_step(hako_time_t increment_step);
Expand Down
6 changes: 5 additions & 1 deletion src/include/hako_asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ struct hako_asset_callbacks_s {
int (*on_reset)(hako_asset_context_t*);
};
#define HAKO_ASSET_MIN_DELTA_TIME_USEC 1000
extern int hako_asset_register(const char *asset_name, const char *config_path, hako_asset_callbacks_t *callbacks, hako_time_t delta_usec);
typedef enum {
HAKO_ASSET_MODEL_PLANT = 0,
HAKO_ASSET_MODEL_CONTROLLER
} HakoAssetModelType;
extern int hako_asset_register(const char *asset_name, const char *config_path, hako_asset_callbacks_t *callbacks, hako_time_t delta_usec, HakoAssetModelType model);
extern int hako_asset_start(void);
extern int hako_asset_pdu_read(const char *robo_name, HakoPduChannelIdType lchannel, char *buffer, size_t buffer_len);
extern int hako_asset_pdu_write(const char *robo_name, HakoPduChannelIdType lchannel, const char *buffer, size_t buffer_len);
Expand Down

0 comments on commit d5997f8

Please sign in to comment.