k/joint/
mimic.rs

1/*
2  Copyright 2020 Takashi Ogura
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      http://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*/
16use nalgebra::RealField;
17
18/// Information for copying joint state of other joint
19///
20/// For example, `Mimic` is used to calculate the position of the gripper(R) from
21/// gripper(L). In that case, the code like below will be used.
22///
23/// ```
24/// let mimic_for_gripper_r = k::joint::Mimic::new(-1.0, 0.0);
25/// ```
26///
27/// output position (mimic_position() is calculated by `joint positions = joint[name] * multiplier + origin`
28///
29#[derive(Debug, Clone)]
30pub struct Mimic<T: RealField> {
31    pub multiplier: T,
32    pub origin: T,
33}
34
35impl<T> Mimic<T>
36where
37    T: RealField,
38{
39    /// Create new instance of Mimic
40    ///
41    /// # Examples
42    ///
43    /// ```
44    /// let m = k::joint::Mimic::<f64>::new(1.0, 0.5);
45    /// ```
46    pub fn new(multiplier: T, origin: T) -> Self {
47        Mimic { multiplier, origin }
48    }
49    /// Calculate the mimic joint position
50    ///
51    /// # Examples
52    ///
53    /// ```
54    /// let m = k::joint::Mimic::<f64>::new(1.0, 0.5);
55    /// assert_eq!(m.mimic_position(0.2), 0.7); // 0.2 * 1.0 + 0.5
56    /// ```
57    ///
58    /// ```
59    /// let m = k::joint::Mimic::<f64>::new(-2.0, -0.4);
60    /// assert_eq!(m.mimic_position(0.2), -0.8); // 0.2 * -2.0 - 0.4
61    /// ```
62    pub fn mimic_position(&self, from_position: T) -> T {
63        from_position * self.multiplier.clone() + self.origin.clone()
64    }
65}