Skip to content

Commit 8aed0c9

Browse files
authored
RSDK-3589 add wrapper for navigation service (#323)
1 parent 3cd492c commit 8aed0c9

15 files changed

+876
-1
lines changed

src/viam/api/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
225225
${PROTO_GEN_DIR}/service/motion/v1/motion.grpc.pb.h
226226
${PROTO_GEN_DIR}/service/motion/v1/motion.pb.cc
227227
${PROTO_GEN_DIR}/service/motion/v1/motion.pb.h
228+
${PROTO_GEN_DIR}/service/navigation/v1/navigation.grpc.pb.cc
229+
${PROTO_GEN_DIR}/service/navigation/v1/navigation.grpc.pb.h
230+
${PROTO_GEN_DIR}/service/navigation/v1/navigation.pb.cc
231+
${PROTO_GEN_DIR}/service/navigation/v1/navigation.pb.h
228232
${PROTO_GEN_DIR}/tagger/v1/tagger.grpc.pb.cc
229233
${PROTO_GEN_DIR}/tagger/v1/tagger.grpc.pb.h
230234
${PROTO_GEN_DIR}/tagger/v1/tagger.pb.cc
@@ -328,6 +332,8 @@ target_sources(viamapi
328332
${PROTO_GEN_DIR}/service/mlmodel/v1/mlmodel.pb.cc
329333
${PROTO_GEN_DIR}/service/motion/v1/motion.grpc.pb.cc
330334
${PROTO_GEN_DIR}/service/motion/v1/motion.pb.cc
335+
${PROTO_GEN_DIR}/service/navigation/v1/navigation.grpc.pb.cc
336+
${PROTO_GEN_DIR}/service/navigation/v1/navigation.pb.cc
331337
${PROTO_GEN_DIR}/tagger/v1/tagger.grpc.pb.cc
332338
${PROTO_GEN_DIR}/tagger/v1/tagger.pb.cc
333339
PUBLIC FILE_SET viamapi_includes TYPE HEADERS
@@ -385,6 +391,8 @@ target_sources(viamapi
385391
${PROTO_GEN_DIR}/../../viam/api/service/mlmodel/v1/mlmodel.pb.h
386392
${PROTO_GEN_DIR}/../../viam/api/service/motion/v1/motion.grpc.pb.h
387393
${PROTO_GEN_DIR}/../../viam/api/service/motion/v1/motion.pb.h
394+
${PROTO_GEN_DIR}/../../viam/api/service/navigation/v1/navigation.grpc.pb.h
395+
${PROTO_GEN_DIR}/../../viam/api/service/navigation/v1/navigation.pb.h
388396
${PROTO_GEN_DIR}/../../viam/api/tagger/v1/tagger.pb.h
389397
)
390398

src/viam/sdk/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ target_sources(viamsdk
119119
services/generic.cpp
120120
services/mlmodel.cpp
121121
services/motion.cpp
122+
services/navigation.cpp
122123
services/private/generic_client.cpp
123124
services/private/generic_server.cpp
124125
services/private/mlmodel.cpp
125126
services/private/mlmodel_client.cpp
126127
services/private/mlmodel_server.cpp
127128
services/private/motion_client.cpp
128129
services/private/motion_server.cpp
130+
services/private/navigation_client.cpp
131+
services/private/navigation_server.cpp
129132
services/service.cpp
130133
spatialmath/geometry.cpp
131134
spatialmath/orientation.cpp
@@ -179,6 +182,7 @@ target_sources(viamsdk
179182
../../viam/sdk/services/generic.hpp
180183
../../viam/sdk/services/mlmodel.hpp
181184
../../viam/sdk/services/motion.hpp
185+
../../viam/sdk/services/navigation.hpp
182186
../../viam/sdk/services/service.hpp
183187
../../viam/sdk/spatialmath/geometry.hpp
184188
../../viam/sdk/spatialmath/orientation.hpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// @file common/proto_utils.hpp
2+
///
3+
/// @brief Utils that require generated proto includes. These should be #included
4+
/// in cpp implementation files, but not in wrapper headers consumed by third party code.
5+
#pragma once
6+
7+
#include <viam/api/common/v1/common.pb.h>
8+
9+
namespace viam {
10+
namespace sdk {
11+
namespace impl {
12+
13+
/// @brief Copies elements from a protobuf repeated pointer array into a std::vector. Src type
14+
/// must have a `to_proto` method.
15+
template <typename Src, typename Dst>
16+
void vecToRepeatedPtr(const std::vector<Src>& vec, google::protobuf::RepeatedPtrField<Dst>& dest) {
17+
dest.Clear();
18+
dest.Reserve(vec.size());
19+
for (auto& x : vec) {
20+
*dest.Add() = x.to_proto();
21+
}
22+
}
23+
24+
/// @brief Non-member to_proto() version. (necessary for moving generated types out of wrapper
25+
/// headers). Takes explicit `to_proto`.
26+
template <typename Src, typename Dst>
27+
void vecToRepeatedPtr(const std::vector<Src>& vec,
28+
google::protobuf::RepeatedPtrField<Dst>& dest,
29+
Dst to_proto(const Src&)) {
30+
dest.Clear();
31+
dest.Reserve(vec.size());
32+
for (auto& x : vec) {
33+
*dest.Add() = to_proto(x);
34+
}
35+
}
36+
37+
/// @brief Copies elements from a std::vector into a protobuf repeated pointer array. Dst type
38+
/// must have a `from_proto` static method.
39+
template <typename Src, typename Dst>
40+
void repeatedPtrToVec(const google::protobuf::RepeatedPtrField<Src>& src, std::vector<Dst>& vec) {
41+
vec.clear();
42+
vec.reserve(src.size());
43+
for (auto& x : src) {
44+
vec.push_back(Dst::from_proto(x));
45+
}
46+
}
47+
48+
/// @brief Non-member from_proto() version. (necessary for moving generated types out of wrapper
49+
/// headers). Takes explicit `from_proto`.
50+
template <typename Src, typename Dst>
51+
void repeatedPtrToVec(const google::protobuf::RepeatedPtrField<Src>& src,
52+
std::vector<Dst>& vec,
53+
Dst from_proto(const Src&)) {
54+
vec.clear();
55+
vec.reserve(src.size());
56+
for (auto& x : src) {
57+
vec.push_back(from_proto(x));
58+
}
59+
}
60+
61+
} // namespace impl
62+
} // namespace sdk
63+
} // namespace viam

src/viam/sdk/registry/registry.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include <viam/sdk/services/private/mlmodel_server.hpp>
4949
#include <viam/sdk/services/private/motion_client.hpp>
5050
#include <viam/sdk/services/private/motion_server.hpp>
51+
#include <viam/sdk/services/private/navigation_client.hpp>
52+
#include <viam/sdk/services/private/navigation_server.hpp>
5153
#include <viam/sdk/services/service.hpp>
5254

5355
namespace viam {
@@ -190,6 +192,7 @@ void register_resources() {
190192
Registry::register_resource<impl::GenericServiceClient, impl::GenericServiceServer>();
191193
Registry::register_resource<impl::MLModelServiceClient, impl::MLModelServiceServer>();
192194
Registry::register_resource<impl::MotionClient, impl::MotionServer>();
195+
Registry::register_resource<impl::NavigationClient, impl::NavigationServer>();
193196
}
194197

195198
void Registry::initialize() {

src/viam/sdk/services/motion.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class Motion : public Service {
247247
/// @param world_state Obstacles to avoid and transforms to add to the robot for the duration of
248248
/// the move.
249249
/// @param constraints Constraints to apply to how the robot will move.
250-
/// @extra Any additional arguments to the method.
250+
/// @param extra Any additional arguments to the method.
251251
/// @return Whether or not the move was successful.
252252
virtual bool move(const pose_in_frame& destination,
253253
const Name& name,

src/viam/sdk/services/navigation.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <viam/sdk/services/navigation.hpp>
2+
3+
#include <viam/api/service/navigation/v1/navigation.pb.h>
4+
#include <viam/sdk/common/private/proto_utils.hpp>
5+
#include <viam/sdk/common/utils.hpp>
6+
7+
namespace viam {
8+
namespace sdk {
9+
10+
Navigation::Navigation(std::string name) : Service(std::move(name)){};
11+
12+
API Navigation::api() const {
13+
return API::get<Navigation>();
14+
}
15+
16+
API API::traits<Navigation>::api() {
17+
return {kRDK, kService, "navigation"};
18+
}
19+
20+
} // namespace sdk
21+
} // namespace viam

src/viam/sdk/services/navigation.hpp

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/// @file services/navigation.hpp
2+
///
3+
/// @brief Defines a `Navigation` service.
4+
#pragma once
5+
6+
#include <string>
7+
8+
#include <viam/sdk/common/pose.hpp>
9+
#include <viam/sdk/common/utils.hpp>
10+
#include <viam/sdk/services/service.hpp>
11+
#include <viam/sdk/spatialmath/geometry.hpp>
12+
13+
namespace viam {
14+
namespace sdk {
15+
16+
class Navigation : public Service {
17+
public:
18+
/// @enum Mode
19+
/// @brief Enum affecting this nav service's goal.
20+
/// @ingroup Navigation
21+
enum class Mode : uint8_t {
22+
k_unspecified,
23+
k_manual,
24+
k_waypoint,
25+
k_explore,
26+
};
27+
28+
/// @enum MapType
29+
/// @brief Is the map navigating in GPS or a custom map.
30+
/// @ingroup Navigation
31+
enum class MapType : uint8_t {
32+
k_unspecified,
33+
k_none,
34+
k_gps,
35+
};
36+
37+
/// @struct LocationResponse
38+
/// @brief Location and direction.
39+
/// @ingroup Navigation
40+
struct LocationResponse {
41+
geo_point location;
42+
double compass_heading;
43+
44+
bool operator==(const LocationResponse& rhs) const {
45+
return compass_heading == rhs.compass_heading && location == rhs.location;
46+
}
47+
};
48+
49+
/// @struct Properties
50+
/// @brief A set of attributes for this nav service.
51+
/// @ingroup Navigation
52+
struct Properties {
53+
MapType map_type;
54+
};
55+
56+
/// @struct Waypoint
57+
/// @brief A location with an `id` handle that can be used to uniquely identify and remove it.
58+
/// @ingroup Navigation
59+
struct Waypoint {
60+
std::string id;
61+
geo_point location;
62+
};
63+
64+
/// @struct Path
65+
/// @brief A user-provided destination and a set of geopoints to get there.
66+
/// @ingroup Navigation
67+
struct Path {
68+
std::string destination_waypoint_id;
69+
std::vector<geo_point> geopoints;
70+
71+
bool operator==(const Path& rhs) const {
72+
return destination_waypoint_id == rhs.destination_waypoint_id &&
73+
geopoints == rhs.geopoints;
74+
}
75+
};
76+
77+
API api() const override;
78+
79+
/// @brief Get the current mode.
80+
/// @param extra Any additional arguments to the method.
81+
/// @return Current mode.
82+
virtual Mode get_mode(const ProtoStruct& extra) = 0;
83+
84+
/// @brief Set the current mode.
85+
/// @param mode Desired mode.
86+
/// @param extra Any additional arguments to the method.
87+
virtual void set_mode(const Mode mode, const ProtoStruct& extra) = 0;
88+
89+
/// @brief Get the current location.
90+
/// @param extra Any additional arguments to the method.
91+
/// @return Current location.
92+
virtual LocationResponse get_location(const ProtoStruct& extra) = 0;
93+
94+
/// @brief Get the waypoints this nav service knows about.
95+
/// @param extra Any additional arguments to the method.
96+
/// @return List of waypoints.
97+
virtual std::vector<Waypoint> get_waypoints(const ProtoStruct& extra) = 0;
98+
99+
/// @brief Add a waypoint.
100+
/// @param location Coordinate of the new waypoint.
101+
virtual void add_waypoint(const geo_point& location, const ProtoStruct& extra) = 0;
102+
103+
/// @brief Remove a waypoint by ID.
104+
/// @param id The string ID of the waypoint to remove.
105+
/// @param extra Any additional arguments to the method.
106+
virtual void remove_waypoint(const std::string id, const ProtoStruct& extra) = 0;
107+
108+
/// @brief Get the obstacles this nav service knows about.
109+
/// @param extra Any additional arguments to the method.
110+
/// @return List of shapes.
111+
virtual std::vector<geo_geometry> get_obstacles(const ProtoStruct& extra) = 0;
112+
113+
/// @brief Get the paths this nav service knows about.
114+
/// @param extra Any additional arguments to the method.
115+
/// @return List of paths.
116+
virtual std::vector<Path> get_paths(const ProtoStruct& extra) = 0;
117+
118+
/// @brief Get this nav service's properties.
119+
/// @return Properties.
120+
virtual Properties get_properties() = 0;
121+
122+
/// @brief Do an arbitrary command.
123+
/// @param command Freeform fields that are service-specific.
124+
/// @return Freeform result of the command.
125+
virtual ProtoStruct do_command(const ProtoStruct& command) = 0;
126+
127+
// overloads without `extra` param:
128+
129+
inline Mode get_mode() {
130+
return get_mode({});
131+
}
132+
133+
inline void set_mode(const Mode mode) {
134+
set_mode(mode, {});
135+
}
136+
137+
inline LocationResponse get_location() {
138+
return get_location({});
139+
}
140+
141+
inline std::vector<Waypoint> get_waypoints() {
142+
return get_waypoints({});
143+
}
144+
145+
inline void add_waypoint(const geo_point& location) {
146+
add_waypoint(location, {});
147+
}
148+
149+
inline void remove_waypoint(const std::string id) {
150+
remove_waypoint(id, {});
151+
}
152+
153+
inline std::vector<geo_geometry> get_obstacles() {
154+
return get_obstacles({});
155+
}
156+
157+
inline std::vector<Path> get_paths() {
158+
return get_paths({});
159+
}
160+
161+
protected:
162+
explicit Navigation(std::string name);
163+
};
164+
165+
template <>
166+
struct API::traits<Navigation> {
167+
static API api();
168+
};
169+
170+
} // namespace sdk
171+
} // namespace viam

0 commit comments

Comments
 (0)