Viam C++ SDK current
Loading...
Searching...
No Matches
motion.hpp
1
4#pragma once
5
6#include <string>
7
8#include <viam/api/service/motion/v1/motion.pb.h>
9
10#include <viam/sdk/common/pose.hpp>
11#include <viam/sdk/common/proto_type.hpp>
12#include <viam/sdk/common/utils.hpp>
13#include <viam/sdk/common/world_state.hpp>
14#include <viam/sdk/resource/resource_api.hpp>
15#include <viam/sdk/services/service.hpp>
16#include <viam/sdk/spatialmath/geometry.hpp>
17
18namespace viam {
19namespace sdk {
20
30
31 service::motion::v1::ObstacleDetector to_proto() const;
32 static obstacle_detector from_proto(const service::motion::v1::ObstacleDetector& proto);
33 friend bool operator==(const obstacle_detector& lhs, const obstacle_detector& rhs);
34 friend std::ostream& operator<<(std::ostream& os, const obstacle_detector& v);
35};
36
42 std::vector<obstacle_detector> obstacle_detectors;
43
45 boost::optional<double> position_polling_frequency_hz;
46
48 boost::optional<double> obstacle_polling_frequency_hz;
49
51 boost::optional<double> plan_deviation_m;
52
54 boost::optional<double> linear_m_per_sec;
55
57 boost::optional<double> angular_degs_per_sec;
58
59 service::motion::v1::MotionConfiguration to_proto() const;
60 static motion_configuration from_proto(const service::motion::v1::MotionConfiguration& proto);
61 friend bool operator==(const motion_configuration& lhs, const motion_configuration& rhs);
62 friend std::ostream& operator<<(std::ostream& os, const motion_configuration& v);
63};
64
74class Motion : public Service {
75 public:
79 enum class plan_state : uint8_t {
80 k_in_progress,
81 k_stopped,
82 k_succeeded,
83 k_failed,
84 };
85
86 static plan_state from_proto(const service::motion::v1::PlanState& proto);
87 static service::motion::v1::PlanState to_proto(const plan_state& state);
88
92 struct plan_status {
95
97 std::chrono::time_point<long long, std::chrono::nanoseconds> timestamp;
98
101 boost::optional<std::string> reason;
102
103 static plan_status from_proto(const service::motion::v1::PlanStatus& proto);
104 static std::vector<plan_status> from_proto(
105 const google::protobuf::RepeatedPtrField<service::motion::v1::PlanStatus>& proto);
106 service::motion::v1::PlanStatus to_proto() const;
107 friend bool operator==(const plan_status& lhs, const plan_status& rhs);
108 };
109
115 std::string plan_id;
116
119
121 std::string execution_id;
122
125
126 static plan_status_with_id from_proto(const service::motion::v1::PlanStatusWithID& proto);
127 service::motion::v1::PlanStatusWithID to_proto() const;
128 friend bool operator==(const plan_status_with_id& lhs, const plan_status_with_id& rhs);
129 };
130
134 struct steps {
137 typedef std::unordered_map<std::string, pose> step;
138
140 std::vector<step> steps;
141
142 static struct steps from_proto(
143 const google::protobuf::RepeatedPtrField<service::motion::v1::PlanStep>& proto);
144
145 static service::motion::v1::PlanStep to_proto(const step& step);
146 friend bool operator==(const struct steps& lhs, const struct steps& rhs);
147 };
148
152 struct plan {
154 std::string id;
155
158
161 std::string execution_id;
162
164 struct steps steps;
165
166 static plan from_proto(const service::motion::v1::Plan& proto);
167 service::motion::v1::Plan to_proto() const;
168 friend bool operator==(const plan& lhs, const plan& rhs);
169 };
170
177 struct plan plan;
178
181
183 std::vector<plan_status> status_history;
184
185 static plan_with_status from_proto(const service::motion::v1::PlanWithStatus& proto);
186 static std::vector<plan_with_status> from_proto(
187 const google::protobuf::RepeatedPtrField<service::motion::v1::PlanWithStatus>& proto);
188 service::motion::v1::PlanWithStatus to_proto() const;
189 friend bool operator==(const plan_with_status& lhs, const plan_with_status& rhs);
190 };
191
195 float line_tolerance_mm;
196 float orientation_tolerance_degs;
197 };
198
203 float orientation_tolerance_degs;
204 };
205
210 std::string frame1;
211 std::string frame2;
212 };
213 std::vector<allowed_frame_collisions> allows;
214 };
215
219 struct constraints {
220 std::vector<linear_constraint> linear_constraints;
221 std::vector<orientation_constraint> orientation_constraints;
222 std::vector<collision_specification> collision_specifications;
223
224 static constraints from_proto(const service::motion::v1::Constraints& proto);
225 service::motion::v1::Constraints to_proto() const;
226 };
227
228 API api() const override;
229
237 inline bool move(const pose_in_frame& destination,
238 const Name& name,
239 const std::shared_ptr<WorldState>& world_state,
240 const std::shared_ptr<constraints>& constraints) {
241 return move(destination, name, world_state, constraints, {});
242 }
243
252 virtual bool move(const pose_in_frame& destination,
253 const Name& name,
254 const std::shared_ptr<WorldState>& world_state,
255 const std::shared_ptr<constraints>& constraints,
256 const AttributeMap& extra) = 0;
257
264 inline std::string move_on_map(
265 const pose& destination,
266 const Name& component_name,
267 const Name& slam_name,
268 const std::shared_ptr<motion_configuration>& motion_configuration,
269 const std::vector<GeometryConfig>& obstacles) {
270 return move_on_map(
271 destination, component_name, slam_name, motion_configuration, obstacles, {});
272 }
273
281 virtual std::string move_on_map(
282 const pose& destination,
283 const Name& component_name,
284 const Name& slam_name,
285 const std::shared_ptr<motion_configuration>& motion_configuration,
286 const std::vector<GeometryConfig>& obstacles,
287 const AttributeMap& extra) = 0;
288
299 inline std::string move_on_globe(
300 const geo_point& destination,
301 const boost::optional<double>& heading,
302 const Name& component_name,
303 const Name& movement_sensor_name,
304 const std::vector<geo_geometry>& obstacles,
305 const std::shared_ptr<motion_configuration>& motion_configuration,
306 const std::vector<geo_geometry>& bounding_regions) {
307 return move_on_globe(destination,
308 heading,
309 component_name,
310 movement_sensor_name,
311 obstacles,
313 bounding_regions,
314 {});
315 }
316
328 virtual std::string move_on_globe(
329 const geo_point& destination,
330 const boost::optional<double>& heading,
331 const Name& component_name,
332 const Name& movement_sensor_name,
333 const std::vector<geo_geometry>& obstacles,
334 const std::shared_ptr<motion_configuration>& motion_configuration,
335 const std::vector<geo_geometry>& bounding_regions,
336 const AttributeMap& extra) = 0;
337
346 const Name& component_name,
347 const std::string& destination_frame,
348 const std::vector<WorldState::transform>& supplemental_transforms) {
349 return get_pose(component_name, destination_frame, supplemental_transforms, {});
350 }
351
361 const Name& component_name,
362 const std::string& destination_frame,
363 const std::vector<WorldState::transform>& supplemental_transforms,
364 const AttributeMap& extra) = 0;
365
368 inline void stop_plan(const Name& component_name) {
369 return stop_plan(component_name, {});
370 }
371
375 virtual void stop_plan(const Name& component_name, const AttributeMap& extra) = 0;
376
382 inline plan_with_status get_latest_plan(const Name& component_name) {
383 return get_latest_plan(component_name, {});
384 }
385
392 virtual plan_with_status get_latest_plan(const Name& component_name,
393 const AttributeMap& extra) = 0;
394
401 inline std::pair<plan_with_status, std::vector<plan_with_status>>
403 return get_latest_plan_with_replan_history(component_name, {});
404 }
405
413 virtual std::pair<plan_with_status, std::vector<plan_with_status>>
414 get_latest_plan_with_replan_history(const Name& component_name, const AttributeMap& extra) = 0;
415
422 inline plan_with_status get_plan(const Name& component_name, const std::string& execution_id) {
423 return get_plan(component_name, execution_id, {});
424 }
425
433 virtual plan_with_status get_plan(const Name& component_name,
434 const std::string& execution_id,
435 const AttributeMap& extra) = 0;
436
444 inline std::pair<plan_with_status, std::vector<plan_with_status>> get_plan_with_replan_history(
445 const Name& component_name, const std::string& execution_id) {
446 return get_plan_with_replan_history(component_name, execution_id, {});
447 }
448
457 virtual std::pair<plan_with_status, std::vector<plan_with_status>> get_plan_with_replan_history(
458 const Name& component_name, const std::string& execution_id, const AttributeMap& extra) = 0;
459
464 inline std::vector<plan_status_with_id> list_plan_statuses() {
465 return list_plan_statuses({});
466 }
467
473 virtual std::vector<plan_status_with_id> list_plan_statuses(const AttributeMap& extra) = 0;
474
479 inline std::vector<plan_status_with_id> list_active_plan_statuses() {
480 return list_plan_statuses({});
481 }
482
488 virtual std::vector<plan_status_with_id> list_active_plan_statuses(
489 const AttributeMap& extra) = 0;
490
494 virtual AttributeMap do_command(const AttributeMap& command) = 0;
495
496 protected:
497 explicit Motion(std::string name);
498};
499
500template <>
502 static API api();
503};
504
505} // namespace sdk
506} // namespace viam
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
The Motion service coordinates motion planning across all components of a given robot....
Definition motion.hpp:74
virtual pose_in_frame get_pose(const Name &component_name, const std::string &destination_frame, const std::vector< WorldState::transform > &supplemental_transforms, const AttributeMap &extra)=0
Get the pose of any component on the robot.
pose_in_frame get_pose(const Name &component_name, const std::string &destination_frame, const std::vector< WorldState::transform > &supplemental_transforms)
Get the pose of any component on the robot.
Definition motion.hpp:345
virtual plan_with_status get_plan(const Name &component_name, const std::string &execution_id, const AttributeMap &extra)=0
Returns the plan and state history of the requested plan. Returns a result if the last execution is s...
API api() const override
Returns the API associated with a particular resource.
virtual plan_with_status get_latest_plan(const Name &component_name, const AttributeMap &extra)=0
Returns the plan and state history of the most recent execution to move a component....
std::string move_on_map(const pose &destination, const Name &component_name, const Name &slam_name, const std::shared_ptr< motion_configuration > &motion_configuration, const std::vector< GeometryConfig > &obstacles)
Moves any component on the robot to a specific destination on a SLAM map.
Definition motion.hpp:264
bool move(const pose_in_frame &destination, const Name &name, const std::shared_ptr< WorldState > &world_state, const std::shared_ptr< constraints > &constraints)
Moves any compononent on the robot to a specified destination.
Definition motion.hpp:237
std::string move_on_globe(const geo_point &destination, const boost::optional< double > &heading, const Name &component_name, const Name &movement_sensor_name, const std::vector< geo_geometry > &obstacles, const std::shared_ptr< motion_configuration > &motion_configuration, const std::vector< geo_geometry > &bounding_regions)
Moves any component on the robot to a specific destination on a globe.
Definition motion.hpp:299
void stop_plan(const Name &component_name)
Stop a currently executing motion plan.
Definition motion.hpp:368
plan_with_status get_plan(const Name &component_name, const std::string &execution_id)
Returns the plan and state history of the requested plan. Returns a result if the last execution is s...
Definition motion.hpp:422
virtual std::string move_on_globe(const geo_point &destination, const boost::optional< double > &heading, const Name &component_name, const Name &movement_sensor_name, const std::vector< geo_geometry > &obstacles, const std::shared_ptr< motion_configuration > &motion_configuration, const std::vector< geo_geometry > &bounding_regions, const AttributeMap &extra)=0
Moves any component on the robot to a specific destination on a globe.
virtual void stop_plan(const Name &component_name, const AttributeMap &extra)=0
Stop a currently executing motion plan.
virtual std::string move_on_map(const pose &destination, const Name &component_name, const Name &slam_name, const std::shared_ptr< motion_configuration > &motion_configuration, const std::vector< GeometryConfig > &obstacles, const AttributeMap &extra)=0
Moves any component on the robot to a specific destination on a SLAM map.
plan_state
Describes the possible states a plan can be in.
Definition motion.hpp:79
std::pair< plan_with_status, std::vector< plan_with_status > > get_latest_plan_with_replan_history(Name component_name)
Returns the plan, state history, and replan history of the most recent execution to move a component....
Definition motion.hpp:402
virtual bool move(const pose_in_frame &destination, const Name &name, const std::shared_ptr< WorldState > &world_state, const std::shared_ptr< constraints > &constraints, const AttributeMap &extra)=0
Moves any compononent on the robot to a specified destination.
virtual std::pair< plan_with_status, std::vector< plan_with_status > > get_plan_with_replan_history(const Name &component_name, const std::string &execution_id, const AttributeMap &extra)=0
Returns the plan, state history, and replan history of the requested plan. Returns a result if the la...
std::pair< plan_with_status, std::vector< plan_with_status > > get_plan_with_replan_history(const Name &component_name, const std::string &execution_id)
Returns the plan, state history, and replan history of the requested plan. Returns a result if the la...
Definition motion.hpp:444
std::vector< plan_status_with_id > list_plan_statuses()
Returns the status of plans created by MoveOnGlobe requests. Includes statuses of plans that are exec...
Definition motion.hpp:464
plan_with_status get_latest_plan(const Name &component_name)
Returns the plan and state history of the most recent execution to move a component....
Definition motion.hpp:382
virtual AttributeMap do_command(const AttributeMap &command)=0
Send/receive arbitrary commands to the resource.
virtual std::vector< plan_status_with_id > list_active_plan_statuses(const AttributeMap &extra)=0
Returns the status of currently active plans created by MoveOnGlobe requests. Includes statuses of pl...
std::vector< plan_status_with_id > list_active_plan_statuses()
Returns the status of currently active plans created by MoveOnGlobe requests. Includes statuses of pl...
Definition motion.hpp:479
virtual std::pair< plan_with_status, std::vector< plan_with_status > > get_latest_plan_with_replan_history(const Name &component_name, const AttributeMap &extra)=0
Returns the plan, state history, and replan history of the most recent execution to move a component....
virtual std::vector< plan_status_with_id > list_plan_statuses(const AttributeMap &extra)=0
Returns the status of plans created by MoveOnGlobe requests. Includes statuses of plans that are exec...
A name for specific instances of resources.
Definition resource_api.hpp:63
virtual std::string name() const
Return the resource's name.
Definition service.hpp:12
Definition resource_api.hpp:50
used to selectively apply obstacle avoidance to specific parts of the robot.
Definition motion.hpp:208
Specifies all constraints to be passed to Viam's motion planning, along with any optional parameters.
Definition motion.hpp:219
Specifies that the component being moved should move linearly to its goal.
Definition motion.hpp:194
Specifies that the component being moved will not deviate its orientation beyond the specified thresh...
Definition motion.hpp:202
The motion plan status, plus plan ID, component name, and execution ID.
Definition motion.hpp:113
plan_status status
The plan status.
Definition motion.hpp:124
Name component_name
The component to be moved. Used for tracking and stopping.
Definition motion.hpp:118
std::string execution_id
The unique ID which identifies the plan execution.
Definition motion.hpp:121
std::string plan_id
The unique ID of the plan.
Definition motion.hpp:115
Describes the state of a given plan at a point in time.
Definition motion.hpp:92
plan_state state
The state of the plan execution.
Definition motion.hpp:94
std::chrono::time_point< long long, std::chrono::nanoseconds > timestamp
The time the executing plan transitioned to the state.
Definition motion.hpp:97
boost::optional< std::string > reason
The reason for the state change. The error message if the plan failed, or the re-plan reason if re-pl...
Definition motion.hpp:101
Describes a plan, its current status, and all status changes that have occurred previously on that pl...
Definition motion.hpp:175
plan_status status
The current status of the plan.
Definition motion.hpp:180
std::vector< plan_status > status_history
The prior status changes that have happened during plan execution.
Definition motion.hpp:183
Describes a motion plan.
Definition motion.hpp:152
Name component_name
The component requested to be moved. Used for tracking and stopping.
Definition motion.hpp:157
std::string id
The plan's unique ID.
Definition motion.hpp:154
std::string execution_id
The unique ID which identifies the execution. Multiple plans can share the same execution_id if they ...
Definition motion.hpp:161
An ordered list of plan steps.
Definition motion.hpp:134
std::vector< step > steps
The ordered list of steps.
Definition motion.hpp:140
std::unordered_map< std::string, pose > step
An individual "step", representing the state each component (keyed as a fully qualified component nam...
Definition motion.hpp:137
Definition geometry.hpp:80
Defines configuration options for certain Motion APIs.
Definition motion.hpp:40
boost::optional< double > angular_degs_per_sec
Optional angular velocity to target when turning.
Definition motion.hpp:57
std::vector< obstacle_detector > obstacle_detectors
The obstacle detectors to be used for the API call.
Definition motion.hpp:42
boost::optional< double > linear_m_per_sec
Optional linear velocity to target when moving.
Definition motion.hpp:54
boost::optional< double > position_polling_frequency_hz
If not null, sets the frequency to poll for the position of the robot.
Definition motion.hpp:45
boost::optional< double > plan_deviation_m
Optional distance in meters a robot is allowed to deviate from the motion plan.
Definition motion.hpp:51
boost::optional< double > obstacle_polling_frequency_hz
If not null, sets the frequency to poll the vision service(s) for new obstacles.
Definition motion.hpp:48
Definition motion.hpp:25
Name camera
The name of the camera component to be used for obstacle detection.
Definition motion.hpp:29
Name vision_service
The name of the vision service to be used for obstacle detection.
Definition motion.hpp:27
Definition pose.hpp:30
Definition pose.hpp:18