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>
14#include <viam/sdk/common/world_state.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;
28
29} // namespace v1
30} // namespace robot
31
32namespace sdk {
33
34namespace impl {
35struct LogBackend;
36}
37
39
47 public:
51 enum class status : uint8_t {
52 k_initializing,
53 k_running,
54 k_unspecified,
55 };
56
57 friend std::ostream& operator<<(std::ostream& os, const status& v);
58
61 ProtoStruct kinematics;
62 friend bool operator==(const frame_system_config& lhs, const frame_system_config& rhs);
63 };
64
65 struct operation {
66 std::string id;
67 std::string method;
68 boost::optional<std::string> session_id;
69 ProtoStruct arguments;
70 boost::optional<time_pt> started;
71 friend bool operator==(const operation& lhs, const operation& rhs);
72 };
73
74 explicit RobotClient(ViamChannel channel);
75
77
84 void refresh();
85
89 void close();
90
94 static std::shared_ptr<RobotClient> at_address(const std::string& address,
95 const Options& options);
96
102 static std::shared_ptr<RobotClient> at_local_socket(const std::string& address,
103 const Options& options);
104
108 static std::shared_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
109
110 std::vector<Name> resource_names() const;
111
123 std::shared_ptr<Resource> resource_by_name(const Name& name);
124
125 template <typename T>
130 std::shared_ptr<T> resource_by_name(std::string name) {
131 return std::dynamic_pointer_cast<T>(resource_by_name({API::get<T>(), "", std::move(name)}));
132 }
133
136 std::vector<frame_system_config> get_frame_system_config(
137 const std::vector<WorldState::transform>& additional_transforms = {});
138
141 std::vector<operation> get_operations();
142
148 const pose_in_frame& query,
149 std::string destination,
150 const std::vector<WorldState::transform>& additional_transforms = {});
151
154 void block_for_operation(std::string id);
155
157 void stop_all();
158
161 void stop_all(const std::unordered_map<Name, ProtoStruct>& extra);
162
165 void cancel_operation(std::string id);
166
169
170 private:
171 friend class ModuleService;
172 friend struct impl::LogBackend;
173
174 void log(const std::string& name,
175 const std::string& level,
176 const std::string& message,
177 time_pt time);
178
179 // Makes this RobotClient manage logging by sending logs over grpc to viam-server.
180 // This is private and only ever called by ModuleService; in other words it is only called when
181 // viam-server is running a Viam C++ SDK application as a module.
182 // Disables console logging so as to avoid log message duplication; console logging is
183 // re-enabled on destruction.
184 void connect_logging();
185
186 void refresh_every();
187
188 std::thread refresh_thread_;
189 std::atomic<bool> should_refresh_;
190 std::chrono::seconds refresh_interval_;
191
192 ViamChannel viam_channel_;
193
194 struct impl;
195 std::unique_ptr<impl> impl_;
196
197 mutable std::mutex lock_;
198
199 std::vector<Name> resource_names_;
200 ResourceManager resource_manager_;
201};
202
203namespace proto_convert_details {
204
205template <>
206struct from_proto_impl<robot::v1::Operation> {
207 RobotClient::operation operator()(const robot::v1::Operation*) const;
208};
209
210template <>
211struct from_proto_impl<robot::v1::FrameSystemConfig> {
212 RobotClient::frame_system_config operator()(const robot::v1::FrameSystemConfig*) const;
213};
214
215} // namespace proto_convert_details
216} // namespace sdk
217} // 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
Definition dial.hpp:114
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:46
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.
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:130
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:15
status
the current status of the robot
Definition client.hpp:51
Defines the resource registry and associated types.
Definition client.hpp:65
Definition world_state.hpp:32
Definition log_backend.hpp:18
Definition pose.hpp:39