Skip to content

Commit

Permalink
added link velocity components and changes to physics systems
Browse files Browse the repository at this point in the history
Signed-off-by: yaswanth1701 <[email protected]>
  • Loading branch information
yaswanth1701 committed Jul 22, 2024
1 parent 3c2a3ce commit f3244fa
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 43 deletions.
54 changes: 54 additions & 0 deletions include/gz/sim/components/AngularVelocityReset.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024 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.
*
*/
#ifndef GZ_SIM_COMPONENTS_WORLDANGULARVELOCITYRESET_HH_
#define GZ_SIM_COMPONENTS_WORLDANGULARELOCITYRESET_HH_

#include <gz/math/Vector3.hh>
#include <gz/sim/components/Component.hh>
#include <gz/sim/components/Factory.hh>
#include <gz/sim/components/Serialization.hh>
#include <gz/sim/config.hh>

namespace gz
{
namespace sim
{
// Inline bracket to help doxygen filtering.
inline namespace GZ_SIM_VERSION_NAMESPACE {
namespace components
{
/// \brief Link initial angular velocity in its own frame and
/// in SI units (rad/s).The angular velocity of entity is
/// represented by gz::math::vector3d.
using AngularVelocityReset = Component<math::Vector3d,
class AngularVelocityResetTag>;
GZ_SIM_REGISTER_COMPONENT(
"gz_sim_components.AngularVelocityReset", AngularVelocityReset)

/// \brief Link initial angular velocity in world frame and
/// in SI units (rad/s). The angular velocity of entity is
// represented by gz::math::vector3d.
using WorldAngularVelocityReset = Component<math::Vector3d,
class WorldAngularVelocityResetTag>;
GZ_SIM_REGISTER_COMPONENT(
"gz_sim_components.WorldAngularVelocityReset", WorldAngularVelocityReset)
}
}
}
}

#endif
54 changes: 54 additions & 0 deletions include/gz/sim/components/LinearVelocityReset.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024 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.
*
*/
#ifndef GZ_SIM_COMPONENTS_WORLDLINEARVELOCITYRESET_HH_
#define GZ_SIM_COMPONENTS_WORLDLINEARVELOCITYRESET_HH_

#include <gz/math/Vector3.hh>
#include <gz/sim/components/Component.hh>
#include <gz/sim/components/Factory.hh>
#include <gz/sim/components/Serialization.hh>
#include <gz/sim/config.hh>

namespace gz
{
namespace sim
{
// Inline bracket to help doxygen filtering.
inline namespace GZ_SIM_VERSION_NAMESPACE {
namespace components
{
/// \brief Entity initial linear velocity in its own frame and
/// in SI units (m/s). The linear velocity of entity is
/// represented by gz::math::vector3d.
using LinearVelocityReset = Component<math::Vector3d ,
class LinearVelocityResetTag>;
GZ_SIM_REGISTER_COMPONENT(
"gz_sim_components.LinearVelocityReset", LinearVelocityReset)

/// \brief Entity initial linear velocity in world frame and
/// in SI units (m/s). The linear velocity of entity is
/// represented by gz::math::vector3d.
using WorldLinearVelocityReset = Component<math::Vector3d ,
class WorldLinearVelocityResetTag>;
GZ_SIM_REGISTER_COMPONENT(
"gz_sim_components.WorldLinearVelocityReset", WorldLinearVelocityReset)
}
}
}
}

#endif
209 changes: 166 additions & 43 deletions src/systems/physics/Physics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#include "gz/sim/components/AngularAcceleration.hh"
#include "gz/sim/components/AngularVelocity.hh"
#include "gz/sim/components/AngularVelocityCmd.hh"
#include "gz/sim/components/AngularVelocityReset.hh"
#include "gz/sim/components/AxisAlignedBox.hh"
#include "gz/sim/components/BatterySoC.hh"
#include "gz/sim/components/CanonicalLink.hh"
Expand All @@ -123,6 +124,7 @@
#include "gz/sim/components/LinearAcceleration.hh"
#include "gz/sim/components/LinearVelocity.hh"
#include "gz/sim/components/LinearVelocityCmd.hh"
#include "gz/sim/components/LinearVelocityReset.hh"
#include "gz/sim/components/Link.hh"
#include "gz/sim/components/Model.hh"
#include "gz/sim/components/Name.hh"
Expand Down Expand Up @@ -2643,6 +2645,123 @@ void PhysicsPrivate::UpdatePhysics(EntityComponentManager &_ecm)
return true;
});

// Set initial link linear velocity
_ecm.Each<components::Link, components::WorldLinearVelocityReset>(

Check warning on line 2649 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2649

Added line #L2649 was not covered by tests
[&](const Entity &_entity, const components::Link *,
const components::WorldLinearVelocityReset *_worldlinearvelocityreset)
{
if (!this->entityLinkMap.HasEntity(_entity))
{
gzwarn << "Failed to find link [" << _entity
<< "]." << std::endl;
return true;
}

auto linkPtrPhys = this->entityLinkMap.Get(_entity);
if (nullptr == linkPtrPhys)
return true;

auto freeGroup = linkPtrPhys->FindFreeGroup();
if (!freeGroup)
return true;

auto rootLinkPtr = freeGroup->RootLink();
if(rootLinkPtr != linkPtrPhys)
{
gzwarn << "Attempting to set linear velocity for link [ " << _entity
<< " ] which is not root link of the FreeGroup."
<< "Velocity won't be set." << std::endl;

Check warning on line 2674 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2672-L2674

Added lines #L2672 - L2674 were not covered by tests
return true;
}

this->entityFreeGroupMap.AddEntity(_entity, freeGroup);

Check warning on line 2679 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2679

Added line #L2679 was not covered by tests
auto worldLinearVelFeature =
this->entityFreeGroupMap
.EntityCast<WorldVelocityCommandFeatureList>(_entity);
if (!worldLinearVelFeature)

Check warning on line 2683 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2683

Added line #L2683 was not covered by tests
{
static bool informed{false};
if (!informed)
{
gzdbg << "Attempting to set link linear velocity, but the "
<< "physics engine doesn't support velocity commands. "
<< "Velocity won't be set."
<< std::endl;
informed = true;

Check warning on line 2692 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2692

Added line #L2692 was not covered by tests
}
return true;

Check warning on line 2694 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2694

Added line #L2694 was not covered by tests
}

// Linear velocity in world frame
math::Vector3d worldLinearVel = _worldlinearvelocityreset->Data();

Check warning on line 2698 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2696-L2698

Added lines #L2696 - L2698 were not covered by tests

worldLinearVelFeature->SetWorldLinearVelocity(

Check warning on line 2700 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2700

Added line #L2700 was not covered by tests
math::eigen3::convert(worldLinearVel));

return true;
});

// Set initial link angular velocity
_ecm.Each<components::Link, components::WorldAngularVelocityReset>(
[&](const Entity &_entity, const components::Link *,
const components::WorldAngularVelocityReset
*_worldangularvelocityreset)
{
if (!this->entityLinkMap.HasEntity(_entity))
{
gzwarn << "Failed to find link [" << _entity
<< "]." << std::endl;
return true;
}

auto linkPtrPhys = this->entityLinkMap.Get(_entity);

Check warning on line 2719 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2719

Added line #L2719 was not covered by tests
if (nullptr == linkPtrPhys)
return true;

Check warning on line 2722 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2722

Added line #L2722 was not covered by tests
auto freeGroup = linkPtrPhys->FindFreeGroup();
if (!freeGroup)
return true;

Check warning on line 2726 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2724-L2726

Added lines #L2724 - L2726 were not covered by tests
auto rootLinkPtr = freeGroup->RootLink();
if(rootLinkPtr != linkPtrPhys)
{
gzwarn << "Attempting to set angular velocity for link [ " << _entity
<< " ] which is not root link of the FreeGroup."

Check warning on line 2731 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2729-L2731

Added lines #L2729 - L2731 were not covered by tests
<< "Velocity won't be set." << std::endl;

return true;
}

Check warning on line 2735 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2733-L2735

Added lines #L2733 - L2735 were not covered by tests

this->entityFreeGroupMap.AddEntity(_entity, freeGroup);

Check warning on line 2738 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2737-L2738

Added lines #L2737 - L2738 were not covered by tests
auto worldAngularVelFeature =
this->entityFreeGroupMap

Check warning on line 2740 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2740

Added line #L2740 was not covered by tests
.EntityCast<WorldVelocityCommandFeatureList>(_entity);

Check warning on line 2742 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2742

Added line #L2742 was not covered by tests
if (!worldAngularVelFeature)
{

Check warning on line 2744 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2744

Added line #L2744 was not covered by tests
static bool informed{false};
if (!informed)
{

Check warning on line 2747 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2747

Added line #L2747 was not covered by tests
gzdbg << "Attempting to set link angular velocity, but the "
<< "physics engine doesn't support velocity commands. "
<< "Velocity won't be set."
<< std::endl;
informed = true;

Check warning on line 2752 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2751-L2752

Added lines #L2751 - L2752 were not covered by tests
}
return true;
}

Check warning on line 2755 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2755

Added line #L2755 was not covered by tests
// Angular velocity in world frame
math::Vector3d worldAngularVel = _worldangularvelocityreset->Data();

Check warning on line 2757 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2757

Added line #L2757 was not covered by tests

worldAngularVelFeature->SetWorldAngularVelocity(
math::eigen3::convert(worldAngularVel));

Check warning on line 2761 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2759-L2761

Added lines #L2759 - L2761 were not covered by tests
return true;
});

Check warning on line 2763 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L2763

Added line #L2763 was not covered by tests


// Populate bounding box info
// Only compute bounding box if component exists to avoid unnecessary
Expand Down Expand Up @@ -3578,6 +3697,34 @@ void PhysicsPrivate::UpdateSim(EntityComponentManager &_ecm,
_ecm.RemoveComponent<components::JointVelocityReset>(entity);
}

std::vector<Entity> entitiesLinearVelocityReset;
_ecm.Each<components::WorldLinearVelocityReset>(
[&](const Entity &_entity,
components::WorldLinearVelocityReset *) -> bool
{
entitiesLinearVelocityReset.push_back(_entity);
return true;
});

for (const auto entity : entitiesLinearVelocityReset)
{
_ecm.RemoveComponent<components::WorldLinearVelocityReset>(entity);
}

std::vector<Entity> entitiesAngularVelocityReset;
_ecm.Each<components::WorldAngularVelocityReset>(
[&](const Entity &_entity,
components::WorldAngularVelocityReset *) -> bool
{
entitiesAngularVelocityReset.push_back(_entity);
return true;
});

for (const auto entity : entitiesAngularVelocityReset)
{
_ecm.RemoveComponent<components::WorldAngularVelocityReset>(entity);
}

std::vector<Entity> entitiesCustomContactSurface;
_ecm.Each<components::EnableContactSurfaceCustomization>(
[&](const Entity &_entity,
Expand Down Expand Up @@ -3628,58 +3775,34 @@ void PhysicsPrivate::UpdateSim(EntityComponentManager &_ecm,
return true;
});

{
std::vector<Entity> entitiesJointVelocityCmd;
_ecm.Each<components::JointVelocityCmd>(
[&](const Entity &_entity, components::JointVelocityCmd *) -> bool
{
entitiesJointVelocityCmd.push_back(_entity);
return true;
});

for (const auto entity : entitiesJointVelocityCmd)
{
_ecm.RemoveComponent<components::JointVelocityCmd>(entity);
}
}
_ecm.Each<components::JointVelocityCmd>(
[&](const Entity &, components::JointVelocityCmd *_vel) -> bool
{

Check warning on line 3780 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L3780

Added line #L3780 was not covered by tests
std::fill(_vel->Data().begin(), _vel->Data().end(), 0.0);
return true;
});

_ecm.Each<components::SlipComplianceCmd>(
[&](const Entity &, components::SlipComplianceCmd *_slip) -> bool
{
std::fill(_slip->Data().begin(), _slip->Data().end(), 0.0);
return true;
});
GZ_PROFILE_END();

{
std::vector<Entity> entitiesAngularVelocityCmd;
_ecm.Each<components::AngularVelocityCmd>(
[&](const Entity &_entity, components::AngularVelocityCmd *) -> bool
{
entitiesAngularVelocityCmd.push_back(_entity);
return true;
});

for (const auto entity : entitiesAngularVelocityCmd)
{
_ecm.RemoveComponent<components::AngularVelocityCmd>(entity);
}
}

{
std::vector<Entity> entitiesLinearVelocityCmd;
_ecm.Each<components::LinearVelocityCmd>(
[&](const Entity &_entity, components::LinearVelocityCmd *) -> bool
{
entitiesLinearVelocityCmd.push_back(_entity);
return true;
});
_ecm.Each<components::AngularVelocityCmd>(
[&](const Entity &, components::AngularVelocityCmd *_vel) -> bool

Check warning on line 3794 in src/systems/physics/Physics.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/physics/Physics.cc#L3794

Added line #L3794 was not covered by tests
{
_vel->Data() = math::Vector3d::Zero;
return true;
});

for (const auto entity : entitiesLinearVelocityCmd)
{
_ecm.RemoveComponent<components::LinearVelocityCmd>(entity);
}
}
GZ_PROFILE_END();
_ecm.Each<components::LinearVelocityCmd>(
[&](const Entity &, components::LinearVelocityCmd *_vel) -> bool
{
_vel->Data() = math::Vector3d::Zero;
return true;
});

// Update joint positions
GZ_PROFILE_BEGIN("Joints");
Expand Down

0 comments on commit f3244fa

Please sign in to comment.