|
20 | 20 | #include <vector>
|
21 | 21 | #include <string>
|
22 | 22 | #include <memory>
|
| 23 | +#include <functional> |
23 | 24 |
|
24 | 25 | namespace dynamixel_hardware_interface
|
25 | 26 | {
|
@@ -637,12 +638,12 @@ std::string Dynamixel::DxlErrorToString(DxlError error_num)
|
637 | 638 | }
|
638 | 639 | }
|
639 | 640 |
|
640 |
| -DxlError Dynamixel::ReadMultiDxlData() |
| 641 | +DxlError Dynamixel::ReadMultiDxlData(double period_ms) |
641 | 642 | {
|
642 | 643 | if (read_type_ == SYNC) {
|
643 |
| - return GetDxlValueFromSyncRead(); |
| 644 | + return GetDxlValueFromSyncRead(period_ms); |
644 | 645 | } else {
|
645 |
| - return GetDxlValueFromBulkRead(); |
| 646 | + return GetDxlValueFromBulkRead(period_ms); |
646 | 647 | }
|
647 | 648 | }
|
648 | 649 |
|
@@ -814,39 +815,27 @@ DxlError Dynamixel::SetSyncReadHandler(std::vector<uint8_t> id_arr)
|
814 | 815 | return DxlError::OK;
|
815 | 816 | }
|
816 | 817 |
|
817 |
| -DxlError Dynamixel::GetDxlValueFromSyncRead() |
| 818 | +DxlError Dynamixel::GetDxlValueFromSyncRead(double period_ms) |
818 | 819 | {
|
819 |
| - // SyncRead tx |
820 |
| - int dxl_comm_result = group_sync_read_->txRxPacket(); |
821 |
| - if (dxl_comm_result != COMM_SUCCESS) { |
822 |
| - fprintf(stderr, "SyncRead TxRx Fail [Dxl Size : %d] [Error code : %d]\n", read_data_list_.size(), dxl_comm_result); |
823 |
| - return DxlError::SYNC_READ_FAIL; |
| 820 | + DxlError comm_result = ProcessReadCommunication( |
| 821 | + group_sync_read_, group_bulk_read_, port_handler_, period_ms, true); |
| 822 | + if (comm_result != DxlError::OK) { |
| 823 | + return comm_result; |
824 | 824 | }
|
825 | 825 |
|
826 | 826 | for (auto it_read_data : read_data_list_) {
|
827 |
| - uint8_t ID = it_read_data.id; |
828 |
| - uint16_t IN_ADDR = indirect_info_read_[ID].indirect_data_addr; |
829 |
| - |
830 |
| - for (size_t item_index = 0; item_index < indirect_info_read_[ID].cnt; item_index++) { |
831 |
| - uint8_t SIZE = indirect_info_read_[ID].item_size.at(item_index); |
832 |
| - if (item_index > 0) {IN_ADDR += indirect_info_read_[ID].item_size.at(item_index - 1);} |
833 |
| - |
834 |
| - uint32_t dxl_getdata = group_sync_read_->getData(ID, IN_ADDR, SIZE); |
835 |
| - |
836 |
| - if (indirect_info_read_[ID].item_name.at(item_index) == "Present Position") { |
837 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
838 |
| - dxl_info_.ConvertValueToRadian(ID, static_cast<int32_t>(dxl_getdata)); |
839 |
| - } else if (indirect_info_read_[ID].item_name.at(item_index) == "Present Velocity") { |
840 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
841 |
| - dxl_info_.ConvertValueRPMToVelocityRPS(ID, static_cast<int32_t>(dxl_getdata)); |
842 |
| - } else if (indirect_info_read_[ID].item_name.at(item_index) == "Present Current") { |
843 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
844 |
| - dxl_info_.ConvertCurrentToEffort(ID, static_cast<int16_t>(dxl_getdata)); |
845 |
| - } else { |
846 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
847 |
| - static_cast<double>(dxl_getdata); |
848 |
| - } |
849 |
| - } |
| 827 | + uint8_t id = it_read_data.id; |
| 828 | + uint16_t indirect_addr = indirect_info_read_[id].indirect_data_addr; |
| 829 | + |
| 830 | + ProcessReadData( |
| 831 | + id, |
| 832 | + indirect_addr, |
| 833 | + indirect_info_read_[id].item_name, |
| 834 | + indirect_info_read_[id].item_size, |
| 835 | + it_read_data.item_data_ptr_vec, |
| 836 | + [this](uint8_t id, uint16_t addr, uint8_t size) { |
| 837 | + return group_sync_read_->getData(id, addr, size); |
| 838 | + }); |
850 | 839 | }
|
851 | 840 | return DxlError::OK;
|
852 | 841 | }
|
@@ -888,7 +877,7 @@ DxlError Dynamixel::SetBulkReadItemAndHandler()
|
888 | 877 |
|
889 | 878 | if (SetBulkReadHandler(id_arr) != DxlError::OK) {
|
890 | 879 | fprintf(stderr, "Cannot set the BulkRead handler.\n");
|
891 |
| - return DxlError::SYNC_READ_FAIL; |
| 880 | + return DxlError::BULK_READ_FAIL; |
892 | 881 | }
|
893 | 882 |
|
894 | 883 | fprintf(stderr, "Success to set BulkRead handler using indirect address\n");
|
@@ -937,42 +926,115 @@ DxlError Dynamixel::SetBulkReadHandler(std::vector<uint8_t> id_arr)
|
937 | 926 | return DxlError::OK;
|
938 | 927 | }
|
939 | 928 |
|
940 |
| -DxlError Dynamixel::GetDxlValueFromBulkRead() |
| 929 | +DxlError Dynamixel::GetDxlValueFromBulkRead(double period_ms) |
941 | 930 | {
|
942 |
| - int dxl_comm_result = group_bulk_read_->txRxPacket(); |
943 |
| - if (dxl_comm_result != COMM_SUCCESS) { |
944 |
| - fprintf(stderr, "BulkRead TxRx Fail [Dxl Size : %d] [Error code : %d]\n", read_data_list_.size(), dxl_comm_result); |
945 |
| - return DxlError::BULK_READ_FAIL; |
| 931 | + DxlError comm_result = ProcessReadCommunication( |
| 932 | + group_sync_read_, group_bulk_read_, port_handler_, period_ms, false); |
| 933 | + if (comm_result != DxlError::OK) { |
| 934 | + return comm_result; |
946 | 935 | }
|
947 | 936 |
|
948 | 937 | for (auto it_read_data : read_data_list_) {
|
949 |
| - uint8_t ID = it_read_data.id; |
950 |
| - uint16_t IN_ADDR = indirect_info_read_[ID].indirect_data_addr; |
951 |
| - |
952 |
| - for (size_t item_index = 0; item_index < indirect_info_read_[ID].cnt; item_index++) { |
953 |
| - uint8_t SIZE = indirect_info_read_[ID].item_size.at(item_index); |
954 |
| - if (item_index > 0) {IN_ADDR += indirect_info_read_[ID].item_size.at(item_index - 1);} |
955 |
| - |
956 |
| - uint32_t dxl_getdata = group_bulk_read_->getData(ID, IN_ADDR, SIZE); |
957 |
| - |
958 |
| - if (indirect_info_read_[ID].item_name.at(item_index) == "Present Position") { |
959 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
960 |
| - dxl_info_.ConvertValueToRadian(ID, static_cast<int32_t>(dxl_getdata)); |
961 |
| - } else if (indirect_info_read_[ID].item_name.at(item_index) == "Present Velocity") { |
962 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
963 |
| - dxl_info_.ConvertValueRPMToVelocityRPS(ID, static_cast<int32_t>(dxl_getdata)); |
964 |
| - } else if (indirect_info_read_[ID].item_name.at(item_index) == "Present Current") { |
965 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
966 |
| - dxl_info_.ConvertCurrentToEffort(ID, static_cast<int16_t>(dxl_getdata)); |
967 |
| - } else { |
968 |
| - *it_read_data.item_data_ptr_vec.at(item_index) = |
969 |
| - static_cast<double>(dxl_getdata); |
970 |
| - } |
| 938 | + uint8_t id = it_read_data.id; |
| 939 | + uint16_t indirect_addr = indirect_info_read_[id].indirect_data_addr; |
| 940 | + |
| 941 | + ProcessReadData( |
| 942 | + id, |
| 943 | + indirect_addr, |
| 944 | + indirect_info_read_[id].item_name, |
| 945 | + indirect_info_read_[id].item_size, |
| 946 | + it_read_data.item_data_ptr_vec, |
| 947 | + [this](uint8_t id, uint16_t addr, uint8_t size) { |
| 948 | + return group_bulk_read_->getData(id, addr, size); |
| 949 | + }); |
| 950 | + } |
| 951 | + return DxlError::OK; |
| 952 | +} |
| 953 | + |
| 954 | +DxlError Dynamixel::ProcessReadCommunication( |
| 955 | + dynamixel::GroupFastSyncRead * group_sync_read, |
| 956 | + dynamixel::GroupFastBulkRead * group_bulk_read, |
| 957 | + dynamixel::PortHandler * port_handler, |
| 958 | + double period_ms, |
| 959 | + bool is_sync) |
| 960 | +{ |
| 961 | + int dxl_comm_result; |
| 962 | + |
| 963 | + // Send packet |
| 964 | + if (is_sync) { |
| 965 | + dxl_comm_result = group_sync_read->txPacket(); |
| 966 | + if (dxl_comm_result != COMM_SUCCESS) { |
| 967 | + fprintf(stderr, "SyncRead Tx Fail [Dxl Size : %d] [Error code : %d]\n", |
| 968 | + read_data_list_.size(), dxl_comm_result); |
| 969 | + return DxlError::SYNC_READ_FAIL; |
| 970 | + } |
| 971 | + } else { |
| 972 | + dxl_comm_result = group_bulk_read->txPacket(); |
| 973 | + if (dxl_comm_result != COMM_SUCCESS) { |
| 974 | + fprintf(stderr, "BulkRead Tx Fail [Dxl Size : %d] [Error code : %d]\n", |
| 975 | + read_data_list_.size(), dxl_comm_result); |
| 976 | + return DxlError::BULK_READ_FAIL; |
| 977 | + } |
| 978 | + } |
| 979 | + |
| 980 | + // Set timeout if period_ms is specified |
| 981 | + if (period_ms > 0) { |
| 982 | + port_handler->setPacketTimeout(period_ms); |
| 983 | + } |
| 984 | + |
| 985 | + // Receive packet |
| 986 | + if (is_sync) { |
| 987 | + dxl_comm_result = group_sync_read->rxPacket(); |
| 988 | + if (dxl_comm_result != COMM_SUCCESS) { |
| 989 | + fprintf(stderr, "SyncRead Rx Fail [Dxl Size : %d] [Error code : %d]\n", |
| 990 | + read_data_list_.size(), dxl_comm_result); |
| 991 | + return DxlError::SYNC_READ_FAIL; |
| 992 | + } |
| 993 | + } else { |
| 994 | + dxl_comm_result = group_bulk_read->rxPacket(); |
| 995 | + if (dxl_comm_result != COMM_SUCCESS) { |
| 996 | + fprintf(stderr, "BulkRead Rx Fail [Dxl Size : %d] [Error code : %d]\n", |
| 997 | + read_data_list_.size(), dxl_comm_result); |
| 998 | + return DxlError::BULK_READ_FAIL; |
971 | 999 | }
|
972 | 1000 | }
|
| 1001 | + |
973 | 1002 | return DxlError::OK;
|
974 | 1003 | }
|
975 | 1004 |
|
| 1005 | +DxlError Dynamixel::ProcessReadData( |
| 1006 | + uint8_t id, |
| 1007 | + uint16_t indirect_addr, |
| 1008 | + const std::vector<std::string> & item_names, |
| 1009 | + const std::vector<uint8_t> & item_sizes, |
| 1010 | + const std::vector<std::shared_ptr<double>> & data_ptrs, |
| 1011 | + std::function<uint32_t(uint8_t, uint16_t, uint8_t)> get_data_func) |
| 1012 | +{ |
| 1013 | + uint16_t current_addr = indirect_addr; |
| 1014 | + |
| 1015 | + for (size_t item_index = 0; item_index < item_names.size(); item_index++) { |
| 1016 | + uint8_t size = item_sizes[item_index]; |
| 1017 | + if (item_index > 0) {current_addr += item_sizes[item_index - 1];} |
| 1018 | + |
| 1019 | + uint32_t dxl_getdata = get_data_func(id, current_addr, size); |
| 1020 | + |
| 1021 | + if (item_names[item_index] == "Present Position") { |
| 1022 | + *data_ptrs[item_index] = dxl_info_.ConvertValueToRadian(id, |
| 1023 | + static_cast<int32_t>(dxl_getdata)); |
| 1024 | + } else if (item_names[item_index] == "Present Velocity") { |
| 1025 | + *data_ptrs[item_index] = dxl_info_.ConvertValueRPMToVelocityRPS(id, |
| 1026 | + static_cast<int32_t>(dxl_getdata)); |
| 1027 | + } else if (item_names[item_index] == "Present Current") { |
| 1028 | + *data_ptrs[item_index] = dxl_info_.ConvertCurrentToEffort(id, |
| 1029 | + static_cast<int16_t>(dxl_getdata)); |
| 1030 | + } else { |
| 1031 | + *data_ptrs[item_index] = static_cast<double>(dxl_getdata); |
| 1032 | + } |
| 1033 | + } |
| 1034 | + return DxlError::OK; |
| 1035 | +} |
| 1036 | + |
| 1037 | + |
976 | 1038 | void Dynamixel::ResetIndirectRead(std::vector<uint8_t> id_arr)
|
977 | 1039 | {
|
978 | 1040 | IndirectInfo temp;
|
|
0 commit comments