Skip to content

Commit 65bc609

Browse files
author
Shota Aoki
authored
Dynamixelクラスの追加 (#17)
* Lintでdynamixelディレクトリをチェックする * dynamixelクラスを追加し、hardwareクラスに組み込む * ライブラリのREADMEにファイルの説明を追加 * sample01のREADMEのコンフィグファイルにdynamixel情報を追加 * 各コンフィグファイルにDynamixel情報を追加 * ライブラリのバージョンを1.1.0に更新 * includeを修正 * 親ディレクトリのincludeが発生していたため、dynamixelフォルダを削除した * README修正 * ADDR_TORQUE_ENABLEをhardwareから削除 * 使用していない定数を削除
1 parent 06e5e17 commit 65bc609

32 files changed

+484
-165
lines changed

rt_manipulators_lib/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ $ ./uninstall_library.bash
7070
- `hardware_communicator.hpp/cpp` : `Hardware`クラスのうち、Dynamixelとの通信機能を実装しています
7171
- `hardware_joints.hpp/cpp` : `Hardware`クラスのうち、ジョイント情報を扱う機能を実装しています
7272
- `joints.hpp/cpp` : ジョイント情報を定義しています
73+
- `config_file_parser.hpp/cpp` : コンフィグファイルの読み取りを担います
74+
- `dynamixel_*` : 各Dynamixelと通信するためのデータ変換を担います
7375

7476
### 運動学関連
7577

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2022 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_DYNAMIXEL_BASE_HPP_
16+
#define RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_BASE_HPP_
17+
18+
#include <memory>
19+
#include <string>
20+
21+
#include "hardware_communicator.hpp"
22+
23+
namespace dynamixel_base {
24+
25+
using comm_t = std::shared_ptr<hardware_communicator::Communicator>;
26+
27+
class DynamixelBase {
28+
public:
29+
explicit DynamixelBase(const uint8_t id) : id_(id), name_("base") {}
30+
~DynamixelBase() {}
31+
32+
uint8_t get_id() const { return id_; }
33+
std::string get_name() const { return name_; }
34+
virtual bool write_torque_enable(
35+
const dynamixel_base::comm_t & comm, const bool enable) { return false; }
36+
37+
protected:
38+
uint8_t id_;
39+
std::string name_;
40+
};
41+
42+
} // namespace dynamixel_base
43+
44+
#endif // RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_BASE_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2022 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_DYNAMIXEL_XM_HPP_
16+
#define RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_XM_HPP_
17+
18+
#include "dynamixel_base.hpp"
19+
20+
namespace dynamixel_xm {
21+
22+
class DynamixelXM : public dynamixel_base::DynamixelBase {
23+
public:
24+
explicit DynamixelXM(const uint8_t id);
25+
bool write_torque_enable(const dynamixel_base::comm_t & comm, const bool enable);
26+
};
27+
28+
} // namespace dynamixel_xm
29+
30+
#endif // RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_XM_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 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_DYNAMIXEL_XM430_HPP_
16+
#define RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_XM430_HPP_
17+
18+
#include "dynamixel_xm.hpp"
19+
20+
namespace dynamixel_xm430 {
21+
22+
class DynamixelXM430 : public dynamixel_xm::DynamixelXM {
23+
public:
24+
explicit DynamixelXM430(const uint8_t id);
25+
};
26+
27+
} // namespace dynamixel_xm430
28+
29+
#endif // RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_XM430_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 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_DYNAMIXEL_XM540_HPP_
16+
#define RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_XM540_HPP_
17+
18+
#include "dynamixel_xm.hpp"
19+
20+
namespace dynamixel_xm540 {
21+
22+
class DynamixelXM540 : public dynamixel_xm::DynamixelXM {
23+
public:
24+
explicit DynamixelXM540(const uint8_t id);
25+
};
26+
27+
} // namespace dynamixel_xm540
28+
29+
#endif // RT_MANIPULATORS_LIB_INCLUDE_DYNAMIXEL_XM540_HPP_

rt_manipulators_lib/include/hardware.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ class Hardware {
8686
const uint16_t i);
8787

8888
protected:
89-
bool write_byte_data_to_group(const std::string& group_name, const uint16_t address,
90-
const uint8_t write_data);
9189
bool write_word_data_to_group(const std::string& group_name, const uint16_t address,
9290
const uint16_t write_data);
9391
bool write_double_word_data_to_group(const std::string& group_name, const uint16_t address,

rt_manipulators_lib/include/joint.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
#define RT_MANIPULATORS_LIB_INCLUDE_JOINT_HPP_
1717

1818
#include <cstdint>
19+
#include <memory>
1920
#include <string>
2021
#include <vector>
2122

23+
#include "dynamixel_base.hpp"
24+
2225
namespace joint {
2326

2427
class Joint {
2528
public:
2629
Joint(const uint8_t id, const uint8_t operating_mode);
30+
Joint(const uint8_t id, const uint8_t operating_mode, const std::string dynamixel_name);
2731
uint8_t id() const;
2832
uint8_t operating_mode() const;
2933
void set_position_limit_margin(const double position_radian);
@@ -50,6 +54,8 @@ class Joint {
5054
double get_goal_velocity() const;
5155
double get_goal_current() const;
5256

57+
std::shared_ptr<dynamixel_base::DynamixelBase> dxl;
58+
5359
private:
5460
uint8_t id_;
5561
uint8_t operating_mode_;

rt_manipulators_lib/src/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ add_library(${library_name}
1212
kinematics.cpp
1313
kinematics_utils.cpp
1414
config_file_parser.cpp
15+
dynamixel_xm.cpp
16+
dynamixel_xm430.cpp
17+
dynamixel_xm540.cpp
1518
)
16-
set_target_properties(${library_name} PROPERTIES VERSION 1.0.0 SOVERSION 1)
19+
set_target_properties(${library_name} PROPERTIES VERSION 1.1.0 SOVERSION 1)
1720

1821
target_include_directories(${library_name} PUBLIC
1922
${PROJECT_SOURCE_DIR}/include

rt_manipulators_lib/src/config_file_parser.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,21 @@ bool parse(const std::string& config_yaml, hardware_joints::Joints & parsed_join
6464
joint_names.push_back(joint_name);
6565
auto joint_id = config[joint_name]["id"].as<int>();
6666
auto ope_mode = config[joint_name]["operating_mode"].as<int>();
67+
std::string dynamixel_name = "";
6768
double position_limit_margin = 0;
6869
double current_limit_margin = 0;
70+
71+
if (config[joint_name]["dynamixel"]) {
72+
dynamixel_name = config[joint_name]["dynamixel"].as<std::string>();
73+
}
6974
if (config[joint_name]["pos_limit_margin"]) {
7075
position_limit_margin = config[joint_name]["pos_limit_margin"].as<double>();
7176
}
7277
if (config[joint_name]["current_limit_margin"]) {
7378
current_limit_margin = config[joint_name]["current_limit_margin"].as<double>();
7479
}
7580

76-
auto joint = joint::Joint(joint_id, ope_mode);
81+
auto joint = joint::Joint(joint_id, ope_mode, dynamixel_name);
7782
joint.set_position_limit_margin(position_limit_margin);
7883
joint.set_current_limit_margin(current_limit_margin);
7984
parsed_joints.append_joint(joint_name, joint);
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 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+
16+
#include "dynamixel_xm.hpp"
17+
18+
19+
namespace dynamixel_xm {
20+
21+
const uint16_t ADDR_TORQUE_ENABLE = 64;
22+
23+
DynamixelXM::DynamixelXM(const uint8_t id)
24+
: dynamixel_base::DynamixelBase(id) {
25+
name_ = "XM";
26+
}
27+
28+
bool DynamixelXM::write_torque_enable(const dynamixel_base::comm_t & comm, const bool enable) {
29+
return comm->write_byte_data(id_, ADDR_TORQUE_ENABLE, enable);
30+
}
31+
32+
} // namespace dynamixel_xm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2022 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+
16+
#include "dynamixel_xm430.hpp"
17+
18+
19+
namespace dynamixel_xm430 {
20+
21+
DynamixelXM430::DynamixelXM430(const uint8_t id)
22+
: dynamixel_xm::DynamixelXM(id) {
23+
name_ = "XM430";
24+
}
25+
26+
} // namespace dynamixel_xm430
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2022 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+
16+
#include "dynamixel_xm540.hpp"
17+
18+
19+
namespace dynamixel_xm540 {
20+
21+
DynamixelXM540::DynamixelXM540(const uint8_t id)
22+
: dynamixel_xm::DynamixelXM(id) {
23+
name_ = "XM540";
24+
}
25+
26+
} // namespace dynamixel_xm540

rt_manipulators_lib/src/hardware.cpp

+13-23
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace rt_manipulators_cpp {
2424
// Ref: https://emanual.robotis.com/docs/en/dxl/x/xm430-w350/
2525
// Ref: https://emanual.robotis.com/docs/en/dxl/x/xm540-w270/
2626
// Ref: https://emanual.robotis.com/docs/en/dxl/x/xm540-w150/
27-
const double PROTOCOL_VERSION = 2.0;
2827
const int DXL_HOME_POSITION = 2048;
2928
const double TO_RADIANS = (180.0 / 2048.0) * M_PI / 180.0;
3029
const double TO_DXL_POS = 1.0 / TO_RADIANS;
@@ -47,7 +46,6 @@ const uint16_t ADDR_OPERATING_MODE = 11;
4746
const uint16_t ADDR_CURRENT_LIMIT = 38;
4847
const uint16_t ADDR_MAX_POSITION_LIMIT = 48;
4948
const uint16_t ADDR_MIN_POSITION_LIMIT = 52;
50-
const uint16_t ADDR_TORQUE_ENABLE = 64;
5149
const uint16_t ADDR_VELOCITY_I_GAIN = 76;
5250
const uint16_t ADDR_VELOCITY_P_GAIN = 78;
5351
const uint16_t ADDR_POSITION_D_GAIN = 80;
@@ -194,9 +192,11 @@ void Hardware::disconnect() {
194192
}
195193

196194
bool Hardware::torque_on(const std::string& group_name) {
197-
if (!write_byte_data_to_group(group_name, ADDR_TORQUE_ENABLE, 1)) {
198-
std::cerr << group_name << "グループのトルクONに失敗しました." << std::endl;
199-
return false;
195+
for (const auto & joint_name : joints_.group(group_name)->joint_names()) {
196+
if (!joints_.joint(joint_name)->dxl->write_torque_enable(comm_, true)) {
197+
std::cerr << joint_name << "ジョイントのトルクONに失敗しました." << std::endl;
198+
return false;
199+
}
200200
}
201201

202202
// 安全のため、サーボの現在角度を目標角度に設定する
@@ -212,7 +212,14 @@ bool Hardware::torque_on(const std::string& group_name) {
212212
}
213213

214214
bool Hardware::torque_off(const std::string& group_name) {
215-
return write_byte_data_to_group(group_name, ADDR_TORQUE_ENABLE, 0);
215+
bool retval = true;
216+
for (const auto & joint_name : joints_.group(group_name)->joint_names()) {
217+
if (!joints_.joint(joint_name)->dxl->write_torque_enable(comm_, false)) {
218+
std::cerr << joint_name << "ジョイントのトルクOFFに失敗しました." << std::endl;
219+
retval = false;
220+
}
221+
}
222+
return retval;
216223
}
217224

218225
bool Hardware::sync_read(const std::string& group_name) {
@@ -709,23 +716,6 @@ bool Hardware::write_velocity_pi_gain_to_group(const std::string& group_name, co
709716
return true;
710717
}
711718

712-
bool Hardware::write_byte_data_to_group(const std::string& group_name, const uint16_t address,
713-
const uint8_t write_data) {
714-
if (!joints_.has_group(group_name)) {
715-
std::cerr << group_name << "はjoint_groupsに存在しません." << std::endl;
716-
return false;
717-
}
718-
719-
bool retval = true;
720-
for (const auto & joint_name : joints_.group(group_name)->joint_names()) {
721-
auto id = joints_.joint(joint_name)->id();
722-
if (!comm_->write_byte_data(id, address, write_data)) {
723-
retval = false;
724-
}
725-
}
726-
return retval;
727-
}
728-
729719
bool Hardware::write_word_data_to_group(const std::string& group_name, const uint16_t address,
730720
const uint16_t write_data) {
731721
if (!joints_.has_group(group_name)) {

0 commit comments

Comments
 (0)