Skip to content

Commit 3cd6714

Browse files
Update collision and cartesian constraints to use new variable set
1 parent abd5b16 commit 3cd6714

File tree

6 files changed

+861
-0
lines changed

6 files changed

+861
-0
lines changed

trajopt_ifopt/include/trajopt_ifopt/constraints/cartesian_position_constraint.h

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ TRAJOPT_IGNORE_WARNINGS_POP
3636
namespace trajopt_ifopt
3737
{
3838
class JointPosition;
39+
class Var;
3940

4041
/** @brief Contains Cartesian pose constraint information */
4142
struct CartPosInfo
@@ -202,5 +203,124 @@ class CartPosConstraint : public ifopt::ConstraintSet
202203

203204
static thread_local tesseract_common::TransformMap transforms_cache; // NOLINT
204205
};
206+
207+
class CartPosConstraint2 : public ifopt::ConstraintSet
208+
{
209+
public:
210+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
211+
212+
using Ptr = std::shared_ptr<CartPosConstraint>;
213+
using ConstPtr = std::shared_ptr<const CartPosConstraint>;
214+
using ErrorFunctionType = std::function<Eigen::VectorXd(const Eigen::Isometry3d&, const Eigen::Isometry3d&)>;
215+
using ErrorDiffFunctionType = std::function<Eigen::VectorXd(const Eigen::VectorXd&,
216+
const Eigen::Isometry3d&,
217+
const Eigen::Isometry3d&,
218+
tesseract_common::TransformMap&)>;
219+
220+
CartPosConstraint2(const CartPosInfo& info,
221+
std::shared_ptr<const Var> position_var,
222+
const std::string& name = "CartPos");
223+
224+
CartPosConstraint2(CartPosInfo info,
225+
std::shared_ptr<const Var> position_var,
226+
const Eigen::VectorXd& coeffs,
227+
const std::string& name = "CartPos");
228+
229+
/**
230+
* @brief CalcValues Calculates the values associated with the constraint
231+
* @param joint_vals Input joint values for which FK is solved
232+
* @return Error of FK solution from target, size 6. The first 3 terms are associated with position and the last 3 are
233+
* associated with orientation.
234+
*/
235+
Eigen::VectorXd CalcValues(const Eigen::Ref<const Eigen::VectorXd>& joint_vals) const;
236+
/**
237+
* @brief Returns the values associated with the constraint. In this case that is the concatenated joint values
238+
* associated with each of the joint positions should be n_dof_ * n_vars_ long
239+
* @return
240+
*/
241+
Eigen::VectorXd GetValues() const override;
242+
243+
/**
244+
* @brief Returns the "bounds" of this constraint. How these are enforced is up to the solver
245+
* @return Returns the "bounds" of this constraint
246+
*/
247+
std::vector<ifopt::Bounds> GetBounds() const override;
248+
249+
void SetBounds(const std::vector<ifopt::Bounds>& bounds);
250+
251+
/**
252+
* @brief Fills the jacobian block associated with the constraint
253+
* @param jac_block Block of the overall jacobian associated with these constraints
254+
*/
255+
void CalcJacobianBlock(const Eigen::Ref<const Eigen::VectorXd>& joint_vals, Jacobian& jac_block) const;
256+
/**
257+
* @brief Fills the jacobian block associated with the given var_set.
258+
* @param var_set Name of the var_set to which the jac_block is associated
259+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
260+
*/
261+
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
262+
263+
/**
264+
* @brief Gets the Cartesian Pose info used to create this constraint
265+
* @return The Cartesian Pose info used to create this constraint
266+
*/
267+
const CartPosInfo& GetInfo() const;
268+
CartPosInfo& GetInfo();
269+
270+
/**
271+
* @brief Set the target pose
272+
* @param pose
273+
*/
274+
void SetTargetPose(const Eigen::Isometry3d& target_frame_offset);
275+
276+
/**
277+
* @brief Returns the target pose for the constraint
278+
* @return The target pose for the constraint
279+
*/
280+
Eigen::Isometry3d GetTargetPose() const;
281+
282+
/**
283+
* @brief Returns the current TCP pose in world frame given the input kinematic info and the current variable values
284+
* @return The current TCP pose given the input kinematic info and the current variable values
285+
*/
286+
Eigen::Isometry3d GetCurrentPose() const;
287+
288+
/** @brief If true, numeric differentiation will be used. Default: true
289+
*
290+
* Note: While the logic for using the jacobian from KDL will be used if set to false, this has been buggy. Set this
291+
* to false at your own risk.
292+
*/
293+
bool use_numeric_differentiation{ true };
294+
295+
private:
296+
/** @brief The var_set associated with this constraint */
297+
std::string var_set_;
298+
299+
/** @brief The number of joints in a single JointPosition */
300+
long n_dof_;
301+
302+
/** @brief The constraint coefficients */
303+
Eigen::VectorXd coeffs_;
304+
305+
/** @brief Bounds on the positions of each joint */
306+
std::vector<ifopt::Bounds> bounds_;
307+
308+
/**
309+
* @brief Pointers to the vars used by this constraint.
310+
* Do not access them directly. Instead use this->GetVariables()->GetComponent(position_var->GetName())->GetValues()
311+
*/
312+
std::shared_ptr<const Var> position_var_;
313+
314+
/** @brief The kinematic information used when calculating error */
315+
CartPosInfo info_;
316+
317+
/** @brief Error function for calculating the error in the position given the source and target positions */
318+
ErrorFunctionType error_function_{ nullptr };
319+
320+
/** @brief The error function to calculate the error difference used for jacobian calculations */
321+
ErrorDiffFunctionType error_diff_function_{ nullptr };
322+
323+
static thread_local tesseract_common::TransformMap transforms_cache; // NOLINT
324+
};
205325
} // namespace trajopt_ifopt
206326
#endif

trajopt_ifopt/include/trajopt_ifopt/constraints/collision/continuous_collision_constraint.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ TRAJOPT_IGNORE_WARNINGS_POP
3434
namespace trajopt_ifopt
3535
{
3636
class JointPosition;
37+
class Var;
3738
class ContinuousCollisionEvaluator;
3839

3940
class ContinuousCollisionConstraint : public ifopt::ConstraintSet
@@ -116,6 +117,90 @@ class ContinuousCollisionConstraint : public ifopt::ConstraintSet
116117

117118
std::shared_ptr<ContinuousCollisionEvaluator> collision_evaluator_;
118119
};
120+
121+
class ContinuousCollisionConstraint2 : public ifopt::ConstraintSet
122+
{
123+
public:
124+
using Ptr = std::shared_ptr<ContinuousCollisionConstraint>;
125+
using ConstPtr = std::shared_ptr<const ContinuousCollisionConstraint>;
126+
127+
/**
128+
* @brief ContinuousCollisionConstraintIfopt
129+
* @param collision_evaluator The continuous collision evaluator to use
130+
* @param position_vars The position vars associated with this constraint.
131+
* @param position_vars_fixed Indicate if the position var is fixed
132+
* @param max_num_cnt The max number of constraits to include
133+
* @param fixed_sparsity This is mostly need for snopt which requires sparsity to not change
134+
* @param name
135+
*/
136+
ContinuousCollisionConstraint2(std::shared_ptr<ContinuousCollisionEvaluator> collision_evaluator,
137+
std::array<std::shared_ptr<const Var>, 2> position_vars,
138+
bool vars0_fixed,
139+
bool vars1_fixed,
140+
int max_num_cnt = 1,
141+
bool fixed_sparsity = false,
142+
const std::string& name = "LVSCollision");
143+
144+
/**
145+
* @brief Returns the values associated with the constraint.
146+
* @warning Make sure that the values returns are not just the violation but the constraint values.
147+
* Remember the values are the constant in the quadratic function, so if you only return the
148+
* violation then if it is not violating the constraint this would be zero which means it
149+
* will always appear to be on the constraint boundary which will cause issue when solving.
150+
* If it is not voliating the constraint then return the max negative number.
151+
* If no contacts are found return the negative of the collision margin buffer. This is why
152+
* it is important to not set the collision margin buffer to zero.
153+
* @return The constraint values not the violations
154+
*/
155+
Eigen::VectorXd GetValues() const override;
156+
157+
/**
158+
* @brief Returns the "bounds" of this constraint. How these are enforced is up to the solver
159+
* @return Returns the "bounds" of this constraint
160+
*/
161+
std::vector<ifopt::Bounds> GetBounds() const override;
162+
/**
163+
* @brief Fills the jacobian block associated with the given var_set.
164+
* @param var_set Name of the var_set to which the jac_block is associated
165+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
166+
*/
167+
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
168+
169+
/**
170+
* @brief Sets the bounds on the collision distance
171+
* @param bounds New bounds that will be set. Should be size 1
172+
*/
173+
void SetBounds(const std::vector<ifopt::Bounds>& bounds);
174+
175+
/**
176+
* @brief Get the collision evaluator. This exposed for plotter callbacks
177+
* @return The collision evaluator
178+
*/
179+
std::shared_ptr<ContinuousCollisionEvaluator> GetCollisionEvaluator() const;
180+
181+
private:
182+
/** @brief The var_set associated with this constraint */
183+
std::string var_set_;
184+
185+
/** @brief The number of joints in a single JointPosition */
186+
long n_dof_;
187+
188+
/** @brief Bounds on the constraint value. Default: std::vector<Bounds>(1, ifopt::BoundSmallerZero) */
189+
std::vector<ifopt::Bounds> bounds_;
190+
191+
/**
192+
* @brief Pointers to the vars used by this constraint.
193+
* Do not access them directly. Instead use this->GetVariables()->GetComponent(position_var->GetName())->GetValues()
194+
*/
195+
std::array<std::shared_ptr<const Var>, 2> position_vars_;
196+
bool vars0_fixed_{ false };
197+
bool vars1_fixed_{ false };
198+
199+
/** @brief Used to initialize jacobian because snopt sparsity cannot change */
200+
std::vector<Eigen::Triplet<double>> triplet_list_;
201+
202+
std::shared_ptr<ContinuousCollisionEvaluator> collision_evaluator_;
203+
};
119204
} // namespace trajopt_ifopt
120205

121206
#endif // TRAJOPT_IFOPT_CONTINUOUS_COLLISION_CONSTRAINT_H

trajopt_ifopt/include/trajopt_ifopt/constraints/collision/discrete_collision_constraint.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ TRAJOPT_IGNORE_WARNINGS_POP
3434
namespace trajopt_ifopt
3535
{
3636
class JointPosition;
37+
class Var;
3738
class DiscreteCollisionEvaluator;
3839

3940
class DiscreteCollisionConstraint : public ifopt::ConstraintSet
@@ -113,5 +114,87 @@ class DiscreteCollisionConstraint : public ifopt::ConstraintSet
113114

114115
std::shared_ptr<DiscreteCollisionEvaluator> collision_evaluator_;
115116
};
117+
118+
class DiscreteCollisionConstraint2 : public ifopt::ConstraintSet
119+
{
120+
public:
121+
using Ptr = std::shared_ptr<DiscreteCollisionConstraint>;
122+
using ConstPtr = std::shared_ptr<const DiscreteCollisionConstraint>;
123+
124+
DiscreteCollisionConstraint2(std::shared_ptr<DiscreteCollisionEvaluator> collision_evaluator,
125+
std::shared_ptr<const Var> position_var,
126+
int max_num_cnt = 1,
127+
bool fixed_sparsity = false,
128+
const std::string& name = "DiscreteCollisionV3");
129+
130+
/**
131+
* @brief Returns the values associated with the constraint.
132+
* @warning Make sure that the values returns are not just the violation but the constraint values.
133+
* Remember the values are the constant in the quadratic function, so if you only return the
134+
* violation then if it is not violating the constraint this would be zero which means it
135+
* will always appear to be on the constraint boundary which will cause issue when solving.
136+
* If it is not voliating the constraint then return the max negative number.
137+
* If no contacts are found return the negative of the collision margin buffer. This is why
138+
* it is important to not set the collision margin buffer to zero.
139+
* @return The constraint values not the violations
140+
*/
141+
Eigen::VectorXd GetValues() const override;
142+
143+
/**
144+
* @brief Returns the "bounds" of this constraint. How these are enforced is up to the solver
145+
* @return Returns the "bounds" of this constraint
146+
*/
147+
std::vector<ifopt::Bounds> GetBounds() const override;
148+
149+
/**
150+
* @brief Fills the jacobian block associated with the given var_set.
151+
* @param var_set Name of the var_set to which the jac_block is associated
152+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
153+
*/
154+
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
155+
156+
/** @brief Calculates the values associated with the constraint */
157+
Eigen::VectorXd CalcValues(const Eigen::Ref<const Eigen::VectorXd>& joint_vals) const;
158+
159+
/**
160+
* @brief Sets the bounds on the collision distance
161+
* @param bounds New bounds that will be set. Should be size 1
162+
*/
163+
void SetBounds(const std::vector<ifopt::Bounds>& bounds);
164+
165+
/**
166+
* @brief Fills the jacobian block associated with the constraint
167+
* @param jac_block Block of the overall jacobian associated with these constraints
168+
*/
169+
void CalcJacobianBlock(const Eigen::Ref<const Eigen::VectorXd>& joint_vals, Jacobian& jac_block) const;
170+
171+
/**
172+
* @brief Get the collision evaluator. This exposed for plotter callbacks
173+
* @return The collision evaluator
174+
*/
175+
std::shared_ptr<DiscreteCollisionEvaluator> GetCollisionEvaluator() const;
176+
177+
private:
178+
/** @brief The var_set associated with this constraint */
179+
std::string var_set_;
180+
181+
/** @brief The number of joints in a single JointPosition */
182+
long n_dof_;
183+
184+
/** @brief Bounds on the constraint value. Default: std::vector<Bounds>(1, ifopt::BoundSmallerZero) */
185+
std::vector<ifopt::Bounds> bounds_;
186+
187+
/**
188+
* @brief Pointers to the vars used by this constraint.
189+
* Do not access them directly. Instead use this->GetVariables()->GetComponent(position_var->GetName())->GetValues()
190+
*/
191+
std::shared_ptr<const Var> position_var_;
192+
193+
/** @brief Used to initialize jacobian because snopt sparsity cannot change */
194+
std::vector<Eigen::Triplet<double>> triplet_list_;
195+
196+
std::shared_ptr<DiscreteCollisionEvaluator> collision_evaluator_;
197+
};
198+
116199
} // namespace trajopt_ifopt
117200
#endif

0 commit comments

Comments
 (0)