Skip to content

Commit a7f1f6d

Browse files
Add JointAccellConstraint and JointJerkConstraint (#275)
1 parent 5665eea commit a7f1f6d

File tree

14 files changed

+1438
-22
lines changed

14 files changed

+1438
-22
lines changed

trajopt_ifopt/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ set(TRAJOPT_IFOPT_SOURCE_FILES
3838
src/costs/squared_cost.cpp
3939
src/constraints/cartesian_position_constraint.cpp
4040
src/constraints/inverse_kinematics_constraint.cpp
41+
src/constraints/joint_acceleration_constraint.cpp
42+
src/constraints/joint_jerk_constraint.cpp
4143
src/constraints/joint_position_constraint.cpp
4244
src/constraints/joint_velocity_constraint.cpp
4345
src/constraints/collision/collision_types.cpp
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @file joint_acceleration_constraint.h
3+
* @brief The joint_acceleration constraint
4+
*
5+
* @author Ben Greenberg
6+
* @date April 22, 2021
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @copyright Copyright (c) 2021, Southwest Research Institute
11+
*
12+
* @par License
13+
* Software License Agreement (Apache License)
14+
* @par
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
* @par
20+
* Unless required by applicable law or agreed to in writing, software
21+
* distributed under the License is distributed on an "AS IS" BASIS,
22+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
* See the License for the specific language governing permissions and
24+
* limitations under the License.
25+
*/
26+
#ifndef TRAJOPT_IFOPT_JOINT_JOINT_ACCELERATION_CONSTRAINT_H
27+
#define TRAJOPT_IFOPT_JOINT_JOINT_ACCELERATION_CONSTRAINT_H
28+
29+
#include <trajopt_utils/macros.h>
30+
TRAJOPT_IGNORE_WARNINGS_PUSH
31+
#include <ifopt/constraint_set.h>
32+
#include <Eigen/Eigen>
33+
TRAJOPT_IGNORE_WARNINGS_POP
34+
35+
#include <trajopt_ifopt/variable_sets/joint_position_variable.h>
36+
37+
namespace trajopt_ifopt
38+
{
39+
/**
40+
* @brief This creates a joint acceleration constraint and allows bounds to be set on a joint position
41+
*
42+
* Joint acceleration is calculated as a = th_2 - 2th_1 + th_0
43+
*/
44+
class JointAccelConstraint : public ifopt::ConstraintSet
45+
{
46+
public:
47+
using Ptr = std::shared_ptr<JointAccelConstraint>;
48+
using ConstPtr = std::shared_ptr<const JointAccelConstraint>;
49+
50+
/**
51+
* @brief Constructs a acceleration contraint from these variables, setting the bounds to the target
52+
* @param targets Joint Acceleration targets (length should be n_dof). Upper and lower bounds are set to this value
53+
* @param position_vars Joint positions used to calculate acceleration. These vars are assumed to be continuous and in
54+
* order.
55+
* @param name Name of the constraint
56+
*/
57+
JointAccelConstraint(const Eigen::VectorXd& targets,
58+
const std::vector<trajopt_ifopt::JointPosition::ConstPtr>& position_vars,
59+
const std::string& name = "JointAccel");
60+
61+
/**
62+
* @brief Constructs a acceleration contraint from these variables, setting the bounds to those passed in.
63+
* @param bounds Bounds on target joint acceleration (length should be n_dof)
64+
* @param position_vars Joint positions used to calculate acceleration. These vars are assumed to be continuous and in
65+
* order.
66+
* @param name Name of the constraint
67+
*/
68+
JointAccelConstraint(const std::vector<ifopt::Bounds>& bounds,
69+
const std::vector<trajopt_ifopt::JointPosition::ConstPtr>& position_vars,
70+
const std::string& name = "JointAccel");
71+
72+
/**
73+
* @brief Returns the values associated with the constraint. In this case that is the approximate joint acceleration.
74+
* @return Returns jointAcceleration. Length is n_dof_ * n_vars
75+
*/
76+
Eigen::VectorXd GetValues() const override;
77+
78+
/**
79+
* @brief Returns the "bounds" of this constraint. How these are enforced is up to the solver
80+
* @return Returns the "bounds" of this constraint
81+
*/
82+
std::vector<ifopt::Bounds> GetBounds() const override;
83+
84+
/**
85+
* @brief Fills the jacobian block associated with the given var_set.
86+
* @param var_set Name of the var_set to which the jac_block is associated
87+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
88+
*/
89+
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
90+
91+
private:
92+
/** @brief The number of joints in a single JointPosition */
93+
long n_dof_;
94+
/** @brief The number of JointPositions passed in */
95+
long n_vars_;
96+
97+
/** @brief Bounds on the velocities of each joint */
98+
std::vector<ifopt::Bounds> bounds_;
99+
100+
/** @brief Pointers to the vars used by this constraint.
101+
*
102+
* Do not access them directly. Instead use this->GetVariables()->GetComponent(position_var->GetName())->GetValues()*/
103+
std::vector<trajopt_ifopt::JointPosition::ConstPtr> position_vars_;
104+
std::unordered_map<std::string, Eigen::Index> index_map_;
105+
};
106+
}; // namespace trajopt_ifopt
107+
#endif
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @file joint_jerk_constraint.h
3+
* @brief The join jerk constraint
4+
*
5+
* @author Levi Armstrong
6+
* @date November 1, 2021
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @copyright Copyright (c) 2021, Southwest Research Institute
11+
*
12+
* @par License
13+
* Software License Agreement (Apache License)
14+
* @par
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
* @par
20+
* Unless required by applicable law or agreed to in writing, software
21+
* distributed under the License is distributed on an "AS IS" BASIS,
22+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
* See the License for the specific language governing permissions and
24+
* limitations under the License.
25+
*/
26+
#ifndef TRAJOPT_IFOPT_JOINT_JOINT_JERK_CONSTRAINT_H
27+
#define TRAJOPT_IFOPT_JOINT_JOINT_JERK_CONSTRAINT_H
28+
29+
#include <trajopt_utils/macros.h>
30+
TRAJOPT_IGNORE_WARNINGS_PUSH
31+
#include <ifopt/constraint_set.h>
32+
#include <Eigen/Eigen>
33+
TRAJOPT_IGNORE_WARNINGS_POP
34+
35+
#include <trajopt_ifopt/variable_sets/joint_position_variable.h>
36+
37+
namespace trajopt_ifopt
38+
{
39+
/**
40+
* @brief This creates a joint acceleration constraint and allows bounds to be set on a joint position
41+
*
42+
* Joint acceleration is calculated as a = th_2 - 2th_1 + th_0
43+
*/
44+
class JointJerkConstraint : public ifopt::ConstraintSet
45+
{
46+
public:
47+
using Ptr = std::shared_ptr<JointJerkConstraint>;
48+
using ConstPtr = std::shared_ptr<const JointJerkConstraint>;
49+
50+
/**
51+
* @brief Constructs a jerk constraint from these variables, setting the bounds to the target
52+
* @param targets Joint Jerk targets (length should be n_dof). Upper and lower bounds are set to this value
53+
* @param position_vars Joint positions used to calculate acceleration. These vars are assumed to be continuous and in
54+
* order.
55+
* @param name Name of the constraint
56+
*/
57+
JointJerkConstraint(const Eigen::VectorXd& targets,
58+
const std::vector<trajopt_ifopt::JointPosition::ConstPtr>& position_vars,
59+
const std::string& name = "JointJerk");
60+
61+
/**
62+
* @brief Constructs a jerk constraint from these variables, setting the bounds to those passed in.
63+
* @param bounds Bounds on target joint acceleration (length should be n_dof)
64+
* @param position_vars Joint positions used to calculate acceleration. These vars are assumed to be continuous and in
65+
* order.
66+
* @param name Name of the constraint
67+
*/
68+
JointJerkConstraint(const std::vector<ifopt::Bounds>& bounds,
69+
const std::vector<trajopt_ifopt::JointPosition::ConstPtr>& position_vars,
70+
const std::string& name = "JointJerk");
71+
72+
/**
73+
* @brief Returns the values associated with the constraint. In this case that is the approximate joint jerk.
74+
* @return Returns joint jerk. Length is n_dof_ * n_vars
75+
*/
76+
Eigen::VectorXd GetValues() const override;
77+
78+
/**
79+
* @brief Returns the "bounds" of this constraint. How these are enforced is up to the solver
80+
* @return Returns the "bounds" of this constraint
81+
*/
82+
std::vector<ifopt::Bounds> GetBounds() const override;
83+
84+
/**
85+
* @brief Fills the jacobian block associated with the given var_set.
86+
* @param var_set Name of the var_set to which the jac_block is associated
87+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
88+
*/
89+
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
90+
91+
private:
92+
/** @brief The number of joints in a single JointPosition */
93+
long n_dof_;
94+
/** @brief The number of JointPositions passed in */
95+
long n_vars_;
96+
97+
/** @brief Bounds on the velocities of each joint */
98+
std::vector<ifopt::Bounds> bounds_;
99+
100+
/** @brief Pointers to the vars used by this constraint.
101+
*
102+
* Do not access them directly. Instead use this->GetVariables()->GetComponent(position_var->GetName())->GetValues()*/
103+
std::vector<trajopt_ifopt::JointPosition::ConstPtr> position_vars_;
104+
std::unordered_map<std::string, Eigen::Index> index_map_;
105+
};
106+
}; // namespace trajopt_ifopt
107+
#endif

trajopt_ifopt/include/trajopt_ifopt/constraints/joint_velocity_constraint.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ class JointVelConstraint : public ifopt::ConstraintSet
4848
using ConstPtr = std::shared_ptr<const JointVelConstraint>;
4949

5050
/**
51-
* @brief Constructs a velocity contraint from these variables, setting the bounds to the target
51+
* @brief Constructs a velocity constraint from these variables, setting the bounds to the target
5252
* @param targets Joint Velocity targets (length should be n_dof). Upper and lower bounds are set to this value
5353
* @param position_vars Joint positions used to calculate velocity. These vars are assumed to be continuous and in
5454
* order.
5555
* @param name Name of the constraint
5656
*/
5757
JointVelConstraint(const Eigen::VectorXd& targets,
58-
std::vector<JointPosition::ConstPtr> position_vars,
58+
const std::vector<JointPosition::ConstPtr>& position_vars,
5959
const std::string& name = "JointVel");
6060

6161
/**
62-
* @brief Constructs a velocity contraint from these variables, setting the bounds to those passed in.
62+
* @brief Constructs a velocity constraint from these variables, setting the bounds to those passed in.
6363
* @param bounds Bounds on target joint velocity (length should be n_dof)
6464
* @param position_vars Joint positions used to calculate velocity. These vars are assumed to be continuous and in
6565
* order.
6666
* @param name Name of the constraint
6767
*/
6868
JointVelConstraint(const std::vector<ifopt::Bounds>& bounds,
69-
std::vector<JointPosition::ConstPtr> position_vars,
69+
const std::vector<JointPosition::ConstPtr>& position_vars,
7070
const std::string& name = "JointVel");
7171

7272
/**
@@ -84,7 +84,7 @@ class JointVelConstraint : public ifopt::ConstraintSet
8484
/**
8585
* @brief Fills the jacobian block associated with the given var_set.
8686
* @param var_set Name of the var_set to which the jac_block is associated
87-
* @param jac_block Block of the overal jacobian associated with these constraints and the var_set variable
87+
* @param jac_block Block of the overall jacobian associated with these constraints and the var_set variable
8888
*/
8989
void FillJacobianBlock(std::string var_set, Jacobian& jac_block) const override;
9090

trajopt_ifopt/include/trajopt_ifopt/utils/ifopt_utils.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,36 @@ Eigen::VectorXd calcBoundsErrors(const Eigen::Ref<const Eigen::VectorXd>& input,
126126
Eigen::VectorXd calcBoundsViolations(const Eigen::Ref<const Eigen::VectorXd>& input,
127127
const std::vector<ifopt::Bounds>& bounds);
128128

129+
/**
130+
* @brief Calculate the numerical cost gradient at the provided values
131+
* @param x The variable values to calculate the gradient about
132+
* @param nlp The nlp problem
133+
* @param epsilon The epsilon to leverage for perturbing the values
134+
* @return The numerical cost gradient
135+
*/
129136
ifopt::Problem::VectorXd calcNumericalCostGradient(const double* x, ifopt::Problem& nlp, double epsilon = 1e-8);
130137

138+
/**
139+
* @brief Calculate the numerical constraint gradient at the provided values
140+
* @param x The variable values to calculate the gradient about
141+
* @param nlp The nlp problem
142+
* @param epsilon The epsilon to leverage for perturbing the values
143+
* @return The numerical constraint gradient
144+
*/
131145
ifopt::Problem::Jacobian calcNumericalConstraintGradient(const double* x, ifopt::Problem& nlp, double epsilon = 1e-8);
132146

147+
/**
148+
* @brief Calculate the numerical constraint gradient at the provided values
149+
* @details This is primarily used buy unit tests to avoid needing to create a problem to calculate the gradient of a
150+
* single constraint.
151+
* @param variables The variable values to calculate the gradient about
152+
* @param constraint_set The constraint to numerically calculate the gradient for
153+
* @param epsilon The epsilon to leverage for perturbing the values
154+
* @return The numerical constraint gradient
155+
*/
156+
ifopt::Problem::Jacobian calcNumericalConstraintGradient(ifopt::Component& variables,
157+
ifopt::ConstraintSet& constraint_set,
158+
double epsilon = 1e-8);
159+
133160
} // namespace trajopt_ifopt
134161
#endif

0 commit comments

Comments
 (0)