Viam C++ SDK current
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <string>
7#include <thread>
8
9#include <grpcpp/channel.h>
10
11#include <viam/sdk/common/pose.hpp>
12#include <viam/sdk/common/utils.hpp>
13#include <viam/sdk/common/world_state.hpp>
14#include <viam/sdk/components/component.hpp>
16#include <viam/sdk/resource/resource.hpp>
17#include <viam/sdk/rpc/dial.hpp>
18#include <viam/sdk/services/service.hpp>
19
20namespace viam {
21namespace sdk {
22
23using grpc::Channel;
24
26
38 public:
40 std::string subtype;
41 std::string model;
42 friend bool operator==(const discovery_query& lhs, const discovery_query& rhs);
43 };
44
45 struct discovery {
46 discovery_query query;
47 ProtoStruct results;
48 friend bool operator==(const discovery& lhs, const discovery& rhs);
49 };
50
53 ProtoStruct kinematics;
54 friend bool operator==(const frame_system_config& lhs, const frame_system_config& rhs);
55 };
56
57 struct status {
58 boost::optional<Name> name;
59 ProtoStruct status_map;
60 // TODO: RSDK-6574: revisit time_point
61 boost::optional<std::chrono::time_point<long long, std::chrono::nanoseconds>>
62 last_reconfigured;
63 friend bool operator==(const status& lhs, const status& rhs);
64 };
65
66 struct operation {
67 std::string id;
68 std::string method;
69 boost::optional<std::string> session_id;
70 ProtoStruct arguments;
71 // TODO: RSDK-6574: revisit time_point
72 boost::optional<std::chrono::time_point<long long, std::chrono::nanoseconds>> started;
73 friend bool operator==(const operation& lhs, const operation& rhs);
74 };
75
77 void refresh();
78 void close();
79
83 static std::shared_ptr<RobotClient> at_address(const std::string& address,
84 const Options& options);
85
91 static std::shared_ptr<RobotClient> at_local_socket(const std::string& address,
92 const Options& options);
93
99 static std::shared_ptr<RobotClient> with_channel(std::shared_ptr<ViamChannel> channel,
100 const Options& options);
101 RobotClient(std::shared_ptr<ViamChannel> channel);
102 std::vector<Name> resource_names() const;
103
115 std::shared_ptr<Resource> resource_by_name(const Name& name);
116
117 template <typename T>
122 std::shared_ptr<T> resource_by_name(std::string name) {
123 return std::dynamic_pointer_cast<T>(resource_by_name({API::get<T>(), "", std::move(name)}));
124 }
125
128 std::vector<frame_system_config> get_frame_system_config(
129 const std::vector<WorldState::transform>& additional_transforms = {});
130
133 std::vector<operation> get_operations();
134
138 std::vector<status> get_status(std::vector<Name>& components);
139
142 std::vector<status> get_status();
143
144 std::vector<discovery> discover_components(const std::vector<discovery_query>& queries);
145
151 const pose_in_frame& query,
152 std::string destination,
153 const std::vector<WorldState::transform>& additional_transforms = {});
154
157 void block_for_operation(std::string id);
158
160 void stop_all();
161
164 void stop_all(const std::unordered_map<Name, ProtoStruct>& extra);
165
168 void cancel_operation(std::string id);
169
170 private:
171 std::vector<std::shared_ptr<std::thread>> threads_;
172 std::atomic<bool> should_refresh_;
173 unsigned int refresh_interval_;
174 std::shared_ptr<Channel> channel_;
175 std::shared_ptr<ViamChannel> viam_channel_;
176 bool should_close_channel_;
177 struct impl;
178 std::unique_ptr<impl> impl_;
179 mutable std::mutex lock_;
180 std::vector<Name> resource_names_;
181 ResourceManager resource_manager_;
182 void refresh_every();
183};
184
185} // namespace sdk
186} // namespace viam
A name for specific instances of resources.
Definition resource_api.hpp:63
Definition dial.hpp:75
Defines a resource manager for use by anything that tracks resources.
Definition resource_manager.hpp:24
gRPC client for a robot, to be used for all interactions with a robot. There are two ways to instanti...
Definition client.hpp:37
static std::shared_ptr< RobotClient > at_local_socket(const std::string &address, const Options &options)
Creates a robot client connected to the robot at the provided local socket.
static std::shared_ptr< RobotClient > with_channel(std::shared_ptr< ViamChannel > channel, const Options &options)
Creates a robot client connected to the provided channel.
std::vector< status > get_status()
Get the status of all robot components.
std::shared_ptr< Resource > resource_by_name(const Name &name)
Lookup and return a shared_ptr to a resource.
pose_in_frame transform_pose(const pose_in_frame &query, std::string destination, const std::vector< WorldState::transform > &additional_transforms={})
Transform a given Pose to a new specified destination which is a reference frame.
std::vector< frame_system_config > get_frame_system_config(const std::vector< WorldState::transform > &additional_transforms={})
Get the configuration of the frame system of the given robot.
void stop_all()
Cancel all operations for the robot and stop all actuators and movement.
static std::shared_ptr< RobotClient > at_address(const std::string &address, const Options &options)
Create a robot client connected to the robot at the provided address.
void block_for_operation(std::string id)
Blocks on the specified operation of the robot, returning when it is complete.
void cancel_operation(std::string id)
Cancel a specified operation on the robot.
std::vector< operation > get_operations()
Get the list of operations currently running on a robot.
std::shared_ptr< T > resource_by_name(std::string name)
Lookup and return a shared_ptr to a resource of the requested type.
Definition client.hpp:122
std::vector< status > get_status(std::vector< Name > &components)
Get the status of the requested robot components.
void stop_all(const std::unordered_map< Name, ProtoStruct > &extra)
Cancel all operations for the robot and stop all actuators and movement.
Defines the resource registry and associated types.
Definition client.hpp:45
Definition client.hpp:66
Definition client.hpp:57
Definition world_state.hpp:21
Definition pose.hpp:30