Skip to content

Commit

Permalink
feat: Add command for setting acceleration/deceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
2b-t committed Nov 18, 2023
1 parent e6af0cc commit b0b26ff
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* \file acceleration_function_index.hpp
* \mainpage
* Index of the acceleration/deceleration types
* \author
* Tobit Flatscher (github.com/2b-t)
*/

#ifndef MYACTUATOR_RMD__ACCELERATION_FUNCTION_INDEX
#define MYACTUATOR_RMD__ACCELERATION_FUNCTION_INDEX
#pragma once

#include <cstdint>


namespace myactuator_rmd {

/**\enum AccelerationFunctionIndex
* \brief
* Strongly typed enum for the different acceleration (from initial to maximum speed)/
* deceleration (from maximum speed to stop) types
*/
enum class AccelerationFunctionIndex: std::uint8_t {
POSITION_PLANNING_ACCELERATION = 0x00,
POSITION_PLANNING_DECELERATION = 0x01,
VELOCITY_PLANNING_ACCELERATION = 0x02,
VELOCITY_PLANNING_DECELERATION = 0x03
};

}

#endif // MYACTUATOR_RMD__ACCELERATION_FUNCTION_INDEX
12 changes: 12 additions & 0 deletions include/myactuator_rmd/driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstdint>
#include <string>

#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/actuator_state/control_mode.hpp"
#include "myactuator_rmd/actuator_state/feedback.hpp"
#include "myactuator_rmd/actuator_state/gains.hpp"
Expand Down Expand Up @@ -261,6 +262,17 @@ namespace myactuator_rmd {
*/
Feedback sendVelocitySetpoint(float const speed);

/**\fn setAcceleration
* \brief
* Write the acceleration/deceleration for the different modes to RAM and ROM (persistent)
*
* \param[in] acceleration
* The desired acceleration/deceleration in dps with a resolution of 1 dps/s [100, 60000]
* \param[in] mode
* The mode of the desired acceleration/deceleration to be set
*/
void setAcceleration(std::uint32_t const acceleration, AccelerationFunctionIndex const mode);

/**\fn setControllerGains
* \brief
* Write the currently used controller gains either to RAM (not persistent after reboot) or ROM (persistent)
Expand Down
2 changes: 1 addition & 1 deletion include/myactuator_rmd/protocol/command_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace myactuator_rmd {
WRITE_PID_PARAMETERS_TO_RAM = 0x31,
WRITE_PID_PARAMETERS_TO_ROM = 0x32,
READ_ACCELERATION = 0x42,
// WRITE_ACCELERATION_TO_RAM_AND_ROM = 0x43,
WRITE_ACCELERATION_TO_RAM_AND_ROM = 0x43,
READ_MULTI_TURN_ENCODER_POSITION = 0x60,
READ_MULTI_TURN_ENCODER_ORIGINAL_POSITION = 0x61,
READ_MULTI_TURN_ENCODER_ZERO_OFFSET = 0x62,
Expand Down
45 changes: 45 additions & 0 deletions include/myactuator_rmd/protocol/requests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define MYACTUATOR_RMD__PROTOCOL__REQUESTS
#pragma once

#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/actuator_state/gains.hpp"
#include "myactuator_rmd/protocol/command_type.hpp"
#include "myactuator_rmd/protocol/single_motor_message.hpp"
Expand Down Expand Up @@ -37,6 +38,50 @@ namespace myactuator_rmd {
using ReleaseBrakeRequest = SingleMotorRequest<CommandType::RELEASE_BRAKE>;
using ResetRequest = SingleMotorRequest<CommandType::RESET_SYSTEM>;

/**\class SetAccelerationRequest
* \brief
* Request for setting the maximum acceleration/deceleration of the actuator
*/
class SetAccelerationRequest: public SingleMotorRequest<CommandType::WRITE_ACCELERATION_TO_RAM_AND_ROM> {
public:
/**\fn SetAccelerationRequest
* \brief
* Class constructor
*
* \param[in] acceleration
* The desired acceleration/deceleration in dps with a resolution of 1 dps [100, 60000]
* \param[in] mode
* The mode of the desired acceleration/deceleration to be set
*/
SetAccelerationRequest(std::uint32_t const acceleration, AccelerationFunctionIndex const mode);
SetAccelerationRequest() = delete;
SetAccelerationRequest(SetAccelerationRequest const&) = default;
SetAccelerationRequest& operator = (SetAccelerationRequest const&) = default;
SetAccelerationRequest(SetAccelerationRequest&&) = default;
SetAccelerationRequest& operator = (SetAccelerationRequest&&) = default;
using SingleMotorRequest::SingleMotorRequest;

/**\fn getAcceleration
* \brief
* Get the acceleration
*
* \return
* The acceleration in degree per second**2 [100, 60000]
*/
[[nodiscard]]
std::uint32_t getAcceleration() const noexcept;

/**\fn getMode
* \brief
* Get the acceleration mode
*
* \return
* The acceleration mode
*/
[[nodiscard]]
AccelerationFunctionIndex getMode() const noexcept;
};

/**\class SetGainsRequest
* \brief
* Base class for all requests for setting controller gains
Expand Down
1 change: 1 addition & 0 deletions include/myactuator_rmd/protocol/responses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ namespace myactuator_rmd {
std::uint32_t getVersion() const noexcept;
};

using SetAccelerationResponse = SingleMotorResponse<CommandType::WRITE_ACCELERATION_TO_RAM_AND_ROM>;
using ShutdownMotorResponse = SingleMotorResponse<CommandType::SHUTDOWN_MOTOR>;
using StopMotorResponse = SingleMotorResponse<CommandType::STOP_MOTOR>;

Expand Down
6 changes: 6 additions & 0 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ namespace myactuator_rmd {
return response.getStatus();
}

void Driver::setAcceleration(std::uint32_t const acceleration, AccelerationFunctionIndex const mode) {
SetAccelerationRequest const request {acceleration, mode};
[[maybe_unused]] auto const response {sendRecv<SetAccelerationResponse>(request)};
return;
}

Gains Driver::setControllerGains(Gains const& gains, bool const is_persistent) {
if (is_persistent) {
SetControllerGainsPersistentlyRequest const request {gains};
Expand Down
20 changes: 20 additions & 0 deletions src/protocol/requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,32 @@
#include <cstring>
#include <string>

#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/protocol/single_motor_message.hpp"
#include "myactuator_rmd/exceptions.hpp"


namespace myactuator_rmd {

SetAccelerationRequest::SetAccelerationRequest(std::uint32_t const acceleration, AccelerationFunctionIndex const mode)
: SingleMotorRequest{} {
if ((acceleration < 100) || (acceleration > 60000)) {
throw ValueRangeException("Acceleration value '" + std::to_string(acceleration) + "' out of range [100, 60000]");
}
auto const acceleration_function_index {static_cast<std::uint8_t>(mode)};
setAt(acceleration_function_index, 1);
setAt(acceleration, 4);
return;
}

std::uint32_t SetAccelerationRequest::getAcceleration() const noexcept {
return getAs<std::uint32_t>(4);
}

AccelerationFunctionIndex SetAccelerationRequest::getMode() const noexcept {
return static_cast<AccelerationFunctionIndex>(getAs<std::uint8_t>(1));
}

SetPositionAbsoluteRequest::SetPositionAbsoluteRequest(float const position, float const max_speed)
: SingleMotorRequest{} {
if ((position < -360.0f) || (position > 360.0f)) {
Expand Down
35 changes: 35 additions & 0 deletions test/protocol/requests_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,50 @@
* Tobit Flatscher (github.com/2b-t)
*/

#include <cstdint>

#include <gtest/gtest.h>

#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/actuator_state/gains.hpp"
#include "myactuator_rmd/protocol/requests.hpp"


namespace myactuator_rmd {
namespace test {

TEST(SetPositionPlanningAccelerationRequestTest, parsing) {
myactuator_rmd::SetAccelerationRequest const request {{0x43, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00}};
std::uint32_t const acceleration {request.getAcceleration()};
AccelerationFunctionIndex const mode {request.getMode()};
EXPECT_EQ(acceleration, 10000);
EXPECT_EQ(mode, AccelerationFunctionIndex::POSITION_PLANNING_ACCELERATION);
}

TEST(SetPositionPlanningDecelerationRequestTest, parsing) {
myactuator_rmd::SetAccelerationRequest const request {{0x43, 0x01, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00}};
std::uint32_t const acceleration {request.getAcceleration()};
AccelerationFunctionIndex const mode {request.getMode()};
EXPECT_EQ(acceleration, 10000);
EXPECT_EQ(mode, AccelerationFunctionIndex::POSITION_PLANNING_DECELERATION);
}

TEST(SetVelocityPlanningAccelerationRequestTest, parsing) {
myactuator_rmd::SetAccelerationRequest const request {{0x43, 0x02, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00}};
std::uint32_t const acceleration {request.getAcceleration()};
AccelerationFunctionIndex const mode {request.getMode()};
EXPECT_EQ(acceleration, 10000);
EXPECT_EQ(mode, AccelerationFunctionIndex::VELOCITY_PLANNING_ACCELERATION);
}

TEST(SetVelocityPlanningDecelerationRequestTest, parsing) {
myactuator_rmd::SetAccelerationRequest const request {{0x43, 0x03, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00}};
std::uint32_t const acceleration {request.getAcceleration()};
AccelerationFunctionIndex const mode {request.getMode()};
EXPECT_EQ(acceleration, 10000);
EXPECT_EQ(mode, AccelerationFunctionIndex::VELOCITY_PLANNING_DECELERATION);
}

TEST(SetControllerGainsPersistentlyRequestTest, parsing) {
myactuator_rmd::SetControllerGainsPersistentlyRequest const request {{0x32, 0x00, 0x55, 0x19, 0x55, 0x19, 0x55, 0x19}};
myactuator_rmd::Gains const gains {request.getGains()};
Expand Down

0 comments on commit b0b26ff

Please sign in to comment.