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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
Copyright 2017 Takashi Ogura

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use std::{path::Path, sync::Arc};

use k::nalgebra as na;
use na::RealField;
use ncollide3d::shape::Compound;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::*;

use crate::{
    collision::{CollisionDetector, RobotCollisionDetector},
    errors::*,
    funcs::*,
};

/// Collision Avoidance Path Planner
pub struct JointPathPlanner<N>
where
    N: RealField + Copy + k::SubsetOf<f64>,
{
    /// Robot for reference (read only and assumed to hold the latest full states)
    reference_robot: Arc<k::Chain<N>>,
    /// Robot collision detector
    robot_collision_detector: RobotCollisionDetector<N>,
    /// Unit length for searching
    ///
    /// If the value is large, the path become sparse.
    pub step_length: N,
    /// Max num of RRT search loop
    pub max_try: usize,
    /// Num of path smoothing trials
    pub num_smoothing: usize,
}

impl<N> JointPathPlanner<N>
where
    N: RealField + num_traits::Float + k::SubsetOf<f64>,
{
    /// Create `JointPathPlanner`
    pub fn new(
        reference_robot: Arc<k::Chain<N>>,
        robot_collision_detector: RobotCollisionDetector<N>,
        step_length: N,
        max_try: usize,
        num_smoothing: usize,
    ) -> Self {
        JointPathPlanner {
            reference_robot,
            robot_collision_detector,
            step_length,
            max_try,
            num_smoothing,
        }
    }

    /// Check if the joint_positions are OK
    fn is_feasible(
        &self,
        using_joints: &k::Chain<N>,
        joint_positions: &[N],
        objects: &Compound<N>,
    ) -> bool {
        match using_joints.set_joint_positions(joint_positions) {
            Ok(()) => !self.robot_collision_detector.is_collision_detected(objects),
            Err(err) => {
                debug!("is_feasible: {err}");
                false
            }
        }
    }

    /// Check if the joint_positions are OK
    fn is_feasible_with_self(&self, using_joints: &k::Chain<N>, joint_positions: &[N]) -> bool {
        match using_joints.set_joint_positions(joint_positions) {
            Ok(()) => !self.robot_collision_detector.is_self_collision_detected(),
            Err(err) => {
                debug!("is_feasible: {err}");
                false
            }
        }
    }

    /// Plan the sequence of joint angles of `using_joints`
    ///
    /// # Arguments
    ///
    /// - `using_joints`: part of collision_check_robot. the dof of the following angles must be same as this model.
    /// - `start_angles`: initial joint angles of `using_joints`.
    /// - `goal_angles`: goal joint angles of `using_joints`.
    /// - `objects`: The collision between `self.collision_check_robot` and `objects` will be checked.
    pub fn plan(
        &self,
        using_joint_names: &[String],
        start_angles: &[N],
        goal_angles: &[N],
        objects: &Compound<N>,
    ) -> Result<Vec<Vec<N>>> {
        self.sync_joint_positions_with_reference();

        let using_joints =
            create_chain_from_joint_names(self.collision_check_robot(), using_joint_names)?;
        let limits = using_joints.iter_joints().map(|j| j.limits).collect();
        let step_length = self.step_length;
        let max_try = self.max_try;
        let current_angles = using_joints.joint_positions();

        if !self.is_feasible(&using_joints, start_angles, objects) {
            let collision_link_names = self.env_collision_link_names(objects);
            using_joints.set_joint_positions(&current_angles)?;
            return Err(Error::Collision {
                point: UnfeasibleTrajectoryPoint::Start,
                collision_link_names,
            });
        } else if !self.is_feasible(&using_joints, goal_angles, objects) {
            let collision_link_names = self.env_collision_link_names(objects);
            using_joints.set_joint_positions(&current_angles)?;
            return Err(Error::Collision {
                point: UnfeasibleTrajectoryPoint::Goal,
                collision_link_names,
            });
        }

        let mut path = match rrt::dual_rrt_connect(
            start_angles,
            goal_angles,
            |angles: &[N]| self.is_feasible(&using_joints, angles, objects),
            || generate_random_joint_positions_from_limits(&limits),
            step_length,
            max_try,
        ) {
            Ok(p) => p,
            Err(error) => {
                using_joints.set_joint_positions(&current_angles)?;
                return Err(Error::PathPlanFail(error));
            }
        };
        let num_smoothing = self.num_smoothing;
        rrt::smooth_path(
            &mut path,
            |angles: &[N]| self.is_feasible(&using_joints, angles, objects),
            step_length,
            num_smoothing,
        );

        // The joint positions of using_joint can be changed in the smoothing,
        // so we need to surely set the goal at the end.
        using_joints.set_joint_positions(goal_angles)?;
        Ok(path)
    }

    /// Plan the sequence of joint angles of `using_joints` to avoid self collision.
    ///
    /// # Arguments
    ///
    /// - `using_joints`: part of collision_check_robot. the dof of the following angles must be same as this model.
    /// - `start_angles`: initial joint angles of `using_joints`.
    /// - `goal_angles`: goal joint angles of `using_joints`.
    pub fn plan_avoid_self_collision(
        &self,
        using_joint_names: &[String],
        start_angles: &[N],
        goal_angles: &[N],
    ) -> Result<Vec<Vec<N>>> {
        self.sync_joint_positions_with_reference();

        let using_joints =
            create_chain_from_joint_names(self.collision_check_robot(), using_joint_names)?;
        let limits = using_joints.iter_joints().map(|j| j.limits).collect();
        let step_length = self.step_length;
        let max_try = self.max_try;
        let current_angles = using_joints.joint_positions();

        if !self.is_feasible_with_self(&using_joints, start_angles) {
            let collision_link_names = self.self_collision_link_pairs();
            using_joints.set_joint_positions(&current_angles)?;
            return Err(Error::SelfCollision {
                point: UnfeasibleTrajectoryPoint::Start,
                collision_link_names,
            });
        } else if !self.is_feasible_with_self(&using_joints, goal_angles) {
            let collision_link_names = self.self_collision_link_pairs();
            using_joints.set_joint_positions(&current_angles)?;
            return Err(Error::SelfCollision {
                point: UnfeasibleTrajectoryPoint::Goal,
                collision_link_names,
            });
        }

        let mut path = match rrt::dual_rrt_connect(
            start_angles,
            goal_angles,
            |angles: &[N]| self.is_feasible_with_self(&using_joints, angles),
            || generate_random_joint_positions_from_limits(&limits),
            step_length,
            max_try,
        ) {
            Ok(p) => p,
            Err(error) => {
                using_joints.set_joint_positions(&current_angles)?;
                return Err(Error::PathPlanFail(error));
            }
        };
        let num_smoothing = self.num_smoothing;
        rrt::smooth_path(
            &mut path,
            |angles: &[N]| self.is_feasible_with_self(&using_joints, angles),
            step_length,
            num_smoothing,
        );
        Ok(path)
    }

    /// Synchronize joint positions of the planning robot model with the reference robot
    pub fn sync_joint_positions_with_reference(&self) {
        self.collision_check_robot()
            .set_joint_positions_clamped(self.reference_robot.joint_positions().as_slice());
    }

    /// Calculate the transforms of all of the links
    pub fn update_transforms(&self) -> Vec<na::Isometry3<N>> {
        self.collision_check_robot().update_transforms()
    }

    /// Get the names of the links
    pub fn joint_names(&self) -> Vec<String> {
        self.collision_check_robot()
            .iter_joints()
            .map(|j| j.name.clone())
            .collect()
    }

    /// Get the robot model used for collision checking
    pub fn collision_check_robot(&self) -> &k::Chain<N> {
        &self.robot_collision_detector.robot
    }

    /// Get names of links colliding with environmental objects
    /// objects: environmental objects
    pub fn env_collision_link_names(&self, objects: &Compound<N>) -> Vec<String> {
        self.robot_collision_detector
            .env_collision_link_names(objects)
    }

    /// Get names of self-colliding links
    pub fn self_collision_link_pairs(&self) -> Vec<(String, String)> {
        self.robot_collision_detector.self_collision_link_pairs()
    }
}

/// Builder pattern to create `JointPathPlanner`
pub struct JointPathPlannerBuilder<N>
where
    N: RealField + Copy + k::SubsetOf<f64>,
{
    reference_robot: Option<Arc<k::Chain<N>>>,
    robot_collision_detector: RobotCollisionDetector<N>,
    step_length: N,
    max_try: usize,
    num_smoothing: usize,
    collision_check_margin: Option<N>,
    self_collision_pairs: Vec<(String, String)>,
}

impl<N> JointPathPlannerBuilder<N>
where
    N: RealField + num_traits::Float + k::SubsetOf<f64>,
{
    /// Create from components
    ///
    /// There are also some utility functions to create from urdf
    pub fn new(robot_collision_detector: RobotCollisionDetector<N>) -> Self {
        JointPathPlannerBuilder {
            reference_robot: None,
            robot_collision_detector,
            step_length: na::convert(0.1),
            max_try: 5000,
            num_smoothing: 100,
            collision_check_margin: None,
            self_collision_pairs: vec![],
        }
    }

    pub fn reference_robot(mut self, robot: Arc<k::Chain<N>>) -> Self {
        self.reference_robot = Some(robot);
        self
    }

    pub fn collision_check_margin(mut self, length: N) -> Self {
        self.collision_check_margin = Some(length);
        self
    }

    pub fn step_length(mut self, step_length: N) -> Self {
        self.step_length = step_length;
        self
    }

    pub fn max_try(mut self, max_try: usize) -> Self {
        self.max_try = max_try;
        self
    }

    pub fn num_smoothing(mut self, num_smoothing: usize) -> Self {
        self.num_smoothing = num_smoothing;
        self
    }

    pub fn self_collision_pairs(mut self, self_collision_pairs: Vec<(String, String)>) -> Self {
        self.self_collision_pairs = self_collision_pairs;
        self
    }

    pub fn finalize(mut self) -> Result<JointPathPlanner<N>> {
        if let Some(margin) = self.collision_check_margin {
            self.robot_collision_detector.collision_detector.prediction = margin;
        }

        match self.reference_robot {
            Some(robot) => {
                let mut planner = JointPathPlanner::new(
                    robot,
                    self.robot_collision_detector,
                    self.step_length,
                    self.max_try,
                    self.num_smoothing,
                );
                planner.robot_collision_detector.self_collision_pairs = self.self_collision_pairs;
                Ok(planner)
            }
            None => Err(Error::ReferenceRobot("JointPathBuilder".to_owned())),
        }
    }

    /// Try to create `JointPathPlannerBuilder` instance from URDF file and end link name
    pub fn from_urdf_file<P>(file: P) -> Result<JointPathPlannerBuilder<N>>
    where
        P: AsRef<Path>,
    {
        let urdf_robot = urdf_rs::utils::read_urdf_or_xacro(file.as_ref())?;
        Ok(JointPathPlannerBuilder::from_urdf_robot_with_base_dir(
            urdf_robot,
            file.as_ref().parent(),
        ))
    }

    /// Try to create `JointPathPlannerBuilder` instance from `urdf_rs::Robot` instance
    pub fn from_urdf_robot_with_base_dir(
        urdf_robot: urdf_rs::Robot,
        base_path: Option<&Path>,
    ) -> JointPathPlannerBuilder<N> {
        let robot = k::Chain::from(&urdf_robot);
        let default_margin = na::convert(0.0);
        let collision_detector = CollisionDetector::from_urdf_robot_with_base_dir(
            &urdf_robot,
            base_path,
            default_margin,
        );
        let robot_collision_detector =
            RobotCollisionDetector::new(robot, collision_detector, vec![]);
        JointPathPlannerBuilder::new(robot_collision_detector)
    }
}

#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct JointPathPlannerConfig {
    #[serde(default = "default_step_length")]
    step_length: f64,
    #[serde(default = "default_max_try")]
    max_try: usize,
    #[serde(default = "default_num_smoothing")]
    num_smoothing: usize,
    #[serde(default = "default_margin")]
    margin: f64,
}

fn default_step_length() -> f64 {
    0.1
}

fn default_max_try() -> usize {
    5000
}

fn default_num_smoothing() -> usize {
    100
}

fn default_margin() -> f64 {
    0.001
}

impl Default for JointPathPlannerConfig {
    fn default() -> Self {
        Self {
            step_length: default_step_length(),
            max_try: default_max_try(),
            num_smoothing: default_num_smoothing(),
            margin: default_margin(),
        }
    }
}

pub fn create_joint_path_planner<P: AsRef<Path>>(
    urdf_path: P,
    self_collision_check_pairs: Vec<(String, String)>,
    config: &JointPathPlannerConfig,
    robot: Arc<k::Chain<f64>>,
) -> JointPathPlanner<f64> {
    JointPathPlannerBuilder::from_urdf_file(urdf_path)
        .unwrap()
        .step_length(config.step_length)
        .max_try(config.max_try)
        .num_smoothing(config.num_smoothing)
        .collision_check_margin(config.margin)
        .self_collision_pairs(self_collision_check_pairs)
        .reference_robot(robot)
        .finalize()
        .unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::collision::create_all_collision_pairs;

    #[test]
    fn from_urdf() {
        let urdf_path = Path::new("sample.urdf");
        let _planner = JointPathPlannerBuilder::from_urdf_file(urdf_path)
            .unwrap()
            .collision_check_margin(0.01)
            .finalize();
    }

    #[test]
    fn test_create_joint_path_planner() {
        let urdf_path = Path::new("sample.urdf");
        let robot = k::Chain::from_urdf_file(urdf_path).unwrap();
        let planner = create_joint_path_planner(
            urdf_path,
            create_all_collision_pairs(&robot),
            &JointPathPlannerConfig::default(),
            Arc::new(robot),
        );

        let l_shoulder_yaw_node = planner
            .collision_check_robot()
            .find("l_shoulder_yaw")
            .unwrap();
        let using_joints = k::SerialChain::from_end(l_shoulder_yaw_node);
        let using_joint_names = using_joints
            .iter_joints()
            .map(|j| j.name.to_owned())
            .collect::<Vec<String>>();

        assert!(planner
            .plan_avoid_self_collision(using_joint_names.as_slice(), &[0.0], &[0.0],)
            .is_ok());
        // an error occurs in the start
        assert!(planner
            .plan_avoid_self_collision(using_joint_names.as_slice(), &[1.57], &[0.0],)
            .is_err());
        // an error occurs in the goal
        assert!(planner
            .plan_avoid_self_collision(using_joint_names.as_slice(), &[0.0], &[1.57],)
            .is_err());
    }

    // This test potentially fails because of RRT-based planning,
    // i.e. the success rate is not 100%.
    // Therefore, this test is treated as a `flaky test`.
    #[flaky_test::flaky_test]
    fn test_plan_avoid_self_collision() {
        let urdf_path = Path::new("sample.urdf");
        let robot = k::Chain::from_urdf_file(urdf_path).unwrap();

        // cross the arms only by moving the right arm
        k::SerialChain::from_end(robot.find("r_tool_fixed").unwrap())
            .set_joint_positions_clamped(&[0.9, 0.0, 0.0, 0.0, 0.67, 0.0]);

        let planner = create_joint_path_planner(
            urdf_path,
            create_all_collision_pairs(&robot),
            &JointPathPlannerConfig::default(),
            Arc::new(robot),
        );

        let l_tool_node = planner
            .collision_check_robot()
            .find("l_tool_fixed")
            .unwrap();
        let using_joints = k::SerialChain::from_end(l_tool_node);
        let using_joint_names = using_joints
            .iter_joints()
            .map(|j| j.name.to_owned())
            .collect::<Vec<String>>();

        // an error occurs since the arms collide
        assert!(planner
            .plan_avoid_self_collision(using_joint_names.as_slice(), &[0.0; 6], &[0.0; 6],)
            .is_err());
        // the planner PROBABLY generates a trajectory avoiding self-collisions
        assert!(planner
            .plan_avoid_self_collision(
                using_joint_names.as_slice(),
                &[0.0, -0.3, 0.0, 0.0, 0.0, 0.0],
                &[0.0, 0.3, 0.0, 0.0, 0.0, 0.0],
            )
            .is_ok());
    }
}