Skip to content

Commit 40544ae

Browse files
author
hanjun
committed
add check
1 parent 1af2633 commit 40544ae

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

src/module/mujoco_sim_module/common/xmodel_reader.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace aimrt_mujoco_sim::mujoco_sim_module::common {
88
std::optional<int32_t> GetJointSensorIdByJointName(const mjModel* m, std::string_view joint_name, mjtSensor sensor_type) {
99
int32_t jointId = mj_name2id(m, mjOBJ_JOINT, joint_name.data());
1010
if (jointId < 0) {
11-
AIMRT_ERROR_THROW("Invalid joint name: {}.", joint_name);
11+
AIMRT_ERROR_THROW("Invalid joint name: {}, cannot find a joint sensor.", joint_name);
1212
return std::nullopt;
1313
}
1414

@@ -34,7 +34,7 @@ std::optional<int32_t> GetJointactfrcIdByJointName(const mjModel* m, std::string
3434

3535
std::optional<std::string> GetJointSensorNameByJointName(const mjModel* m, std::string_view joint_name, mjtSensor sensor_type) {
3636
if (auto sensor_id = GetJointSensorIdByJointName(m, joint_name, sensor_type)) {
37-
return mj_id2name(m, mjOBJ_SENSOR, *sensor_id);
37+
return mj_id2name(m, mjOBJ_SENSOR, sensor_id.value_or(-1));
3838
}
3939
return std::nullopt;
4040
}
@@ -54,7 +54,7 @@ std::optional<std::string> GetJointactfrcNameByJointName(const mjModel* m, std::
5454
std::optional<int32_t> GetJointActIdByJointName(const mjModel* m, std::string_view joint_name) {
5555
int32_t joint_id = mj_name2id(m, mjOBJ_JOINT, joint_name.data());
5656
if (joint_id < 0) {
57-
AIMRT_ERROR_THROW("Invalid joint name: {}.", joint_name);
57+
AIMRT_ERROR_THROW("Invalid joint name: {}, cannot find a joint actuator.", joint_name);
5858
return std::nullopt;
5959
}
6060

@@ -67,10 +67,10 @@ std::optional<int32_t> GetJointActIdByJointName(const mjModel* m, std::string_vi
6767
}
6868

6969
std::optional<std::string> GetJointActNameByJointName(const mjModel* m, std::string_view joint_name) {
70-
int32_t actuatorId = GetJointActIdByJointName(m, joint_name.data()).value_or(-1);
71-
if (actuatorId < 0) return std::nullopt;
72-
73-
return mj_id2name(m, mjOBJ_ACTUATOR, actuatorId);
70+
if (auto actuator_id = GetJointActIdByJointName(m, joint_name.data())) {
71+
return mj_id2name(m, mjOBJ_ACTUATOR, actuator_id.value_or(-1));
72+
}
73+
return std::nullopt;
7474
}
7575

7676
std::optional<std::string> GetJointActTypeByJointName(const mjModel* m, std::string_view joint_name) {
@@ -115,6 +115,8 @@ std::optional<std::string> GetJointActTypeByJointName(const mjModel* m, std::str
115115
}
116116

117117
std::optional<int32_t> GetSensorIdBySensorName(const mjModel* m, std::string_view sensor_name) {
118+
if (sensor_name.empty()) return std::nullopt;
119+
118120
int32_t sensor_id = mj_name2id(m, mjOBJ_SENSOR, sensor_name.data());
119121
if (sensor_id < 0) {
120122
AIMRT_ERROR_THROW("Invalid sensor name: {}.", sensor_name);

src/module/mujoco_sim_module/subscriber/joint_actuator_subscriber.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ void JointActuatorSubscriberBase::ApplyCtrlData() {
6060
void JointActuatorSubscriberBase::RegisterActuatorAddr() {
6161
for (auto const& joint : options_.joints) {
6262
int32_t actuator_id = common::GetJointActIdByJointName(m_, joint.bind_joint).value_or(-1);
63-
AIMRT_CHECK_ERROR_THROW(actuator_id >= 0, "Joint actuator id for joint '{}' is not found.",
64-
joint.bind_joint);
6563

6664
actuator_addr_vec_.emplace_back(actuator_id);
6765
joint_names_vec_.emplace_back(joint.name);

0 commit comments

Comments
 (0)