Skip to content

Commit 3a08336

Browse files
create manipulator model (#29)
1 parent 6e78b23 commit 3a08336

File tree

107 files changed

+677
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+677
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ set(dynamics_model_srcs
8585
src/dynamics_model/bicycle.cpp
8686
src/dynamics_model/cartpole.cpp
8787
src/dynamics_model/quadrotor.cpp
88+
src/dynamics_model/manipulator.cpp
8889
src/dynamics_model/spacecraft_linear.cpp
8990
)
9091

include/cddp-cpp/cddp.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "dynamics_model/bicycle.hpp"
3535
#include "dynamics_model/cartpole.hpp"
3636
#include "dynamics_model/quadrotor.hpp"
37+
#include "dynamics_model/manipulator.hpp"
3738
#include "dynamics_model/spacecraft_linear.hpp"
3839

3940

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
Copyright 2024 Tomo Sasaki
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#ifndef CDDP_MANIPULATOR_HPP
18+
#define CDDP_MANIPULATOR_HPP
19+
20+
#include "cddp_core/dynamical_system.hpp"
21+
#include <Eigen/Dense>
22+
23+
namespace cddp {
24+
/**
25+
* @brief Manipulator model implementation
26+
*
27+
* This class implements a simplified PUMA manipulator model with state vector
28+
* [q1, q2, q3, dq1, dq2, dq3] where q1, q2, q3 are joint angles and dq1, dq2, dq3
29+
* are joint velocities. The control input is the joint torques [tau1, tau2, tau3].
30+
*/
31+
class Manipulator : public DynamicalSystem {
32+
public:
33+
/**
34+
* Constructor for simplified PUMA manipulator model
35+
* @param timestep Time step for discretization
36+
* @param integration_type Integration method ("euler" or "rk4")
37+
*/
38+
Manipulator(double timestep, std::string integration_type = "rk4");
39+
40+
/**
41+
* Computes the continuous-time dynamics of the manipulator
42+
* State vector: [q1, q2, q3, dq1, dq2, dq3]
43+
* Control vector: [tau1, tau2, tau3]
44+
* @param state Current state vector
45+
* @param control Current control input (joint torques)
46+
* @return State derivative vector
47+
*/
48+
Eigen::VectorXd getContinuousDynamics(const Eigen::VectorXd& state,
49+
const Eigen::VectorXd& control) const override;
50+
51+
/**
52+
* Computes the discrete-time dynamics
53+
* @param state Current state vector
54+
* @param control Current control input
55+
* @return Next state vector
56+
*/
57+
Eigen::VectorXd getDiscreteDynamics(const Eigen::VectorXd& state,
58+
const Eigen::VectorXd& control) const override {
59+
return DynamicalSystem::getDiscreteDynamics(state, control);
60+
}
61+
62+
/**
63+
* Computes the Jacobian of the dynamics with respect to the state
64+
* @param state Current state vector
65+
* @param control Current control input
66+
* @return State Jacobian matrix (A matrix)
67+
*/
68+
Eigen::MatrixXd getStateJacobian(const Eigen::VectorXd& state,
69+
const Eigen::VectorXd& control) const override;
70+
71+
/**
72+
* Computes the Jacobian of the dynamics with respect to the control input
73+
* @param state Current state vector
74+
* @param control Current control input
75+
* @return Control Jacobian matrix (B matrix)
76+
*/
77+
Eigen::MatrixXd getControlJacobian(const Eigen::VectorXd& state,
78+
const Eigen::VectorXd& control) const override;
79+
80+
/**
81+
* Computes the Hessian of the dynamics with respect to the state
82+
* @param state Current state vector
83+
* @param control Current control input
84+
* @return State Hessian matrix
85+
*/
86+
Eigen::MatrixXd getStateHessian(const Eigen::VectorXd& state,
87+
const Eigen::VectorXd& control) const override;
88+
89+
/**
90+
* Computes the Hessian of the dynamics with respect to the control
91+
* @param state Current state vector
92+
* @param control Current control input
93+
* @return Control Hessian matrix
94+
*/
95+
Eigen::MatrixXd getControlHessian(const Eigen::VectorXd& state,
96+
const Eigen::VectorXd& control) const override;
97+
98+
/**
99+
* Computes the forward kinematics for the end-effector
100+
* @param state Current state vector (only joint angles are used)
101+
* @return 4x4 homogeneous transformation matrix of the end-effector
102+
*/
103+
Eigen::Matrix4d getForwardKinematics(const Eigen::VectorXd& state) const;
104+
105+
/**
106+
* Gets the end-effector position in world coordinates
107+
* @param state Current state vector
108+
* @return 3D position vector of the end-effector
109+
*/
110+
Eigen::Vector3d getEndEffectorPosition(const Eigen::VectorXd& state) const;
111+
112+
/**
113+
* Helper function to compute transformation matrices
114+
* @param q1,q2,q3 Joint angles
115+
* @return Vector of 4x4 transformation matrices [T01, T12, T23, T34]
116+
*/
117+
std::vector<Eigen::Matrix4d> getTransformationMatrices(
118+
double q1, double q2, double q3) const;
119+
120+
/**
121+
* Get link length by identifier
122+
* @param link_id 'a' for la_, 'b' for lb_, 'c' for lc_
123+
* @return Link length value
124+
*/
125+
double getLinkLength(char link_id) const {
126+
switch(link_id) {
127+
case 'a': return la_;
128+
case 'b': return lb_;
129+
case 'c': return lc_;
130+
default: return 0.0;
131+
}
132+
}
133+
134+
private:
135+
// Link lengths (match MATLAB example)
136+
const double la_{1.0}; // Link a length
137+
const double lb_{0.2}; // Link b length
138+
const double lc_{1.0}; // Link c length
139+
const double gravity_{9.81};
140+
141+
// DH parameters for PUMA-like configuration
142+
const double alpha1_{-M_PI/2}; // rotation about x-axis between z0 and z1
143+
const double alpha2_{0.0}; // rotation about x-axis between z1 and z2
144+
const double alpha3_{0.0}; // rotation about x-axis between z2 and z3
145+
146+
// State dimensions
147+
static constexpr int NUM_JOINTS = 3;
148+
static constexpr int STATE_DIM = 2 * NUM_JOINTS; // positions and velocities
149+
static constexpr int CONTROL_DIM = NUM_JOINTS; // joint torques
150+
151+
/**
152+
* Computes the rotation matrix about the x-axis
153+
* @param alpha Angle of rotation
154+
* @return 4x4 rotation matrix
155+
*/
156+
Eigen::Matrix4d rotX(double alpha) const;
157+
158+
/**
159+
* Computes the rotation matrix about the y-axis
160+
* @param beta Angle of rotation
161+
* @return 4x4 rotation matrix
162+
*/
163+
Eigen::Matrix4d rotY(double beta) const;
164+
165+
/**
166+
* Computes the rotation matrix about the z-axis
167+
* @param theta Angle of rotation
168+
* @return 4x4 rotation matrix
169+
*/
170+
Eigen::Matrix4d rotZ(double theta) const;
171+
172+
/**
173+
* Computes the mass matrix M(q) of the manipulator
174+
* @param q Joint positions
175+
* @return Mass matrix
176+
*/
177+
Eigen::MatrixXd getMassMatrix(const Eigen::VectorXd& q) const;
178+
179+
/**
180+
* Computes the gravity compensation terms
181+
* @param q Joint positions
182+
* @return Gravity torque vector
183+
*/
184+
Eigen::VectorXd getGravityVector(const Eigen::VectorXd& q) const;
185+
};
186+
187+
} // namespace cddp
188+
189+
#endif // CDDP_MANIPULATOR_HPP

results/tests/manipulator_frame_0.png

83.9 KB

results/tests/manipulator_frame_1.png

84.1 KB
85 KB
85.1 KB
85.3 KB
85 KB
85.3 KB

0 commit comments

Comments
 (0)