Kinematics Animation Multithread
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Finger Class Reference

This class generates a finger of n links. The number of links and their properties are defined by a json configuration file. It also calculates the forwared kinematics of the finger given the rotation of its joints and its origin. More...

#include <finger.h>

Collaboration diagram for Finger:
Collaboration graph
[legend]

Public Member Functions

 Finger ()
 
void initialize (const std::string &name_id, const nlohmann::json &json_file, igl::opengl::glfw::Viewer *viewer, int mesh_idx)
 Initialize finger. More...
 
void update (const std::vector< dm::JointState > &state)
 Update state. More...
 
std::vector< Eigen::MatrixXd > get_vertices (void)
 Get finger vertices. More...
 
std::vector< int > get_frame_ids (void)
 Get the ids of the finger frames. More...
 
void load_mesh_files (igl::opengl::glfw::Viewer *viewer)
 Load finger mesh files. More...
 
std::vector< dm::JointStateget_state (void)
 Get current state of the finger. More...
 

Private Member Functions

void parse_json_file (const nlohmann::json &json_file)
 Parses the finger configuration json file. More...
 
void initialize_mesh_containers (void)
 Initialize mesh files. More...
 
void get_mesh_data (igl::opengl::glfw::Viewer *viewer)
 Get mesh data. More...
 
void postprocess_meshes (void)
 Postproccess meshes. More...
 
void initialize_state (const std::vector< double > &link_lengths, const dm::JointState &origin)
 Initialize state. More...
 

Private Attributes

std::string m_name_id
 Finger name id. More...
 
dm::JointState m_origin
 Finger origin. More...
 
std::vector< double > m_link_lengths
 The lengths of the finger links. More...
 
std::vector< int > m_frame_ids
 The frame ids of the finger. More...
 
std::string m_joint_rel_filename = "share/joint.obj"
 Joint mesh file. More...
 
std::string m_bone_rel_filename = "share/bone.obj"
 Bone (link) mesh file. More...
 
std::vector< std::string > m_meshes_filenames
 Mesh files for joints and bones(links). More...
 
double m_joint_scale = 0.05
 Joint scales. More...
 
std::vector< double > m_geom_scales
 Finger scales. More...
 
int m_viewer_data_lower_idx
 Viewer data lower idx see (Hand::m_viewer_data_lower_idx). More...
 
int m_viewer_data_upper_idx
 Viewer data upper idx (see Hand::m_viewer_data_lower_idx). More...
 
std::vector< Eigen::MatrixXd > m_vertices_data_o
 Vertices data (original). More...
 
std::vector< Eigen::MatrixXd > m_vertices_data_oh
 Vertices data (original homogeneous). More...
 
std::vector< Eigen::MatrixXd > m_vertices_data
 Vertices data. More...
 
std::vector< Eigen::MatrixXi > m_faces_data
 Faces data. More...
 
std::vector< dm::JointStatem_state_vec
 Finger state. More...
 
int m_state_size
 State size. More...
 
std::vector< Eigen::Matrix4d > m_local_transform
 Local transforamation matrix. More...
 
std::vector< Eigen::Matrix4d > m_global_transform
 Global transforamation matrix. More...
 

Detailed Description

This class generates a finger of n links. The number of links and their properties are defined by a json configuration file. It also calculates the forwared kinematics of the finger given the rotation of its joints and its origin.

Definition at line 20 of file finger.h.

Constructor & Destructor Documentation

◆ Finger()

Finger::Finger ( )
inline

Definition at line 23 of file finger.h.

23{};

Member Function Documentation

◆ get_frame_ids()

std::vector< int > Finger::get_frame_ids ( void  )
inline

Get the ids of the finger frames.

Definition at line 36 of file finger.h.

36{ return m_frame_ids; };
std::vector< int > m_frame_ids
The frame ids of the finger.
Definition: finger.h:56

◆ get_mesh_data()

void Finger::get_mesh_data ( igl::opengl::glfw::Viewer *  viewer)
private

Get mesh data.

It passes a copy of the vertex data from the viewer to the local member variables of the finger instance.

Parameters
viewerPointer to the viewer object.

Definition at line 197 of file finger.cpp.

198{
199 for (size_t i = m_viewer_data_lower_idx; i <= m_viewer_data_upper_idx; i++)
200 {
201 // Push back vertices data
202 m_vertices_data_o.push_back(viewer->data_list.at(i).V);
203
204 // Push back faces data
205 m_faces_data.push_back(viewer->data_list.at(i).F);
206 }
207}
int m_viewer_data_lower_idx
Viewer data lower idx see (Hand::m_viewer_data_lower_idx).
Definition: finger.h:82
std::vector< Eigen::MatrixXd > m_vertices_data_o
Vertices data (original).
Definition: finger.h:93
std::vector< Eigen::MatrixXi > m_faces_data
Faces data.
Definition: finger.h:102
int m_viewer_data_upper_idx
Viewer data upper idx (see Hand::m_viewer_data_lower_idx).
Definition: finger.h:85
Here is the caller graph for this function:

◆ get_state()

std::vector< dm::JointState > Finger::get_state ( void  )
inline

Get current state of the finger.

Definition at line 42 of file finger.h.

42{ return m_state_vec; }
std::vector< dm::JointState > m_state_vec
Finger state.
Definition: finger.h:114

◆ get_vertices()

std::vector< Eigen::MatrixXd > Finger::get_vertices ( void  )
inline

Get finger vertices.

Definition at line 33 of file finger.h.

33{ return m_vertices_data; }
std::vector< Eigen::MatrixXd > m_vertices_data
Vertices data.
Definition: finger.h:99

◆ initialize()

void Finger::initialize ( const std::string &  name_id,
const nlohmann::json &  json_file,
igl::opengl::glfw::Viewer *  viewer,
int  mesh_idx 
)

Initialize finger.

This initialization function first parses the finger configuration file (see Hand::m_config_rel_path) and sets up the properties of the finger. It also loads and processes the meshes for the links and the joints and initializes the finger state.

Parameters
name_idThe name id of the finger.
json_fileThe json finger configuration file.
viewerPointer to the viewer handle.
mesh_idxThe mesh index.

Definition at line 13 of file finger.cpp.

16{
17 // Get finger id
18 m_name_id = name_id;
19
20 // Parse the json file
21 parse_json_file(json_file);
22
23 // Initialize mesh files
25
26 // Load mesh files
27 load_mesh_files(viewer);
28
29 // Set viewer data indices
30 m_viewer_data_lower_idx = mesh_idx;
32 (m_meshes_filenames.size() - 1);
33
34 // Get mesh data
35 get_mesh_data(viewer);
36
37 // Postprocess meshes
39
40 // Initialize state
42
43 // Initialize meshes
45}
void initialize_state(const std::vector< double > &link_lengths, const dm::JointState &origin)
Initialize state.
Definition: finger.cpp:130
std::vector< double > m_link_lengths
The lengths of the finger links.
Definition: finger.h:53
void load_mesh_files(igl::opengl::glfw::Viewer *viewer)
Load finger mesh files.
Definition: finger.cpp:184
void postprocess_meshes(void)
Postproccess meshes.
Definition: finger.cpp:213
std::vector< std::string > m_meshes_filenames
Mesh files for joints and bones(links).
Definition: finger.h:70
void parse_json_file(const nlohmann::json &json_file)
Parses the finger configuration json file.
Definition: finger.cpp:248
dm::JointState m_origin
Finger origin.
Definition: finger.h:50
void update(const std::vector< dm::JointState > &state)
Update state.
Definition: finger.cpp:60
void get_mesh_data(igl::opengl::glfw::Viewer *viewer)
Get mesh data.
Definition: finger.cpp:197
std::string m_name_id
Finger name id.
Definition: finger.h:47
void initialize_mesh_containers(void)
Initialize mesh files.
Definition: finger.cpp:160
Here is the call graph for this function:

◆ initialize_mesh_containers()

void Finger::initialize_mesh_containers ( void  )
private

Initialize mesh files.

It initializes the mesh containers for its link and joint based on their properties defined in the configuration file Hand::m_config_rel_path.

Definition at line 160 of file finger.cpp.

161{
162 // Get mesh files absolute filenames
163 auto joint_mesh_abs = std::filesystem::current_path() / m_joint_rel_filename;
164 auto bone_mesh_abs = std::filesystem::current_path() / m_bone_rel_filename;
165
166 // Generate meshses filenames
167 for (size_t i = 0; i < m_link_lengths.size(); i++)
168 {
169 // Meshes filenames configuration
170 m_meshes_filenames.push_back(joint_mesh_abs.string());
171 m_meshes_filenames.push_back(bone_mesh_abs.string());
172
173 // Geometry scales configurations
174 m_geom_scales.push_back(m_joint_scale);
175 m_geom_scales.push_back(m_link_lengths.at(i));
176 }
177}
std::string m_joint_rel_filename
Joint mesh file.
Definition: finger.h:64
double m_joint_scale
Joint scales.
Definition: finger.h:76
std::string m_bone_rel_filename
Bone (link) mesh file.
Definition: finger.h:67
std::vector< double > m_geom_scales
Finger scales.
Definition: finger.h:79
Here is the caller graph for this function:

◆ initialize_state()

void Finger::initialize_state ( const std::vector< double > &  link_lengths,
const dm::JointState origin 
)
private

Initialize state.

It initializes the state of the finger based on the link_lengths and their origins as defined from the configuration file (Hand::m_config_rel_path)

Parameters
link_lengthsThe lengths of the links.
originThe origins of the links.

Definition at line 130 of file finger.cpp.

132{
133 // State size
134 m_state_size = link_lengths.size();
135
136 // Initialize state vector
138
139 // Set origin
140 m_state_vec.at(0) = origin;
141
142 // Generate initial state vector states
143 int idx = 0;
144
145 for (size_t i = 1; i < m_state_size; i++)
146 {
147 // Set link lengths
148 m_state_vec.at(i).position(0) = link_lengths.at(idx);
149
150 // Update idx
151 idx++;
152 }
153}
int m_state_size
State size.
Definition: finger.h:117
Here is the caller graph for this function:

◆ load_mesh_files()

void Finger::load_mesh_files ( igl::opengl::glfw::Viewer *  viewer)

Load finger mesh files.

Simply loads the meshes to the viewer.

Parameters
viewerPointer to the viewer object.

Definition at line 184 of file finger.cpp.

185{
186 for (size_t i = 0; i < m_meshes_filenames.size(); i++)
187 {
188 viewer->load_mesh_from_file(m_meshes_filenames.at(i));
189 }
190}
Here is the caller graph for this function:

◆ parse_json_file()

void Finger::parse_json_file ( const nlohmann::json &  json_file)
private

Parses the finger configuration json file.

Simply parses the finger configuration file.

Parameters
json_fileThe json file namek

Definition at line 248 of file finger.cpp.

249{
250 // Get lengths
251 auto lengths_json = json_file[m_name_id]["Lengths"];
252
253 // Get frame ids
254 auto frames_json = json_file[m_name_id]["Frames"];
255
256 // Get origin position
257 auto origin_position_json = json_file[m_name_id]["Origin"]["Position"];
258
259 // Get origin orientation
260 auto origin_euler_json = json_file[m_name_id]["Origin"]["Euler"];
261
262 for(size_t i = 0; i < lengths_json.size(); i++)
263 {
264 // Push back link lengths
265 m_link_lengths.push_back(lengths_json.at(i));
266
267 // Set origin position
268 m_origin.position(0) = origin_position_json.at(0);
269 m_origin.position(1) = origin_position_json.at(1);
270 m_origin.position(2) = origin_position_json.at(2);
271
272 // Set origin orientation
273 m_origin.euler(0) = origin_euler_json.at(0);
274 m_origin.euler(1) = origin_euler_json.at(1);
275 m_origin.euler(2) = origin_euler_json.at(2);
276 }
277
278 for(size_t i = 0; i < frames_json.size(); i++)
279 {
280 m_frame_ids.push_back(frames_json.at(i));
281 }
282}
Eigen::Vector3d position
Definition: dynamics_math.h:21
Eigen::Vector3d euler
Definition: dynamics_math.h:24
Here is the caller graph for this function:

◆ postprocess_meshes()

void Finger::postprocess_meshes ( void  )
private

Postproccess meshes.

It sets the scale of the meshes based on the links length and the joints desired size.

Definition at line 213 of file finger.cpp.

214{
215 // Scale meshes
216 for (size_t i = 0; i < m_vertices_data_o.size(); i++)
217 {
218 /************** Scale data *******************/
219 Eigen::Matrix3d scale_matrix;
220 scale_matrix << m_geom_scales.at(i), 0.0, 0.0,
221 0.0, m_geom_scales.at(i), 0.0, 0.0, 0.0, m_geom_scales.at(i);
222
223 m_vertices_data_o.at(i) = (scale_matrix *
224 m_vertices_data_o.at(i).transpose()).transpose();
225
226 /************** Generate homogeneous vertices data *******************/
227 // Get mesh i
228 Eigen::MatrixXd mesh_i = m_vertices_data_o.at(i);
229
230 // Initialize homogeneous coordinates
231 Eigen::MatrixXd mesh_i_oh = Eigen::MatrixXd::Ones(mesh_i.rows(),
232 mesh_i.cols() + 1);
233
234 // Generate matrix of homogeneous coordinates
235 mesh_i_oh.col(0) = mesh_i.col(0);
236 mesh_i_oh.col(1) = mesh_i.col(1);
237 mesh_i_oh.col(2) = mesh_i.col(2);
238
239 m_vertices_data_oh.push_back(mesh_i_oh);
240 }
241}
std::vector< Eigen::MatrixXd > m_vertices_data_oh
Vertices data (original homogeneous).
Definition: finger.h:96
Here is the caller graph for this function:

◆ update()

void Finger::update ( const std::vector< dm::JointState > &  state)

Update state.

This function updates the finger's state (position and orientation of its links and joints). It performs the forward kinematics for each link of the finger. It first calculates the local transform of each links with respect to each previous link. Then it calculates the global transforms (with respect to the hand's base frame $ f_{{W}_{0}}$) by performing and iterative compound transormation (post-miltiply rules, see Forward Kinematics Spong * Robot Modeling and Control). The frame conventions and the definitions of the rotation and translation matrices used are given in the handover document.

Parameters
stateThe vector of joint euler angles and postions as defined in dm::JointStateu.

Definition at line 60 of file finger.cpp.

61{
62 // Update state vector
63 m_state_vec = state;
64
65 // Resize states
68
69 /* Loop through state vector components and
70 generate transformation matrices */
71 for (size_t i = 0; i < m_state_vec.size(); i++)
72 {
73 /*********** Local transformation ***********/
74 // Initialize transformation matrices
75 Eigen::Matrix4d local_transform = Eigen::Matrix4d::Identity();
76
77 // Position vector
78 local_transform.block<3, 1>(0, 3) = m_state_vec.at(i).position;
79
80 // Rotation matrix
81 local_transform.block<3, 3>(0, 0) =
83
84 // Push back locak transform
85 m_local_transform.at(i) = local_transform;
86
87 /*********** Global transformation ***********/
88 if (i == 0) {
89 // Global transform
91 }
92 else {
93 // Global transform
94 m_global_transform.at(i) = m_global_transform.at(i-1) *
96 }
97 }
98
99 // Resize vertices data
100 m_vertices_data.resize(2 * m_state_size);
101
102 // Loop through global transformation matrices
103 for (size_t i = 0; i < m_global_transform.size(); i++)
104 {
105 // Get global transformation matrix
106 Eigen::Matrix4d t_mat = m_global_transform.at(i);
107
108 /****************** Transform vertices ********************/
109 // Joint vertices
110 Eigen::MatrixXd joint_vert =
111 (t_mat * m_vertices_data_oh.at(2*i).transpose()).transpose();
112
113 // Link vertices
114 Eigen::MatrixXd link_vert =
115 (t_mat * m_vertices_data_oh.at(2*i+1).transpose()).transpose();
116
117 // Push back vertices
118 m_vertices_data.at(2*i) = joint_vert.leftCols<3>();
119 m_vertices_data.at(2*i+1) = link_vert.leftCols<3>();
120 }
121}
static Eigen::Matrix3d rotation(double phi, double theta, double psi)
Euler rotation matrix z-y'-x''.
std::vector< Eigen::Matrix4d > m_global_transform
Global transforamation matrix.
Definition: finger.h:123
std::vector< Eigen::Matrix4d > m_local_transform
Local transforamation matrix.
Definition: finger.h:120
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_bone_rel_filename

std::string Finger::m_bone_rel_filename = "share/bone.obj"
private

Bone (link) mesh file.

Definition at line 67 of file finger.h.

◆ m_faces_data

std::vector<Eigen::MatrixXi> Finger::m_faces_data
private

Faces data.

Definition at line 102 of file finger.h.

◆ m_frame_ids

std::vector<int> Finger::m_frame_ids
private

The frame ids of the finger.

Definition at line 56 of file finger.h.

◆ m_geom_scales

std::vector<double> Finger::m_geom_scales
private

Finger scales.

Definition at line 79 of file finger.h.

◆ m_global_transform

std::vector<Eigen::Matrix4d> Finger::m_global_transform
private

Global transforamation matrix.

Definition at line 123 of file finger.h.

◆ m_joint_rel_filename

std::string Finger::m_joint_rel_filename = "share/joint.obj"
private

Joint mesh file.

Definition at line 64 of file finger.h.

◆ m_joint_scale

double Finger::m_joint_scale = 0.05
private

Joint scales.

Definition at line 76 of file finger.h.

◆ m_link_lengths

std::vector<double> Finger::m_link_lengths
private

The lengths of the finger links.

Definition at line 53 of file finger.h.

◆ m_local_transform

std::vector<Eigen::Matrix4d> Finger::m_local_transform
private

Local transforamation matrix.

Definition at line 120 of file finger.h.

◆ m_meshes_filenames

std::vector<std::string> Finger::m_meshes_filenames
private

Mesh files for joints and bones(links).

Definition at line 70 of file finger.h.

◆ m_name_id

std::string Finger::m_name_id
private

Finger name id.

Definition at line 47 of file finger.h.

◆ m_origin

dm::JointState Finger::m_origin
private

Finger origin.

Definition at line 50 of file finger.h.

◆ m_state_size

int Finger::m_state_size
private

State size.

Definition at line 117 of file finger.h.

◆ m_state_vec

std::vector<dm::JointState> Finger::m_state_vec
private

Finger state.

Definition at line 114 of file finger.h.

◆ m_vertices_data

std::vector<Eigen::MatrixXd> Finger::m_vertices_data
private

Vertices data.

Definition at line 99 of file finger.h.

◆ m_vertices_data_o

std::vector<Eigen::MatrixXd> Finger::m_vertices_data_o
private

Vertices data (original).

Definition at line 93 of file finger.h.

◆ m_vertices_data_oh

std::vector<Eigen::MatrixXd> Finger::m_vertices_data_oh
private

Vertices data (original homogeneous).

Definition at line 96 of file finger.h.

◆ m_viewer_data_lower_idx

int Finger::m_viewer_data_lower_idx
private

Viewer data lower idx see (Hand::m_viewer_data_lower_idx).

Definition at line 82 of file finger.h.

◆ m_viewer_data_upper_idx

int Finger::m_viewer_data_upper_idx
private

Viewer data upper idx (see Hand::m_viewer_data_lower_idx).

Definition at line 85 of file finger.h.


The documentation for this class was generated from the following files: