-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
differential_inverse_kinematics_integrator.cc
190 lines (165 loc) · 7.29 KB
/
differential_inverse_kinematics_integrator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "drake/multibody/inverse_kinematics/differential_inverse_kinematics_integrator.h" // noqa
#include "drake/common/text_logging.h"
#include "drake/math/rigid_transform.h"
#include "drake/systems/framework/basic_vector.h"
namespace drake {
namespace multibody {
using systems::Context;
DifferentialInverseKinematicsIntegrator::
DifferentialInverseKinematicsIntegrator(
const MultibodyPlant<double>& robot,
const Frame<double>& frame_E, double time_step,
const DifferentialInverseKinematicsParameters& parameters,
const Context<double>* context, bool log_only_when_result_state_changes)
: robot_(robot),
frame_E_(frame_E),
parameters_(parameters),
time_step_(time_step) {
parameters_.set_time_step(time_step);
X_WE_desired_index_ = this->DeclareAbstractInputPort(
"X_WE_desired", Value<math::RigidTransformd>{})
.get_index();
robot_state_index_ =
this->DeclareVectorInputPort("robot_state", robot.num_multibody_states())
.get_index();
use_robot_state_index_ =
this->DeclareAbstractInputPort("use_robot_state", Value<bool>{})
.get_index();
this->DeclarePeriodicDiscreteUpdateEvent(
time_step, 0, &DifferentialInverseKinematicsIntegrator::Integrate);
this->DeclareDiscreteState(robot.num_positions());
if (log_only_when_result_state_changes) {
this->DeclareDiscreteState(Vector1d(static_cast<double>(
DifferentialInverseKinematicsStatus::kSolutionFound)));
}
this->DeclareVectorOutputPort(
"joint_positions", robot.num_positions(),
&DifferentialInverseKinematicsIntegrator::CopyPositionsOut,
{all_state_ticket()});
this->DeclareInitializationDiscreteUpdateEvent(
&DifferentialInverseKinematicsIntegrator::Initialize);
// We keep a Context for the MultibodyPlant as a cache entry for use in
// evaluating the kinematics.
auto robot_context = robot.CreateDefaultContext();
if (context) {
robot_.ValidateContext(*context);
robot_context->SetTimeStateAndParametersFrom(*context);
}
robot_context_cache_entry_ = &this->DeclareCacheEntry(
"robot context", *robot_context,
&DifferentialInverseKinematicsIntegrator::UpdateRobotContext);
}
void DifferentialInverseKinematicsIntegrator::SetPositions(
Context<double>* context,
const Eigen::Ref<const Eigen::VectorXd>& positions) const {
DRAKE_DEMAND(positions.size() == robot_.num_positions());
context->SetDiscreteState(0, positions);
}
math::RigidTransformd
DifferentialInverseKinematicsIntegrator::ForwardKinematics(
const Context<double>& context) const {
const Context<double>& robot_context =
robot_context_cache_entry_->Eval<Context<double>>(context);
return robot_.EvalBodyPoseInWorld(robot_context, frame_E_.body()) *
frame_E_.CalcPoseInBodyFrame(robot_context);
}
const DifferentialInverseKinematicsParameters&
DifferentialInverseKinematicsIntegrator::get_parameters() const {
return parameters_;
}
DifferentialInverseKinematicsParameters&
DifferentialInverseKinematicsIntegrator::get_mutable_parameters() {
return parameters_;
}
void DifferentialInverseKinematicsIntegrator::UpdateRobotContext(
const Context<double>& context,
Context<double>* robot_context) const {
if (this->get_input_port(robot_state_index_).HasValue(context) &&
this->get_input_port(use_robot_state_index_).HasValue(context) &&
this->get_input_port(use_robot_state_index_).Eval<bool>(context)) {
robot_.SetPositions(robot_context, this->get_input_port(robot_state_index_)
.Eval(context)
.head(robot_.num_positions()));
} else {
robot_.SetPositions(robot_context,
context.get_discrete_state(0).get_value());
}
}
systems::EventStatus DifferentialInverseKinematicsIntegrator::Integrate(
const Context<double>& context,
systems::DiscreteValues<double>* discrete_state) const {
const AbstractValue* input =
this->EvalAbstractInput(context, X_WE_desired_index_);
DRAKE_DEMAND(input != nullptr);
DRAKE_THROW_UNLESS(parameters_.get_time_step() == time_step_);
const math::RigidTransformd& X_WE_desired =
input->get_value<math::RigidTransformd>();
const Context<double>& robot_context =
robot_context_cache_entry_->Eval<Context<double>>(context);
DifferentialInverseKinematicsResult result = DoDifferentialInverseKinematics(
robot_, robot_context, X_WE_desired, frame_E_, parameters_);
const auto& positions = robot_.GetPositions(robot_context);
if (result.status == DifferentialInverseKinematicsStatus::kNoSolutionFound) {
if (this->num_discrete_state_groups() == 1) {
drake::log()->warn(
"Differential IK could not find a solution at time {}.",
context.get_time());
}
discrete_state->set_value(0, positions);
} else {
discrete_state->set_value(
0, positions + time_step_ * result.joint_velocities.value());
}
if (this->num_discrete_state_groups() > 1) {
// Note: Casting to double here is ugly, but it avoids significant
// additional complexity (of declaring abstract state, and a CacheEntry to
// share the update from the diff IK solution across multiple components of
// the Context, etc). We only cast from the enum to double, and avoid the
// other direction.
const double status_quo = discrete_state->get_vector(1).GetAtIndex(0);
discrete_state->get_mutable_vector(1).SetAtIndex(
0, static_cast<double>(result.status));
// We only assume that the equality of two enums can be tested on the
// converted double; we make no assumption about the value of the cast.
if (static_cast<double>(result.status) != status_quo) {
switch (result.status) {
case DifferentialInverseKinematicsStatus::kSolutionFound:
drake::log()->warn(
"Differential IK started finding solutions again at time {}.",
context.get_time());
break;
case DifferentialInverseKinematicsStatus::kNoSolutionFound:
drake::log()->warn(
"Differential IK started returning status:\"no solution\" at "
"time {}.",
context.get_time());
break;
case DifferentialInverseKinematicsStatus::kStuck:
drake::log()->warn(
"Differential IK started returning status:\"stuck\" at time {}.",
context.get_time());
break;
}
}
}
return systems::EventStatus::Succeeded();
}
void DifferentialInverseKinematicsIntegrator::CopyPositionsOut(
const Context<double>& context,
systems::BasicVector<double>* output) const {
output->SetFrom(context.get_discrete_state(0));
}
systems::EventStatus DifferentialInverseKinematicsIntegrator::Initialize(
const systems::Context<double>& context,
systems::DiscreteValues<double>* discrete_state) const {
if (this->get_input_port(robot_state_index_).HasValue(context)) {
Eigen::VectorXd state =
this->get_input_port(robot_state_index_).Eval(context);
DRAKE_DEMAND(state.size() == robot_.num_multibody_states());
discrete_state->set_value(0, state.head(robot_.num_positions()));
return systems::EventStatus::Succeeded();
}
return systems::EventStatus::DidNothing();
}
} // namespace multibody
} // namespace drake