|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -#include "hardware.hpp" |
16 |
| -#include <yaml-cpp/yaml.h> |
17 | 15 | #include <algorithm>
|
18 | 16 | #include <cmath>
|
19 |
| -#include <fstream> |
20 | 17 | #include <iostream>
|
21 | 18 |
|
| 19 | +#include "hardware.hpp" |
| 20 | +#include "config_file_parser.hpp" |
| 21 | + |
22 | 22 | namespace rt_manipulators_cpp {
|
23 | 23 |
|
24 | 24 | // Ref: https://emanual.robotis.com/docs/en/dxl/x/xm430-w350/
|
@@ -90,22 +90,48 @@ Hardware::~Hardware() {
|
90 | 90 |
|
91 | 91 | bool Hardware::load_config_file(const std::string& config_yaml) {
|
92 | 92 | // コンフィグファイルの読み込み
|
93 |
| - if (parse_config_file(config_yaml) == false) { |
| 93 | + if (config_file_parser::parse(config_yaml, joints_) == false) { |
94 | 94 | return false;
|
95 | 95 | }
|
96 | 96 |
|
97 |
| - // sync_readとsync_writeの関係をチェック |
98 | 97 | for (const auto& [group_name, group] : joints_.groups()) {
|
99 |
| - if (group->sync_write_velocity_enabled() && !group->sync_read_position_enabled()) { |
100 |
| - std::cerr << group_name << "グループはvelocityをsync_writeしますが, "; |
101 |
| - std::cerr << "positionをsync_readしません." << std::endl; |
102 |
| - std::cerr << "positionもsync_readするようにコンフィグファイルを修正して下さい." << std::endl; |
| 98 | + // ジョイントリミットの読み込みと設定 |
| 99 | + for (const auto& joint_name : group->joint_names()) { |
| 100 | + auto joint_id = joints_.joint(joint_name)->id(); |
| 101 | + uint32_t dxl_max_pos_limit = 0; |
| 102 | + uint32_t dxl_min_pos_limit = 0; |
| 103 | + uint16_t dxl_current_limit = 0; |
| 104 | + if (!comm_->read_double_word_data(joint_id, ADDR_MAX_POSITION_LIMIT, dxl_max_pos_limit)) { |
| 105 | + std::cerr << joint_name << "のMax Position Limitの読み取りに失敗しました." << std::endl; |
| 106 | + return false; |
| 107 | + } |
| 108 | + if (!comm_->read_double_word_data(joint_id, ADDR_MIN_POSITION_LIMIT, dxl_min_pos_limit)) { |
| 109 | + std::cerr << joint_name << "のMin Position Limitの読み取りに失敗しました." << std::endl; |
| 110 | + return false; |
| 111 | + } |
| 112 | + if (!comm_->read_word_data(joint_id, ADDR_CURRENT_LIMIT, dxl_current_limit)) { |
| 113 | + std::cerr << joint_name << "のCurrent Limitの読み取りに失敗しました." << std::endl; |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + joints_.joint(joint_name)->set_position_limit( |
| 118 | + dxl_pos_to_radian(static_cast<int32_t>(dxl_min_pos_limit)), |
| 119 | + dxl_pos_to_radian(static_cast<int32_t>(dxl_max_pos_limit))); |
| 120 | + joints_.joint(joint_name)->set_current_limit( |
| 121 | + dxl_current_to_ampere(dxl_current_limit)); |
| 122 | + } |
| 123 | + |
| 124 | + if (!write_operating_mode(group_name)) { |
| 125 | + std::cerr << group_name << "のOperating Modeを設定できません." << std::endl; |
103 | 126 | return false;
|
104 | 127 | }
|
105 |
| - if (group->sync_write_current_enabled() && !group->sync_read_position_enabled()) { |
106 |
| - std::cerr << group_name << "グループはcurrentをsync_writeしますが, "; |
107 |
| - std::cerr << "positionをsync_readしません." << std::endl; |
108 |
| - std::cerr << "positionもsync_readするようにコンフィグファイルを修正して下さい." << std::endl; |
| 128 | + |
| 129 | + if (!create_sync_read_group(group_name)) { |
| 130 | + std::cerr << group_name << "のsync readグループを作成できません." << std::endl; |
| 131 | + return false; |
| 132 | + } |
| 133 | + if (!create_sync_write_group(group_name)) { |
| 134 | + std::cerr << group_name << "のsync writeグループを作成できません." << std::endl; |
109 | 135 | return false;
|
110 | 136 | }
|
111 | 137 | }
|
@@ -734,121 +760,6 @@ bool Hardware::write_double_word_data_to_group(const std::string& group_name,
|
734 | 760 | return retval;
|
735 | 761 | }
|
736 | 762 |
|
737 |
| -bool Hardware::parse_config_file(const std::string& config_yaml) { |
738 |
| - // yamlファイルを読み取り、joints_に格納する |
739 |
| - std::ifstream fs(config_yaml); |
740 |
| - if (!fs.is_open()) { |
741 |
| - std::cerr << "コンフィグファイル:" << config_yaml << "が存在しません." << std::endl; |
742 |
| - return false; |
743 |
| - } |
744 |
| - |
745 |
| - YAML::Node config = YAML::LoadFile(config_yaml); |
746 |
| - for (const auto & config_joint_group : config["joint_groups"]) { |
747 |
| - auto group_name = config_joint_group.first.as<std::string>(); |
748 |
| - if (joints_.has_group(group_name)) { |
749 |
| - std::cerr << group_name << "グループが2つ以上存在します." << std::endl; |
750 |
| - return false; |
751 |
| - } |
752 |
| - |
753 |
| - if (!config["joint_groups"][group_name]["joints"]) { |
754 |
| - std::cerr << group_name << "グループに'joints'が設定されていせん。" << std::endl; |
755 |
| - return false; |
756 |
| - } |
757 |
| - |
758 |
| - std::vector<JointName> joint_names; |
759 |
| - for (const auto & config_joint : config["joint_groups"][group_name]["joints"]) { |
760 |
| - auto joint_name = config_joint.as<std::string>(); |
761 |
| - if (joints_.has_joint(joint_name)) { |
762 |
| - std::cerr << joint_name << "ジョイントが2つ以上存在します." << std::endl; |
763 |
| - return false; |
764 |
| - } |
765 |
| - |
766 |
| - if (!config[joint_name]) { |
767 |
| - std::cerr << joint_name << "ジョイントの設定が存在しません." << std::endl; |
768 |
| - return false; |
769 |
| - } |
770 |
| - |
771 |
| - if (!config[joint_name]["id"] || !config[joint_name]["operating_mode"]) { |
772 |
| - std::cerr << joint_name << "にidまたはoperating_modeが設定されていません." << std::endl; |
773 |
| - return false; |
774 |
| - } |
775 |
| - |
776 |
| - joint_names.push_back(joint_name); |
777 |
| - auto joint_id = config[joint_name]["id"].as<int>(); |
778 |
| - auto ope_mode = config[joint_name]["operating_mode"].as<int>(); |
779 |
| - |
780 |
| - uint32_t dxl_max_pos_limit = 0; |
781 |
| - uint32_t dxl_min_pos_limit = 0; |
782 |
| - uint16_t dxl_current_limit = 0; |
783 |
| - if (!comm_->read_double_word_data(joint_id, ADDR_MAX_POSITION_LIMIT, dxl_max_pos_limit)) { |
784 |
| - std::cerr << joint_name << "のMax Position Limitの読み取りに失敗しました." << std::endl; |
785 |
| - return false; |
786 |
| - } |
787 |
| - if (!comm_->read_double_word_data(joint_id, ADDR_MIN_POSITION_LIMIT, dxl_min_pos_limit)) { |
788 |
| - std::cerr << joint_name << "のMin Position Limitの読み取りに失敗しました." << std::endl; |
789 |
| - return false; |
790 |
| - } |
791 |
| - if (!comm_->read_word_data(joint_id, ADDR_CURRENT_LIMIT, dxl_current_limit)) { |
792 |
| - std::cerr << joint_name << "のCurrent Limitの読み取りに失敗しました." << std::endl; |
793 |
| - return false; |
794 |
| - } |
795 |
| - |
796 |
| - // 角度リミット値をラジアンに変換 |
797 |
| - double max_position_limit = dxl_pos_to_radian(static_cast<int32_t>(dxl_max_pos_limit)); |
798 |
| - double min_position_limit = dxl_pos_to_radian(static_cast<int32_t>(dxl_min_pos_limit)); |
799 |
| - |
800 |
| - // 角度リミット値にマージンを加算する |
801 |
| - if (config[joint_name]["pos_limit_margin"]) { |
802 |
| - max_position_limit -= config[joint_name]["pos_limit_margin"].as<double>(); |
803 |
| - min_position_limit += config[joint_name]["pos_limit_margin"].as<double>(); |
804 |
| - } |
805 |
| - |
806 |
| - // 電流リミット値をアンペアに変換 |
807 |
| - double current_limit = dxl_current_to_ampere(dxl_current_limit); |
808 |
| - // 電流リミット値にマージンを加算する |
809 |
| - if (config[joint_name]["current_limit_margin"]) { |
810 |
| - // current_limitは0以上の値にする |
811 |
| - current_limit = std::max( |
812 |
| - current_limit - config[joint_name]["current_limit_margin"].as<double>(), |
813 |
| - 0.0); |
814 |
| - } |
815 |
| - |
816 |
| - auto joint = joint::Joint( |
817 |
| - joint_id, ope_mode, max_position_limit, min_position_limit, current_limit); |
818 |
| - joints_.append_joint(joint_name, joint); |
819 |
| - } |
820 |
| - std::vector<std::string> sync_read_targets; |
821 |
| - std::vector<std::string> sync_write_targets; |
822 |
| - if (config["joint_groups"][group_name]["sync_read"]) { |
823 |
| - sync_read_targets = |
824 |
| - config["joint_groups"][group_name]["sync_read"].as<std::vector<std::string>>(); |
825 |
| - } |
826 |
| - if (config["joint_groups"][group_name]["sync_write"]) { |
827 |
| - sync_write_targets = |
828 |
| - config["joint_groups"][group_name]["sync_write"].as<std::vector<std::string>>(); |
829 |
| - } |
830 |
| - |
831 |
| - auto joint_group = joint::JointGroup(joint_names, sync_read_targets, sync_write_targets); |
832 |
| - joints_.append_group(group_name, joint_group); |
833 |
| - |
834 |
| - if (!write_operating_mode(group_name)) { |
835 |
| - std::cerr << group_name << "のOperating Modeを設定できません." << std::endl; |
836 |
| - return false; |
837 |
| - } |
838 |
| - |
839 |
| - if (!create_sync_read_group(group_name)) { |
840 |
| - std::cerr << group_name << "のsync readグループを作成できません." << std::endl; |
841 |
| - return false; |
842 |
| - } |
843 |
| - if (!create_sync_write_group(group_name)) { |
844 |
| - std::cerr << group_name << "のsync writeグループを作成できません." << std::endl; |
845 |
| - return false; |
846 |
| - } |
847 |
| - } |
848 |
| - |
849 |
| - return true; |
850 |
| -} |
851 |
| - |
852 | 763 | bool Hardware::write_operating_mode(const std::string& group_name) {
|
853 | 764 | // サーボモータから動作モードを読み取り、
|
854 | 765 | // コンフィグファイルと一致しない場合、動作モードを書き込む
|
|
0 commit comments