forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotation_constraint.cc
176 lines (144 loc) · 6.91 KB
/
rotation_constraint.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
#include "drake/solvers/rotation_constraint.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <limits>
using std::numeric_limits;
using drake::symbolic::Expression;
using Eigen::VectorXd;
using Eigen::MatrixXi;
namespace drake {
namespace solvers {
MatrixDecisionVariable<3, 3> NewRotationMatrixVars(MathematicalProgram* prog,
const std::string& name) {
MatrixDecisionVariable<3, 3> R = prog->NewContinuousVariables<3, 3>(name);
// Forall i,j, -1 <= R(i,j) <=1.
prog->AddBoundingBoxConstraint(-1, 1, R);
// -1 <= trace(R) <= 3.
// Proof sketch:
// orthonormal => |lambda_i|=1.
// R is real => eigenvalues either real or appear in complex conj pairs.
// Case 1: All real (lambda_i \in {-1,1}).
// det(R)=lambda_1*lambda_2*lambda_3=1 => lambda_1=lambda_2, lambda_3=1.
// Case 2: Two imaginary, pick conj(lambda_1) = lambda_2.
// => lambda_1*lambda_2 = 1. => lambda_3 = 1.
// and also => lambda_1 + lambda_2 = 2*Re(lambda_1) \in [-2,2].
prog->AddLinearConstraint(Eigen::RowVector3d::Ones(), -1, 3, R.diagonal());
return R;
}
void AddBoundingBoxConstraintsImpliedByRollPitchYawLimits(
MathematicalProgram* prog,
const Eigen::Ref<const MatrixDecisionVariable<3, 3>>& R,
RollPitchYawLimits limits) {
// Based on the RPY to Rotation Matrix conversion:
// [ cp*cy, cy*sp*sr - cr*sy, sr*sy + cr*cy*sp]
// [ cp*sy, cr*cy + sp*sr*sy, cr*sp*sy - cy*sr]
// [ -sp, cp*sr, cp*cr]
// where cz = cos(z) and sz = sin(z), and using
// kRoll_NegPI_2_to_PI_2 = 1 << 1, // => cos(r)>=0
// kRoll_0_to_PI = 1 << 2, // => sin(r)>=0
// kPitch_NegPI_2_to_PI_2 = 1 << 3, // => cos(p)>=0
// kPitch_0_to_PI = 1 << 4, // => sin(p)>=0
// kYaw_NegPI_2_to_PI_2 = 1 << 5, // => cos(y)>=0
// kYaw_0_to_PI = 1 << 6, // => sin(y)>=0
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kYaw_NegPI_2_to_PI_2))
prog->AddBoundingBoxConstraint(0, 1, R(0, 0));
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kYaw_0_to_PI))
prog->AddBoundingBoxConstraint(0, 1, R(1, 0));
if (limits & kPitch_0_to_PI) prog->AddBoundingBoxConstraint(-1, 0, R(2, 0));
if ((limits & kRoll_NegPI_2_to_PI_2) && (limits & kYaw_NegPI_2_to_PI_2) &&
(limits & kPitch_0_to_PI) && (limits & kRoll_0_to_PI) &&
(limits & kYaw_0_to_PI))
prog->AddBoundingBoxConstraint(0, 1, R(1, 1));
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kRoll_0_to_PI))
prog->AddBoundingBoxConstraint(0, 1, R(2, 1));
if ((limits & kRoll_0_to_PI) && (limits & kYaw_0_to_PI) &&
(limits & kRoll_NegPI_2_to_PI_2) && (limits & kYaw_NegPI_2_to_PI_2) &&
(limits & kPitch_0_to_PI))
prog->AddBoundingBoxConstraint(0, 1, R(0, 2));
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kRoll_NegPI_2_to_PI_2))
prog->AddBoundingBoxConstraint(0, 1, R(2, 2));
}
void AddBoundingBoxConstraintsImpliedByRollPitchYawLimitsToBinary(
MathematicalProgram* prog,
const Eigen::Ref<const MatrixDecisionVariable<3, 3>>& B,
RollPitchYawLimits limits) {
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kYaw_NegPI_2_to_PI_2))
prog->AddBoundingBoxConstraint(1, 1, B(0, 0));
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kYaw_0_to_PI))
prog->AddBoundingBoxConstraint(1, 1, B(1, 0));
if (limits & kPitch_0_to_PI) prog->AddBoundingBoxConstraint(0, 0, B(2, 0));
if ((limits & kRoll_NegPI_2_to_PI_2) && (limits & kYaw_NegPI_2_to_PI_2) &&
(limits & kPitch_0_to_PI) && (limits & kRoll_0_to_PI) &&
(limits & kYaw_0_to_PI))
prog->AddBoundingBoxConstraint(1, 1, B(1, 1));
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kRoll_0_to_PI))
prog->AddBoundingBoxConstraint(1, 1, B(2, 1));
if ((limits & kRoll_0_to_PI) && (limits & kYaw_0_to_PI) &&
(limits & kRoll_NegPI_2_to_PI_2) && (limits & kYaw_NegPI_2_to_PI_2) &&
(limits & kPitch_0_to_PI))
prog->AddBoundingBoxConstraint(1, 1, B(0, 2));
if ((limits & kPitch_NegPI_2_to_PI_2) && (limits & kRoll_NegPI_2_to_PI_2))
prog->AddBoundingBoxConstraint(1, 1, B(2, 2));
}
void AddRotationMatrixSpectrahedralSdpConstraint(
MathematicalProgram* prog,
const Eigen::Ref<const MatrixDecisionVariable<3, 3>>& R) {
// Equation 10 in
// Semidefinite descriptions of the convex hull of rotation matrices
// by James Saunderson, Pablo Parrilo and Alan Willsky
Matrix4<symbolic::Expression> M;
// clang-format off
M << 1 - R(0, 0) - R(1, 1) + R(2, 2), R(0, 2) + R(2, 0), R(0, 1) - R(1, 0), R(1, 2) + R(2, 1), // #NOLINT
R(0, 2) + R(2, 0), 1 + R(0, 0) - R(1, 1) - R(2, 2), R(1, 2) - R(2, 1), R(0, 1) + R(1, 0), // #NOLINT
R(0, 1) - R(1, 0), R(1, 2) - R(2, 1), 1 + R(0, 0) + R(1, 1) + R(2, 2), R(2, 0) - R(0, 2), // #NOLINT
R(1, 2) + R(2, 1), R(0, 1) + R(1, 0), R(2, 0) - R(0, 2), 1 - R(0, 0) + R(1, 1) - R(2, 2); // #NOLINT
// clang-format on
prog->AddPositiveSemidefiniteConstraint(M);
}
namespace {
void AddOrthogonalConstraint(
MathematicalProgram* prog,
const Eigen::Ref<const VectorDecisionVariable<3>>& v1,
const Eigen::Ref<const VectorDecisionVariable<3>>& v2) {
// We do this by introducing
// |v1+v2|^2 = v1'v1 + 2v1'v2 + v2'v2 <= 2
// |v1-v2|^2 = v1'v1 - 2v1'v2 + v2'v2 <= 2
// This is tight when v1'v1 = 1 and v2'v2 = 1.
// TODO(russt): Consider generalizing this to |v1+alpha*v2|^2 <= 1+alpha^2,
// for any real-valued alpha. When |R1|<|R2|<=1 or |R2|<|R1|<=1,
// different alphas represent different constraints.
// |v1+v2|^2 <= 2
// Implemented as a Lorenz cone using z = [ sqrt(2); v1+v2 ].
Vector4<symbolic::Expression> z;
z << std::sqrt(2), v1 + v2;
prog->AddLorentzConeConstraint(z);
// |v1-v2|^2 <= 2
// Implemented as a Lorenz cone using z = [ sqrt(2); v1-v2 ].
z.tail<3>() = v1 - v2;
prog->AddLorentzConeConstraint(z);
}
} // namespace
void AddRotationMatrixOrthonormalSocpConstraint(
MathematicalProgram* prog,
const Eigen::Ref<const MatrixDecisionVariable<3, 3>>& R) {
// All columns should be unit length (but we can only write Ri'Ri<=1),
// implemented as a rotated Lorenz cone with z = Ax+b = [1;1;R.col(i)].
Eigen::Matrix<double, 5, 3> A = Eigen::Matrix<double, 5, 3>::Zero();
A.bottomRows<3>() = Eigen::Matrix3d::Identity();
Eigen::Matrix<double, 5, 1> b;
b << 1, 1, 0, 0, 0;
for (int i = 0; i < 3; i++) {
prog->AddRotatedLorentzConeConstraint(A, b, R.col(i));
prog->AddRotatedLorentzConeConstraint(A, b, R.row(i).transpose());
}
AddOrthogonalConstraint(prog, R.col(0), R.col(1)); // R0'*R1 = 0.
AddOrthogonalConstraint(prog, R.col(1), R.col(2)); // R1'*R2 = 0.
AddOrthogonalConstraint(prog, R.col(0), R.col(2)); // R0'*R2 = 0.
// Same for the rows
AddOrthogonalConstraint(prog, R.row(0).transpose(), R.row(1).transpose());
AddOrthogonalConstraint(prog, R.row(1).transpose(), R.row(2).transpose());
AddOrthogonalConstraint(prog, R.row(0).transpose(), R.row(2).transpose());
}
} // namespace solvers
} // namespace drake