Skip to content

Commit

Permalink
Adding read_data method
Browse files Browse the repository at this point in the history
  • Loading branch information
ShotaAk committed Jan 5, 2024
1 parent 00c6d34 commit a47e0fa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
29 changes: 29 additions & 0 deletions rt_manipulators_lib/include/hardware.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,35 @@ class Hardware {
return false;
}

template <typename IdentifyT, typename DataT>
bool read_data(const IdentifyT & identify, const uint16_t & addr, DataT & data)
{
if (!joints_.has_joint(identify)) {
std::cerr << "Joint: " << identify << " is not registered." << std::endl;
return false;
}

bool result = false;
if (std::is_same<DataT, uint8_t>::value) {
uint8_t tmp_data = 0x00;
result = comm_->read_byte_data(joints_.joint(identify)->id(), addr, tmp_data);
data = tmp_data;
}
if (std::is_same<DataT, uint16_t>::value) {
uint16_t tmp_data = 0x00;
result = comm_->read_word_data(joints_.joint(identify)->id(), addr, tmp_data);
data = tmp_data;
}
if (std::is_same<DataT, uint32_t>::value) {
uint32_t tmp_data = 0x00;
result = comm_->read_double_word_data(joints_.joint(identify)->id(), addr, tmp_data);
data = tmp_data;
}

return result;
}



protected:
std::shared_ptr<hardware_communicator::Communicator> comm_;
Expand Down
37 changes: 37 additions & 0 deletions rt_manipulators_lib/test/test_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,42 @@ TEST(HardwareTest, write_data) {
Verify(Method(mock, write_byte_data)).Never();
Verify(Method(mock, write_word_data)).Never();
Verify(Method(mock, write_double_word_data)).Never();
}

TEST(HardwareTest, read_data) {
auto mock = create_comm_mock();
rt_manipulators_cpp::Hardware hardware(
std::unique_ptr<hardware_communicator::Communicator>(&mock.get()));

EXPECT_TRUE(hardware.load_config_file("../config/ok_has_dynamixel_name.yaml"));

uint8_t byte_data = 0x00;
uint16_t word_data = 0x00;
uint32_t double_word_data = 0x00;
// Return false when joint name or id is not found
mock.ClearInvocationHistory();
EXPECT_FALSE(hardware.read_data("joint0", 0x00, byte_data));
EXPECT_FALSE(hardware.read_data(0, 0x00, byte_data));
Verify(Method(mock, read_byte_data)).Never();
Verify(Method(mock, read_word_data)).Never();
Verify(Method(mock, read_double_word_data)).Never();

// Identify joint via joint name
EXPECT_FALSE(hardware.read_data("joint1", 0x00, byte_data));
Verify(Method(mock, read_byte_data)).Once();

// Identify joint via joint id
EXPECT_FALSE(hardware.read_data(2, 0x00, word_data));
Verify(Method(mock, read_word_data)).Once();

EXPECT_FALSE(hardware.read_data("joint3", 0x00, double_word_data));
Verify(Method(mock, read_double_word_data)).Once();

// Return false when data type is not matched
double invalid_type_data = 0.0;
mock.ClearInvocationHistory();
EXPECT_FALSE(hardware.read_data("joint1", 0x00, invalid_type_data));
Verify(Method(mock, read_byte_data)).Never();
Verify(Method(mock, read_word_data)).Never();
Verify(Method(mock, read_double_word_data)).Never();
}

0 comments on commit a47e0fa

Please sign in to comment.