2121import org .ros .node .AbstractNodeMain ;
2222import org .ros .node .ConnectedNode ;
2323import org .ros .internal .message .Message ;
24+ import org .ros .message .Duration ;
2425import actionlib_tutorials .FibonacciActionGoal ;
2526import actionlib_tutorials .FibonacciActionFeedback ;
2627import actionlib_tutorials .FibonacciActionResult ;
3132import actionlib_msgs .GoalID ;
3233import actionlib_msgs .GoalStatus ;
3334import org .apache .commons .logging .Log ;
34- import org .apache .commons .logging .LogFactory ;
3535
3636
3737/**
3838 * Class to test the actionlib client.
3939 * @author Ernesto Corbellini ecorbellini@ekumenlabs.com
4040 */
4141public 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+ }
4247 private ActionClient ac = null ;
4348 private volatile boolean resultReceived = false ;
44- private Log log = LogFactory . getLog ( ActionClient . class ) ;
49+ private Log log ;
4550
4651 @ Override
4752 public GraphName getDefaultNodeName () {
@@ -52,48 +57,49 @@ public GraphName getDefaultNodeName() {
5257 public void onStart (ConnectedNode node ) {
5358 ac = new ActionClient <FibonacciActionGoal , FibonacciActionFeedback , FibonacciActionResult >(node , "/fibonacci" , FibonacciActionGoal ._TYPE , FibonacciActionFeedback ._TYPE , FibonacciActionResult ._TYPE );
5459 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 ;
5863
64+ log = node .getLog ();
5965 // Attach listener for the callbacks
6066 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+ }
6576
6677 // 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 ;
8578 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..." );
8983 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 " );
9691
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!" );
97103 System .exit (0 );
98104 }
99105
@@ -104,11 +110,10 @@ public void resultReceived(FibonacciActionResult message) {
104110 int i ;
105111
106112 resultReceived = true ;
107-
108- System .out .print ("Got Fibonacci result sequence!" );
113+ System .out .print ("Got Fibonacci result sequence: " );
109114 for (i =0 ; i <sequence .length ; i ++)
110115 System .out .print (Integer .toString (sequence [i ]) + " " );
111- System .out .print ( " \n " );
116+ System .out .println ( " " );
112117 }
113118
114119 @ Override
0 commit comments