Skip to content

Commit

Permalink
RSDK-7750: Add SetRPM to c++ SDK (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston authored Jun 6, 2024
1 parent 756af88 commit f437b22
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/viam/examples/modules/complex/proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: b70d5bc6e99147928d9d4455fd60fc7a
commit: f0e53af8f2fc4556b94f482688b57223
digest: shake256:de26a277fc28b8b411ecf58729d78d32fcf15090ffd998a4469225b17889bfb51442eaab04bb7a8d88d203ecdf0a9febd4ffd52c18ed1c2229160c7bd353ca95
13 changes: 13 additions & 0 deletions src/viam/sdk/components/motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ class Motor : public Component, public Stoppable {
/// @throws `Exception` if position reporting is not supported
virtual void go_to(double rpm, double position_revolutions, const AttributeMap& extra) = 0;

/// @brief Move the motor indefinitely at a specified speed which is expressed in RPM.
/// @param rpm Speed of motor travel in rotations per minute
/// @throws `Exception` if position reporting is not supported
inline void set_rpm(double rpm) {
return set_rpm(rpm, {});
}

/// @brief Move the motor indefinitely at a specified speed which is expressed in RPM.
/// @param rpm Speed of motor travel in rotations per minute
/// @param extra Any additional arguments to the method.
/// @throws `Exception` if position reporting is not supported
virtual void set_rpm(double rpm, const AttributeMap& extra) = 0;

/// @brief Sets the current position of the motor as the new zero position.
/// @param offset Motor position
/// @throws `Exception` if position reporting is not supported
Expand Down
6 changes: 6 additions & 0 deletions src/viam/sdk/components/private/motor_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ void MotorClient::go_to(double rpm, double position_revolutions, const Attribute
.invoke();
}

void MotorClient::set_rpm(double rpm, const AttributeMap& extra) {
return make_client_helper(this, *stub_, &StubType::SetRPM)
.with(extra, [&](auto& request) { request.set_rpm(rpm); })
.invoke();
}

void MotorClient::reset_zero_position(double offset, const AttributeMap& extra) {
return make_client_helper(this, *stub_, &StubType::ResetZeroPosition)
.with(extra, [&](auto& request) { request.set_offset(offset); })
Expand Down
2 changes: 2 additions & 0 deletions src/viam/sdk/components/private/motor_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MotorClient : public Motor {
void set_power(double power_pct, const AttributeMap& extra) override;
void go_for(double rpm, double revolutions, const AttributeMap& extra) override;
void go_to(double rpm, double position_revolutions, const AttributeMap& extra) override;
void set_rpm(double rpm, const AttributeMap& extra) override;
void reset_zero_position(double offset, const AttributeMap& extra) override;
position get_position(const AttributeMap& extra) override;
properties get_properties(const AttributeMap& extra) override;
Expand All @@ -51,6 +52,7 @@ class MotorClient : public Motor {
using Motor::go_to;
using Motor::reset_zero_position;
using Motor::set_power;
using Motor::set_rpm;
using Motor::stop;

private:
Expand Down
7 changes: 7 additions & 0 deletions src/viam/sdk/components/private/motor_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ ::grpc::Status MotorServer::GoTo(::grpc::ServerContext*,
});
}

::grpc::Status MotorServer::SetRPM(::grpc::ServerContext*,
const ::viam::component::motor::v1::SetRPMRequest* request,
::viam::component::motor::v1::SetRPMResponse*) noexcept {
return make_service_helper<Motor>("MotorServer::SetRPM", this, request)(
[&](auto& helper, auto& motor) { motor->set_rpm(request->rpm(), helper.getExtra()); });
}

::grpc::Status MotorServer::ResetZeroPosition(
::grpc::ServerContext*,
const ::viam::component::motor::v1::ResetZeroPositionRequest* request,
Expand Down
4 changes: 4 additions & 0 deletions src/viam/sdk/components/private/motor_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class MotorServer : public ResourceServer,
const ::viam::component::motor::v1::GoToRequest* request,
::viam::component::motor::v1::GoToResponse* response) noexcept override;

::grpc::Status SetRPM(::grpc::ServerContext* context,
const ::viam::component::motor::v1::SetRPMRequest* request,
::viam::component::motor::v1::SetRPMResponse* response) noexcept override;

::grpc::Status ResetZeroPosition(
::grpc::ServerContext* context,
const ::viam::component::motor::v1::ResetZeroPositionRequest* request,
Expand Down
5 changes: 5 additions & 0 deletions src/viam/sdk/tests/mocks/mock_motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ void MockMotor::go_to(double rpm, double position_revolutions, const AttributeMa
position_ = position_revolutions;
}

void MockMotor::set_rpm(double rpm, const AttributeMap&) {
power_status_.is_on = rpm != 0.0;
position_ += 1;
}

void MockMotor::reset_zero_position(double offset, const AttributeMap&) {
position_ -= offset;
}
Expand Down
2 changes: 2 additions & 0 deletions src/viam/sdk/tests/mocks/mock_motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MockMotor : public Motor {
void set_power(double power_pct, const sdk::AttributeMap& extra) override;
void go_for(double rpm, double revolutions, const sdk::AttributeMap& extra) override;
void go_to(double rpm, double position_revolutions, const sdk::AttributeMap& extra) override;
void set_rpm(double rpm, const sdk::AttributeMap& extra) override;
void reset_zero_position(double offset, const sdk::AttributeMap& extra) override;
Motor::position get_position(const sdk::AttributeMap& extra) override;
Motor::properties get_properties(const sdk::AttributeMap& extra) override;
Expand All @@ -36,6 +37,7 @@ class MockMotor : public Motor {
using Motor::go_to;
using Motor::reset_zero_position;
using Motor::set_power;
using Motor::set_rpm;
using Motor::stop;

private:
Expand Down
14 changes: 14 additions & 0 deletions src/viam/sdk/tests/test_motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ BOOST_AUTO_TEST_CASE(mock_go_to) {
BOOST_CHECK(motor->get_position() == 1.0);
}

BOOST_AUTO_TEST_CASE(mock_set_rpm) {
std::shared_ptr<MockMotor> motor = MockMotor::get_mock_motor();
motor->set_rpm(1.0);
BOOST_CHECK(motor->get_power_status().is_on);
}

BOOST_AUTO_TEST_CASE(mock_reset_zero_position) {
std::shared_ptr<MockMotor> motor = MockMotor::get_mock_motor();
motor->go_to(1.0, 1.0);
Expand Down Expand Up @@ -142,6 +148,14 @@ BOOST_AUTO_TEST_CASE(test_go_to) {
});
}

BOOST_AUTO_TEST_CASE(test_set_rpm) {
std::shared_ptr<MockMotor> mock = MockMotor::get_mock_motor();
client_to_mock_pipeline<Motor>(mock, [](Motor& client) {
client.set_rpm(1.0);
BOOST_CHECK(client.get_position() >= 1.0);
});
}

BOOST_AUTO_TEST_CASE(test_reset_zero_position) {
std::shared_ptr<MockMotor> mock = MockMotor::get_mock_motor();
client_to_mock_pipeline<Motor>(mock, [](Motor& client) {
Expand Down

0 comments on commit f437b22

Please sign in to comment.