Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSDK-8757] allow disabling of individual capture configs #312

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion micro-rdk/src/common/data_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ pub struct DataCollectorConfig {
pub method: CollectionMethod,
pub capture_frequency_hz: f32,
pub capacity: usize,
pub disabled: bool,
}

impl TryFrom<&Kind> for DataCollectorConfig {
type Error = AttributeError;
fn try_from(value: &Kind) -> Result<Self, Self::Error> {
let disabled = match value.get("disabled") {
Ok(val) => match val {
Some(Kind::BoolValue(v)) => *v,
_ => false,
},
Err(_) => false,
};
let method_str: String = value
.get("method")?
.ok_or(AttributeError::KeyNotFound("method".to_string()))?
Expand All @@ -45,7 +53,7 @@ impl TryFrom<&Kind> for DataCollectorConfig {
.unwrap_or(&Kind::NumberValue(DEFAULT_CACHE_SIZE_KB))
.try_into()?;
let capacity = (capacity_kb * 1000.0) as usize;
if capacity < 1000 {
if (capacity < 1000) && !disabled {
Comment on lines 55 to +56
Copy link
Member

@mattjperez mattjperez Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not actually a change from this PR but wouldn't this (without the casting to usize) be the same as if capacity_kb < 1.0?

return Err(AttributeError::ValidationError(
"cache size must be at least 1KB".to_string(),
));
Expand All @@ -64,6 +72,7 @@ impl TryFrom<&Kind> for DataCollectorConfig {
method,
capture_frequency_hz,
capacity,
disabled,
})
}
}
Expand Down Expand Up @@ -308,6 +317,7 @@ mod tests {
assert!(matches!(conf.method, CollectionMethod::Readings));
assert_eq!(conf.capture_frequency_hz, 100.0);
assert_eq!(conf.capacity, (DEFAULT_CACHE_SIZE_KB * 1000.0) as usize);
assert_eq!(conf.disabled, false);

let kind_map = HashMap::from([
(
Expand All @@ -316,12 +326,27 @@ mod tests {
),
("capture_frequency_hz".to_string(), Kind::NumberValue(100.0)),
("cache_size_kb".to_string(), Kind::NumberValue(2.0)),
("disabled".to_string(), Kind::BoolValue(true)),
]);
let conf_kind = Kind::StructValue(kind_map);
let conf: DataCollectorConfig = (&conf_kind).try_into()?;
assert!(matches!(conf.method, CollectionMethod::AngularVelocity));
assert_eq!(conf.capture_frequency_hz, 100.0);
assert_eq!(conf.capacity, 2000);
assert_eq!(conf.disabled, true);

let kind_map = HashMap::from([
(
"method".to_string(),
Kind::StringValue("AngularVelocity".to_string()),
),
("capture_frequency_hz".to_string(), Kind::NumberValue(100.0)),
("cache_size_kb".to_string(), Kind::NumberValue(2.0)),
("disabled".to_string(), Kind::BoolValue(false)),
]);
let conf_kind = Kind::StructValue(kind_map);
let conf: DataCollectorConfig = (&conf_kind).try_into()?;
assert_eq!(conf.disabled, false);

let kind_map = HashMap::from([
(
Expand All @@ -335,6 +360,21 @@ mod tests {
let conf = DataCollectorConfig::try_from(&conf_kind);
assert!(conf.is_err());

let kind_map = HashMap::from([
(
"method".to_string(),
Kind::StringValue("AngularVelocity".to_string()),
),
("capture_frequency_hz".to_string(), Kind::NumberValue(100.0)),
("cache_size_kb".to_string(), Kind::NumberValue(0.5)),
("disabled".to_string(), Kind::BoolValue(true)),
]);
let conf_kind = Kind::StructValue(kind_map);
let conf = DataCollectorConfig::try_from(&conf_kind);
assert!(conf.is_ok());
let conf = conf.unwrap();
assert_eq!(conf.disabled, true);

let kind_map = HashMap::from([
(
"method".to_string(),
Expand Down
6 changes: 4 additions & 2 deletions micro-rdk/src/common/robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ impl LocalRobot {
}
#[cfg(feature = "data")]
for cfg in config.data_collector_configs.iter() {
self.data_collector_configs
.push((new_resource_name.clone(), cfg.clone()));
if !cfg.disabled {
self.data_collector_configs
.push((new_resource_name.clone(), cfg.clone()));
}
}
self.insert_resource(
model,
Expand Down
Loading