Skip to content

Commit

Permalink
[core] Add velocity-torque slope when approaching maximum torque.
Browse files Browse the repository at this point in the history
  • Loading branch information
duburcqa committed May 23, 2024
1 parent f8f34db commit b62a026
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions core/include/jiminy/core/hardware/basic_motors.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace jiminy
GenericConfig config = AbstractMotorBase::getDefaultMotorOptions();

config["enableVelocityLimit"] = false;
config["velocityEffortInvSlope"] = 0.0;
config["enableEffortLimit"] = true;
config["enableFriction"] = false;
config["frictionViscousPositive"] = 0.0;
Expand All @@ -33,6 +34,10 @@ namespace jiminy
{
/// \brief Wether velocity limit is enabled.
const bool enableVelocityLimit;
/// \brief Inverse constant decrease rate of the maximum torque wrt velocity when
/// approaching the maximum velocity. The maximum torque is equal to zero at
/// maximum velocity.
const double velocityEffortInvSlope;
/// \brief Wether effort limit is enabled.
const bool enableEffortLimit;
/// \brief Wether joint friction is enabled.
Expand Down Expand Up @@ -63,6 +68,7 @@ namespace jiminy
SimpleMotorOptions(const GenericConfig & options) :
AbstractMotorOptions(options),
enableVelocityLimit(boost::get<bool>(options.at("enableVelocityLimit"))),
velocityEffortInvSlope{boost::get<double>(options.at("velocityEffortInvSlope"))},
enableEffortLimit(boost::get<bool>(options.at("enableEffortLimit"))),
enableFriction{boost::get<bool>(options.at("enableFriction"))},
frictionViscousPositive{boost::get<double>(options.at("frictionViscousPositive"))},
Expand Down
14 changes: 6 additions & 8 deletions core/src/hardware/basic_motors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ namespace jiminy
effortMax = effortLimit_;
if (motorOptions_->enableVelocityLimit)
{
if (vMotor < -velocityLimit_)
{
effortMin = 0.0;
}
if (vMotor > velocityLimit_)
{
effortMax = 0.0;
}
const double velocityThr = std::max(
velocityLimit_ - effortLimit_ * motorOptions_->velocityEffortInvSlope, 0.0);
effortMin *= std::clamp(
(velocityLimit_ + vMotor) / (velocityLimit_ - velocityThr), 0.0, 1.0);
effortMax *= std::clamp(
(velocityLimit_ - vMotor) / (velocityLimit_ - velocityThr), 0.0, 1.0);
}
}
data() = motorOptions_->mechanicalReduction * std::clamp(command, effortMin, effortMax);
Expand Down
2 changes: 2 additions & 0 deletions python/gym_jiminy/envs/gym_jiminy/envs/acrobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def _initialize_observation_space(self) -> None:
"""
state_space = self._get_agent_state_space(use_theoretical_model=True)
position_space, velocity_space = state_space['q'], state_space['v']
assert isinstance(position_space, gym.spaces.Box)
assert isinstance(velocity_space, gym.spaces.Box)
self.observation_space = gym.spaces.Box(

Check warning on line 162 in python/gym_jiminy/envs/gym_jiminy/envs/acrobot.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

python/gym_jiminy/envs/gym_jiminy/envs/acrobot.py#L162

Attribute 'observation_space' defined outside __init__
low=np.concatenate((position_space.low, velocity_space.low)),
high=np.concatenate((position_space.high, velocity_space.high)),
Expand Down
2 changes: 2 additions & 0 deletions python/gym_jiminy/rllib/gym_jiminy/rllib/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ def loss(self,
train_batch: SampleBatch) -> Union[TensorType, List[TensorType]]:
"""Compute PPO loss with additional regularizations.
"""
# pylint: disable=possibly-used-before-assignment

with torch.no_grad():
# Extract some proxies from convenience
observation_true = train_batch["obs"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def __init__(self,
) -> None:
""" TODO: Write documentation.
"""
# pylint: disable=possibly-used-before-assignment

# Call base implementation
super().__init__(env)

Expand Down

0 comments on commit b62a026

Please sign in to comment.