Accessing the robot’s capabilities from javascript#

Documentation of JavaScript library pallib.js#

pallib.js is a JavaScript library that permits execution of the most common ARI commands from custom JavaScript code, without any ROS knowledge.

Example of function call:

my_web.executeMotion("bow", () => {my_web.disableBtn("bow_btn")});

Library structure#

pallib.js depends on rrlib.js which is a JavaScript that provides a wrapper over the REST interface of the robot, in a similar manner to RoslibJs.

pallib.js#
import * as RRLIB from './rrlib.js'

The user can import only rrlib.js and develop the webpage with it as well, however, this library is targeted to experienced ROS users, for simple use it is recommended to use pallib.js.

pallib.js contains a JavaScript class which provides the functions that will send commands to the robot: at the moment motions and text-to-speech.

Function executeMotion()#

ARI will execute an existing named motion.

pallib.js#
executeMotion(motion, cb)

Parameters to be passed:

  • motion: string with the ros_name of the motion to be executed (ex.: “bow”, “nod”).

  • cb: callback function to be executed once the robot accepted the motion.

Example#
executeMotion("bow", () => {console.log("start!")})

Function executeAndTrackMotion()#

ARI will execute an existing named motion and execute a callback after it is finished.

pallib.js#
executeAndTrackMotion(motion, tracking_interval, end_cb, goal_cb)

Parameters to be passed:

  • motion: string with the ros_name of the motion to be executed (ex.: “bow”, “nod”).

  • tracking_interval: number of milliseconds after which the motion_goal_tracker will be called to check if ARI ended the motion execution (recommended value: 1000).

  • end_cb: callback function to be executed after the robot finished the motion.

  • goal_cb: callback function to be executed once the robot accepted the motion. [Optional]

Example#
executeAndTrackMotion("bow", 1000, () => {console.log("end!")}, () => {console.log("start!")})

Function say()#

ARI will say a specific text in a given language.

pallib.js#
say(text, lang, cb)

Parameters to be passed:

  • text: string with the text to be said.

  • lang: string with international language code (ex.: en_GB, es_ES, ru_RU).

  • cb: callback function to be executed once the robot accepted the sentence to be said.

Example#
say("Hello, how are you?", ``en_GB``, () => {console.log("start!")})

Function sayAndTrack()#

ARI will say a sentence in a language, and execute a callback when it is finished.

pallib.js#
sayAndTrack(text, lang, tracking_interval, end_cb, goal_cb)

Parameters to be passed:

  • text: string with the text to be said.

  • lang: string with international language code (ex.: en_GB, es_ES, ru_RU).

  • tracking_interval: number of milliseconds after which the tts_goal_tracker will be called to check if ARI ended the speech execution (recommended value: 1000).

  • end_cb: callback function to be executed after the robot finished the sentence to be said.

  • goal_cb: callback function to be executed once the robot accepted the sentence to be said. [Optional]

Example#
sayAndTrack("Hello, how are you?", ``en_GB``, 1000, () => {console.log("end!")}, () => {console.log("start!")})

Function sayPregen()#

ARI will say a specific text stored on the robot.

pallib.js#
sayPregen(section, key, lang, cb)

Parameters to be passed:

  • section: section identifier on the robot’s dictionaries.

  • key: key of the sentence to be said.

  • lang: string with international language code (ex.: en_GB, es_ES, ru_RU).

  • cb: callback function to be executed once the robot accepted the sentence to be said.

Example#
sayPregen(section, key, ``en_GB``, () => {console.log("start!")})

Function sayPregenAndTrack()#

ARI will say a specific text stored on the robot, and execute a callback when it is finished.

pallib.js#
sayPregenAndTrack(section, key, lang, goal_cb, tracking_interval, end_cb)

Parameters to be passed:

  • section: section identifier on the robot’s dictionaries.

  • key: key of the sentence to be said.

  • lang: string with international language code (ex.: en_GB, es_ES, u_RU).

  • tracking_interval: number of milliseconds after which the tts_goal_tracker will be called to check if ARI ended the speech execution (recommended value: 1000).

  • end_cb: callback function to be executed after the robot finished the sentence to be said.

  • goal_cb: callback function to be executed once the robot accepted the sentence to be said.

Functions sendInput(), sendAccept(), sendReject(), sendCancel()#

These functions were created for internal use in some particular cases when the webpage should notify of a specific interaction to the robot.

How to use the library in a custom code#

Import pallib.js#

Once the page is uploaded to the robot via the TouchScreen Manager, the code will be placed in the same file level as the folder js, which contains a folder modules with pallib.js and rrlib.js files.

Example of custom file structure as:

/js
  /lib
  /modules
    pallib.js
    rrlib.js

/my_web
  /css
  /script
    main.js
  index.html

Refer to the main.js as a module from the html file named index.html:

index.html#
<script type="module" src="js/main.js"></script>

Import pallib.js from main.js:

main.js#
import * as PalLib from '../../js/modules/pallib.js'

Create a JavaScript class#

To use functions from pallib.js it is recommended to create a JavaScript class.

main.js

 1class MyWeb {
 2    constructor() {
 3        this.pal_lib = new PalLib();
 4        /* custom variables go here... */
 5    }
 6      init() {
 7           /* custom js... */
 8        this.pal_lib.init();
 9      }
10      /* pallib.js custom functions... */
11}
12
13let my_web = new MyWeb();

Use of pallib.js functions in custom JavaScript code#

The next step is to create custom functions which use the library functions.

Example of a use case. When a user clicks the button “Hello” on the ARI touchscreen, all buttons will change its CSS to disable mode; after the robot says “Hello, I am ARI”, all buttons will change its style to enable mode.

sayAndTrack() function:

 1class MyWeb {
 2  constructor() {
 3    this.pal_lib = new PalLib(); // Instantiate the library
 4    /* custom variables go here... */
 5  }
 6
 7  init() {
 8    /* custom js... */
 9    this.pal_lib.init(); // Connect to the robot, must be done before sending any command
10  }
11
12  // Disable screen buttons as soon as robot start saying  "Hello, I  am ARI" and enable the buttons when the phrase will be said
13  sayHello() {
14    this.pal_lib.sayAndTrack("Hello, I am ARI", ``en_GB``, 1000, () => {this.enableBtns()}, () => {this.disableBtns()});
15  }
16}
17
18let my_web = new MyWeb();

And eventListener jQuery example for the front-end:

1$(document).ready(function () {
2  my_web.init();* // Must be called before sending any command*
3  $("#btn_hello").on('touchend', () => {
4    * // eventListener to hello btn*
5    my_web.sayHello();;
6  });
7});

Documents/examples of interest#

Import the rrlib library, create the respective ROS action client inside the constructor, in this example, /tts, and with this, you can send an action goal indicating the text to say and, if desired, what animation to do with it.

 1import * as RRLIB from '../../js/modules/rrlib.js'
 2
 3constructor() {
 4  this.ros = new RRLIB.Ros({
 5    host: 'http://' + window.location.hostname
 6  });
 7  this.tts_action = new RRLIB.ActionClient({
 8    ros: this.ros,
 9    name: 'tts'
10  });
11  this.sc_timeout = -1;
12}
13
14init() {
15  let param = new RRLIB.Param({
16    ros: this.ros,
17    name: 'robot_info'
18  });
19}
20
21firstPhrase() {
22  let goal_id = '';
23  // Respond
24  this.tts_action.sendGoal({
25    rawtext: {
26      text: "Hey, Juana, <mark name='doTrick trickName=show_left'/> it is
27      time to take your pills! <
28      break time = '500ms' / > They are in the right
29      drawer,
30      the second one from the top ",
31      lang_id: "en_GB"
32    }
33  }, (response) => {
34    goal_id = response.goal_id;
35  });
36}

See also#