Source: actionlib/Goal.js

  1. /**
  2. * @fileOverview
  3. * @author Russell Toris - rctoris@wpi.edu
  4. */
  5. var Message = require('../core/Message');
  6. var EventEmitter2 = require('eventemitter2').EventEmitter2;
  7. /**
  8. * An actionlib goal that is associated with an action server.
  9. *
  10. * Emits the following events:
  11. * * 'timeout' - If a timeout occurred while sending a goal.
  12. *
  13. * @constructor
  14. * @param {Object} options
  15. * @param {ActionClient} options.actionClient - The ROSLIB.ActionClient to use with this goal.
  16. * @param {Object} options.goalMessage - The JSON object containing the goal for the action server.
  17. */
  18. function Goal(options) {
  19. var that = this;
  20. this.actionClient = options.actionClient;
  21. this.goalMessage = options.goalMessage;
  22. this.isFinished = false;
  23. // Used to create random IDs
  24. var date = new Date();
  25. // Create a random ID
  26. this.goalID = 'goal_' + Math.random() + '_' + date.getTime();
  27. // Fill in the goal message
  28. this.goalMessage = new Message({
  29. goal_id : {
  30. stamp : {
  31. secs : 0,
  32. nsecs : 0
  33. },
  34. id : this.goalID
  35. },
  36. goal : this.goalMessage
  37. });
  38. this.on('status', function(status) {
  39. that.status = status;
  40. });
  41. this.on('result', function(result) {
  42. that.isFinished = true;
  43. that.result = result;
  44. });
  45. this.on('feedback', function(feedback) {
  46. that.feedback = feedback;
  47. });
  48. // Add the goal
  49. this.actionClient.goals[this.goalID] = this;
  50. }
  51. Goal.prototype.__proto__ = EventEmitter2.prototype;
  52. /**
  53. * Send the goal to the action server.
  54. *
  55. * @param {number} [timeout] - A timeout length for the goal's result.
  56. */
  57. Goal.prototype.send = function(timeout) {
  58. var that = this;
  59. that.actionClient.goalTopic.publish(that.goalMessage);
  60. if (timeout) {
  61. setTimeout(function() {
  62. if (!that.isFinished) {
  63. that.emit('timeout');
  64. }
  65. }, timeout);
  66. }
  67. };
  68. /**
  69. * Cancel the current goal.
  70. */
  71. Goal.prototype.cancel = function() {
  72. var cancelMessage = new Message({
  73. id : this.goalID
  74. });
  75. this.actionClient.cancelTopic.publish(cancelMessage);
  76. };
  77. module.exports = Goal;