|
1 | 1 | /**
|
2 | 2 | * Copyright 2015 Ekumen www.ekumenlabs.com
|
3 |
| - * |
| 3 | + * <p> |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
| 7 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15 | 15 | */
|
16 | 16 |
|
17 | 17 |
|
18 |
| - |
19 |
| -import java.util.List; |
20 |
| -import org.ros.namespace.GraphName; |
21 |
| -import org.ros.node.AbstractNodeMain; |
22 |
| -import org.ros.node.ConnectedNode; |
23 |
| -import org.ros.message.Duration; |
24 |
| -import actionlib_tutorials.FibonacciActionGoal; |
25 |
| -import actionlib_tutorials.FibonacciActionFeedback; |
26 |
| -import actionlib_tutorials.FibonacciActionResult; |
27 |
| -import actionlib_tutorials.FibonacciGoal; |
28 |
| -import actionlib_tutorials.FibonacciFeedback; |
29 |
| -import actionlib_tutorials.FibonacciResult; |
30 |
| -import actionlib_msgs.GoalStatusArray; |
31 | 18 | import actionlib_msgs.GoalID;
|
32 | 19 | import actionlib_msgs.GoalStatus;
|
| 20 | +import actionlib_msgs.GoalStatusArray; |
| 21 | +import actionlib_tutorials.*; |
33 | 22 | import com.github.rosjava_actionlib.ActionClient;
|
34 | 23 | import com.github.rosjava_actionlib.ActionClientListener;
|
35 | 24 | import com.github.rosjava_actionlib.ClientStateMachine;
|
36 | 25 | import org.apache.commons.logging.Log;
|
| 26 | +import org.ros.message.Duration; |
| 27 | +import org.ros.namespace.GraphName; |
| 28 | +import org.ros.node.AbstractNodeMain; |
| 29 | +import org.ros.node.ConnectedNode; |
| 30 | + |
| 31 | +import java.util.List; |
37 | 32 |
|
38 | 33 |
|
39 | 34 | /**
|
40 | 35 | * Class to test the actionlib client.
|
41 | 36 | * @author Ernesto Corbellini [email protected]
|
42 | 37 | */
|
43 | 38 | public class TestClient extends AbstractNodeMain implements ActionClientListener<FibonacciActionFeedback, FibonacciActionResult> {
|
44 |
| - static { |
45 |
| - // comment this line if you want logs activated |
46 |
| - System.setProperty("org.apache.commons.logging.Log", |
47 |
| - "org.apache.commons.logging.impl.NoOpLog"); |
48 |
| - } |
49 |
| - private ActionClient ac = null; |
50 |
| - private volatile boolean resultReceived = false; |
51 |
| - private Log log; |
52 |
| - |
53 |
| - @Override |
54 |
| - public GraphName getDefaultNodeName() { |
55 |
| - return GraphName.of("fibonacci_test_client"); |
56 |
| - } |
57 |
| - |
58 |
| - @Override |
59 |
| - public void onStart(ConnectedNode node) { |
60 |
| - ac = new ActionClient<FibonacciActionGoal, FibonacciActionFeedback, FibonacciActionResult>(node, "/fibonacci", FibonacciActionGoal._TYPE, FibonacciActionFeedback._TYPE, FibonacciActionResult._TYPE); |
61 |
| - FibonacciActionGoal goalMessage; |
62 |
| - GoalID gid; |
63 |
| - Duration serverTimeout = new Duration(20); |
64 |
| - boolean serverStarted; |
65 |
| - |
66 |
| - log = node.getLog(); |
67 |
| - // Attach listener for the callbacks |
68 |
| - ac.attachListener(this); |
69 |
| - System.out.println("\nWaiting for action server to start..."); |
70 |
| - serverStarted = ac.waitForActionServerToStart(new Duration(20)); |
71 |
| - if (serverStarted) { |
72 |
| - System.out.println("Action server started.\n"); |
| 39 | + static { |
| 40 | + // comment this line if you want logs activated |
| 41 | + System.setProperty("org.apache.commons.logging.Log", |
| 42 | + "org.apache.commons.logging.impl.NoOpLog"); |
73 | 43 | }
|
74 |
| - else { |
75 |
| - System.out.println("No actionlib server found after waiting for " + serverTimeout.totalNsecs()/1e9 + " seconds!"); |
76 |
| - System.exit(1); |
| 44 | + |
| 45 | + private ActionClient ac = null; |
| 46 | + private volatile boolean resultReceived = false; |
| 47 | + private Log log; |
| 48 | + |
| 49 | + @Override |
| 50 | + public GraphName getDefaultNodeName() { |
| 51 | + return GraphName.of("fibonacci_test_client"); |
77 | 52 | }
|
78 | 53 |
|
79 |
| - // Create Fibonacci goal message |
80 |
| - goalMessage = (FibonacciActionGoal)ac.newGoalMessage(); |
81 |
| - FibonacciGoal fibonacciGoal = goalMessage.getGoal(); |
82 |
| - // set Fibonacci parameter |
83 |
| - fibonacciGoal.setOrder(3); |
84 |
| - System.out.println("Sending goal..."); |
85 |
| - ac.sendGoal(goalMessage); |
86 |
| - gid = ac.getGoalId(goalMessage); |
87 |
| - System.out.println("Sent goal with ID: " + gid.getId()); |
88 |
| - System.out.println("Waiting for goal to complete..."); |
89 |
| - while (ac.getGoalState() != ClientStateMachine.ClientStates.DONE) { |
90 |
| - sleep(1); |
| 54 | + @Override |
| 55 | + public void onStart(ConnectedNode node) { |
| 56 | + ac = new ActionClient<FibonacciActionGoal, FibonacciActionFeedback, FibonacciActionResult>(node, "/fibonacci", FibonacciActionGoal._TYPE, FibonacciActionFeedback._TYPE, FibonacciActionResult._TYPE); |
| 57 | + FibonacciActionGoal goalMessage; |
| 58 | + GoalID gid; |
| 59 | + Duration serverTimeout = new Duration(20); |
| 60 | + boolean serverStarted; |
| 61 | + |
| 62 | + log = node.getLog(); |
| 63 | + // Attach listener for the callbacks |
| 64 | + ac.attachListener(this); |
| 65 | + System.out.println("\nWaiting for action server to start..."); |
| 66 | + serverStarted = ac.waitForActionServerToStart(new Duration(20)); |
| 67 | + if (serverStarted) { |
| 68 | + System.out.println("Action server started.\n"); |
| 69 | + } else { |
| 70 | + System.out.println("No actionlib server found after waiting for " + serverTimeout.totalNsecs() / 1e9 + " seconds!"); |
| 71 | + System.exit(1); |
| 72 | + } |
| 73 | + |
| 74 | + // Create Fibonacci goal message |
| 75 | + goalMessage = (FibonacciActionGoal) ac.newGoalMessage(); |
| 76 | + FibonacciGoal fibonacciGoal = goalMessage.getGoal(); |
| 77 | + // set Fibonacci parameter |
| 78 | + fibonacciGoal.setOrder(3); |
| 79 | + System.out.println("Sending goal..."); |
| 80 | + ac.sendGoal(goalMessage); |
| 81 | + gid = ac.getGoalId(goalMessage); |
| 82 | + System.out.println("Sent goal with ID: " + gid.getId()); |
| 83 | + System.out.println("Waiting for goal to complete..."); |
| 84 | + while (ac.getGoalState() != ClientStateMachine.ClientStates.DONE) { |
| 85 | + sleep(1); |
| 86 | + } |
| 87 | + System.out.println("Goal completed!\n"); |
| 88 | + |
| 89 | + System.out.println("Sending a new goal..."); |
| 90 | + ac.sendGoal(goalMessage); |
| 91 | + gid = ac.getGoalId(goalMessage); |
| 92 | + System.out.println("Sent goal with ID: " + gid.getId()); |
| 93 | + System.out.println("Cancelling this goal..."); |
| 94 | + ac.sendCancel(gid); |
| 95 | + while (ac.getGoalState() != ClientStateMachine.ClientStates.DONE) { |
| 96 | + sleep(1); |
| 97 | + } |
| 98 | + System.out.println("Goal cancelled succesfully.\n"); |
| 99 | + System.out.println("Bye!"); |
| 100 | + System.exit(0); |
91 | 101 | }
|
92 |
| - System.out.println("Goal completed!\n"); |
93 |
| - |
94 |
| - System.out.println("Sending a new goal..."); |
95 |
| - ac.sendGoal(goalMessage); |
96 |
| - gid = ac.getGoalId(goalMessage); |
97 |
| - System.out.println("Sent goal with ID: " + gid.getId()); |
98 |
| - System.out.println("Cancelling this goal..."); |
99 |
| - ac.sendCancel(gid); |
100 |
| - while (ac.getGoalState() != ClientStateMachine.ClientStates.DONE) { |
101 |
| - sleep(1); |
| 102 | + |
| 103 | + @Override |
| 104 | + public void resultReceived(FibonacciActionResult message) { |
| 105 | + FibonacciResult result = message.getResult(); |
| 106 | + int[] sequence = result.getSequence(); |
| 107 | + int i; |
| 108 | + |
| 109 | + resultReceived = true; |
| 110 | + System.out.print("Got Fibonacci result sequence: "); |
| 111 | + for (i = 0; i < sequence.length; i++) |
| 112 | + System.out.print(Integer.toString(sequence[i]) + " "); |
| 113 | + System.out.println(""); |
102 | 114 | }
|
103 |
| - System.out.println("Goal cancelled succesfully.\n"); |
104 |
| - System.out.println("Bye!"); |
105 |
| - System.exit(0); |
106 |
| - } |
107 |
| - |
108 |
| - @Override |
109 |
| - public void resultReceived(FibonacciActionResult message) { |
110 |
| - FibonacciResult result = message.getResult(); |
111 |
| - int[] sequence = result.getSequence(); |
112 |
| - int i; |
113 |
| - |
114 |
| - resultReceived = true; |
115 |
| - System.out.print("Got Fibonacci result sequence: "); |
116 |
| - for (i=0; i<sequence.length; i++) |
117 |
| - System.out.print(Integer.toString(sequence[i]) + " "); |
118 |
| - System.out.println(""); |
119 |
| - } |
120 |
| - |
121 |
| - @Override |
122 |
| - public void feedbackReceived(FibonacciActionFeedback message) { |
123 |
| - FibonacciFeedback result = message.getFeedback(); |
124 |
| - int[] sequence = result.getSequence(); |
125 |
| - int i; |
126 |
| - |
127 |
| - System.out.print("Feedback from Fibonacci server: "); |
128 |
| - for (i=0; i<sequence.length; i++) |
129 |
| - System.out.print(Integer.toString(sequence[i]) + " "); |
130 |
| - System.out.print("\n"); |
131 |
| - } |
132 |
| - |
133 |
| - @Override |
134 |
| - public void statusReceived(GoalStatusArray status) { |
135 |
| - List<GoalStatus> statusList = status.getStatusList(); |
136 |
| - for(GoalStatus gs:statusList) { |
137 |
| - log.info("GoalID: " + gs.getGoalId().getId() + " -- GoalStatus: " + gs.getStatus() + " -- " + gs.getText()); |
| 115 | + |
| 116 | + @Override |
| 117 | + public void feedbackReceived(FibonacciActionFeedback message) { |
| 118 | + FibonacciFeedback result = message.getFeedback(); |
| 119 | + int[] sequence = result.getSequence(); |
| 120 | + int i; |
| 121 | + |
| 122 | + System.out.print("Feedback from Fibonacci server: "); |
| 123 | + for (i = 0; i < sequence.length; i++) |
| 124 | + System.out.print(Integer.toString(sequence[i]) + " "); |
| 125 | + System.out.print("\n"); |
138 | 126 | }
|
139 |
| - log.info("Current state of our goal: " + ClientStateMachine.ClientStates.translateState(ac.getGoalState())); |
140 |
| - } |
141 | 127 |
|
142 |
| - void sleep(long msec) { |
143 |
| - try { |
144 |
| - Thread.sleep(msec); |
| 128 | + @Override |
| 129 | + public void statusReceived(GoalStatusArray status) { |
| 130 | + List<GoalStatus> statusList = status.getStatusList(); |
| 131 | + for (GoalStatus gs : statusList) { |
| 132 | + log.info("GoalID: " + gs.getGoalId().getId() + " -- GoalStatus: " + gs.getStatus() + " -- " + gs.getText()); |
| 133 | + } |
| 134 | + log.info("Current state of our goal: " + ClientStateMachine.ClientStates.translateState(ac.getGoalState())); |
145 | 135 | }
|
146 |
| - catch (InterruptedException ex) { |
| 136 | + |
| 137 | + void sleep(long msec) { |
| 138 | + try { |
| 139 | + Thread.sleep(msec); |
| 140 | + } catch (InterruptedException ex) { |
| 141 | + } |
147 | 142 | }
|
148 |
| - } |
149 | 143 | }
|
0 commit comments