## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## for a custom note with links: ## note = ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= ## descriptive title for the tutorial ## title = Writing an actionlib client with roslibjs ## multi-line description to be displayed in search ## description = This tutorial shows you how to create an actionlib client and pass a goal. ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link= ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory ## keywords = roslibjs, web interface, actionlib, Robot Web Tools #################################### <> <> === Getting Started === This tutorial involves writing a single HTML file, which will contain the HTML and JavaScript needed to communicate with ROS over rosbridge. To begin, create a file `fibonacci.html` with your favorite text editor. The complete code for this tutorial can be found in the [[https://github.com/RobotWebTools/roslibjs/tree/develop/examples|roslibjs repo]]. === The HTML Code === {{{ #!cplusplus block=html_example

Fibonacci ActionClient Example

Check the Web Console for output

}}} === Code Explanation === Now that we have an example, let's look at each component. <> We need to create a `Ros` node object to communicate with a [[rosbridge_suite|rosbridge server]]. In this example, the script will connect to `localhost` on the default port of `9090`. <> A `ROSLIB.ActionClient` acts an [[actionlib|actionlib client]], with the ability to send goals and receive responses from an `actionlib` server. In this example, we'll be calling the Fibonacci `actionlib server`, which computes a Fibonacci sequence based on the goal and provides feedback along the way. The server name is the `actionlib server` name. Following the `actionlib` convention, the server name will be the prefix for topics to subscribe to, like /fibonacci/feedback, /fibonacci/result, etc. The action name corresponds to the `actionlib server` base message type name to use. Documentation of all the constructor options and available functions can be found in the [[http://robotwebtools.org/jsdoc/roslibjs/current/ActionClient.html|ROSLIB.ActionClient docs]]. <> `ROSLIB.Goal` is an `actionlib` goal to send to the `actionlib server`. In this case, the goal is Fibonnaci sequence length to compute to. The [[http://robotwebtools.org/jsdoc/roslibjs/current/Goal.html|ROSLIB.Goal]] docs describe all the constructor options and functions available. <> After a goal is sent to the `actionlib server`, the server will provide "feedback" updates while reaching its goal. In this case, every new computed Fibonacci number will return as a "feedback" event, which will be outputted to the Web Console in the callback function. <> When a goal was reached (or failed to reach), a "result" message will be emitted. This example prints out the result and the Fibonacci sequence it contains. <> This adds a listener for a `connection` event to the `ros` object. The following two blocks do the same for `error` and `close` events. This way, we can monitor the connection to the [[rosbridge_suite|rosbridge server]]. <> Finally, we send the goal to the `actionlib server`. Notice how the event handlers like `goal.on('feedback', ...)` are declared before sending the goal to be ready to handle events right away. === Running the Example === At this point we are ready to run the example. To do so, you will need to have installed the following packages: * ros-hydro-ros-base (basic ROS installation) * ros-hydro-rosbridge-server ([[rosbridge_server]]) * ros-hydro-actionlib-tutorials (Contains the Fibonacci actionlib server) To begin, we will launch ROS. To do so, run the following in a terminal: {{{ roscore }}} Next, we'll start the Fibonacci `actionlib server`: {{{ rosrun actionlib_tutorials fibonacci_server }}} Once everything is running, we can launch the `rosbridge v2.0` server with the following: {{{ roslaunch rosbridge_server rosbridge_websocket.launch }}} Finally, you are now ready to bring up your HTML page in a web browser. You can open up the file directly in the browser without running a web server. The data outputted happens in the Web Console. ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE