Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
84a4ed3
Move non-widget RobotProperty stuff out of widgets and fix wrong
xiyuoh Sep 5, 2025
62db8b5
Merge remote-tracking branch 'origin/main' into xiyu/fix_properties
xiyuoh Sep 5, 2025
3d451cf
Cleanup
xiyuoh Sep 5, 2025
e8f7bf2
Cleaner initialization
xiyuoh Sep 9, 2025
19e9e93
Better fix for overwriting
xiyuoh Sep 9, 2025
116d038
Make IsStatic optional
xiyuoh Sep 9, 2025
0359b9a
Add commands
xiyuoh Sep 9, 2025
37a8b7c
Make a bunch of systems -> observers
xiyuoh Oct 2, 2025
4e64c12
Merge branch 'main' into xiyu/fix_properties
xiyuoh Oct 3, 2025
98699d4
Revert making update_model_instances into observers
xiyuoh Oct 3, 2025
a08c036
Better support for multi-kind RobotProperty
xiyuoh Oct 3, 2025
916a471
Fix slotcar overwriting robot properties
xiyuoh Oct 3, 2025
840da45
Merge remote-tracking branch 'origin/main' into xiyu/fix_properties
xiyuoh Oct 3, 2025
de31bf4
Introduce EmptyRobotProperty
xiyuoh Oct 6, 2025
ac22d44
Move registration logic to site module
xiyuoh Oct 6, 2025
d59c399
Style
xiyuoh Oct 6, 2025
41d2a43
CI
xiyuoh Oct 6, 2025
15bff39
Fix unable to enable Amb/Mech System
xiyuoh Oct 8, 2025
9a1325d
Enable EmptyRobotProperty for Mobility and PowerSource
xiyuoh Oct 13, 2025
66bc2eb
Merge remote-tracking branch 'origin/main' into xiyu/fix_properties
xiyuoh Oct 13, 2025
6e54e79
Review comments
xiyuoh Oct 21, 2025
cea8006
Merge remote-tracking branch 'origin/main' into xiyu/fix_properties
xiyuoh Oct 21, 2025
e385f04
Merge branch 'main' into xiyu/fix_properties
xiyuoh Oct 23, 2025
e5089be
Merge branch 'main' into xiyu/fix_properties
xiyuoh Oct 24, 2025
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
6 changes: 5 additions & 1 deletion crates/rmf_site_editor/src/sdf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,12 @@ fn load_model<'a, 'b>(
if plugin.name == "slotcar".to_string()
|| plugin.filename == "libslotcar.so".to_string()
{
// Use commands instead of direct access to world as we
// want this to be queued up after robot data has been
// inserted into model descriptions during loading
world
.entity_mut(e)
.commands()
.entity(e)
Copy link
Member

Choose a reason for hiding this comment

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

This looks a bit odd. What the SDF loader does is serialize the whole content of the world into a bevy::Scene then later we will spawn the Scene.
I think the scene is later spawned through write_to_world_with and that only spawns entities and components, not commands.
So I wonder, are you sure the command here is being applied?

Copy link
Member Author

Choose a reason for hiding this comment

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

So this is the main change that fixes the "saved robot properties are being overwritten by the SDF loader" bug for me. If I undo this change cargo test would fail, and after some logging it turned out the original and destination JSONs are different, with RobotProperty being overwritten. This is what I think is happening without commands():

  1. load.rs inserts the Robot component into relevant model description entities. This would prepare the linked systems to insert appropriate RobotProperty (here) and RobotPropertyKind (here) components.
  2. Later in the model loading workflow, the SDF loader iterates through all SDF parts (links/visual/etc.) and inserts the slotcar components into those descendant entities instantly with direct world access. This is because within the SDF loader we don't know what the affiliated descriptions are, since they're not in the same World as where everything else is.
  3. The insert_slotcar_components system searches for non-description entities with these RobotPropertyKind components, and insert them into the appropriate affiliated description entities. This is done by writing a Change event with the updated Robot serialized properties.
  4. The ChangePlugin updates ModelProperty<Robot> in the next cycle, and eventually propagates to the affiliated model instances via the update_model_instances system.

TLDR I think steps (1) to (3) take place in cycle N, and step (4) takes place in cycle N+1. This means the inserted components in (1) are not known to insert_slotcar_components in step (3) and will be overwritten. By using commands() we're essentially forcing step 3. to only find slotcar models with robot properties in cycle N+1, and be able to query components from step 1. to check if properties are already present.

Copy link
Member

Choose a reason for hiding this comment

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

My assertion here is that the world you see here is not the App's world, but a brand new one that is immediately serialized into a scene, and in this process only entities and components are saved, the commands are effectively dropped.
I tested this by actually removing the whole statement that uses a command to add components altogether and verified that the unit test still passes. This makes me think that our unit test doesn't actually verify that this statement is working

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for pointing that out, I just tried opening the demo map and the tinyRobots do not have the properties inserted in. Let me think of a more reliable fix!

.insert(DifferentialDrive::from(&plugin.elements))
.insert(Battery::from(&plugin.elements))
.insert(AmbientSystem::from(&plugin.elements))
Expand Down
4 changes: 4 additions & 0 deletions crates/rmf_site_editor/src/site/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub use measurement::*;
pub mod model;
pub use model::*;

pub mod model_property;
pub use model_property::*;

pub mod modifier;
pub use modifier::*;

Expand Down Expand Up @@ -306,6 +309,7 @@ impl Plugin for SitePlugin {
PropertyPlugin::<OnLevel<Entity>, Robot>::default(),
SlotcarSdfPlugin,
MaterialPlugin::<ExtendedMaterial<StandardMaterial, LaneArrowMaterial>>::default(),
RobotPropertiesPlugin::default(),
))
.add_plugins((InfiniteGridPlugin,))
.add_issue_type(&DUPLICATED_DOOR_NAME_ISSUE_UUID, "Duplicate door name")
Expand Down
92 changes: 92 additions & 0 deletions crates/rmf_site_editor/src/site/model_property.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2025 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

use crate::site::{AssetSource, IsStatic, ModelProperty, Scale};
use bevy::{
ecs::{component::ComponentId, system::EntityCommands},
prelude::*,
};
use std::collections::HashMap;

/// Function that inserts a default property into an entity
pub type InsertModelPropertyFn = fn(EntityCommands);

pub fn get_insert_model_property_fn<T: Component + Default>() -> InsertModelPropertyFn {
|mut e_commands| {
e_commands.insert(T::default());
}
}

/// Function that removes a property, if it exists, from an entity
pub type RemoveModelPropertyFn = fn(EntityCommands);

pub fn get_remove_model_property_fn<T: Component + Default>() -> RemoveModelPropertyFn {
|mut e_commands| {
e_commands.remove::<T>();
}
}

/// This resource keeps track of all the properties that can be configured for a model description.
#[derive(Resource)]
pub struct ModelPropertyData {
pub required: HashMap<ComponentId, (String, InsertModelPropertyFn, RemoveModelPropertyFn)>,
pub optional: HashMap<ComponentId, (String, InsertModelPropertyFn, RemoveModelPropertyFn)>,
}

impl FromWorld for ModelPropertyData {
fn from_world(world: &mut World) -> Self {
let mut required = HashMap::new();
world.register_component::<ModelProperty<AssetSource>>();
required.insert(
world
.components()
.component_id::<ModelProperty<AssetSource>>()
.unwrap(),
(
"Asset Source".to_string(),
get_insert_model_property_fn::<ModelProperty<AssetSource>>(),
get_remove_model_property_fn::<ModelProperty<AssetSource>>(),
),
);
world.register_component::<ModelProperty<Scale>>();
required.insert(
world
.components()
.component_id::<ModelProperty<Scale>>()
.unwrap(),
(
"Scale".to_string(),
get_insert_model_property_fn::<ModelProperty<Scale>>(),
get_remove_model_property_fn::<ModelProperty<Scale>>(),
),
);
world.register_component::<ModelProperty<IsStatic>>();
required.insert(
world
.components()
.component_id::<ModelProperty<IsStatic>>()
.unwrap(),
(
"Is Static".to_string(),
get_insert_model_property_fn::<IsStatic>(),
get_remove_model_property_fn::<IsStatic>(),
),
);
let optional = HashMap::new();
Self { required, optional }
}
}
38 changes: 37 additions & 1 deletion crates/rmf_site_editor/src/site/robot_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*
*/

use crate::site::{Change, Group, ModelMarker, ModelProperty, Robot};
use crate::site::{
update_model_instances, Change, Group, ModelMarker, ModelProperty, ModelPropertyData, Robot,
};
use bevy::prelude::*;
use rmf_site_format::robot_properties::*;
use serde_json::Map;
Expand All @@ -28,6 +30,40 @@ pub struct UpdateRobotPropertyKinds {
pub value: serde_json::Value,
}

#[derive(Default)]
pub struct RobotPropertiesPlugin {}

impl Plugin for RobotPropertiesPlugin {
fn build(&self, app: &mut App) {
// Allows us to toggle Robot as a configurable property
// from the model description inspector
app.world_mut().register_component::<ModelProperty<Robot>>();
let component_id = app
.world()
.components()
.component_id::<ModelProperty<Robot>>()
.unwrap();
app.init_resource::<ModelPropertyData>()
.world_mut()
.resource_mut::<ModelPropertyData>()
.optional
.insert(
component_id,
(
"Robot".to_string(),
|mut e_cmd| {
e_cmd.insert(ModelProperty::<Robot>::default());
},
|mut e_cmd| {
e_cmd.remove::<ModelProperty<Robot>>();
},
),
);
app.add_event::<UpdateRobotPropertyKinds>()
.add_systems(PreUpdate, update_model_instances::<Robot>);
}
}

/// This system monitors changes to ModelProperty<Robot> and inserts robot property components
/// to the model descriptions
pub fn update_robot_property_components<T: RobotProperty>(
Expand Down
104 changes: 63 additions & 41 deletions crates/rmf_site_editor/src/site/slotcar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ impl Plugin for SlotcarSdfPlugin {
}
}

/// When loading SDF models, some RobotPropertyKinds may be directly inserted into
/// a model instance descendant instead of the instance or affiliated description.
/// This system checks for such cases and inserts the respective components into
/// the affiliated model description (if there is no other RobotProperty data
/// present) and remove it from the original entity.
fn insert_slotcar_components(
mut commands: Commands,
mut change_robot_property: EventWriter<Change<ModelProperty<Robot>>>,
is_static: Query<&ModelProperty<IsStatic>, (With<ModelMarker>, With<Group>)>,
robot_property_kinds: Query<
// All 4 components will be present (see sdf_loader.rs)
(
Entity,
&DifferentialDrive,
Expand All @@ -57,7 +63,14 @@ fn insert_slotcar_components(
),
(Without<ModelMarker>, Without<Group>),
>,
robot_properties: Query<(&Mobility, &PowerSource), (With<ModelMarker>, With<Group>)>,
robot_properties: Query<
(
Option<&Mobility>,
Option<&PowerSource>,
Option<&PowerDissipation>,
),
(With<ModelMarker>, With<Group>),
>,
model_descriptions: Query<&ModelProperty<Robot>, (With<ModelMarker>, With<Group>)>,
model_instances: ModelPropertyQuery<Robot>,
child_of: Query<&ChildOf>,
Expand All @@ -66,18 +79,11 @@ fn insert_slotcar_components(
robot_property_kinds.iter()
{
if !model_descriptions.get(e).is_ok() {
// A non-description entity has the RobotPropertyKind component, it could have been inserted into a
// model instance descendent when processing importing robot plugins
// Insert this component in the affiliated description and remove it from the original entity
let mut description_entity: Option<Entity> = None;
let mut target_entity: Entity = e;
while let Ok(parent) = child_of.get(target_entity).map(|co| co.parent()) {
if let Some(desc) = model_instances.get(parent).ok().and_then(|a| a.0) {
if !robot_properties.get(desc).is_ok()
&& is_static.get(desc).is_ok_and(|is| !is.0 .0)
{
description_entity = Some(desc);
}
description_entity = Some(desc);
break;
}
target_entity = parent;
Expand All @@ -94,45 +100,61 @@ fn insert_slotcar_components(
// and Robot will result in the later system run to overwrite changes
// from the earlier system. For now we will process data from Battery/
// DifferentialDrive/MechanicalSystem/AmbientSystem in a single system.
if let Ok(mobility_value) = serialize_robot_property_from_kind::<
Mobility,
DifferentialDrive,
>(differential_drive.clone())
{
robot.properties.insert(Mobility::label(), mobility_value);
}

if let Ok(power_source_value) =
serialize_robot_property_from_kind::<PowerSource, Battery>(battery.clone())
{
robot
.properties
.insert(PowerSource::label(), power_source_value);
}
// Only allow overwriting RobotProperties that are not present
let Ok((mobility, power_source, power_dissipation)) = robot_properties.get(desc)
Copy link
Member

Choose a reason for hiding this comment

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

I might be wrong but I can't figure out if this would ever fail. The query only looks for optional components so I suspect it would actually always succeed, if any component is missing it will be set to None itself.
The only case this could possibly fail is if the entity doesn't exist but that's probably not going to happen since it comes from another query.
Is this intentional? What are you trying to test here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I'm trying to figure out if any combination of RobotProperty was already present in the description, and only overwrite components that are not present. So it was intentional to query for optional RobotProperty components for all model descriptions.

But on second thought if a particular component is present in a saved JSON and another is not, the latter is likely meant to be disabled. I'll remove the Option and check for present RobotProperty components as a group.

Copy link
Member Author

Choose a reason for hiding this comment

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

19e9e93, turned it into a QueryFilter instead since we don't need the actual values

Copy link
Member Author

Choose a reason for hiding this comment

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

Note, IsStatic has to be optional else slotcar parameters may not be inserted at all, even if there's no existing params

else {
continue;
};

let mut power_dissipation_config = Map::new();
if let Some(power_dissipation_map) = power_dissipation_config
.entry("config")
.or_insert(Value::Object(Map::new()))
.as_object_mut()
{
if let Ok(mechanical_system_value) =
serialize_robot_property_kind::<MechanicalSystem>(mechanical_system.clone())
if mobility.is_none() && is_static.get(desc).is_ok_and(|is| !is.0 .0) {
if let Ok(mobility_value) = serialize_robot_property_from_kind::<
Mobility,
DifferentialDrive,
>(differential_drive.clone())
{
power_dissipation_map
.insert(MechanicalSystem::label(), mechanical_system_value);
robot.properties.insert(Mobility::label(), mobility_value);
}
if let Ok(ambient_system_value) =
serialize_robot_property_kind::<AmbientSystem>(ambient_system.clone())
}

if power_source.is_none() {
if let Ok(power_source_value) =
serialize_robot_property_from_kind::<PowerSource, Battery>(battery.clone())
{
power_dissipation_map.insert(AmbientSystem::label(), ambient_system_value);
robot
.properties
.insert(PowerSource::label(), power_source_value);
}
}
if !power_dissipation_config.is_empty() {
robot.properties.insert(
PowerDissipation::label(),
Value::Object(power_dissipation_config),
);

if power_dissipation.is_none() {
let mut power_dissipation_config = Map::new();
if let Some(power_dissipation_map) = power_dissipation_config
.entry("config")
.or_insert(Value::Object(Map::new()))
.as_object_mut()
{
if let Ok(mechanical_system_value) =
serialize_robot_property_kind::<MechanicalSystem>(
mechanical_system.clone(),
)
{
power_dissipation_map
.insert(MechanicalSystem::label(), mechanical_system_value);
}
if let Ok(ambient_system_value) =
serialize_robot_property_kind::<AmbientSystem>(ambient_system.clone())
{
power_dissipation_map
.insert(AmbientSystem::label(), ambient_system_value);
}
}
if !power_dissipation_config.is_empty() {
robot.properties.insert(
PowerDissipation::label(),
Value::Object(power_dissipation_config),
);
}
}

change_robot_property.write(Change::new(ModelProperty(robot), desc));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
use super::*;
use crate::{
site::{
recall_plugin::UpdateRecallSet, robot_properties::*, update_model_instances, Change, Group,
IssueKey, ModelMarker, ModelProperty, ModelPropertyQuery, NameInSite, Recall, RecallPlugin,
Robot, SiteUpdateSet,
recall_plugin::UpdateRecallSet, robot_properties::*, Change, Group, IssueKey, ModelMarker,
ModelProperty, ModelPropertyQuery, NameInSite, Recall, RecallPlugin, Robot, SiteUpdateSet,
},
widgets::Inspect,
AppState, Issue, ModelPropertyData, ValidateWorkspace,
AppState, Issue, ValidateWorkspace,
};
use bevy::{
ecs::{component::Mutable, hierarchy::ChildOf, system::SystemParam},
Expand Down Expand Up @@ -64,29 +63,6 @@ impl Plugin for InspectRobotPropertiesPlugin {
fn build(&self, app: &mut App) {
// Allows us to toggle Robot as a configurable property
// from the model description inspector
app.world_mut().register_component::<ModelProperty<Robot>>();
let component_id = app
.world()
.components()
.component_id::<ModelProperty<Robot>>()
.unwrap();
app.add_systems(PreUpdate, update_model_instances::<Robot>)
.init_resource::<ModelPropertyData>()
.world_mut()
.resource_mut::<ModelPropertyData>()
.optional
.insert(
component_id,
(
"Robot".to_string(),
|mut e_cmd| {
e_cmd.insert(ModelProperty::<Robot>::default());
},
|mut e_cmd| {
e_cmd.remove::<ModelProperty<Robot>>();
},
),
);
let inspector = app.world().resource::<ModelDescriptionInspector>().id;
let widget = Widget::<Inspect>::new::<InspectRobotProperties>(app.world_mut());
let id = app
Expand All @@ -98,7 +74,7 @@ impl Plugin for InspectRobotPropertiesPlugin {
.insert_resource(RobotPropertiesInspector { id });
app.world_mut()
.init_resource::<RobotPropertyWidgetRegistry>();
app.add_event::<UpdateRobotPropertyKinds>().add_systems(
app.add_systems(
PreUpdate,
check_for_invalid_robot_property_kinds
.after(SiteUpdateSet::ProcessChangesFlush)
Expand Down
Loading
Loading