Fixes to touch_grid config parsing.

PiperOrigin-RevId: 530561729
Change-Id: I67f6dfd137aa54ce2141e5d5c30b5b888665c4a2
This commit is contained in:
Yuval Tassa
2023-05-09 03:41:17 -07:00
committed by Copybara-Service
parent 4c487e07ed
commit b36c611745
+22 -22
View File
@@ -177,52 +177,52 @@ TouchGrid* TouchGrid::Create(const mjModel* m, mjData* d,
int nchannel = strtod(mj_getPluginConfig(m, instance, "nchannel"), nullptr);
if (!nchannel) nchannel = 1;
if (nchannel < 1 || nchannel > 6) {
mju_warning("nchannel must be between 1 and 6");
mju_error("nchannel must be between 1 and 6");
return nullptr;
}
// size
std::vector<int> size;
std::vector<mjtNum> fov;
ReadVector(
size, std::string(mj_getPluginConfig(m, instance, "size")).c_str());
ReadVector(
fov, std::string(mj_getPluginConfig(m, instance, "fov")).c_str());
if (size[0] < 0 || size[1] < 0) {
mju_warning("Horizontal and vertical resolutions must be positive");
std::string size_str = std::string(mj_getPluginConfig(m, instance, "size"));
ReadVector(size, size_str.c_str());
if (size.size()!= 2) {
mju_error("Both horizontal and vertical resolutions must be specified");
return nullptr;
}
if (size.size()!= 2) {
mju_warning("Both horizontal and vertical resolutions must be specified");
if (size[0] <= 0 || size[1] <= 0) {
mju_error("Horizontal and vertical resolutions must be positive");
return nullptr;
}
// field of view
std::vector<mjtNum> fov;
std::string fov_str = std::string(mj_getPluginConfig(m, instance, "fov"));
ReadVector(fov, fov_str.c_str());
if (fov.size()!= 2) {
mju_error(
"Both horizontal and vertical fields of view must be specified");
return nullptr;
}
if (fov[0] <= 0 || fov[0] > 180) {
mju_warning("`fov[0]` must be a float between (0, 180] degrees");
mju_error("`fov[0]` must be a float between (0, 180] degrees");
return nullptr;
}
if (fov[1] <= 0 || fov[1] > 90) {
mju_warning("`fov[1]` must be a float between (0, 90] degrees");
return nullptr;
}
if (fov.size()!= 2) {
mju_warning(
"Both horizontal and vertical fields of view must be specified");
mju_error("`fov[1]` must be a float between (0, 90] degrees");
return nullptr;
}
// gamma
mjtNum gamma = strtod(mj_getPluginConfig(m, instance, "gamma"), nullptr);
if (gamma < 0 || gamma > 1) {
mju_warning("`gamma` must be a nonnegative float between [0, 1]");
mju_error("`gamma` must be a nonnegative float between [0, 1]");
return nullptr;
}
return new TouchGrid(m, d, instance, nchannel, size.data(), fov.data(),
gamma);
} else {
mju_warning("Invalid or missing parameters in touch_grid sensor plugin");
mju_error("Invalid or missing parameters in touch_grid sensor plugin");
return nullptr;
}
}
@@ -489,7 +489,7 @@ void TouchGrid::RegisterPlugin() {
plugin.name = "mujoco.sensor.touch_grid";
plugin.capabilityflags |= mjPLUGIN_SENSOR;
// Parameterized by 5 attributes.
// Parameterized by 4 attributes.
const char* attributes[] = {"nchannel", "size", "fov", "gamma"};
plugin.nattribute = sizeof(attributes) / sizeof(attributes[0]);
plugin.attributes = attributes;
@@ -502,8 +502,8 @@ void TouchGrid::RegisterPlugin() {
int nchannel = strtod(mj_getPluginConfig(m, instance, "nchannel"), nullptr);
if (!nchannel) nchannel = 1;
std::vector<int> size;
ReadVector(
size, std::string(mj_getPluginConfig(m, instance, "size")).c_str());
std::string size_str = std::string(mj_getPluginConfig(m, instance, "size"));
ReadVector(size, size_str.c_str());
return nchannel * size[0] * size[1];
};