|
| 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