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
189
190
191
192
193
194
195
196
197
#![allow(clippy::trivially_copy_pass_by_ref, clippy::ptr_arg)]
use std::f64::consts::PI;
use k::{nalgebra as na, nalgebra::RealField};
use num_traits::Float;
use trajectory::{CubicSpline, Trajectory};
use crate::errors::*;
type Limits<T> = Vec<Option<k::joint::Range<T>>>;
pub fn generate_clamped_joint_positions_from_limits<T>(
angles: &[T],
limits: &Limits<T>,
) -> Result<Vec<T>>
where
T: RealField,
{
if angles.len() != limits.len() {
return Err(Error::DofMismatch(angles.len(), limits.len()));
}
Ok(limits
.iter()
.zip(angles.iter())
.map(|(range, angle)| match *range {
Some(ref range) => {
if *angle > range.max {
range.max
} else if *angle < range.min {
range.min
} else {
*angle
}
}
None => *angle,
})
.collect())
}
#[deprecated(
since = "0.7.0",
note = "Please use k::Chain::set_joint_positions_clamped"
)]
pub fn set_clamped_joint_positions<T>(chain: &k::Chain<T>, vec: &[T]) -> Result<()>
where
T: RealField + k::SubsetOf<f64>,
{
let limits = chain.iter_joints().map(|j| j.limits).collect::<Vec<_>>();
let clamped = generate_clamped_joint_positions_from_limits(vec, &limits)?;
chain.set_joint_positions(&clamped)?;
Ok(())
}
pub fn generate_random_joint_positions_from_limits<T>(limits: &Limits<T>) -> Vec<T>
where
T: RealField,
{
limits
.iter()
.map(|range| match *range {
Some(ref range) => (range.max - range.min) * na::convert(rand::random()) + range.min,
None => na::convert::<f64, T>(rand::random::<f64>() - 0.5) * na::convert(2.0 * PI),
})
.collect()
}
#[track_caller]
pub fn modify_to_nearest_angle<T>(vec1: &[T], vec2: &mut [T], limits: &Limits<T>)
where
T: RealField,
{
assert_eq!(vec1.len(), vec2.len());
for i in 0..vec1.len() {
if limits[i].is_none() {
let pi2 = T::pi() * na::convert(2.0);
let dist1 = (vec1[i] - vec2[i]).abs();
let dist2 = (vec1[i] - (vec2[i] - pi2)).abs();
if dist1 > dist2 {
vec2[i] -= pi2;
} else {
let dist3 = (vec1[i] - (vec2[i] + pi2)).abs();
if dist1 > dist3 {
vec2[i] += pi2;
}
}
}
}
}
#[derive(Debug, Clone)]
pub struct TrajectoryPoint<T> {
pub position: Vec<T>,
pub velocity: Vec<T>,
pub acceleration: Vec<T>,
}
impl<T> TrajectoryPoint<T> {
pub fn new(position: Vec<T>, velocity: Vec<T>, acceleration: Vec<T>) -> Self {
Self {
position,
velocity,
acceleration,
}
}
}
pub fn interpolate<T>(
points: &[Vec<T>],
total_duration: T,
unit_duration: T,
) -> Option<Vec<TrajectoryPoint<T>>>
where
T: Float,
{
let key_frame_unit_duration = total_duration / (T::from(points.len())? - T::one());
let times = (0_usize..points.len())
.map(|i| T::from(i).unwrap() * key_frame_unit_duration)
.collect::<Vec<T>>();
assert_eq!(times.len(), points.len());
let spline = CubicSpline::new(times, points.to_vec())?;
let mut t = T::zero();
let mut ret = Vec::with_capacity(points.len());
while t < total_duration {
ret.push(TrajectoryPoint {
position: spline.position(t)?,
velocity: spline.velocity(t)?,
acceleration: spline.acceleration(t)?,
});
t = t + unit_duration;
}
ret.push(TrajectoryPoint {
position: spline.position(total_duration)?,
velocity: spline.velocity(total_duration)?,
acceleration: spline.acceleration(total_duration)?,
});
Some(ret)
}
pub fn set_random_joint_positions<T>(robot: &k::Chain<T>) -> ::std::result::Result<(), k::Error>
where
T: RealField + k::SubsetOf<f64>,
{
let limits = robot.iter_joints().map(|j| j.limits).collect();
robot.set_joint_positions(&generate_random_joint_positions_from_limits(&limits))
}