|
21 | 21 | #include <algorithm> |
22 | 22 | #include <fstream> |
23 | 23 | #include <memory> |
24 | | -#include <nlohmann/json.hpp> |
25 | 24 | #include <string> |
26 | 25 | #include <vector> |
27 | 26 |
|
28 | 27 | #include "tachimawari/joint/tf2/tf2_manager.hpp" |
29 | 28 |
|
| 29 | +#include "jitsuyo/config.hpp" |
| 30 | +#include "nlohmann/json.hpp" |
| 31 | + |
30 | 32 | namespace tachimawari::joint |
31 | 33 | { |
32 | 34 |
|
33 | 35 | Tf2Manager::Tf2Manager() {} |
34 | 36 |
|
35 | 37 | bool Tf2Manager::load_configuration(const std::string & path) |
36 | 38 | { |
37 | | - std::string ss = path + "frame_measurements.json"; |
38 | | - |
39 | | - std::ifstream input(ss, std::ifstream::in); |
40 | | - if (!input.is_open()) { |
41 | | - throw std::runtime_error("Unable to open `" + ss + "`!"); |
| 39 | + nlohmann::json config; |
| 40 | + if (!jitsuyo::load_config(path, "frame_measurements.json", config)) { |
| 41 | + return false; |
42 | 42 | } |
43 | 43 |
|
44 | | - nlohmann::json config = nlohmann::json::parse(input); |
| 44 | + bool valid_config = true; |
45 | 45 |
|
46 | 46 | for (auto & item : config.items()) { |
47 | 47 | // Get all config |
48 | | - try { |
49 | | - std::string name = item.key(); |
50 | | - double translation_x = item.value().at("translation").at("x"); |
51 | | - double translation_y = item.value().at("translation").at("y"); |
52 | | - double translation_z = item.value().at("translation").at("z"); |
53 | | - double const_roll = item.value().at("const_rpy").at("roll"); |
54 | | - double const_pitch = item.value().at("const_rpy").at("pitch"); |
55 | | - double const_yaw = item.value().at("const_rpy").at("yaw"); |
56 | | - auto map_string_id = FrameId::frame_string_id.find(name); |
57 | | - |
58 | | - uint8_t id = map_string_id->second; |
59 | | - frames.push_back( |
60 | | - Frame(id, translation_x, translation_y, translation_z, const_roll, const_pitch, const_yaw)); |
61 | | - } catch (nlohmann::json::parse_error & ex) { |
62 | | - std::cerr << "parse error at byte " << ex.byte << std::endl; |
63 | | - throw ex; |
| 48 | + double translation_x; |
| 49 | + double translation_y; |
| 50 | + double translation_z; |
| 51 | + double const_roll; |
| 52 | + double const_pitch; |
| 53 | + double const_yaw; |
| 54 | + |
| 55 | + std::string name = item.key(); |
| 56 | + |
| 57 | + bool valid_section = true; |
| 58 | + nlohmann::json section; |
| 59 | + if (jitsuyo::assign_val(config, name, section)) { |
| 60 | + nlohmann::json translation; |
| 61 | + nlohmann::json const_rpy; |
| 62 | + |
| 63 | + if (jitsuyo::assign_val(section, "translation", translation)) { |
| 64 | + bool valid_translation = true; |
| 65 | + valid_translation &= jitsuyo::assign_val(translation, "x", translation_x); |
| 66 | + valid_translation &= jitsuyo::assign_val(translation, "y", translation_y); |
| 67 | + valid_translation &= jitsuyo::assign_val(translation, "z", translation_z); |
| 68 | + if (!valid_translation) { |
| 69 | + std::cout << "Error found at section `translation`" << std::endl; |
| 70 | + valid_section = false; |
| 71 | + } |
| 72 | + } else { |
| 73 | + valid_section = false; |
| 74 | + } |
| 75 | + |
| 76 | + if (jitsuyo::assign_val(section, "const_rpy", const_rpy)) { |
| 77 | + bool valid_const_rpy = true; |
| 78 | + valid_const_rpy &= jitsuyo::assign_val(const_rpy, "roll", const_roll); |
| 79 | + valid_const_rpy &= jitsuyo::assign_val(const_rpy, "pitch", const_pitch); |
| 80 | + valid_const_rpy &= jitsuyo::assign_val(const_rpy, "yaw", const_yaw); |
| 81 | + if (!valid_const_rpy) { |
| 82 | + std::cout << "Error found at section `const_rpy`" << std::endl; |
| 83 | + valid_section = false; |
| 84 | + } |
| 85 | + } else { |
| 86 | + valid_section = false; |
| 87 | + } |
| 88 | + } else { |
| 89 | + valid_section = false; |
64 | 90 | } |
| 91 | + |
| 92 | + if (!valid_section) { |
| 93 | + valid_config = false; |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + auto map_string_id = FrameId::frame_string_id.find(name); |
| 98 | + |
| 99 | + uint8_t id = map_string_id->second; |
| 100 | + frames.push_back( |
| 101 | + Frame(id, translation_x, translation_y, translation_z, const_roll, const_pitch, const_yaw)); |
| 102 | + } |
| 103 | + |
| 104 | + if (!valid_config) { |
| 105 | + throw std::runtime_error("Failed to load config file `" + path + "frame_measurements.json`"); |
65 | 106 | } |
66 | 107 |
|
67 | 108 | return true; |
|
0 commit comments