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 FrameSystemConfig;
27class Operation;
28class SendTracesRequest;
29
30} // namespace v1
31} // namespace robot
32
33namespace sdk {
34
35namespace impl {
36struct LogBackend;
37class ParentSendTracesExporter;
38} // namespace impl
39
41
49 public:
53 enum class status : uint8_t {
54 k_initializing,
55 k_running,
56 k_unspecified,
57 };
58
59 friend std::ostream& operator<<(std::ostream& os, const status& v);
60
63 ProtoStruct kinematics;
64 friend bool operator==(const frame_system_config& lhs, const frame_system_config& rhs);
65 };
66
67 struct operation {
68 std::string id;
69 std::string method;
70 boost::optional<std::string> session_id;
71 ProtoStruct arguments;
72 boost::optional<time_pt> started;
73 friend bool operator==(const operation& lhs, const operation& rhs);
74 };
75
76 explicit RobotClient(ViamChannel channel);
77
79
86 void refresh();
87
91 void close();
92
96 static std::shared_ptr<RobotClient> at_address(const std::string& address,
97 const Options& options);
98
104 static std::shared_ptr<RobotClient> at_local_socket(const std::string& address,
105 const Options& options);
106
110 static std::shared_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
111
112 std::vector<Name> resource_names() const;
113
125 std::shared_ptr<Resource> resource_by_name(const Name& name);
126
127 template <typename T>
132 std::shared_ptr<T> resource_by_name(std::string name) {
133 return std::dynamic_pointer_cast<T>(resource_by_name({API::get<T>(), "", std::move(name)}));
134 }
135
138 std::vector<frame_system_config> get_frame_system_config(
139 const std::vector<WorldState::transform>& additional_transforms = {});
140
143 std::vector<operation> get_operations();
144
150 const pose_in_frame& query,
151 std::string destination,
152 const std::vector<WorldState::transform>& additional_transforms = {});
153
156 void block_for_operation(std::string id);
157
159 void stop_all();
160
163 void stop_all(const std::unordered_map<Name, ProtoStruct>& extra);
164
167 void cancel_operation(std::string id);
168
172 pose_in_frame get_pose(const std::string& component_name);
173
180 pose_in_frame get_pose(const std::string& component_name,
181 const std::string& destination_frame,
182 const std::vector<WorldState::transform>& additional_transforms,
183 const ProtoStruct& extra);
184
187
188 private:
189 friend class ModuleService;
190 friend struct impl::LogBackend;
191 friend class impl::ParentSendTracesExporter;
192
193 void log(const std::string& name,
194 const std::string& level,
195 const std::string& message,
196 time_pt time,
197 const std::string& file,
198 unsigned int line);
199
200 // Ships a batch of OTLP traces to the parent. Returns true on success.
201 bool send_traces(const robot::v1::SendTracesRequest* req);
202
203 // Makes this RobotClient manage logging by sending logs over grpc to viam-server.
204 // This is private and only ever called by ModuleService; in other words it is only called when
205 // viam-server is running a Viam C++ SDK application as a module.
206 // Disables console logging so as to avoid log message duplication; console logging is
207 // re-enabled on destruction.
208 void connect_logging();
209
210 // Installs the SDK tracer provider so spans flow back to the parent over this connection.
211 // Only called by ModuleService when running as a module.
212 void connect_tracing();
213
214 void refresh_every();
215 void check_connection();
216
217 std::thread refresh_thread_;
218 std::thread check_connection_thread_;
219 std::atomic<bool> should_refresh_;
220 std::atomic<bool> should_check_connection_;
221 std::chrono::seconds refresh_interval_;
222 std::chrono::seconds check_every_interval_;
223 std::chrono::seconds reconnect_every_interval_;
224
225 ViamChannel viam_channel_;
226
227 struct impl;
228 std::unique_ptr<impl> impl_;
229
230 mutable std::mutex lock_;
231
232 std::vector<Name> resource_names_;
233 ResourceManager resource_manager_;
234};
235
236namespace proto_convert_details {
237
238template <>
239struct from_proto_impl<robot::v1::Operation> {
240 RobotClient::operation operator()(const robot::v1::Operation*) const;
241};
242
243template <>
244struct from_proto_impl<robot::v1::FrameSystemConfig> {
245 RobotClient::frame_system_config operator()(const robot::v1::FrameSystemConfig*) const;
246};
247
248} // namespace proto_convert_details
249} // namespace sdk
250} // 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:48
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:132
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....
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:53
Defines the resource registry and associated types.
Definition client.hpp:67
Represents a pose and two reference frames.
Definition world_state.hpp:47
Definition log_backend.hpp:18
Definition pose.hpp:39
Defines WorldState.