RobotWebTools / roslibjs

The Standard ROS JavaScript Library
https://robotwebtools.github.io/roslibjs
Other
691 stars 382 forks source link

ROS2 cancel action_server goal #718

Closed dines1111 closed 1 month ago

dines1111 commented 6 months ago

Hai, can someone help me regrading how to cancel the action server goal?

This is my current code:

document.addEventListener('DOMContentLoaded', function() {
  var fibonacciClient = new ROSLIB.Action({
    ros : ros,
    name : '/fibonacci',
    actionType : 'action_tutorials_interfaces/Fibonacci'

  });

  // Send an action goal
  var goal = new ROSLIB.ActionGoal({
    order: 10,
  });

  // Variable to store the goal ID
  var actionGoalId ;

  document.getElementById('sendGoalButton').addEventListener('click', function() {
    // Send an action goal

    actionGoalId = fibonacciClient.sendGoal(goal,
      function(result) {
        console.log('Result for action goal on ' + fibonacciClient.name + ': ' + result.result.sequence);
      },
      function(feedback) {
        console.log('Feedback for action on ' + fibonacciClient.name + ': ' + feedback.partial_sequence);
      } 
    ); 
  });

  document.getElementById('cancelGoalButton').addEventListener('click', function() {
    // Check if a goal has been sent previously
    if (actionGoalId) {
      // Send a cancel request for the previously sent goal
      fibonacciClient.cancelGoal(actionGoalId);
      console.log('Cancel request sent for goal: ' + actionGoalId);
      goal_id = null; // Reset goal_id after cancellation
    } else {
      console.log('No goal has been sent yet.');
    }   
  });
});
flxinxout commented 5 months ago

hey, How du you have the ROSLIB.Action class? I have the ActionClient only.

harsh-pal-robotics commented 5 months ago

@flxinxout https://github.com/RobotWebTools/roslibjs/blob/develop/src/core/Action.js It is currently in develop branch, you can clone the repo and point the package.json ROSLIB to a local path.

dines1111 commented 2 months ago

Hai, the solution for this question is simply setting the send_action_goals_in_new_thread parameter on rosbridge to true.