Viam C++ SDK current
Loading...
Searching...
No Matches
motor.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <string>
7
8#include <viam/api/component/motor/v1/motor.pb.h>
9
10#include <viam/sdk/common/proto_type.hpp>
11#include <viam/sdk/common/utils.hpp>
12#include <viam/sdk/config/resource.hpp>
13#include <viam/sdk/resource/stoppable.hpp>
14
15namespace viam {
16namespace sdk {
17
24class Motor : public Component, public Stoppable {
25 public:
28 typedef double position;
29
32 struct power_status {
34 bool is_on;
38 double power_pct;
39 };
40
43 struct properties {
46 };
47
49 static position from_proto(const viam::component::motor::v1::GetPositionResponse& proto);
50
52 static power_status from_proto(const viam::component::motor::v1::IsPoweredResponse& proto);
53
55 static properties from_proto(const viam::component::motor::v1::GetPropertiesResponse& proto);
56
58 static viam::component::motor::v1::GetPositionResponse to_proto(const position& position);
59
61 static viam::component::motor::v1::IsPoweredResponse to_proto(const power_status& power_status);
62
64 static viam::component::motor::v1::GetPropertiesResponse to_proto(const properties& properties);
65
69 inline void set_power(double power_pct) {
70 return set_power(power_pct, {});
71 }
72
77 virtual void set_power(double power_pct, const AttributeMap& extra) = 0;
78
87 inline void go_for(double rpm, double revolutions) {
88 return go_for(rpm, revolutions, {});
89 }
90
100 virtual void go_for(double rpm, double revolutions, const AttributeMap& extra) = 0;
101
107 inline void go_to(double rpm, double position_revolutions) {
108 return go_to(rpm, position_revolutions, {});
109 }
110
117 virtual void go_to(double rpm, double position_revolutions, const AttributeMap& extra) = 0;
118
122 inline void set_rpm(double rpm) {
123 return set_rpm(rpm, {});
124 }
125
130 virtual void set_rpm(double rpm, const AttributeMap& extra) = 0;
131
135 inline void reset_zero_position(double offset) {
136 return reset_zero_position(offset, {});
137 }
138
143 virtual void reset_zero_position(double offset, const AttributeMap& extra) = 0;
144
148 return get_position({});
149 }
150
154 virtual position get_position(const AttributeMap& extra) = 0;
155
159 return get_properties({});
160 }
161
165 virtual properties get_properties(const AttributeMap& extra) = 0;
166
169 return get_power_status({});
170 }
171
174 virtual power_status get_power_status(const AttributeMap& extra) = 0;
175
177 virtual bool is_moving() = 0;
178
182 virtual AttributeMap do_command(const AttributeMap& command) = 0;
183
186 inline std::vector<GeometryConfig> get_geometries() {
187 return get_geometries({});
188 }
189
193 virtual std::vector<GeometryConfig> get_geometries(const AttributeMap& extra) = 0;
194
195 API api() const override;
196
197 protected:
198 explicit Motor(std::string name);
199};
200
201template <>
203 static API api();
204};
205
206bool operator==(const Motor::power_status& lhs, const Motor::power_status& rhs);
207bool operator==(const Motor::properties& lhs, const Motor::properties& rhs);
208
209} // namespace sdk
210} // namespace viam
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
Definition component.hpp:14
A Motor represents physical hardware that controls the rotation of an axle.
Definition motor.hpp:24
static viam::component::motor::v1::GetPositionResponse to_proto(const position &position)
Converts a position struct to its proto representation.
virtual AttributeMap do_command(const AttributeMap &command)=0
Send/receive arbitrary commands to the resource.
void go_to(double rpm, double position_revolutions)
Move the motor to a specific position that is relative to its home position at a specified speed whic...
Definition motor.hpp:107
virtual std::vector< GeometryConfig > get_geometries(const AttributeMap &extra)=0
Returns GeometryConfigs associated with the calling motor.
virtual properties get_properties(const AttributeMap &extra)=0
Returns the properties of the motor which comprises the booleans indicating.
position get_position()
Reports the position of the robot's motor relative to its zero position.
Definition motor.hpp:147
void set_power(double power_pct)
Sets the percentage of the motor's total power that should be employed.
Definition motor.hpp:69
static properties from_proto(const viam::component::motor::v1::GetPropertiesResponse &proto)
Creates a properties struct from its proto representation.
void set_rpm(double rpm)
Move the motor indefinitely at a specified speed which is expressed in RPM.
Definition motor.hpp:122
std::vector< GeometryConfig > get_geometries()
Returns GeometryConfigs associated with the calling motor.
Definition motor.hpp:186
virtual position get_position(const AttributeMap &extra)=0
Reports the position of the robot's motor relative to its zero position.
static power_status from_proto(const viam::component::motor::v1::IsPoweredResponse &proto)
Creates a power_status struct from its proto representation.
virtual power_status get_power_status(const AttributeMap &extra)=0
static viam::component::motor::v1::IsPoweredResponse to_proto(const power_status &power_status)
Converts a power_status struct to its proto representation.
void go_for(double rpm, double revolutions)
Instructs the motor to turn at a specified speed, which is expressed in RPM, for a specified number o...
Definition motor.hpp:87
properties get_properties()
Returns the properties of the motor which comprises the booleans indicating which optional features t...
Definition motor.hpp:158
power_status get_power_status()
Definition motor.hpp:168
virtual void set_rpm(double rpm, const AttributeMap &extra)=0
Move the motor indefinitely at a specified speed which is expressed in RPM.
virtual void reset_zero_position(double offset, const AttributeMap &extra)=0
Sets the current position of the motor as the new zero position.
static viam::component::motor::v1::GetPropertiesResponse to_proto(const properties &properties)
Converts a properties struct to its proto representation.
virtual void go_for(double rpm, double revolutions, const AttributeMap &extra)=0
Instructs the motor to turn at a specified speed, which is expressed in RPM, for a specified number o...
virtual void go_to(double rpm, double position_revolutions, const AttributeMap &extra)=0
Move the motor to a specific position that is relative to its home position at a specified speed whic...
void reset_zero_position(double offset)
Sets the current position of the motor as the new zero position.
Definition motor.hpp:135
static position from_proto(const viam::component::motor::v1::GetPositionResponse &proto)
Creates a position struct from its proto representation.
virtual bool is_moving()=0
Reports if a component is in motion.
virtual void set_power(double power_pct, const AttributeMap &extra)=0
Sets the percentage of the motor's total power that should be employed.
API api() const override
Returns the API associated with a particular resource.
virtual std::string name() const
Return the resource's name.
Definition stoppable.hpp:8
Current position of the motor relative to its home.
Definition resource_api.hpp:50
Information about power-state of the motor.
Definition motor.hpp:32
bool is_on
True if the motor is on.
Definition motor.hpp:34
double power_pct
Definition motor.hpp:38
Features that the specific motor supports.
Definition motor.hpp:43
bool position_reporting
True if the motor supports reporting its position.
Definition motor.hpp:45