Robot Not a Dev? Buy Now
Not a Dev?

Misty Community Forum

Understanding JavaScript

I just finished the Hello World tutorial and I noticed it’s just a list of functions. Aside from the random integer code you don’t declare anything at the beginning and there’s no loop. Am I missing something?

Nope, not missing anything!

At this time Misty’s JavaScript SDK doesn’t support frameworks like Node, so there’s no need to declare anything or import external libraries when you start writing a skill. (That said, you can still use Node and npm when coding remote-running robot applications that use Misty’s REST API).

You can also declare functions and global variables in your skill code. How you choose to do this depends on the skill you’re building. Additional declarations are possible, though not necessary, for the Hello World example.

As for loops, we generally recommend you avoid using conventional JavaScript loops to have Misty repeat a set of commands in your skills, as this consumes a lot of memory and can cause performance issues. Instead, when you want Misty to loop through a set of actions, you can use misty.RegisterTimerEvent(). Write your commands inside the timer event callback function, and at the end of the function, use another misty.RegisterTimerEvent() method to trigger the same callback. This is how we loop through the breathing LED pattern in the Hello World tutorial:

// The breathingLED timer event invokes this callback function.
function _breathingLED() {
    // Values used to modify the RGB intensity of Misty's chest LED.
    // Change these to use a different starting color for the LED.
    var red = 140 / 10.0;
    var green = 0 / 10.0;
    var blue = 220 / 10.0;

    // Incrementally DECREASES the intensity of each color in the LED
    for (var i = 10; i >= 0; i = i - 1) {
        misty.ChangeLED(
            Math.floor(i * red), // red intensity
            Math.floor(i * green), // green intensity
            Math.floor(i * blue)); // blue intensity
        // Pause before next iteration. Increase value for slower
        // breathing; decrease for faster breathing.
        misty.Pause(150);
    }

    // Incrementally INCREASES the intensity of each color in the LED
    for (var i = 0; i <= 10; i = i + 1) {
        misty.ChangeLED(
            Math.floor(i * red), // red intensity
            Math.floor(i * green), // green intensity
            Math.floor(i * blue)); // blue intensity
        // Pause before next iteration. Increase value for slower
        // breathing; decrease for faster breathing.
        misty.Pause(150);
    }
    // Re-registers for the breathingLED timer event, so Misty's LED
    // continues breathing until the skill ends.
    misty.RegisterTimerEvent("breathingLED", 1, false);
}

// Registers for a timer event called breathingLED, and invokes the
// _breathingLED() callback after 1 millisecond.
misty.RegisterTimerEvent("breathingLED", 1, false);

Misty’s documentation provides more information about the architecture of the JavaScript SDK and on-robot skills. The developer docs are always improving, so please let us know if there’s information you can’t find!

2 Likes

Thank you so much Jonathan! I was confused, but that makes sense. Thanks for fixing my errors as well Ben. :stuck_out_tongue_winking_eye: