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
use std::{collections::HashMap, sync::Arc, time::Duration};

use anyhow::format_err;
use arci::{copy_joint_positions, Error, JointTrajectoryClient, TrajectoryPoint};

use crate::{
    msg::trajectory_msgs::{JointTrajectory, JointTrajectoryPoint},
    wrap_joint_trajectory_client, JointStateProvider, LazyJointStateProvider,
    RosControlClientBuilder,
};

pub(crate) fn extract_current_joint_positions_from_state(
    client: &dyn JointTrajectoryClient,
    state_provider: &dyn JointStateProvider,
) -> Result<Vec<f64>, Error> {
    let (state_joint_names, state_joint_positions) = state_provider.get_joint_state()?;
    // TODO: cache index map and use it
    let mut result = vec![0.0; client.joint_names().len()];
    copy_joint_positions(
        &state_joint_names,
        &state_joint_positions,
        &client.joint_names(),
        &mut result,
    )?;
    Ok(result)
}

pub(crate) fn create_joint_trajectory_message_for_send_joint_positions(
    client: &dyn JointTrajectoryClient,
    state_provider: &dyn JointStateProvider,
    positions: &[f64],
    duration: Duration,
    send_partial_joints_goal: bool,
) -> Result<JointTrajectory, arci::Error> {
    if send_partial_joints_goal {
        Ok(JointTrajectory {
            points: vec![JointTrajectoryPoint {
                positions: positions.to_owned(),
                // add zero velocity to use cubic interpolation in trajectory_controller
                velocities: vec![0.0; client.joint_names().len()],
                time_from_start: duration.into(),
                ..Default::default()
            }],
            joint_names: client.joint_names(),
            ..Default::default()
        })
    } else {
        let (state_joint_names, state_joint_positions) = state_provider.get_joint_state()?;
        let partial_names = client.joint_names();
        if partial_names.len() != positions.len() {
            return Err(arci::Error::LengthMismatch {
                model: partial_names.len(),
                input: positions.len(),
            });
        }
        // TODO: cache index map and use it
        let full_names = state_joint_names;
        let full_dof = full_names.len();

        let mut full_positions = state_joint_positions;
        copy_joint_positions(&partial_names, positions, &full_names, &mut full_positions)?;

        let point_with_full_positions = JointTrajectoryPoint {
            positions: full_positions,
            // add zero velocity to use cubic interpolation in trajectory_controller
            velocities: vec![0.0; full_dof],
            time_from_start: duration.into(),
            ..Default::default()
        };
        Ok(JointTrajectory {
            joint_names: full_names,
            points: vec![point_with_full_positions],
            ..Default::default()
        })
    }
}

pub(crate) fn create_joint_trajectory_message_for_send_joint_trajectory(
    client: &dyn JointTrajectoryClient,
    state_provider: &dyn JointStateProvider,
    trajectory: &[TrajectoryPoint],
    send_partial_joints_goal: bool,
) -> Result<JointTrajectory, arci::Error> {
    if send_partial_joints_goal {
        Ok(JointTrajectory {
            points: trajectory
                .iter()
                .map(|tp| JointTrajectoryPoint {
                    positions: tp.positions.clone(),
                    velocities: if let Some(velocities) = &tp.velocities {
                        velocities.clone()
                    } else {
                        vec![]
                    },
                    time_from_start: tp.time_from_start.into(),
                    ..Default::default()
                })
                .collect(),
            joint_names: client.joint_names(),
            ..Default::default()
        })
    } else {
        let (state_joint_names, state_joint_positions) = state_provider.get_joint_state()?;
        // TODO: cache index map and use it
        let current_full_positions = state_joint_positions;
        let full_names = state_joint_names;
        let full_dof = current_full_positions.len();

        Ok(JointTrajectory {
            points: trajectory
                .iter()
                .map(|tp| {
                    let mut full_positions = current_full_positions.clone();
                    copy_joint_positions(
                        &client.joint_names(),
                        &tp.positions,
                        &full_names,
                        &mut full_positions,
                    )?;
                    Ok(JointTrajectoryPoint {
                        positions: full_positions,
                        velocities: if let Some(partial_velocities) = &tp.velocities {
                            let mut full_velocities = vec![0.0; full_dof];
                            copy_joint_positions(
                                &client.joint_names(),
                                partial_velocities,
                                &full_names,
                                &mut full_velocities,
                            )?;
                            full_velocities
                        } else {
                            vec![]
                        },
                        time_from_start: tp.time_from_start.into(),
                        ..Default::default()
                    })
                })
                .collect::<Result<Vec<_>, arci::Error>>()?,
            joint_names: full_names,
            ..Default::default()
        })
    }
}

/// Returns a map of clients for each builder.
///
/// The key for the map is [the name of the client](RosControlClientBuilder::name),
/// and in case of conflict, it becomes an error.
///
/// Returns empty map when `builders` are empty.
pub fn create_joint_trajectory_clients<B>(
    builders: Vec<B>,
    urdf_robot: Option<&urdf_rs::Robot>,
) -> Result<HashMap<String, Arc<dyn JointTrajectoryClient>>, arci::Error>
where
    B: RosControlClientBuilder,
{
    create_joint_trajectory_clients_inner(builders, urdf_robot, false)
}

/// Returns a map of clients that will be created lazily for each builder.
///
/// See [create_joint_trajectory_clients] for more.
pub fn create_joint_trajectory_clients_lazy<B>(
    builders: Vec<B>,
    urdf_robot: Option<&urdf_rs::Robot>,
) -> Result<HashMap<String, Arc<dyn JointTrajectoryClient>>, arci::Error>
where
    B: RosControlClientBuilder,
{
    create_joint_trajectory_clients_inner(builders, urdf_robot, true)
}

fn create_joint_trajectory_clients_inner<B>(
    builders: Vec<B>,
    urdf_robot: Option<&urdf_rs::Robot>,
    lazy: bool,
) -> Result<HashMap<String, Arc<dyn JointTrajectoryClient>>, arci::Error>
where
    B: RosControlClientBuilder,
{
    if builders.is_empty() {
        return Ok(HashMap::default());
    }

    let mut clients = HashMap::new();
    let mut state_topic_name_to_provider: HashMap<String, Arc<LazyJointStateProvider>> =
        HashMap::new();
    for builder in builders {
        if urdf_robot.is_none() {
            builder.wrapper_config().check_urdf_is_not_necessary()?;
        }
        let state_topic_name = builder.state_topic();
        let joint_state_provider = if let Some(joint_state_provider) =
            state_topic_name_to_provider.get(&state_topic_name)
        {
            joint_state_provider.clone()
        } else {
            let joint_state_provider = builder.build_joint_state_provider(&state_topic_name);
            state_topic_name_to_provider.insert(state_topic_name, joint_state_provider.clone());
            joint_state_provider
        };

        let name = builder.name().to_owned();
        let client = builder.build_joint_trajectory_client(lazy, joint_state_provider)?;
        let client =
            wrap_joint_trajectory_client(builder.wrapper_config().clone(), client, urdf_robot)?;
        if clients.insert(name.clone(), client).is_some() {
            return Err(format_err!("client named '{name}' has already been specified").into());
        }
    }
    Ok(clients)
}