Roslibjs don't get goal result on client
I have a simple ActionLib server, and a roslib js client very similar to the tutorials. When I launch my client in chrome, I see printed in the console:
Connected to websocket server
I can also see in the terminal that I launched my cpp server in:
^CMacBook-Pro-2:ros_ws Fry$ rosrun cpp_action_server_pkg cmd_server
[ INFO] [1447742973.282337000]: cmd: Executing, creating fibonacci sequence of order 7 with seeds 0, 1
[ INFO] [1447742980.283698000]: cmd: Succeeded
So far so good. However, despite the "Suceeded", my js code
goal.on('result', function(result) {
console.log('Final Result: ' + result.sequence);
});
is apparently never called, as I never see 'Final Result:" in my (Chrome) browser console. I see no errors printed out in my roscore terminal, my actionlib server terminal, my rosbridge terminal, nor my browser console.
My full client page is:
<!DOCTYPE html> <html> <head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://cdn.robotwebtools.org/EventEmitter2/current/eventemitter2.js"></script>
<script type="text/javascript" src="http://cdn.robotwebtools.org/roslibjs/current/roslib.js"></script>
<script type="text/javascript" type="text/javascript">
var ros = new ROSLIB.Ros({
url : 'ws://localhost:9090'
});
var cmdClient = new ROSLIB.ActionClient({
ros : ros,
serverName : '/cmd',
actionName : 'cpp_action_server_pkg/CmdAction'
});
var goal = new ROSLIB.Goal({
actionClient : cmdClient,
goalMessage : { order : 7}
});
goal.on('feedback', function(feedback) {
console.log('Feedback: ' + feedback.sequence);
});
goal.on('result', function(result) {
console.log('Final Result: ' + result.sequence);
});
ros.on('connection', function() {
console.log('Connected to websocket server.2');
});
ros.on('error', function(error) {
console.log('Error connecting to websocket server: ', error);
});
ros.on('close', function() {
console.log('Connection to websocket server closed.');
});
goal.send();
</script>
</head>
<body> <h1>Cmd ActionClient
Example</h1> <p>Check the Web
Console for output</p> </body>
</html>
My full cpp server code is:
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include <cpp_action_server_pkg/CmdAction.h>
class CmdAction
{
protected:
ros::NodeHandle nh_;
actionlib::SimpleActionServer<cpp_action_server_pkg::CmdAction> as_; // NodeHandle instance must be created before this line. Otherwise strange error occurs.
std::string action_name_;
// create messages that are used to published feedback/result
cpp_action_server_pkg::CmdFeedback feedback_;
cpp_action_server_pkg::CmdResult result_;
public:
CmdAction(std::string name) :
as_(nh_, name, boost::bind(&CmdAction::executeCB, this, _1), false),
action_name_(name)
{
as_.start();
}
~CmdAction(void)
{
}
void executeCB(const cpp_action_server_pkg::CmdGoalConstPtr &goal)
{
// helper variables
ros::Rate r(1);
bool success = true;
// push_back the seeds for the fibonacci sequence
feedback_.sequence.clear();
feedback_.sequence.push_back(0);
feedback_.sequence.push_back(1);
// publish info to the console for the user
ROS_INFO("%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i", action_name_.c_str(), goal->order, feedback_.sequence[0], feedback_.sequence[1]);
// start executing the action
for(int i=1; i<=goal->order; i++)
{
// check that preempt has not been requested by the client
if (as_.isPreemptRequested() || !ros::ok())
{
ROS_INFO("%s: Preempted", action_name_.c_str());
// set the action state to preempted
as_.setPreempted();
success = false;
break;
}
feedback_.sequence.push_back(feedback_.sequence[i] + feedback_.sequence[i-1]); //the 1 line of actual work!
// publish the feedback
as_.publishFeedback(feedback_);
// this sleep is not necessary, the ...
Hm. To me everything looks normal but I'm not a js expert. What comes in my mind is, that your objects get garbage collected after calling
goal.send();
but this is just a wild guess.If you rostopic echo
/cmd/result
does your result get printed out correctly?@Fry Did you solved this?