Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

JavaScript Modules allow you to extend KRL language within a pico engine. They are often used to take advantage of platform specific features. For example, extending KRL to natively manage GPIO pins of a raspberry pi hosting a pico engine.

...

The pico engine is built on top of node, which makes using npm packages ideal and easy to use to extend KRL.  The following example is from the pico rover project. The pico rover is a mecanum wheeled rover that is natively controlled from KRL with a pico engine running on a raspberry pi. A custom JavaScript module is used as a singleton to control motors attached to Adafruit's MotorHAT

This is a good example of how actions should be defined and used. When the speed of a motor is changed, the physical state is changed.  Actions allow the state of a pico to change, the state of a pico represents the physical state of the analog device. When motorHat methods are called they result in a physical state change.   

MotorHat Module Instantiation Example

...

Code Block
languagejs
let spec = {
    address: 0x60,
    steppers: [],
    dcs: ['M1','M2','M3','M4'],
    servos: []
};
var motorHat = require('motor-hat')(spec); // singleton for module 

...

MotorHat Module Methods

As seen in the previous example, the JavaScript exports methods for the engine to provide in the new module. Below you will see that the motor-hat.js wrapps motor-hat library functions

...

in KRL actions.

...

  This allows developers to take advantage of npm's large librarys to exstend and customize KRL as desired.

Code Block
languagejs
module.exports = {
    dc_setSpeed: {
        type: "action",
        args: ["index","speed"],
        fn: function(args, callback){
			motorHat.dcs[args.index].setSpeed(args.speed);// 0-100 percent
			callback();
        },
    },
	dc_run: {
        type: "action",
        args: ["index","direction"],
        fn: function(args, callback){
			motorHat.dcs[args.index].run(args.direction);// fwd or anything else for back..
			callback();
        },
    },
    dc_stop: {
        type: "action",
        args: ["index"],
        fn: function(args, callback){
			motorHat.dcs[args.index].stop();
			callback();
        },
    }
}

...