Skip to content

Commit cce15c5

Browse files
author
Shota Aoki
authored
リファクタリング:hardware_communicatorクラスの追加 (#6)
* hardware_communicatorクラスにhardwareクラスの通信部分を移植 * sync_write_groupのコードをcommunicatorに移植 * sync_readのデータ取得部分をhardware_communicatorに移植 * static_castで型変換を明示する * sync_read_groupにIDを追加する関数をcommunicatorに移植 * sync_write_groupにIDを追加する関数をcommunicatorに移植 * 外部から使用されない関数をprivateに変更 * hardware_communicatorを派生クラスから使用できるように、protectedに変更 * make_sync_read/write_groupに関数名を変更
1 parent a964916 commit cce15c5

File tree

5 files changed

+403
-194
lines changed

5 files changed

+403
-194
lines changed

rt_manipulators_lib/include/hardware.hpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#ifndef RT_MANIPULATORS_LIB_INCLUDE_HARDWARE_HPP_
1616
#define RT_MANIPULATORS_LIB_INCLUDE_HARDWARE_HPP_
1717

18-
#include <dynamixel_sdk/dynamixel_sdk.h>
1918
#include <chrono>
2019
#include <map>
2120
#include <memory>
@@ -25,6 +24,7 @@
2524

2625
#include "joint.hpp"
2726
#include "hardware_joints.hpp"
27+
#include "hardware_communicator.hpp"
2828

2929
namespace rt_manipulators_cpp {
3030

@@ -81,17 +81,14 @@ class Hardware {
8181
const uint16_t i);
8282

8383
protected:
84-
bool write_byte_data(const uint8_t id, const uint16_t address, const uint8_t write_data);
8584
bool write_byte_data_to_group(const std::string& group_name, const uint16_t address,
8685
const uint8_t write_data);
87-
bool write_word_data(const uint8_t id, const uint16_t address, const uint16_t write_data);
8886
bool write_word_data_to_group(const std::string& group_name, const uint16_t address,
8987
const uint16_t write_data);
90-
bool write_double_word_data(const uint8_t id, const uint16_t address, const uint32_t write_data);
9188
bool write_double_word_data_to_group(const std::string& group_name, const uint16_t address,
9289
const uint32_t write_data);
93-
bool read_byte_data(const uint8_t id, const uint16_t address, uint8_t& read_data);
94-
bool read_double_word_data(const uint8_t id, const uint16_t address, uint32_t& read_data);
90+
91+
std::shared_ptr<hardware_communicator::Communicator> comm_;
9592

9693
private:
9794
bool parse_config_file(const std::string& config_yaml);
@@ -103,9 +100,6 @@ class Hardware {
103100
const uint16_t addr_target, const uint16_t len_target);
104101
void read_write_thread(const std::vector<std::string>& group_names,
105102
const std::chrono::milliseconds& update_cycle_ms);
106-
bool parse_dxl_error(const std::string& func_name, const uint8_t id, const uint16_t address,
107-
const int dxl_comm_result, const uint8_t dxl_packet_error);
108-
bool parse_dxl_error(const std::string& func_name, const int dxl_comm_result);
109103
double dxl_pos_to_radian(const int32_t position);
110104
double dxl_velocity_to_rps(const int32_t velocity) const;
111105
double dxl_current_to_ampere(const int16_t current) const;
@@ -115,17 +109,12 @@ class Hardware {
115109
uint32_t to_dxl_acceleration(const double acceleration_rpss);
116110
uint32_t to_dxl_profile_velocity(const double velocity_rps);
117111

118-
std::shared_ptr<dynamixel::PortHandler> port_handler_;
119-
std::shared_ptr<dynamixel::PacketHandler> packet_handler_;
120112
hardware_joints::Joints joints_;
121-
std::map<JointGroupName, std::shared_ptr<dynamixel::GroupSyncRead>> sync_read_groups_;
122-
std::map<JointGroupName, std::shared_ptr<dynamixel::GroupSyncWrite>> sync_write_groups_;
123113
std::map<JointGroupName, uint16_t> addr_sync_read_position_;
124114
std::map<JointGroupName, uint16_t> addr_sync_read_velocity_;
125115
std::map<JointGroupName, uint16_t> addr_sync_read_current_;
126116
std::map<JointGroupName, uint16_t> addr_sync_read_voltage_;
127117
std::map<JointGroupName, uint16_t> addr_sync_read_temperature_;
128-
bool is_connected_;
129118
bool thread_enable_;
130119
std::shared_ptr<std::thread> read_write_thread_;
131120
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+

rt_manipulators_lib/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_library(${library_name}
88
hardware.cpp
99
joint.cpp
1010
hardware_joints.cpp
11+
hardware_communicator.cpp
1112
)
1213
set_target_properties(${library_name} PROPERTIES VERSION 1.0.0 SOVERSION 1)
1314

0 commit comments

Comments
 (0)