Viam C++ SDK current
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <atomic>
7#include <string>
8#include <thread>
9
10#include <viam/sdk/common/grpc_fwd.hpp>
11#include <viam/sdk/common/pose.hpp>
12#include <viam/sdk/common/proto_convert.hpp>
13#include <viam/sdk/common/utils.hpp>
15#include <viam/sdk/components/component.hpp>
17#include <viam/sdk/resource/resource.hpp>
18#include <viam/sdk/rpc/dial.hpp>
20#include <viam/sdk/services/service.hpp>
21
22namespace viam {
23
24namespace robot {
25namespace v1 {
26
27class ConfigStatus;
28class GetMachineStatusResponse;
29class JobStatus;
30class ModuleStatus;
31class Operation;
32class PackageStatus;
33class ResourceStatus;
34class SendTracesRequest;
35
36} // namespace v1
37} // namespace robot
38
39namespace sdk {
40
41namespace impl {
42struct LogBackend;
43class ParentSendTracesExporter;
44} // namespace impl
45
47
55 public:
59 enum class status : uint8_t {
60 k_initializing,
61 k_running,
62 k_unspecified,
63 };
64
65 friend std::ostream& operator<<(std::ostream& os, const status& v);
66
67 using frame_system_config = FrameSystem::frame_system_config;
68
69 struct operation {
70 std::string id;
71 std::string method;
72 boost::optional<std::string> session_id;
73 ProtoStruct arguments;
74 boost::optional<time_pt> started;
75 friend bool operator==(const operation& lhs, const operation& rhs);
76 };
77
79 enum class resource_state : uint8_t {
80 k_unspecified,
81 k_unconfigured,
82 k_configuring,
83 k_ready,
84 k_removing,
85 k_unhealthy,
86 };
87
88 Name name;
89 resource_state state;
90 boost::optional<time_pt> last_updated;
91 std::string revision;
92 std::string error;
93 friend bool operator==(const resource_status& lhs, const resource_status& rhs);
94 };
95
97 std::string revision;
98 boost::optional<time_pt> last_updated;
99 friend bool operator==(const config_status& lhs, const config_status& rhs);
100 };
101
103 enum class module_state : uint8_t {
104 k_unspecified,
105 k_pending,
106 k_starting,
107 k_ready,
108 k_unhealthy,
109 k_closing,
110 };
111
112 std::string module_name;
113 module_state state;
114 boost::optional<time_pt> last_updated;
115 std::string error;
116 std::uint32_t consecutive_failures;
117 friend bool operator==(const module_status& lhs, const module_status& rhs);
118 };
119
121 enum class package_type : uint8_t {
122 k_unspecified,
123 k_archive,
124 k_ml_model,
125 k_module,
126 k_slam_map,
127 k_ml_training,
128 };
129
130 enum class package_state : uint8_t {
131 k_unspecified,
132 k_downloading,
133 k_loading,
134 k_first_run,
135 k_ready,
136 k_failed,
137 };
138
139 std::string name;
140 package_type type;
141 package_state state;
142 std::string error;
143 boost::optional<time_pt> last_updated;
144 std::string version;
145 std::uint64_t bytes_downloaded;
146 std::uint64_t total_bytes;
147 friend bool operator==(const package_status& lhs, const package_status& rhs);
148 };
149
150 struct job_status {
151 std::string job_name;
152 std::vector<time_pt> recent_successful_runs;
153 std::vector<time_pt> recent_failed_runs;
154 friend bool operator==(const job_status& lhs, const job_status& rhs);
155 };
156
158 status state;
159 std::vector<resource_status> resources;
160 config_status config;
161 std::vector<module_status> modules;
162 std::vector<package_status> packages;
163 std::vector<job_status> job_statuses;
164 friend bool operator==(const machine_status& lhs, const machine_status& rhs);
165 };
166
167 explicit RobotClient(ViamChannel channel);
168
169 ~RobotClient();
170
177 void refresh();
178
182 void close();
183
187 static std::shared_ptr<RobotClient> at_address(const std::string& address,
188 const Options& options);
189
195 static std::shared_ptr<RobotClient> at_local_socket(const std::string& address,
196 const Options& options);
197
201 static std::shared_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
202
203 std::vector<Name> resource_names() const;
204
219 std::shared_ptr<Resource> resource_by_name(const Name& name);
220
221 template <typename T>
226 std::shared_ptr<T> resource_by_name(std::string name) {
227 return std::dynamic_pointer_cast<T>(resource_by_name({API::get<T>(), "", std::move(name)}));
228 }
229
232 std::vector<frame_system_config> get_frame_system_config(
233 const std::vector<WorldState::transform>& additional_transforms = {});
234
237 std::vector<operation> get_operations();
238
244 const pose_in_frame& query,
245 std::string destination,
246 const std::vector<WorldState::transform>& additional_transforms = {});
247
253 std::vector<unsigned char> transform_pcd(const std::vector<unsigned char>& pcd,
254 const std::string& source,
255 const std::string& destination);
256
259 void block_for_operation(std::string id);
260
262 void stop_all();
263
266 void stop_all(const std::unordered_map<Name, ProtoStruct>& extra);
267
270 void cancel_operation(std::string id);
271
275 pose_in_frame get_pose(const std::string& component_name);
276
283 pose_in_frame get_pose(const std::string& component_name,
284 const std::string& destination_frame,
285 const std::vector<WorldState::transform>& additional_transforms,
286 const ProtoStruct& extra);
287
290
291 private:
292 friend class ModuleService;
293 friend struct impl::LogBackend;
294 friend class impl::ParentSendTracesExporter;
295
296 void log(const std::string& name,
297 const std::string& level,
298 const std::string& message,
299 time_pt time,
300 const std::string& file,
301 unsigned int line);
302
303 // Ships a batch of OTLP traces to the parent. Returns true on success.
304 bool send_traces(const robot::v1::SendTracesRequest* req);
305
306 // Makes this RobotClient manage logging by sending logs over grpc to viam-server.
307 // This is private and only ever called by ModuleService; in other words it is only called when
308 // viam-server is running a Viam C++ SDK application as a module.
309 // Disables console logging so as to avoid log message duplication; console logging is
310 // re-enabled on destruction.
311 void connect_logging();
312
313 // Installs the SDK tracer provider so spans flow back to the parent over this connection.
314 // Only called by ModuleService when running as a module.
315 void connect_tracing();
316
317 void refresh_every();
318 void check_connection();
319
320 std::thread refresh_thread_;
321 std::thread check_connection_thread_;
322 std::atomic<bool> should_refresh_;
323 std::atomic<bool> should_check_connection_;
324 std::chrono::seconds refresh_interval_;
325 std::chrono::seconds check_every_interval_;
326 std::chrono::seconds reconnect_every_interval_;
327
328 ViamChannel viam_channel_;
329
330 struct impl;
331 std::unique_ptr<impl> impl_;
332
333 mutable std::mutex lock_;
334
335 std::vector<Name> resource_names_;
336 ResourceManager resource_manager_;
337};
338
339namespace proto_convert_details {
340
341template <>
342struct from_proto_impl<robot::v1::Operation> {
343 RobotClient::operation operator()(const robot::v1::Operation*) const;
344};
345
346template <>
347struct from_proto_impl<robot::v1::ResourceStatus> {
348 RobotClient::resource_status operator()(const robot::v1::ResourceStatus*) const;
349};
350
351template <>
352struct from_proto_impl<robot::v1::ConfigStatus> {
353 RobotClient::config_status operator()(const robot::v1::ConfigStatus*) const;
354};
355
356template <>
357struct from_proto_impl<robot::v1::ModuleStatus> {
358 RobotClient::module_status operator()(const robot::v1::ModuleStatus*) const;
359};
360
361template <>
362struct from_proto_impl<robot::v1::PackageStatus> {
363 RobotClient::package_status operator()(const robot::v1::PackageStatus*) const;
364};
365
366template <>
367struct from_proto_impl<robot::v1::JobStatus> {
368 RobotClient::job_status operator()(const robot::v1::JobStatus*) const;
369};
370
371template <>
372struct from_proto_impl<robot::v1::GetMachineStatusResponse> {
373 RobotClient::machine_status operator()(const robot::v1::GetMachineStatusResponse*) const;
374};
375
376} // namespace proto_convert_details
377} // namespace sdk
378} // namespace viam
Defines the gRPC receiving logic for a module. C++ module authors can construct a ModuleService and u...
Definition service.hpp:30
A name for specific instances of resources.
Definition resource_api.hpp:61
Defines a resource manager for use by anything that tracks resources.
Definition resource_manager.hpp:22
gRPC client for a robot, to be used for all interactions with a robot. There are two ways to instanti...
Definition client.hpp:54
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.
pose_in_frame get_pose(const std::string &component_name)
Get the pose of a component in the world reference frame.
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 close()
Disconnect this robot client from any robot to which it is connected. After calling this method it is...
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:226
pose_in_frame get_pose(const std::string &component_name, const std::string &destination_frame, const std::vector< WorldState::transform > &additional_transforms, const ProtoStruct &extra)
Get the pose of a component in the desired reference frame.
void stop_all(const std::unordered_map< Name, ProtoStruct > &extra)
Cancel all operations for the robot and stop all actuators and movement.
static std::shared_ptr< RobotClient > with_channel(ViamChannel channel, const Options &options)
Creates a robot client connected to the provided channel.
void refresh()
Call out to the robot to see if there are any new resources that need to be registered....
machine_status get_machine_status() const
gets the current status of the machine
std::vector< unsigned char > transform_pcd(const std::vector< unsigned char > &pcd, const std::string &source, const std::string &destination)
Transform a point cloud from one reference frame to another.
Definition dial.hpp:29
Definition dial.hpp:25
Defines the FrameSystem service.
status
the current status of the robot
Definition client.hpp:59
Defines the resource registry and associated types.
One frame of the frame system, with the kinematics of the resource it belongs to.
Definition framesystem.hpp:44
Definition client.hpp:150
Definition client.hpp:102
Definition client.hpp:69
Definition log_backend.hpp:18
Definition pose.hpp:39
Defines WorldState.