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/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
23namespace impl {
24struct LogBackend;
25}
26
28
40 public:
44 enum class status : uint8_t {
45 k_initializing,
46 k_running,
47 k_unspecified,
48 };
49
50 friend std::ostream& operator<<(std::ostream& os, const status& v);
51
54 ProtoStruct kinematics;
55 friend bool operator==(const frame_system_config& lhs, const frame_system_config& rhs);
56 };
57
58 struct operation {
59 std::string id;
60 std::string method;
61 boost::optional<std::string> session_id;
62 ProtoStruct arguments;
63 boost::optional<time_pt> started;
64 friend bool operator==(const operation& lhs, const operation& rhs);
65 };
66
68 void refresh();
69 void close();
70
74 static std::shared_ptr<RobotClient> at_address(const std::string& address,
75 const Options& options);
76
82 static std::shared_ptr<RobotClient> at_local_socket(const std::string& address,
83 const Options& options);
84
90 static std::shared_ptr<RobotClient> with_channel(std::shared_ptr<ViamChannel> channel,
91 const Options& options);
92
93 RobotClient(std::shared_ptr<ViamChannel> channel);
94
95 std::vector<Name> resource_names() const;
96
108 std::shared_ptr<Resource> resource_by_name(const Name& name);
109
110 template <typename T>
115 std::shared_ptr<T> resource_by_name(std::string name) {
116 return std::dynamic_pointer_cast<T>(resource_by_name({API::get<T>(), "", std::move(name)}));
117 }
118
121 std::vector<frame_system_config> get_frame_system_config(
122 const std::vector<WorldState::transform>& additional_transforms = {});
123
126 std::vector<operation> get_operations();
127
133 const pose_in_frame& query,
134 std::string destination,
135 const std::vector<WorldState::transform>& additional_transforms = {});
136
139 void block_for_operation(std::string id);
140
142 void stop_all();
143
146 void stop_all(const std::unordered_map<Name, ProtoStruct>& extra);
147
150 void cancel_operation(std::string id);
151
154
155 private:
156 friend class ModuleService;
157 friend struct impl::LogBackend;
158
159 void log(const std::string& name,
160 const std::string& level,
161 const std::string& message,
162 time_pt time);
163
164 void connect_logging();
165
166 void refresh_every();
167
168 std::vector<std::shared_ptr<std::thread>> threads_;
169
170 std::atomic<bool> should_refresh_;
171 unsigned int refresh_interval_;
172
173 std::shared_ptr<GrpcChannel> channel_;
174 std::shared_ptr<ViamChannel> viam_channel_;
175 bool should_close_channel_;
176
177 struct impl;
178 std::unique_ptr<impl> impl_;
179
180 mutable std::mutex lock_;
181
182 std::vector<Name> resource_names_;
183 ResourceManager resource_manager_;
184};
185
186} // namespace sdk
187} // 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:106
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:39
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::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:115
void stop_all(const std::unordered_map< Name, ProtoStruct > &extra)
Cancel all operations for the robot and stop all actuators and movement.
status get_machine_status() const
gets the current status of the machine
status
the current status of the robot
Definition client.hpp:44
Defines the resource registry and associated types.
Definition client.hpp:58
Definition world_state.hpp:32
Definition log_backend.hpp:18
Definition pose.hpp:39