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>
19#include <viam/sdk/services/service.hpp>
20
21namespace viam {
22
23namespace robot {
24namespace v1 {
25
26class ConfigStatus;
27class FrameSystemConfig;
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
69 ProtoStruct kinematics;
70 friend bool operator==(const frame_system_config& lhs, const frame_system_config& rhs);
71 };
72
73 struct operation {
74 std::string id;
75 std::string method;
76 boost::optional<std::string> session_id;
77 ProtoStruct arguments;
78 boost::optional<time_pt> started;
79 friend bool operator==(const operation& lhs, const operation& rhs);
80 };
81
83 enum class resource_state : uint8_t {
84 k_unspecified,
85 k_unconfigured,
86 k_configuring,
87 k_ready,
88 k_removing,
89 k_unhealthy,
90 };
91
92 Name name;
93 resource_state state;
94 boost::optional<time_pt> last_updated;
95 std::string revision;
96 std::string error;
97 friend bool operator==(const resource_status& lhs, const resource_status& rhs);
98 };
99
101 std::string revision;
102 boost::optional<time_pt> last_updated;
103 friend bool operator==(const config_status& lhs, const config_status& rhs);
104 };
105
107 enum class module_state : uint8_t {
108 k_unspecified,
109 k_pending,
110 k_starting,
111 k_ready,
112 k_unhealthy,
113 k_closing,
114 };
115
116 std::string module_name;
117 module_state state;
118 boost::optional<time_pt> last_updated;
119 std::string error;
120 std::uint32_t consecutive_failures;
121 friend bool operator==(const module_status& lhs, const module_status& rhs);
122 };
123
125 enum class package_type : uint8_t {
126 k_unspecified,
127 k_archive,
128 k_ml_model,
129 k_module,
130 k_slam_map,
131 k_ml_training,
132 };
133
134 enum class package_state : uint8_t {
135 k_unspecified,
136 k_downloading,
137 k_loading,
138 k_first_run,
139 k_ready,
140 k_failed,
141 };
142
143 std::string name;
144 package_type type;
145 package_state state;
146 std::string error;
147 boost::optional<time_pt> last_updated;
148 std::string version;
149 std::uint64_t bytes_downloaded;
150 std::uint64_t total_bytes;
151 friend bool operator==(const package_status& lhs, const package_status& rhs);
152 };
153
154 struct job_status {
155 std::string job_name;
156 std::vector<time_pt> recent_successful_runs;
157 std::vector<time_pt> recent_failed_runs;
158 friend bool operator==(const job_status& lhs, const job_status& rhs);
159 };
160
162 status state;
163 std::vector<resource_status> resources;
164 config_status config;
165 std::vector<module_status> modules;
166 std::vector<package_status> packages;
167 std::vector<job_status> job_statuses;
168 friend bool operator==(const machine_status& lhs, const machine_status& rhs);
169 };
170
171 explicit RobotClient(ViamChannel channel);
172
173 ~RobotClient();
174
181 void refresh();
182
186 void close();
187
191 static std::shared_ptr<RobotClient> at_address(const std::string& address,
192 const Options& options);
193
199 static std::shared_ptr<RobotClient> at_local_socket(const std::string& address,
200 const Options& options);
201
205 static std::shared_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
206
207 std::vector<Name> resource_names() const;
208
220 std::shared_ptr<Resource> resource_by_name(const Name& name);
221
222 template <typename T>
227 std::shared_ptr<T> resource_by_name(std::string name) {
228 return std::dynamic_pointer_cast<T>(resource_by_name({API::get<T>(), "", std::move(name)}));
229 }
230
233 std::vector<frame_system_config> get_frame_system_config(
234 const std::vector<WorldState::transform>& additional_transforms = {});
235
238 std::vector<operation> get_operations();
239
245 const pose_in_frame& query,
246 std::string destination,
247 const std::vector<WorldState::transform>& additional_transforms = {});
248
251 void block_for_operation(std::string id);
252
254 void stop_all();
255
258 void stop_all(const std::unordered_map<Name, ProtoStruct>& extra);
259
262 void cancel_operation(std::string id);
263
267 pose_in_frame get_pose(const std::string& component_name);
268
275 pose_in_frame get_pose(const std::string& component_name,
276 const std::string& destination_frame,
277 const std::vector<WorldState::transform>& additional_transforms,
278 const ProtoStruct& extra);
279
282
283 private:
284 friend class ModuleService;
285 friend struct impl::LogBackend;
286 friend class impl::ParentSendTracesExporter;
287
288 void log(const std::string& name,
289 const std::string& level,
290 const std::string& message,
291 time_pt time,
292 const std::string& file,
293 unsigned int line);
294
295 // Ships a batch of OTLP traces to the parent. Returns true on success.
296 bool send_traces(const robot::v1::SendTracesRequest* req);
297
298 // Makes this RobotClient manage logging by sending logs over grpc to viam-server.
299 // This is private and only ever called by ModuleService; in other words it is only called when
300 // viam-server is running a Viam C++ SDK application as a module.
301 // Disables console logging so as to avoid log message duplication; console logging is
302 // re-enabled on destruction.
303 void connect_logging();
304
305 // Installs the SDK tracer provider so spans flow back to the parent over this connection.
306 // Only called by ModuleService when running as a module.
307 void connect_tracing();
308
309 void refresh_every();
310 void check_connection();
311
312 std::thread refresh_thread_;
313 std::thread check_connection_thread_;
314 std::atomic<bool> should_refresh_;
315 std::atomic<bool> should_check_connection_;
316 std::chrono::seconds refresh_interval_;
317 std::chrono::seconds check_every_interval_;
318 std::chrono::seconds reconnect_every_interval_;
319
320 ViamChannel viam_channel_;
321
322 struct impl;
323 std::unique_ptr<impl> impl_;
324
325 mutable std::mutex lock_;
326
327 std::vector<Name> resource_names_;
328 ResourceManager resource_manager_;
329};
330
331namespace proto_convert_details {
332
333template <>
334struct from_proto_impl<robot::v1::Operation> {
335 RobotClient::operation operator()(const robot::v1::Operation*) const;
336};
337
338template <>
339struct from_proto_impl<robot::v1::FrameSystemConfig> {
340 RobotClient::frame_system_config operator()(const robot::v1::FrameSystemConfig*) const;
341};
342
343template <>
344struct from_proto_impl<robot::v1::ResourceStatus> {
345 RobotClient::resource_status operator()(const robot::v1::ResourceStatus*) const;
346};
347
348template <>
349struct from_proto_impl<robot::v1::ConfigStatus> {
350 RobotClient::config_status operator()(const robot::v1::ConfigStatus*) const;
351};
352
353template <>
354struct from_proto_impl<robot::v1::ModuleStatus> {
355 RobotClient::module_status operator()(const robot::v1::ModuleStatus*) const;
356};
357
358template <>
359struct from_proto_impl<robot::v1::PackageStatus> {
360 RobotClient::package_status operator()(const robot::v1::PackageStatus*) const;
361};
362
363template <>
364struct from_proto_impl<robot::v1::JobStatus> {
365 RobotClient::job_status operator()(const robot::v1::JobStatus*) const;
366};
367
368template <>
369struct from_proto_impl<robot::v1::GetMachineStatusResponse> {
370 RobotClient::machine_status operator()(const robot::v1::GetMachineStatusResponse*) const;
371};
372
373} // namespace proto_convert_details
374} // namespace sdk
375} // 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:227
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
Definition dial.hpp:29
Definition dial.hpp:25
status
the current status of the robot
Definition client.hpp:59
Defines the resource registry and associated types.
Definition client.hpp:100
Definition client.hpp:154
Definition client.hpp:106
Definition client.hpp:73
Represents a pose and two reference frames.
Definition world_state.hpp:47
Definition log_backend.hpp:18
Definition pose.hpp:39
Defines WorldState.