41
41
namespace khi_robot_control
42
42
{
43
43
#define KHI_MAX_CONTROLLER 8
44
+ #define KHI_MAX_ARM 2
44
45
#define KHI_MAX_JOINT 18
45
46
#define KHI_MAX_SIG_SIZE 512
46
47
47
- struct JointData
48
+ struct KhiRobotArmData
48
49
{
49
- int joint_num ;
50
+ int jt_num ;
50
51
std::string name[KHI_MAX_JOINT];
52
+ int type[KHI_MAX_JOINT];
51
53
double cmd[KHI_MAX_JOINT];
52
54
double pos[KHI_MAX_JOINT];
53
55
double vel[KHI_MAX_JOINT];
54
56
double eff[KHI_MAX_JOINT];
57
+ double home[KHI_MAX_JOINT];
55
58
};
56
59
57
- struct RobotInfo
60
+ struct KhiRobotData
61
+ {
62
+ std::string robot_name;
63
+ int arm_num;
64
+ KhiRobotArmData arm[KHI_MAX_ARM];
65
+ };
66
+
67
+ struct KhiRobotControllerInfo
58
68
{
59
69
int state;
60
70
int state_trigger;
61
71
std::string ip_address;
62
- std::string robot_name;
63
- int arm_num;
64
72
double period;
65
73
};
66
74
@@ -119,23 +127,21 @@ class KhiRobotDriver
119
127
{
120
128
for ( int cno = 0 ; cno < KHI_MAX_CONTROLLER; cno++ )
121
129
{
122
- robot_info[cno].state = INIT;
123
- robot_info[cno].state_trigger = NONE;
124
- robot_info[cno].ip_address = " 127.0.0.1" ;
125
- robot_info[cno].robot_name = " " ;
126
- robot_info[cno].arm_num = -1 ;
130
+ cont_info[cno].state = INIT;
131
+ cont_info[cno].state_trigger = NONE;
132
+ cont_info[cno].ip_address = " 127.0.0.1" ;
127
133
}
128
134
129
135
driver_name = __func__;
130
136
}
131
137
132
- int getState ( const int cont_no )
138
+ int getState ( const int & cont_no )
133
139
{
134
140
if ( ( cont_no < 0 ) || ( cont_no > KHI_MAX_CONTROLLER ) ) { return NOT_REGISTERED; }
135
- else { return robot_info [cont_no].state ; }
141
+ else { return cont_info [cont_no].state ; }
136
142
}
137
143
138
- std::string getStateName ( const int cont_no )
144
+ std::string getStateName ( const int & cont_no )
139
145
{
140
146
int state;
141
147
std::string name = " " ;
@@ -149,7 +155,7 @@ class KhiRobotDriver
149
155
return name;
150
156
}
151
157
152
- bool setState ( const int cont_no, const int state )
158
+ bool setState ( const int & cont_no, const int & state )
153
159
{
154
160
if ( !contLimitCheck ( cont_no, KHI_MAX_CONTROLLER ) ) { return false ; }
155
161
@@ -159,16 +165,16 @@ class KhiRobotDriver
159
165
}
160
166
else
161
167
{
162
- if ( robot_info [cont_no].state != state )
168
+ if ( cont_info [cont_no].state != state )
163
169
{
164
- infoPrint ( " State %d: %s -> %s" , cont_no, KhiRobotStateName[robot_info [cont_no].state ].c_str (), KhiRobotStateName[state].c_str () );
165
- robot_info [cont_no].state = state;
170
+ infoPrint ( " State %d: %s -> %s" , cont_no, KhiRobotStateName[cont_info [cont_no].state ].c_str (), KhiRobotStateName[state].c_str () );
171
+ cont_info [cont_no].state = state;
166
172
}
167
173
return true ;
168
174
}
169
175
}
170
176
171
- bool isTransitionState ( const int cont_no )
177
+ bool isTransitionState ( const int & cont_no )
172
178
{
173
179
int state;
174
180
@@ -183,20 +189,20 @@ class KhiRobotDriver
183
189
}
184
190
}
185
191
186
- int getStateTrigger ( const int cont_no )
192
+ int getStateTrigger ( const int & cont_no )
187
193
{
188
194
int state_trigger;
189
195
190
196
if ( ( cont_no < 0 ) || ( cont_no > KHI_MAX_CONTROLLER ) ) { return NONE; }
191
197
else
192
198
{
193
- state_trigger = robot_info [cont_no].state_trigger ;
194
- robot_info [cont_no].state_trigger = NONE;
199
+ state_trigger = cont_info [cont_no].state_trigger ;
200
+ cont_info [cont_no].state_trigger = NONE;
195
201
return state_trigger;
196
202
}
197
203
}
198
204
199
- bool setStateTrigger ( const int cont_no, const int state_trigger )
205
+ bool setStateTrigger ( const int & cont_no, const int & state_trigger )
200
206
{
201
207
if ( !contLimitCheck ( cont_no, KHI_MAX_CONTROLLER ) ) { return false ; }
202
208
@@ -206,25 +212,17 @@ class KhiRobotDriver
206
212
}
207
213
else
208
214
{
209
- if ( robot_info [cont_no].state_trigger != NONE )
215
+ if ( cont_info [cont_no].state_trigger != NONE )
210
216
{
211
217
warnPrint ( " State Trigger is already done %d: %s" , cont_no, KhiRobotStateTriggerName[state_trigger].c_str () );
212
218
}
213
219
infoPrint ( " State Trigger %d: %s" , cont_no, KhiRobotStateTriggerName[state_trigger].c_str () );
214
- robot_info [cont_no].state_trigger = state_trigger;
220
+ cont_info [cont_no].state_trigger = state_trigger;
215
221
return true ;
216
222
}
217
223
}
218
224
219
- bool setRobotName ( const int cont_no, const std::string name )
220
- {
221
- if ( contLimitCheck ( cont_no, KHI_MAX_CONTROLLER ) ) { return false ; }
222
-
223
- robot_info[cont_no].robot_name = name;
224
- return true ;
225
- }
226
-
227
- bool contLimitCheck ( const int cont_no, const int limit )
225
+ bool contLimitCheck ( const int & cont_no, const int & limit )
228
226
{
229
227
if ( ( cont_no < 0 ) || ( cont_no > KHI_MAX_CONTROLLER ) || ( cont_no > limit ) )
230
228
{
@@ -237,7 +235,7 @@ class KhiRobotDriver
237
235
}
238
236
}
239
237
240
- void infoPrint ( const char * format, ... )
238
+ void infoPrint ( const char * format, ... )
241
239
{
242
240
char msg[512 ] = { 0 };
243
241
va_list ap;
@@ -248,7 +246,7 @@ class KhiRobotDriver
248
246
ROS_INFO ( " [%s] %s" , driver_name.c_str (), msg );
249
247
}
250
248
251
- void warnPrint ( const char * format, ... )
249
+ void warnPrint ( const char * format, ... )
252
250
{
253
251
char msg[512 ] = { 0 };
254
252
va_list ap;
@@ -259,7 +257,7 @@ class KhiRobotDriver
259
257
ROS_WARN ( " [%s] %s" , driver_name.c_str (), msg );
260
258
}
261
259
262
- void errorPrint ( const char * format, ... )
260
+ void errorPrint ( const char * format, ... )
263
261
{
264
262
char msg[512 ] = { 0 };
265
263
va_list ap;
@@ -270,48 +268,43 @@ class KhiRobotDriver
270
268
ROS_ERROR ( " [%s] %s" , driver_name.c_str (), msg );
271
269
}
272
270
273
- void jointPrint ( std::string name, const JointData joint )
271
+ void jointPrint ( std::string name, const KhiRobotData& data )
274
272
{
275
273
char msg[512 ] = { 0 };
276
274
char jt_val[16 ] = { 0 };
275
+ const double * value;
277
276
278
277
snprintf ( msg, sizeof (msg), " [%s]\t " , name.c_str () );
279
- if ( name == std::string (" write" ) )
280
- {
281
- for ( int cnt = 0 ; cnt < joint.joint_num ; cnt++ )
282
- {
283
- snprintf ( jt_val, sizeof (jt_val), " %.3lf\t " , joint.cmd [cnt] );
284
- strcat ( msg, jt_val );
285
- }
286
- }
287
- else
278
+ for ( int ano = 0 ; ano < KHI_MAX_ARM; ano++ )
288
279
{
289
- for ( int cnt = 0 ; cnt < joint.joint_num ; cnt++ )
280
+ if ( name == std::string (" write" ) ) { value = data.arm [ano].cmd ; }
281
+ else { value = data.arm [ano].pos ; }
282
+ for ( int jt = 0 ; jt < data.arm [ano].jt_num ; jt++ )
290
283
{
291
- snprintf ( jt_val, sizeof (jt_val), " %.3lf\t " , joint. pos [cnt ] );
284
+ snprintf ( jt_val, sizeof (jt_val), " %.3lf\t " , value[jt ] );
292
285
strcat ( msg, jt_val );
293
286
}
294
287
}
295
288
infoPrint ( " [SIM]%s" , msg );
296
289
}
297
290
298
291
virtual ~KhiRobotDriver () {};
299
- virtual bool initialize ( const int cont_no, const std::string robot_name, const double period, const JointData joint, bool in_simulation = false ) = 0;
300
- virtual bool open ( const int cont_no, const std::string ip_address ) = 0;
301
- virtual bool close ( const int cont_no ) = 0;
302
- virtual bool activate ( const int cont_no, JointData *joint ) = 0;
303
- virtual bool hold ( const int cont_no, const JointData joint ) = 0;
304
- virtual bool deactivate ( const int cont_no ) = 0;
305
- virtual bool readData ( const int cont_no, JointData *joint ) = 0;
306
- virtual bool writeData ( const int cont_no, JointData joint ) = 0;
307
- virtual bool updateState ( const int cont_no ) = 0;
308
- virtual bool getPeriodDiff ( const int cont_no, double * diff ) = 0;
309
- virtual bool commandHandler ( khi_robot_msgs::KhiRobotCmd::Request & req, khi_robot_msgs::KhiRobotCmd::Response & res ) = 0;
292
+ virtual bool initialize ( const int & cont_no, const double & period, KhiRobotData& data, const bool in_simulation = false ) = 0;
293
+ virtual bool open ( const int & cont_no, const std::string& ip_address, KhiRobotData& data ) = 0;
294
+ virtual bool close ( const int & cont_no ) = 0;
295
+ virtual bool activate ( const int & cont_no, KhiRobotData& data ) = 0;
296
+ virtual bool hold ( const int & cont_no, const KhiRobotData& data ) = 0;
297
+ virtual bool deactivate ( const int & cont_no, const KhiRobotData& data ) = 0;
298
+ virtual bool readData ( const int & cont_no, KhiRobotData& data ) = 0;
299
+ virtual bool writeData ( const int & cont_no, const KhiRobotData& data ) = 0;
300
+ virtual bool updateState ( const int & cont_no, const KhiRobotData& data ) = 0;
301
+ virtual bool getPeriodDiff ( const int & cont_no, double & diff ) = 0;
302
+ virtual bool commandHandler ( khi_robot_msgs::KhiRobotCmd::Request& req, khi_robot_msgs::KhiRobotCmd::Response& res ) = 0;
310
303
311
304
protected:
312
305
bool in_simulation;
313
306
std::string driver_name;
314
- RobotInfo robot_info [KHI_MAX_CONTROLLER];
307
+ KhiRobotControllerInfo cont_info [KHI_MAX_CONTROLLER];
315
308
int return_code;
316
309
int error_code;
317
310
};
0 commit comments