@@ -60,13 +60,13 @@ void DynamixelInfo::ReadDxlModelFile(uint8_t id, uint16_t model_num)
60
60
path += it->second ;
61
61
} else {
62
62
fprintf (stderr, " [ERROR] CANNOT FIND THE DXL MODEL FROM FILE LIST.\n " );
63
- return ;
63
+ throw std::runtime_error ( " Cannot find the DXL model from file list " ) ;
64
64
}
65
65
66
66
std::ifstream open_file (path);
67
67
if (open_file.is_open () != 1 ) {
68
68
fprintf (stderr, " [ERROR] CANNOT FIND DXL [%s] MODEL FILE.\n " , path.c_str ());
69
- exit (- 1 );
69
+ throw std::runtime_error ( " Cannot find DXL model file " );
70
70
}
71
71
72
72
DxlInfo temp_dxl_info;
@@ -83,18 +83,28 @@ void DynamixelInfo::ReadDxlModelFile(uint8_t id, uint16_t model_num)
83
83
std::vector<std::string> strs;
84
84
boost::split (strs, line, boost::is_any_of (" \t " ));
85
85
86
- if (strs.at (0 ) == " value_of_zero_radian_position" ) {
87
- temp_dxl_info.value_of_zero_radian_position = static_cast <int32_t >(stoi (strs.at (1 )));
88
- } else if (strs.at (0 ) == " value_of_max_radian_position" ) {
89
- temp_dxl_info.value_of_max_radian_position = static_cast <int32_t >(stoi (strs.at (1 )));
90
- } else if (strs.at (0 ) == " value_of_min_radian_position" ) {
91
- temp_dxl_info.value_of_min_radian_position = static_cast <int32_t >(stoi (strs.at (1 )));
92
- } else if (strs.at (0 ) == " min_radian" ) {
93
- temp_dxl_info.min_radian = static_cast <double >(stod (strs.at (1 )));
94
- } else if (strs.at (0 ) == " max_radian" ) {
95
- temp_dxl_info.max_radian = static_cast <double >(stod (strs.at (1 )));
96
- } else if (strs.at (0 ) == " torque_constant" ) {
97
- temp_dxl_info.torque_constant = static_cast <double >(stod (strs.at (1 )));
86
+ if (strs.size () < 2 ) {
87
+ continue ;
88
+ }
89
+
90
+ try {
91
+ if (strs.at (0 ) == " value_of_zero_radian_position" ) {
92
+ temp_dxl_info.value_of_zero_radian_position = static_cast <int32_t >(stoi (strs.at (1 )));
93
+ } else if (strs.at (0 ) == " value_of_max_radian_position" ) {
94
+ temp_dxl_info.value_of_max_radian_position = static_cast <int32_t >(stoi (strs.at (1 )));
95
+ } else if (strs.at (0 ) == " value_of_min_radian_position" ) {
96
+ temp_dxl_info.value_of_min_radian_position = static_cast <int32_t >(stoi (strs.at (1 )));
97
+ } else if (strs.at (0 ) == " min_radian" ) {
98
+ temp_dxl_info.min_radian = static_cast <double >(stod (strs.at (1 )));
99
+ } else if (strs.at (0 ) == " max_radian" ) {
100
+ temp_dxl_info.max_radian = static_cast <double >(stod (strs.at (1 )));
101
+ } else if (strs.at (0 ) == " torque_constant" ) {
102
+ temp_dxl_info.torque_constant = static_cast <double >(stod (strs.at (1 )));
103
+ }
104
+ } catch (const std::exception& e) {
105
+ std::string error_msg = " Error processing line in model file: " + line + " \n Error: " + e.what ();
106
+ // fprintf(stderr, "[WARN] %s\n", error_msg.c_str());
107
+ throw std::runtime_error (error_msg);
98
108
}
99
109
}
100
110
@@ -108,11 +118,29 @@ void DynamixelInfo::ReadDxlModelFile(uint8_t id, uint16_t model_num)
108
118
std::vector<std::string> strs;
109
119
boost::split (strs, line, boost::is_any_of (" \t " ));
110
120
111
- ControlItem temp;
112
- temp.address = static_cast <uint16_t >(stoi (strs.at (0 )));
113
- temp.size = static_cast <uint8_t >(stoi (strs.at (1 )));
114
- temp.item_name = strs.at (2 );
115
- temp_dxl_info.item .push_back (temp);
121
+ if (strs.size () < 3 ) {
122
+ std::string error_msg = " Malformed control table line: " + line;
123
+ // fprintf(stderr, "[ERROR] %s\n", error_msg.c_str());
124
+ throw std::runtime_error (error_msg);
125
+ }
126
+
127
+ try {
128
+ ControlItem temp;
129
+ temp.address = static_cast <uint16_t >(stoi (strs.at (0 )));
130
+ temp.size = static_cast <uint8_t >(stoi (strs.at (1 )));
131
+ temp.item_name = strs.at (2 );
132
+ temp_dxl_info.item .push_back (temp);
133
+ } catch (const std::exception& e) {
134
+ std::string error_msg = " Error processing control table line: " + line + " \n Error: " + e.what ();
135
+ // fprintf(stderr, "[ERROR] %s\n", error_msg.c_str());
136
+ throw std::runtime_error (error_msg);
137
+ }
138
+ }
139
+
140
+ if (temp_dxl_info.item .empty ()) {
141
+ std::string error_msg = " No control table items found in model file for ID " + std::to_string (id);
142
+ // fprintf(stderr, "[ERROR] %s\n", error_msg.c_str());
143
+ throw std::runtime_error (error_msg);
116
144
}
117
145
118
146
dxl_info_[id] = temp_dxl_info;
0 commit comments