Skip to content

Commit 16e33d4

Browse files
author
Ernesto Corbellini
committed
Added result received event status update.
1 parent 01ef7c8 commit 16e33d4

File tree

5 files changed

+107
-9
lines changed

5 files changed

+107
-9
lines changed

src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ActionClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public void setGoalId(T_ACTION_GOAL goal, GoalID gid) {
162162
* @see actionlib_msgs.GoalID
163163
*/
164164
public void sendCancel(GoalID id) {
165+
goalManager.cancelGoal();
165166
cancelPublisher.publish(id);
166167
}
167168

@@ -251,6 +252,11 @@ private void unsubscribeToServer() {
251252
* depends on the application.
252253
*/
253254
public void gotResult(T_ACTION_RESULT message) {
255+
ActionResult<T_ACTION_RESULT> ar = new ActionResult(message);
256+
if (ar.getGoalStatusMessage().getGoalId().getId().equals(goalManager.actionGoal.getGoalId())) {
257+
goalManager.updateStatus(ar.getGoalStatusMessage().getStatus());
258+
}
259+
goalManager.resultReceived();
254260
// Propagate the callback
255261
if (callbackTarget != null) {
256262
callbackTarget.resultReceived(message);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright 2015 Ekumen www.ekumenlabs.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.ekumen.rosjava_actionlib;
18+
19+
import java.lang.reflect.Method;
20+
import org.ros.internal.message.Message;
21+
import org.ros.message.Time;
22+
import std_msgs.Header;
23+
import actionlib_msgs.GoalID;
24+
import actionlib_msgs.GoalStatus;
25+
26+
/**
27+
* Class to encapsulate the action feedback object.
28+
* @author Ernesto Corbellini [email protected]
29+
*/
30+
public class ActionResult<T_ACTION_RESULT extends Message> {
31+
private T_ACTION_RESULT actionResultMessage = null;
32+
33+
public ActionResult(T_ACTION_RESULT msg) {
34+
actionResultMessage = msg;
35+
}
36+
37+
public Header getHeaderMessage() {
38+
Header h = null;
39+
if (actionResultMessage != null) {
40+
try {
41+
Method m = actionResultMessage.getClass().getMethod("getHeader");
42+
m.setAccessible(true); // workaround for known bug http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6924232
43+
h = (Header)m.invoke(actionResultMessage);
44+
}
45+
catch (Exception e) {
46+
e.printStackTrace(System.out);
47+
}
48+
}
49+
return h;
50+
}
51+
52+
public GoalStatus getGoalStatusMessage() {
53+
GoalStatus gs = null;
54+
if (actionResultMessage != null) {
55+
try {
56+
Method m = actionResultMessage.getClass().getMethod("getStatus");
57+
m.setAccessible(true); // workaround for known bug http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6924232
58+
gs = (GoalStatus)m.invoke(actionResultMessage);
59+
}
60+
catch (Exception e) {
61+
e.printStackTrace(System.out);
62+
}
63+
}
64+
return gs;
65+
}
66+
67+
public Message getResultMessage() {
68+
Message x = null;
69+
if (actionResultMessage != null) {
70+
try {
71+
Method m = actionResultMessage.getClass().getMethod("getResult");
72+
m.setAccessible(true); // workaround for known bug http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6924232
73+
x = (Message)m.invoke(actionResultMessage);
74+
}
75+
catch (Exception e) {
76+
e.printStackTrace(System.out);
77+
}
78+
}
79+
return x;
80+
}
81+
}

src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ClientGoalManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public boolean cancelGoal() {
4848
return stateMachine.cancel();
4949
}
5050

51+
public void resultReceived() {
52+
stateMachine.resultReceived();
53+
}
54+
5155
public void updateStatus(int status) {
5256
stateMachine.transition(status);
5357
}

src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/ClientStateMachine.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,19 @@ public boolean cancel() {
458458
case ClientStates.WAITING_FOR_GOAL_ACK:
459459
case ClientStates.PENDING:
460460
case ClientStates.ACTIVE:
461-
this.state = ClientStates.WAITING_FOR_CANCEL_ACK;
461+
state = ClientStates.WAITING_FOR_CANCEL_ACK;
462462
ret = true;
463463
break;
464464
}
465465
return ret;
466466
}
467467

468+
public void resultReceived() {
469+
if (state == ClientStates.WAITING_FOR_RESULT) {
470+
state = ClientStates.DONE;
471+
}
472+
}
473+
468474
public void markAsLost()
469475
{
470476
}

src/rosjava_actionlib/rosjava_actionlib/src/main/java/com/github/ekumen/rosjava_actionlib/TestClient.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ public void onStart(ConnectedNode node) {
8383
// send another message and cancel it
8484
goalId += i;
8585
goalMessage = (FibonacciActionGoal)ac.newGoalMessage();
86-
goalMessage.getGoal().setOrder(2);
87-
System.out.println("Sending goal ID: " + goalId + "...");
88-
ac.sendGoal(goalMessage, goalId);
89-
System.out.println("Goal sent.");
90-
sleep(2000);
91-
System.out.println("Cancelling goal ID: " + goalId);
86+
goalMessage.getGoal().setOrder(3);
87+
//System.out.println("Sending goal ID: " + goalId + "...");
88+
//ac.sendGoal(goalMessage, goalId);
89+
ac.sendGoal(goalMessage);
9290
GoalID gid = ac.getGoalId(goalMessage);
93-
ac.sendCancel(gid);
94-
sleep(10000);
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);
9596

9697
System.exit(0);
9798
}

0 commit comments

Comments
 (0)