|
| 1 | +// Copyright 2021 RT Corporation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef RT_MANIPULATORS_LIB_INCLUDE_HARDWARE_COMMUNICATOR_HPP_ |
| 16 | +#define RT_MANIPULATORS_LIB_INCLUDE_HARDWARE_COMMUNICATOR_HPP_ |
| 17 | + |
| 18 | +#include <dynamixel_sdk/dynamixel_sdk.h> |
| 19 | +#include <string> |
| 20 | +#include <map> |
| 21 | +#include <memory> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace hardware_communicator { |
| 25 | + |
| 26 | +using dxl_id_t = uint8_t; |
| 27 | +using dxl_address_t = uint16_t; |
| 28 | +using dxl_data_length_t = uint16_t; |
| 29 | +using dxl_error_t = uint8_t; |
| 30 | +using dxl_result_t = int; |
| 31 | +using dxl_byte_t = uint8_t; |
| 32 | +using dxl_word_t = uint16_t; |
| 33 | +using dxl_double_word_t = uint32_t; |
| 34 | +using group_name_t = std::string; |
| 35 | +using GroupSyncRead = dynamixel::GroupSyncRead; |
| 36 | +using GroupSyncWrite = dynamixel::GroupSyncWrite; |
| 37 | + |
| 38 | +// ハードウェアとの通信を担うクラス |
| 39 | +class Communicator{ |
| 40 | + public: |
| 41 | + explicit Communicator(const std::string device_name); |
| 42 | + ~Communicator(); |
| 43 | + bool is_connected(); |
| 44 | + bool connect(const int baudrate = 3000000); |
| 45 | + void disconnect(); |
| 46 | + void make_sync_read_group(const group_name_t & group_name, const dxl_address_t & start_address, |
| 47 | + const dxl_data_length_t & data_length); |
| 48 | + void make_sync_write_group(const group_name_t & group_name, const dxl_address_t & start_address, |
| 49 | + const dxl_data_length_t & data_length); |
| 50 | + bool append_id_to_sync_read_group(const group_name_t & group_name, const dxl_id_t & id); |
| 51 | + bool append_id_to_sync_write_group(const group_name_t & group_name, const dxl_id_t & id, |
| 52 | + std::vector<dxl_byte_t> & init_data); |
| 53 | + bool send_sync_read_packet(const group_name_t & group_name); |
| 54 | + bool send_sync_write_packet(const group_name_t & group_name); |
| 55 | + bool get_sync_read_data(const group_name_t & group_name, const dxl_id_t id, |
| 56 | + const dxl_address_t & address, const dxl_data_length_t & length, |
| 57 | + dxl_double_word_t & read_data); |
| 58 | + bool set_sync_write_data(const group_name_t & group_name, const dxl_id_t id, |
| 59 | + std::vector<dxl_byte_t> & write_data); |
| 60 | + bool write_byte_data(const dxl_id_t & id, const dxl_address_t & address, |
| 61 | + const dxl_byte_t & write_data); |
| 62 | + bool write_word_data(const dxl_id_t & id, const dxl_address_t & address, |
| 63 | + const dxl_word_t & write_data); |
| 64 | + bool write_double_word_data(const dxl_id_t & id, const dxl_address_t & address, |
| 65 | + const dxl_double_word_t & write_data); |
| 66 | + bool read_byte_data(const dxl_id_t & id, const dxl_address_t & address, dxl_byte_t & read_data); |
| 67 | + bool read_double_word_data(const dxl_id_t & id, const dxl_address_t & address, |
| 68 | + dxl_double_word_t & read_data); |
| 69 | + |
| 70 | + private: |
| 71 | + std::shared_ptr<GroupSyncRead> sync_read_group(const group_name_t & name); |
| 72 | + std::shared_ptr<GroupSyncWrite> sync_write_group(const group_name_t & name); |
| 73 | + bool has_sync_read_group(const group_name_t & name); |
| 74 | + bool has_sync_write_group(const group_name_t & name); |
| 75 | + bool parse_dxl_error(const std::string & func_name, const dxl_id_t & id, |
| 76 | + const dxl_address_t & address, const dxl_result_t & dxl_comm_result, |
| 77 | + const dxl_error_t & dxl_packet_error); |
| 78 | + bool parse_dxl_error(const std::string & func_name, const dxl_result_t & dxl_comm_result); |
| 79 | + |
| 80 | + bool is_connected_; |
| 81 | + std::shared_ptr<dynamixel::PortHandler> port_handler_; |
| 82 | + std::shared_ptr<dynamixel::PacketHandler> packet_handler_; |
| 83 | + std::map<group_name_t, std::shared_ptr<GroupSyncRead>> sync_read_groups_; |
| 84 | + std::map<group_name_t, std::shared_ptr<GroupSyncWrite>> sync_write_groups_; |
| 85 | +}; |
| 86 | + |
| 87 | +} // namespace hardware_communicator |
| 88 | + |
| 89 | + |
| 90 | +#endif // RT_MANIPULATORS_LIB_INCLUDE_HARDWARE_COMMUNICATOR_HPP_ |
| 91 | + |
0 commit comments