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

Set link velocity from set_model_state plugin #2440

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b062cc1
modified velocity reset components
yaswanth1701 Jun 6, 2024
bb213b5
updated physic plugin to set initial link velocities
yaswanth1701 Jun 6, 2024
53e2b33
updated set_model_state plugin for creating link velocity resert comp…
yaswanth1701 Jun 6, 2024
d46c143
updated model state sdf to test link velocity reset component
yaswanth1701 Jun 6, 2024
5b73d20
added root link check for link
yaswanth1701 Jun 8, 2024
72d75f9
pre commit hooks
yaswanth1701 Jun 8, 2024
7baa3b8
fixed line length
yaswanth1701 Jun 8, 2024
116a728
minor changes
yaswanth1701 Jun 8, 2024
327eaa3
added inertia to box in set_model_state sdf
yaswanth1701 Jun 8, 2024
9f3555d
minor changes
yaswanth1701 Jun 9, 2024
1a390c0
minor changes
yaswanth1701 Jun 9, 2024
8e730cc
minor changes
yaswanth1701 Jun 9, 2024
3db0e98
resolved formatting comments
yaswanth1701 Jun 11, 2024
b9e559b
resolved linting error
yaswanth1701 Jun 11, 2024
b8deab3
added flag to account for reset components only once
yaswanth1701 Jun 17, 2024
778b2fb
added zero gravity to sdf
yaswanth1701 Jun 17, 2024
0066023
fixed linting errors
yaswanth1701 Jun 19, 2024
37b026c
removed reset components in updatesim physics.cc
yaswanth1701 Jun 22, 2024
4cebd44
fixes linting errors
yaswanth1701 Jun 22, 2024
44554a2
added degree option for angular velocity in set_model_state system
yaswanth1701 Jun 30, 2024
b335da9
resolved linting errors
yaswanth1701 Jun 30, 2024
51f04cc
added some comments to sdf files
yaswanth1701 Jun 30, 2024
3dd6404
changes to degree to radian conversion
yaswanth1701 Jul 11, 2024
cdee544
fixed linting errors
yaswanth1701 Jul 11, 2024
c9d6690
fixed linting errors
yaswanth1701 Jul 11, 2024
6cf3856
minor changes
yaswanth1701 Aug 8, 2024
9de189c
Merge remote-tracking branch 'origin/scpeters/set_model_state_prototy…
scpeters Aug 15, 2024
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
88 changes: 88 additions & 0 deletions examples/worlds/set_model_state.sdf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

The initial joint position should be 90 degrees, and the initial joint
velocity should be -30 degrees / second.

The initial linear velocity should be (0.9, 0.4, 0.1) m/s for the first box
and (0.9, -0.4, 0.1) for the second box, and initial angular velocity should be
(0.1, 5, 0.1) rad/s for both.

-->
<sdf version="1.11">
<world name="set_model_state">
<gravity>0 0 0</gravity>
<plugin
filename="gz-sim-physics-system"
name="gz::sim::systems::Physics">
Expand Down Expand Up @@ -208,5 +214,87 @@
</model_state>
</plugin>
</model>

<model name="model_state_example_link_velocity_radians">
<pose>2 0 1 0 0 0</pose>
<link name="box_link">
<pose>0.0 0.0 0.0 0 0 0</pose>
<inertial auto="true">
<density>277.7</density>
</inertial>
<visual name="box_visual">
<pose>0.0 0.0 0.0 0 0 0</pose>
<geometry>
<box>
<size>0.1 0.4 0.9</size>
</box>
</geometry>
<material>
<ambient>1.0 0.0 0.0 1</ambient>
<diffuse>1.0 0.0 0.0 1</diffuse>
<specular>1.0 0.0 0.0 1</specular>
</material>
</visual>
<collision name="box_collision">
<pose>0.0 0.0 0.0 0 0 0</pose>
<geometry>
<box>
<size>0.1 0.4 0.9</size>
</box>
</geometry>
</collision>
</link>
<plugin
filename="gz-sim-set-model-state-system"
name="gz::sim::systems::SetModelState">
<model_state>
<link_state name="box_link">
<linear_velocity>0.9 0.4 0.1</linear_velocity>
<angular_velocity degrees="false">0.1 5 0.1</angular_velocity>
</link_state>
</model_state>
</plugin>
</model>

<model name="model_state_example_link_velocity_degrees">
<pose>-1 0 1 0 0 0</pose>
<link name="box_link">
<pose>0.0 0.0 0.0 0 0 0</pose>
<inertial auto="true">
<density>277.7</density>
</inertial>
<visual name="box_visual">
<pose>0.0 0.0 0.0 0 0 0</pose>
<geometry>
<box>
<size>0.1 0.4 0.9</size>
</box>
</geometry>
<material>
<ambient>1.0 0.0 0.0 1</ambient>
<diffuse>1.0 0.0 0.0 1</diffuse>
<specular>1.0 0.0 0.0 1</specular>
</material>
</visual>
<collision name="box_collision">
<pose>0.0 0.0 0.0 0 0 0</pose>
<geometry>
<box>
<size>0.1 0.4 0.9</size>
</box>
</geometry>
</collision>
</link>
<plugin
filename="gz-sim-set-model-state-system"
name="gz::sim::systems::SetModelState">
<model_state>
<link_state name="box_link">
<linear_velocity>0.9 -0.4 0.1</linear_velocity>
<angular_velocity degrees="true">5.73 286.479 5.73</angular_velocity>
</link_state>
</model_state>
</plugin>
</model>
</world>
</sdf>
81 changes: 81 additions & 0 deletions src/systems/set_model_state/SetModelState.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@

#include <gz/common/Profiler.hh>
#include <gz/math/Angle.hh>
#include <gz/math/Vector3.hh>
#include <gz/plugin/Register.hh>

#include "gz/sim/components/JointAxis.hh"
#include "gz/sim/components/JointPositionReset.hh"
#include "gz/sim/components/JointVelocityReset.hh"
#include "gz/sim/components/LinearVelocityReset.hh"
#include "gz/sim/components/AngularVelocityReset.hh"
#include "gz/sim/Model.hh"
#include "gz/sim/Util.hh"

Expand Down Expand Up @@ -60,6 +63,32 @@ namespace
}
return false;
}

bool parseVectorWithDegree(math::Vector3d &vector, sdf::ElementConstPtr _elem)
{
if(_elem)
{
// parse degree attribute, default false
std::pair<bool, bool> degreesPair = _elem->Get<bool>("degrees", false);
// parse element vector, default math::Vector3d::Zero
std::pair<math::Vector3d, bool> vectorPair = _elem->Get<math::Vector3d>
("", math::Vector3d::Zero);
if(vectorPair.second)
{
if(degreesPair.first)
{
vector.Set(GZ_DTOR(vectorPair.first.X()),
GZ_DTOR(vectorPair.first.Y()), GZ_DTOR(vectorPair.first.Z()));
}
else
{
vector = vectorPair.first;
}
return true;
}
}
return false;
}
}

class gz::sim::systems::SetModelStatePrivate
Expand Down Expand Up @@ -222,6 +251,58 @@ void SetModelState::Configure(const Entity &_entity,
jointVelocity);
}
}
for (auto linkStateElem = modelStateElem->FindElement("link_state");
linkStateElem != nullptr;
linkStateElem = linkStateElem->GetNextElement("link_state"))
{

std::pair<std::string, bool> namePair =
linkStateElem->Get<std::string>("name", "");
if (!namePair.second)
{
gzerr << "No name specified for link_state, skipping.\n";
continue;
}
const auto &linkName = namePair.first;

Entity linkEntity = this->dataPtr->model.LinkByName(_ecm, linkName);
if (linkEntity == kNullEntity)
{
gzerr << "Unable to find link with name [" << linkName << "] "
<< "in model with name [" << modelName << "], skipping.\n";
continue;
}

math::Vector3d linearVelocity;
auto linearVelocityElem = linkStateElem->FindElement("linear_velocity");

if(linearVelocityElem)
{
std::pair<math::Vector3d, bool> vectorPair =
linearVelocityElem->Get<math::Vector3d>("", math::Vector3d::Zero);
if (vectorPair.second)
{
linearVelocity = vectorPair.first;
_ecm.SetComponentData<components::WorldLinearVelocityReset>(
linkEntity, linearVelocity);
}
}

math::Vector3d angularVelocity;
auto angularVelocityElem =
linkStateElem->FindElement("angular_velocity");

if(angularVelocityElem){
bool parsedVector = parseVectorWithDegree(angularVelocity,
angularVelocityElem);
if(parsedVector)
{
_ecm.SetComponentData<components::WorldAngularVelocityReset>(
linkEntity, angularVelocity);
}
}
}
// \TODO(yaswanth1701) set reset velocity components in body frame.
}

//////////////////////////////////////////////////
Expand Down
Loading