RobotWebTools / roslibjs

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

ROS2 cancel action_server goal #718

Open dines1111 opened 2 months ago

dines1111 commented 2 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 2 weeks ago

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

harsh-pal-robotics commented 2 weeks 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.