Skip to content

Commit

Permalink
feat: Add setting CAN bus Baud rate to driver
Browse files Browse the repository at this point in the history
  • Loading branch information
2b-t committed Nov 19, 2023
1 parent 4e73165 commit 2f7b99b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 7 deletions.
4 changes: 4 additions & 0 deletions bindings/myactuator_rmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ PYBIND11_MODULE(myactuator_rmd, m) {
.def("sendTorqueSetpoint", &myactuator_rmd::Driver::sendTorqueSetpoint)
.def("sendVelocitySetpoint", &myactuator_rmd::Driver::sendVelocitySetpoint)
.def("setAcceleration", &myactuator_rmd::Driver::setAcceleration)
.def("setBaudRate", &myactuator_rmd::Driver::setBaudRate)
.def("setCanId", &myactuator_rmd::Driver::setCanId)
.def("setControllerGains", &myactuator_rmd::Driver::setControllerGains)
.def("shutdownMotor", &myactuator_rmd::Driver::shutdownMotor)
Expand All @@ -73,6 +74,9 @@ PYBIND11_MODULE(myactuator_rmd, m) {
.value("POSITION_PLANNING_DECELERATION", myactuator_rmd::AccelerationFunctionIndex::POSITION_PLANNING_DECELERATION)
.value("VELOCITY_PLANNING_ACCELERATION", myactuator_rmd::AccelerationFunctionIndex::VELOCITY_PLANNING_ACCELERATION)
.value("VELOCITY_PLANNING_DECELERATION", myactuator_rmd::AccelerationFunctionIndex::VELOCITY_PLANNING_DECELERATION);
pybind11::enum_<myactuator_rmd::BaudRate>(m_actuator_state, "BaudRate")
.value("KBPS500", myactuator_rmd::BaudRate::KBPS500)
.value("MBPS1", myactuator_rmd::BaudRate::MBPS1);
pybind11::enum_<myactuator_rmd::ControlMode>(m_actuator_state, "ControlMode")
.value("NONE", myactuator_rmd::ControlMode::NONE)
.value("CURRENT", myactuator_rmd::ControlMode::CURRENT)
Expand Down
29 changes: 29 additions & 0 deletions include/myactuator_rmd/actuator_state/baud_rate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* \file baud_rate.hpp
* \mainpage
* Communication Baud rate of the CAN bus
* \author
* Tobit Flatscher (github.com/2b-t)
*/

#ifndef MYACTUATOR_RMD__BAUD_RATE
#define MYACTUATOR_RMD__BAUD_RATE
#pragma once

#include <cstdint>


namespace myactuator_rmd {

/**\enum BaudRate
* \brief
* Communication Baud rate of the CAN bus
*/
enum class BaudRate: std::uint8_t {
KBPS500 = 0,
MBPS1 = 1,
};

}

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

#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/actuator_state/baud_rate.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 @@ -295,6 +296,15 @@ namespace myactuator_rmd {
*/
void setAcceleration(std::uint32_t const acceleration, AccelerationFunctionIndex const mode);

/**\fn setBaudRate
* \brief
* Set the communication Baud rate
*
* \param[in] baud_rate
* Communication Baud rate that the actuator should operator with
*/
void setBaudRate(BaudRate const baud_rate);

/**\fn setCanId
* \brief
* Set the CAN ID of the device
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 @@ -51,7 +51,7 @@ namespace myactuator_rmd {
READ_SYSTEM_RUNTIME = 0xB1,
READ_SYSTEM_SOFTWARE_VERSION_DATE = 0xB2,
// COMMUNICATION_INTERRUPTION_PROTECTION_TIME_SETTING = 0xB3,
// COMMUNICATION_BAUD_RATE_SETTING = 0xB4,
COMMUNICATION_BAUD_RATE_SETTING = 0xB4,
READ_MOTOR_MODEL = 0xB5,
// FUNCTION_CONTROL = 0x20,
CAN_ID_SETTING = 0x79
Expand Down
25 changes: 25 additions & 0 deletions include/myactuator_rmd/protocol/requests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <cstdint>

#include "myactuator_rmd/actuator_state/baud_rate.hpp"
#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/actuator_state/gains.hpp"
#include "myactuator_rmd/protocol/command_type.hpp"
Expand Down Expand Up @@ -148,6 +149,30 @@ namespace myactuator_rmd {
AccelerationFunctionIndex getMode() const noexcept;
};

/**\class SetBaudRateRequest
* \brief
* Request for setting the Baud rate of the actuator
*/
class SetBaudRateRequest: public SingleMotorRequest<CommandType::COMMUNICATION_BAUD_RATE_SETTING> {
public:
SetBaudRateRequest(BaudRate const baud_rate);
SetBaudRateRequest(SetBaudRateRequest const&) = default;
SetBaudRateRequest& operator = (SetBaudRateRequest const&) = default;
SetBaudRateRequest(SetBaudRateRequest&&) = default;
SetBaudRateRequest& operator = (SetBaudRateRequest&&) = default;
using SingleMotorRequest::SingleMotorRequest;

/**\fn getBaudRate
* \brief
* Get the Baud rate that should be set to the actuator
*
* \return
* The Baud rate that the actuator should be using
*/
[[nodiscard]]
BaudRate getBaudRate() const noexcept;
};

/**\class SetGainsRequest
* \brief
* Base class for all requests for setting controller gains
Expand Down
7 changes: 7 additions & 0 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdint>
#include <string>

#include "myactuator_rmd/actuator_state/baud_rate.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 @@ -178,6 +179,12 @@ namespace myactuator_rmd {
return;
}

void Driver::setBaudRate(BaudRate const baud_rate) {
SetBaudRateRequest const request {baud_rate};
send(request);
return;
}

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

#include "myactuator_rmd/actuator_state/baud_rate.hpp"
#include "myactuator_rmd/actuator_state/acceleration_function_index.hpp"
#include "myactuator_rmd/protocol/single_motor_message.hpp"
#include "myactuator_rmd/exceptions.hpp"
Expand Down Expand Up @@ -51,6 +52,16 @@ namespace myactuator_rmd {
return static_cast<AccelerationFunctionIndex>(getAs<std::uint8_t>(1));
}

SetBaudRateRequest::SetBaudRateRequest(BaudRate const baud_rate)
: SingleMotorRequest{} {
setAt(static_cast<std::uint8_t>(baud_rate), 7);
return;
}

BaudRate SetBaudRateRequest::getBaudRate() const noexcept {
return static_cast<BaudRate>(getAs<std::uint8_t>(7));
}

SetPositionAbsoluteRequest::SetPositionAbsoluteRequest(float const position, float const max_speed)
: SingleMotorRequest{} {
if ((position < -360.0f) || (position > 360.0f)) {
Expand Down
17 changes: 11 additions & 6 deletions test/protocol/requests_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <gtest/gtest.h>

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

Expand All @@ -24,12 +25,16 @@ namespace myactuator_rmd {
EXPECT_EQ(is_write, false);
}

TEST(SetCanIdRequestTest, parsing) {
myactuator_rmd::SetCanIdRequest const request {{0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}};
bool const is_write {request.isWrite()};
std::uint16_t const can_id {request.getCanId()};
EXPECT_EQ(is_write, true);
EXPECT_EQ(can_id, 0x02);
TEST(SetBaudRate0RequestTest, parsing) {
myactuator_rmd::SetBaudRateRequest const request {{0xB4, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}};
BaudRate const baud_rate {request.getBaudRate()};
EXPECT_EQ(baud_rate, BaudRate::KBPS500);
}

TEST(SetBaudRate1RequestTest, parsing) {
myactuator_rmd::SetBaudRateRequest const request {{0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}};
BaudRate const baud_rate {request.getBaudRate()};
EXPECT_EQ(baud_rate, BaudRate::MBPS1);
}

TEST(SetPositionPlanningAccelerationRequestTest, parsing) {
Expand Down

0 comments on commit 2f7b99b

Please sign in to comment.