21
21
import org .ros .node .AbstractNodeMain ;
22
22
import org .ros .node .ConnectedNode ;
23
23
import org .ros .internal .message .Message ;
24
+ import org .ros .message .Duration ;
24
25
import actionlib_tutorials .FibonacciActionGoal ;
25
26
import actionlib_tutorials .FibonacciActionFeedback ;
26
27
import actionlib_tutorials .FibonacciActionResult ;
31
32
import actionlib_msgs .GoalID ;
32
33
import actionlib_msgs .GoalStatus ;
33
34
import org .apache .commons .logging .Log ;
34
- import org .apache .commons .logging .LogFactory ;
35
35
36
36
37
37
/**
38
38
* Class to test the actionlib client.
39
39
* @author Ernesto Corbellini [email protected]
40
40
*/
41
41
public class TestClient extends AbstractNodeMain implements ActionClientListener <FibonacciActionFeedback , FibonacciActionResult > {
42
+ static {
43
+ // comment this line if you want logs activated
44
+ System .setProperty ("org.apache.commons.logging.Log" ,
45
+ "org.apache.commons.logging.impl.NoOpLog" );
46
+ }
42
47
private ActionClient ac = null ;
43
48
private volatile boolean resultReceived = false ;
44
- private Log log = LogFactory . getLog ( ActionClient . class ) ;
49
+ private Log log ;
45
50
46
51
@ Override
47
52
public GraphName getDefaultNodeName () {
@@ -52,48 +57,49 @@ public GraphName getDefaultNodeName() {
52
57
public void onStart (ConnectedNode node ) {
53
58
ac = new ActionClient <FibonacciActionGoal , FibonacciActionFeedback , FibonacciActionResult >(node , "/fibonacci" , FibonacciActionGoal ._TYPE , FibonacciActionFeedback ._TYPE , FibonacciActionResult ._TYPE );
54
59
FibonacciActionGoal goalMessage ;
55
- int repeat = 3 ;
56
- int i = 0 ;
57
- String goalId = "fibonacci_test_" ;
60
+ GoalID gid ;
61
+ Duration serverTimeout = new Duration ( 20 ) ;
62
+ boolean serverStarted ;
58
63
64
+ log = node .getLog ();
59
65
// Attach listener for the callbacks
60
66
ac .attachListener (this );
61
-
62
- System .out .println ("Waiting for actionlib server to start..." );
63
- ac .waitForActionServerToStart ();
64
- System .out .println ("actionlib server started." );
67
+ System .out .println ("\n Waiting for action server to start..." );
68
+ serverStarted = ac .waitForActionServerToStart (new Duration (20 ));
69
+ if (serverStarted ) {
70
+ System .out .println ("Action server started.\n " );
71
+ }
72
+ else {
73
+ System .out .println ("No actionlib server found after waiting for " + serverTimeout .totalNsecs ()/1e9 + " seconds!" );
74
+ System .exit (1 );
75
+ }
65
76
66
77
// Create Fibonacci goal message
67
- //goalMessage = (FibonacciActionGoal)ac.newGoalMessage();
68
- //FibonacciGoal fibonacciGoal = goalMessage.getGoal();
69
-
70
- // set Fibonacci parameter
71
- //fibonacciGoal.setOrder(6);
72
-
73
- /*for (i = 0; i < repeat; i++) {
74
- //sleep(10000);
75
- System.out.println("Sending goal #" + i + "...");
76
- goalMessage = (FibonacciActionGoal)ac.newGoalMessage();
77
- goalMessage.getGoal().setOrder(i*3);
78
- ac.sendGoal(goalMessage, goalId + i);
79
- System.out.println("Goal sent.");
80
- resultReceived = false;
81
- }*/
82
-
83
- // send another message and cancel it
84
- goalId += i ;
85
78
goalMessage = (FibonacciActionGoal )ac .newGoalMessage ();
86
- goalMessage .getGoal ().setOrder (3 );
87
- //System.out.println("Sending goal ID: " + goalId + "...");
88
- //ac.sendGoal(goalMessage, goalId);
79
+ FibonacciGoal fibonacciGoal = goalMessage .getGoal ();
80
+ // set Fibonacci parameter
81
+ fibonacciGoal .setOrder (3 );
82
+ System .out .println ("Sending goal..." );
89
83
ac .sendGoal (goalMessage );
90
- GoalID gid = ac .getGoalId (goalMessage );
91
- System .out .println ("Goal sent. Goal ID: " + gid );
92
- //sleep(1000);
93
- //System.out.println("Cancelling goal ID: " + goalId);
94
- //ac.sendCancel(gid);
95
- sleep (5000 );
84
+ gid = ac .getGoalId (goalMessage );
85
+ System .out .println ("Sent goal with ID: " + gid .getId ());
86
+ System .out .println ("Waiting for goal to complete..." );
87
+ while (ac .getGoalState () != ClientStateMachine .ClientStates .DONE ) {
88
+ sleep (1 );
89
+ }
90
+ System .out .println ("Goal completed!\n " );
96
91
92
+ System .out .println ("Sending a new goal..." );
93
+ ac .sendGoal (goalMessage );
94
+ gid = ac .getGoalId (goalMessage );
95
+ System .out .println ("Sent goal with ID: " + gid .getId ());
96
+ System .out .println ("Cancelling this goal..." );
97
+ ac .sendCancel (gid );
98
+ while (ac .getGoalState () != ClientStateMachine .ClientStates .DONE ) {
99
+ sleep (1 );
100
+ }
101
+ System .out .println ("Goal cancelled succesfully.\n " );
102
+ System .out .println ("Bye!" );
97
103
System .exit (0 );
98
104
}
99
105
@@ -104,11 +110,10 @@ public void resultReceived(FibonacciActionResult message) {
104
110
int i ;
105
111
106
112
resultReceived = true ;
107
-
108
- System .out .print ("Got Fibonacci result sequence!" );
113
+ System .out .print ("Got Fibonacci result sequence: " );
109
114
for (i =0 ; i <sequence .length ; i ++)
110
115
System .out .print (Integer .toString (sequence [i ]) + " " );
111
- System .out .print ( " \n " );
116
+ System .out .println ( " " );
112
117
}
113
118
114
119
@ Override
0 commit comments