1
- /* ******************************************************************************
1
+ /* ******************************************************************************
2
2
* Copyright 2018 ROBOTIS CO., LTD.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -58,32 +58,6 @@ OpenManipulatorController::~OpenManipulatorController()
58
58
59
59
void OpenManipulatorController::startTimerThread ()
60
60
{
61
- // //////////////////////////////////////////////////////////////////
62
- // / Use this when you want to increase the priority of threads.
63
- // //////////////////////////////////////////////////////////////////
64
- // pthread_attr_t attr_;
65
- // int error;
66
- // struct sched_param param;
67
- // pthread_attr_init(&attr_);
68
-
69
- // error = pthread_attr_setschedpolicy(&attr_, SCHED_RR);
70
- // if (error != 0) log::error("pthread_attr_setschedpolicy error = ", (double)error);
71
- // error = pthread_attr_setinheritsched(&attr_, PTHREAD_EXPLICIT_SCHED);
72
- // if (error != 0) log::error("pthread_attr_setinheritsched error = ", (double)error);
73
-
74
- // memset(¶m, 0, sizeof(param));
75
- // param.sched_priority = 31; // RT
76
- // error = pthread_attr_setschedparam(&attr_, ¶m);
77
- // if (error != 0) log::error("pthread_attr_setschedparam error = ", (double)error);
78
-
79
- // if ((error = pthread_create(&this->timer_thread_, &attr_, this->timerThread, this)) != 0)
80
- // {
81
- // log::error("Creating timer thread failed!!", (double)error);
82
- // exit(-1);
83
- // }
84
- // timer_thread_state_ = true;
85
- // //////////////////////////////////////////////////////////////////
86
-
87
61
int error;
88
62
if ((error = pthread_create (&this ->timer_thread_ , NULL , this ->timerThread , this )) != 0 )
89
63
{
@@ -110,17 +84,14 @@ void *OpenManipulatorController::timerThread(void *param)
110
84
controller->process (time);
111
85
112
86
clock_gettime (CLOCK_MONOTONIC, &curr_time);
113
- // ///
114
87
double delta_nsec = controller->getControlPeriod () - ((next_time.tv_sec - curr_time.tv_sec ) + ((double )(next_time.tv_nsec - curr_time.tv_nsec )*0.000000001 ));
115
- // log::info("control time : ", controller->getControlPeriod() - delta_nsec);
88
+
116
89
if (delta_nsec > controller->getControlPeriod ())
117
90
{
118
- // log::warn("Over the control time : ", delta_nsec);
119
91
next_time = curr_time;
120
92
}
121
93
else
122
94
clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &next_time, NULL );
123
- // ///
124
95
}
125
96
return 0 ;
126
97
}
@@ -130,7 +101,7 @@ void *OpenManipulatorController::timerThread(void *param)
130
101
********************************************************************************/
131
102
void OpenManipulatorController::initPublisher ()
132
103
{
133
- // ros message publisher
104
+ // ROS message publisher
134
105
auto om_tools_name = open_manipulator_.getManipulator ()->getAllToolComponentName ();
135
106
136
107
for (auto const & name:om_tools_name)
@@ -159,9 +130,10 @@ void OpenManipulatorController::initPublisher()
159
130
}
160
131
}
161
132
}
133
+
162
134
void OpenManipulatorController::initSubscriber ()
163
135
{
164
- // ros message subscriber
136
+ // ROS message subscriber
165
137
open_manipulator_option_sub_ = node_handle_.subscribe (" option" , 10 , &OpenManipulatorController::openManipulatorOptionCallback, this );
166
138
}
167
139
@@ -202,6 +174,14 @@ void OpenManipulatorController::openManipulatorOptionCallback(const std_msgs::St
202
174
bool OpenManipulatorController::goalJointSpacePathCallback (open_manipulator_msgs::SetJointPosition::Request &req,
203
175
open_manipulator_msgs::SetJointPosition::Response &res)
204
176
{
177
+ // Ignore commands if the robot is moving
178
+ if (open_manipulator_.getMovingState ())
179
+ {
180
+ log::warn (" Robot is moving. Ignoring command." );
181
+ res.is_planned = false ;
182
+ return true ;
183
+ }
184
+
205
185
std::vector <double > target_angle;
206
186
207
187
for (int i = 0 ; i < req.joint_position .joint_name .size (); i ++)
@@ -218,6 +198,14 @@ bool OpenManipulatorController::goalJointSpacePathCallback(open_manipulator_msgs
218
198
bool OpenManipulatorController::goalJointSpacePathToKinematicsPoseCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
219
199
open_manipulator_msgs::SetKinematicsPose::Response &res)
220
200
{
201
+ // Ignore commands if the robot is moving
202
+ if (open_manipulator_.getMovingState ())
203
+ {
204
+ log::warn (" Robot is moving. Ignoring command." );
205
+ res.is_planned = false ;
206
+ return true ;
207
+ }
208
+
221
209
KinematicPose target_pose;
222
210
target_pose.position [0 ] = req.kinematics_pose .pose .position .x ;
223
211
target_pose.position [1 ] = req.kinematics_pose .pose .position .y ;
@@ -241,6 +229,14 @@ bool OpenManipulatorController::goalJointSpacePathToKinematicsPoseCallback(open_
241
229
bool OpenManipulatorController::goalJointSpacePathToKinematicsPositionCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
242
230
open_manipulator_msgs::SetKinematicsPose::Response &res)
243
231
{
232
+ // Ignore commands if the robot is moving
233
+ if (open_manipulator_.getMovingState ())
234
+ {
235
+ log::warn (" Robot is moving. Ignoring command." );
236
+ res.is_planned = false ;
237
+ return true ;
238
+ }
239
+
244
240
KinematicPose target_pose;
245
241
target_pose.position [0 ] = req.kinematics_pose .pose .position .x ;
246
242
target_pose.position [1 ] = req.kinematics_pose .pose .position .y ;
@@ -257,6 +253,14 @@ bool OpenManipulatorController::goalJointSpacePathToKinematicsPositionCallback(o
257
253
bool OpenManipulatorController::goalJointSpacePathToKinematicsOrientationCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
258
254
open_manipulator_msgs::SetKinematicsPose::Response &res)
259
255
{
256
+ // Ignore commands if the robot is moving
257
+ if (open_manipulator_.getMovingState ())
258
+ {
259
+ log::warn (" Robot is moving. Ignoring command." );
260
+ res.is_planned = false ;
261
+ return true ;
262
+ }
263
+
260
264
KinematicPose target_pose;
261
265
262
266
Eigen::Quaterniond q (req.kinematics_pose .pose .orientation .w ,
@@ -270,12 +274,21 @@ bool OpenManipulatorController::goalJointSpacePathToKinematicsOrientationCallbac
270
274
res.is_planned = false ;
271
275
else
272
276
res.is_planned = true ;
277
+
273
278
return true ;
274
279
}
275
280
276
281
bool OpenManipulatorController::goalTaskSpacePathCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
277
282
open_manipulator_msgs::SetKinematicsPose::Response &res)
278
283
{
284
+ // Ignore commands if the robot is moving
285
+ if (open_manipulator_.getMovingState ())
286
+ {
287
+ log::warn (" Robot is moving. Ignoring command." );
288
+ res.is_planned = false ;
289
+ return true ;
290
+ }
291
+
279
292
KinematicPose target_pose;
280
293
target_pose.position [0 ] = req.kinematics_pose .pose .position .x ;
281
294
target_pose.position [1 ] = req.kinematics_pose .pose .position .y ;
@@ -298,6 +311,14 @@ bool OpenManipulatorController::goalTaskSpacePathCallback(open_manipulator_msgs:
298
311
bool OpenManipulatorController::goalTaskSpacePathPositionOnlyCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
299
312
open_manipulator_msgs::SetKinematicsPose::Response &res)
300
313
{
314
+ // Ignore commands if the robot is moving
315
+ if (open_manipulator_.getMovingState ())
316
+ {
317
+ log::warn (" Robot is moving. Ignoring command." );
318
+ res.is_planned = false ;
319
+ return true ;
320
+ }
321
+
301
322
Eigen::Vector3d position;
302
323
position[0 ] = req.kinematics_pose .pose .position .x ;
303
324
position[1 ] = req.kinematics_pose .pose .position .y ;
@@ -314,6 +335,14 @@ bool OpenManipulatorController::goalTaskSpacePathPositionOnlyCallback(open_manip
314
335
bool OpenManipulatorController::goalTaskSpacePathOrientationOnlyCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
315
336
open_manipulator_msgs::SetKinematicsPose::Response &res)
316
337
{
338
+ // Ignore commands if the robot is moving
339
+ if (open_manipulator_.getMovingState ())
340
+ {
341
+ log::warn (" Robot is moving. Ignoring command." );
342
+ res.is_planned = false ;
343
+ return true ;
344
+ }
345
+
317
346
Eigen::Quaterniond q (req.kinematics_pose .pose .orientation .w ,
318
347
req.kinematics_pose .pose .orientation .x ,
319
348
req.kinematics_pose .pose .orientation .y ,
@@ -332,6 +361,14 @@ bool OpenManipulatorController::goalTaskSpacePathOrientationOnlyCallback(open_ma
332
361
bool OpenManipulatorController::goalJointSpacePathFromPresentCallback (open_manipulator_msgs::SetJointPosition::Request &req,
333
362
open_manipulator_msgs::SetJointPosition::Response &res)
334
363
{
364
+ // Ignore commands if the robot is moving
365
+ if (open_manipulator_.getMovingState ())
366
+ {
367
+ log::warn (" Robot is moving. Ignoring command." );
368
+ res.is_planned = false ;
369
+ return true ;
370
+ }
371
+
335
372
std::vector <double > target_angle;
336
373
337
374
for (int i = 0 ; i < req.joint_position .joint_name .size (); i ++)
@@ -348,6 +385,14 @@ bool OpenManipulatorController::goalJointSpacePathFromPresentCallback(open_manip
348
385
bool OpenManipulatorController::goalTaskSpacePathFromPresentCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
349
386
open_manipulator_msgs::SetKinematicsPose::Response &res)
350
387
{
388
+ // Ignore commands if the robot is moving
389
+ if (open_manipulator_.getMovingState ())
390
+ {
391
+ log::warn (" Robot is moving. Ignoring command." );
392
+ res.is_planned = false ;
393
+ return true ;
394
+ }
395
+
351
396
KinematicPose target_pose;
352
397
target_pose.position [0 ] = req.kinematics_pose .pose .position .x ;
353
398
target_pose.position [1 ] = req.kinematics_pose .pose .position .y ;
@@ -371,6 +416,14 @@ bool OpenManipulatorController::goalTaskSpacePathFromPresentCallback(open_manipu
371
416
bool OpenManipulatorController::goalTaskSpacePathFromPresentPositionOnlyCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
372
417
open_manipulator_msgs::SetKinematicsPose::Response &res)
373
418
{
419
+ // Ignore commands if the robot is moving
420
+ if (open_manipulator_.getMovingState ())
421
+ {
422
+ log::warn (" Robot is moving. Ignoring command." );
423
+ res.is_planned = false ;
424
+ return true ;
425
+ }
426
+
374
427
Eigen::Vector3d position;
375
428
position[0 ] = req.kinematics_pose .pose .position .x ;
376
429
position[1 ] = req.kinematics_pose .pose .position .y ;
@@ -387,6 +440,14 @@ bool OpenManipulatorController::goalTaskSpacePathFromPresentPositionOnlyCallback
387
440
bool OpenManipulatorController::goalTaskSpacePathFromPresentOrientationOnlyCallback (open_manipulator_msgs::SetKinematicsPose::Request &req,
388
441
open_manipulator_msgs::SetKinematicsPose::Response &res)
389
442
{
443
+ // Ignore commands if the robot is moving
444
+ if (open_manipulator_.getMovingState ())
445
+ {
446
+ log::warn (" Robot is moving. Ignoring command." );
447
+ res.is_planned = false ;
448
+ return true ;
449
+ }
450
+
390
451
Eigen::Quaterniond q (req.kinematics_pose .pose .orientation .w ,
391
452
req.kinematics_pose .pose .orientation .x ,
392
453
req.kinematics_pose .pose .orientation .y ,
@@ -405,6 +466,14 @@ bool OpenManipulatorController::goalTaskSpacePathFromPresentOrientationOnlyCallb
405
466
bool OpenManipulatorController::goalToolControlCallback (open_manipulator_msgs::SetJointPosition::Request &req,
406
467
open_manipulator_msgs::SetJointPosition::Response &res)
407
468
{
469
+ // Ignore commands if the robot is moving
470
+ if (open_manipulator_.getMovingState ())
471
+ {
472
+ log::warn (" Robot is moving. Ignoring command." );
473
+ res.is_planned = false ;
474
+ return true ;
475
+ }
476
+
408
477
for (int i = 0 ; i < req.joint_position .joint_name .size (); i ++)
409
478
{
410
479
if (!open_manipulator_.makeToolTrajectory (req.joint_position .joint_name .at (i), req.joint_position .position .at (i)))
@@ -444,6 +513,14 @@ bool OpenManipulatorController::setActuatorStateCallback(open_manipulator_msgs::
444
513
bool OpenManipulatorController::goalDrawingTrajectoryCallback (open_manipulator_msgs::SetDrawingTrajectory::Request &req,
445
514
open_manipulator_msgs::SetDrawingTrajectory::Response &res)
446
515
{
516
+ // Ignore commands if the robot is moving
517
+ if (open_manipulator_.getMovingState ())
518
+ {
519
+ log::warn (" Robot is moving. Ignoring command." );
520
+ res.is_planned = false ;
521
+ return true ;
522
+ }
523
+
447
524
try
448
525
{
449
526
if (req.drawing_trajectory_name == " circle" )
@@ -466,7 +543,7 @@ bool OpenManipulatorController::goalDrawingTrajectoryCallback(open_manipulator_m
466
543
draw_line_arg.kinematic .position (1 ) = req.param [1 ]; // y axis (m)
467
544
draw_line_arg.kinematic .position (2 ) = req.param [2 ]; // z axis (m)
468
545
void *p_draw_line_arg = &draw_line_arg;
469
-
546
+
470
547
if (!open_manipulator_.makeCustomTrajectory (CUSTOM_TRAJECTORY_LINE, req.end_effector_name , p_draw_line_arg, req.path_time ))
471
548
res.is_planned = false ;
472
549
else
@@ -626,14 +703,14 @@ void OpenManipulatorController::publishGazeboCommand()
626
703
*****************************************************************************/
627
704
int main (int argc, char **argv)
628
705
{
629
- // init
706
+ // Init ROS
630
707
ros::init (argc, argv, " open_manipulator_controller" );
631
708
ros::NodeHandle node_handle (" " );
632
709
633
710
std::string usb_port = " /dev/ttyUSB0" ;
634
711
std::string baud_rate = " 1000000" ;
635
712
636
- if (argc = 3 )
713
+ if (argc == 3 )
637
714
{
638
715
usb_port = argv[1 ];
639
716
baud_rate = argv[2 ];
@@ -647,9 +724,13 @@ int main(int argc, char **argv)
647
724
648
725
OpenManipulatorController om_controller (usb_port, baud_rate);
649
726
650
- // update
727
+ // Start the timer thread
651
728
om_controller.startTimerThread ();
729
+
730
+ // Set up a periodic publishing timer
652
731
ros::Timer publish_timer = node_handle.createTimer (ros::Duration (om_controller.getControlPeriod ()), &OpenManipulatorController::publishCallback, &om_controller);
732
+
733
+ // Run the loop
653
734
ros::Rate loop_rate (100 );
654
735
while (ros::ok ())
655
736
{
@@ -659,3 +740,4 @@ int main(int argc, char **argv)
659
740
660
741
return 0 ;
661
742
}
743
+
0 commit comments