Skip to content

Commit 451343e

Browse files
Working joint velocity constraint using new variable set
1 parent 3dce733 commit 451343e

File tree

10 files changed

+328
-70
lines changed

10 files changed

+328
-70
lines changed

trajopt_ifopt/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ install(
9494
# Testing ##
9595
# ######################################################################################################################
9696

97-
if(TRAJOPT_ENABLE_TESTING)
97+
# if(TRAJOPT_ENABLE_TESTING)
9898
enable_testing()
9999
add_custom_target(run_tests)
100100
add_subdirectory(test)
101-
endif()
101+
# endif()
102102

103103
if(TRAJOPT_PACKAGE)
104104
cpack(

trajopt_ifopt/include/trajopt_ifopt/constraints/joint_velocity_constraint.h

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ TRAJOPT_IGNORE_WARNINGS_POP
3333
namespace trajopt_ifopt
3434
{
3535
class JointPosition;
36-
36+
class Var;
3737
/**
3838
* @brief This creates a joint velocity constraint and allows bounds to be set on a joint position
3939
*
@@ -108,5 +108,84 @@ class JointVelConstraint : public ifopt::ConstraintSet
108108
std::vector<std::shared_ptr<const JointPosition>> position_vars_;
109109
std::unordered_map<std::string, Eigen::Index> index_map_;
110110
};
111+
112+
/**
113+
* @brief This creates a joint velocity constraint and allows bounds to be set on a joint position
114+
*
115+
* Joint velocity is calculated as v = th_1 - th_0
116+
*/
117+
class JointVelConstraint2 : public ifopt::ConstraintSet
118+
{
119+
public:
120+
using Ptr = std::shared_ptr<JointVelConstraint>;
121+
using ConstPtr = std::shared_ptr<const JointVelConstraint>;
122+
123+
/**
124+
* @brief Constructs a velocity constraint from these variables, setting the bounds to the target
125+
* @param targets Joint Velocity targets (length should be n_dof). Upper and lower bounds are set to this value
126+
* @param position_vars Joint positions used to calculate velocity. These vars are assumed to be continuous and in
127+
* order.
128+
* @param coeffs The joint coefficients to use as weights. If size of 1 then the values is replicated for each joint.
129+
* @param name Name of the constraint
130+
*/
131+
JointVelConstraint2(const Eigen::VectorXd& targets,
132+
const std::vector<std::shared_ptr<const Var>>& position_vars,
133+
const Eigen::VectorXd& coeffs,
134+
const std::string& name = "JointVel");
135+
136+
/**
137+
* @brief Constructs a velocity constraint from these variables, setting the bounds to those passed in.
138+
* @param bounds Bounds on target joint velocity (length should be n_dof)
139+
* @param position_vars Joint positions used to calculate velocity. These vars are assumed to be continuous and in
140+
* order.
141+
* @param coeffs The joint coefficients to use as weights. If size of 1 then the values is replicated for each joint.
142+
* @param name Name of the constraint
143+
*/
144+
JointVelConstraint2(const std::vector<ifopt::Bounds>& bounds,
145+
const std::vector<std::shared_ptr<const Var>>& position_vars,
146+
const Eigen::VectorXd& coeffs,
147+
const std::string& name = "JointVel");
148+
149+
/**
150+
* @brief Returns the values associated with the constraint. In this case that is the approximate joint velocity.
151+
* @return Returns jointVelocity. Length is n_dof_ * (n_vars - 1)
152+
*/
153+
Eigen::VectorXd GetValues() const override;
154+
155+
/**
156+
* @brief Returns the "bounds" of this constraint. How these are enforced is up to the solver
157+
* @return Returns the "bounds" of this constraint
158+
*/
159+
std::vector<ifopt::Bounds> GetBounds() const override;
160+
161+
/**
162+
* @brief Fills the jacobian block associated with the given var_set.
163+
* @param var_set Name of the var_set to which the jac_block is associated
164+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
165+
*/
166+
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
167+
168+
private:
169+
/** @brief The var_set associated with this constraint */
170+
std::string var_set_;
171+
172+
/** @brief The number of joints in a single JointPosition */
173+
long n_dof_;
174+
175+
/** @brief The number of JointPositions passed in */
176+
long n_vars_;
177+
178+
/** @brief The coeff to apply to error and gradient */
179+
Eigen::VectorXd coeffs_;
180+
181+
/** @brief Bounds on the velocities of each joint */
182+
std::vector<ifopt::Bounds> bounds_;
183+
184+
/** @brief Pointers to the vars used by this constraint.
185+
*
186+
* Do not access them directly. Instead use this->GetVariables()->GetComponent(position_var->GetName())->GetValues()
187+
*/
188+
std::vector<std::shared_ptr<const Var>> position_vars_;
189+
};
111190
} // namespace trajopt_ifopt
112191
#endif

trajopt_ifopt/include/trajopt_ifopt/variable_sets/node.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <trajopt_ifopt/variable_sets/var.h>
55
#include <unordered_map>
6+
#include <memory>
67

78
namespace trajopt_ifopt
89
{
@@ -18,7 +19,7 @@ class Node
1819

1920
const std::string& getName() const;
2021

21-
NodesVariables* getParent() const;
22+
const NodesVariables* getParent() const;
2223

2324
std::shared_ptr<const Var> addVar(const std::string& name);
2425

trajopt_ifopt/include/trajopt_ifopt/variable_sets/nodes_variables.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class NodesVariables : public ifopt::VariableSet
4848
public:
4949
using Ptr = std::shared_ptr<NodesVariables>;
5050

51+
/**
52+
* @param variable_name The name of the variables in the optimization problem.
53+
*/
54+
NodesVariables(const std::string& variable_name);
55+
virtual ~NodesVariables() = default;
56+
5157
/**
5258
* @brief Add node to the variable set
5359
* @param node The node to append
@@ -105,13 +111,6 @@ class NodesVariables : public ifopt::VariableSet
105111
Eigen::Index GetDim() const;
106112

107113
protected:
108-
/**
109-
* @param n_dim The number of dimensions (x,y,..) each node has.
110-
* @param variable_name The name of the variables in the optimization problem.
111-
*/
112-
NodesVariables(const std::string& variable_name);
113-
virtual ~NodesVariables() = default;
114-
115114
Eigen::VectorXd values_;
116115
VecBound bounds_; ///< the bounds on the node values.
117116
std::vector<std::shared_ptr<Node>> nodes_;

trajopt_ifopt/include/trajopt_ifopt/variable_sets/var.h

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include <Eigen/Core>
55
#include <vector>
6-
#include <memory>
76

87
namespace trajopt_ifopt
98
{
9+
class Node;
1010
/**
1111
* @brief This is the class which the constraints should be storing
1212
* @details This class contains all information necessary for filling the jacobian
@@ -17,37 +17,53 @@ class Var
1717
public:
1818
Var() = default;
1919
~Var() = default;
20-
Var(Eigen::Index index, std::string name);
21-
Var(Eigen::Index index, Eigen::Index length, std::string identifier, std::vector<std::string> names);
20+
Var(Eigen::Index index, std::string name, Node* parent = nullptr);
21+
Var(Eigen::Index index,
22+
Eigen::Index length,
23+
std::string identifier,
24+
std::vector<std::string> names,
25+
Node* parent = nullptr);
2226
Var(const Var& other) = default;
2327
Var& operator=(const Var&) = default;
2428
Var(Var&&) = default;
2529
Var& operator=(Var&&) = default;
2630

31+
/** @brief Get the identifier for the variable */
32+
const std::string& getIdentifier() const;
33+
34+
/** @brief Get the parent node */
35+
const Node* getParent() const;
36+
2737
/**
2838
* @brief Set the variables. This is the full vector of variables and it will extract its variables from the full
2939
* list.
3040
* @param x The full vector of variables
3141
*/
3242
void setVariables(const Eigen::Ref<const Eigen::VectorXd>& x);
3343

44+
/**
45+
* @brief Get the index in the full set of variables that these are stored
46+
* @return The start index
47+
*/
48+
Eigen::Index getIndex() const;
49+
3450
/**
3551
* @brief Get the variable size
3652
* @return The size
3753
*/
3854
Eigen::Index size() const;
3955

40-
template <typename T>
41-
const T& value() const
42-
{
43-
throw std::runtime_error("This should never be used");
44-
}
56+
/**
57+
* @brief Get variable values
58+
* @return The values
59+
*/
60+
const Eigen::VectorXd& value() const;
4561

46-
template <typename T>
47-
const T& name() const
48-
{
49-
throw std::runtime_error("This should never be used");
50-
}
62+
/**
63+
* @brief Get teh variable names
64+
* @return The names
65+
*/
66+
const std::vector<std::string>& name() const;
5167

5268
private:
5369
friend class Node;
@@ -56,12 +72,7 @@ class Var
5672
std::string identifier_;
5773
std::vector<std::string> names_;
5874
Eigen::VectorXd values_;
59-
60-
/**
61-
* @brief Get the index in the full set of variables that these are stored
62-
* @return The start index
63-
*/
64-
Eigen::Index getIndex() const;
75+
Node* parent_{ nullptr };
6576

6677
/**
6778
* @brief Increment the start index by the provided value
@@ -70,37 +81,6 @@ class Var
7081
void incrementIndex(Eigen::Index value);
7182
};
7283

73-
template <>
74-
inline const double& Var::value() const
75-
{
76-
assert(values_.size() == 1);
77-
assert(index_ > -1);
78-
assert(length_ == 1);
79-
return values_[0];
80-
}
81-
82-
template <>
83-
inline const Eigen::VectorXd& Var::value() const
84-
{
85-
assert(index_ > -1);
86-
assert(length_ > 1);
87-
assert(names_.size() > 1);
88-
return values_;
89-
}
90-
91-
template <>
92-
inline const std::string& Var::name() const
93-
{
94-
assert(names_.size() == 1);
95-
return names_[0];
96-
}
97-
98-
template <>
99-
inline const std::vector<std::string>& Var::name() const
100-
{
101-
assert(names_.size() > 1);
102-
return names_;
103-
}
10484
} // namespace trajopt_ifopt
10585

10686
#endif // TRAJOPT_IFOPT_VAR_H

0 commit comments

Comments
 (0)