1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use nalgebra::{DMatrix};
use crate::segment::{Segment};
use crate::joint::{JointType};
#[derive(Clone)]
pub struct Chain {
segments: Vec<Segment>,
num_joints: usize,
num_ind_joints: usize,
num_unlocked_joints: usize,
locked_joints: Vec<bool>,
coupling_matrix: DMatrix<f64>,
}
impl Chain {
pub fn default() -> Chain {
Chain {
segments: Vec::new(),
num_joints: 0,
num_ind_joints: 0,
num_unlocked_joints: 0,
locked_joints: vec![],
coupling_matrix: DMatrix::identity(0,0),
}
}
pub fn get_num_segments(&self) -> usize {
self.segments.len()
}
pub fn get_num_joints(&self) -> usize {
self.num_joints
}
pub fn get_num_ind_joints(&self) -> usize {
self.num_ind_joints
}
pub fn get_coupling_matrix(&self) -> DMatrix<f64> {
self.coupling_matrix.clone()
}
pub fn add_segment(&mut self, segment: Segment) {
self.segments.push(segment);
if segment.get_joint_type() != JointType::NoJoint {
self.num_joints += 1;
self.locked_joints.push(false);
self.num_unlocked_joints += 1;
self.num_ind_joints = self.num_unlocked_joints;
self.coupling_matrix = DMatrix::identity(
self.num_unlocked_joints, self.num_ind_joints);
}
}
pub fn add_chain(&mut self, mut chain: Chain) {
self.segments.append(&mut chain.segments);
self.num_joints += chain.num_joints;
}
pub fn get_segment(&self, index: usize) -> Segment {
self.segments[index]
}
pub fn get_locked_joints(&self) -> Vec<bool> {
self.locked_joints.clone()
}
pub fn set_coupling(&mut self, new_locked_joints: Vec<bool>,
new_coupling_matrix: DMatrix<f64>){
let new_num_unlocked_joints = new_locked_joints
.iter().filter(|&n| !(*n)).count();
if new_coupling_matrix.nrows() != new_num_unlocked_joints{
panic!("Wrong size of coupling arguments. Matrix has {} rows,
but the vector says there should be {}",
new_coupling_matrix.nrows(),
new_num_unlocked_joints);
}
self.locked_joints = new_locked_joints;
self.num_unlocked_joints = new_num_unlocked_joints;
self.num_ind_joints = new_coupling_matrix.ncols();
self.coupling_matrix = new_coupling_matrix;
}
}
#[cfg(test)]
pub mod tests{
use crate::geometry::{Frame, EulerBuild};
use crate::joint::{Joint, JointType};
use crate::segment::{Segment};
use crate::chains::{Chain};
pub fn create_testing_chain() -> Chain {
let mut chain = Chain::default();
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::NoJoint),
Frame::from_translation_euler(
0.1, -0.2, 0.3, -0.1, -0.2, 0.3),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
0.2, 0.5, 0.1, 0.58, -1.2, 2.3),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
0.02, 0.3, -0.81, 3.0, -1.7, 1.3),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
0.4, -0.27, 0.19, 1.57, -1.57, 0.0),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
0.24, -0.18, 0.16, 0.01, 0.0, -0.01),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
1.0, -0.5, -0.01, 0.05, -1.02, 0.34),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
0.02, 0.58, -0.87, 0.95, -1.74, 0.68),
0.0
));
chain.add_segment(Segment::new(
Joint::default().set_type(JointType::RotZ),
Frame::from_translation_euler(
0.2, 0.1, -0.1, -0.76, -0.94, 0.25),
0.0
));
chain
}
}