...
...
...
...
Note |
---|
Engine Compatibility |
JavaScript Modules allow you to extend KRL 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 following sections describe how JavaScript Modules work:
Table of Contents | ||
---|---|---|
|
Defining a JavaScript Module
...
Package.json Example
Code Block | ||||
---|---|---|---|---|
| ||||
{ "name": "example-krl-modules", "private": true, "dependencies": { "pico-engine": "^0.13.2" }, "scripts": { "start": "pico-engine" }, "pico-engine": { "port": 4080, "modules": { "myCalcJsModule": "./myJsModule.js" } } } |
...
JavaScript Module Example
...
myJsModule.js
Code Block |
---|
module.exports = { random : { type: "function", args: ["min","max"], fn: function(args, callback){ var randomNumber = Math.floor(Math.random()*( args.max - args.min +1) + args.min ); callback(null, randomNumber); }, }, multiply: { type: "action", args: ["a", "b"], fn: function(args, callback){ var result = args.a * args.b; callback(null, result); }, }, divide : { type: "action", args: ["n", "d"], fn: function(args, callback){ callback(null, ( args.n / args.d ) ); }, }, }; |
...
Module Instantiation Example
instantiation
Code Block | ||||
---|---|---|---|---|
| ||||
global { random = function(_min, _max){ myCalcJsModule:random(_min, _max) } times = defaction(_left, _right){ myCalcJsModule:multiply(_left,_right) } division = defaction(_numerator,_denominator){ myCalcJsModule:divide(_numerator,_denominator) } } |
...
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
This is an example of how the MotorHat module is used in KRL. This example comes from wheels.krl.
KRL example
Code Block | ||||
---|---|---|---|---|
| ||||
speed = defaction(speed){ motorHat:dc_setSpeed(ent:motorIndex,speed.as("Number").defaultsTo(50)); } run = defaction(direction){ motorHat:dc_run(ent:motorIndex,direction); } stop = defaction(){ motorHat:dc_stop(ent:motorIndex); } |
...
MotorHat module takes advantage of the motor-hat npm library. The npm library was installed manually installed manually but could be listed as a new dependency new dependency of the engine.
This example is taken from motor-hat.js and shows how the npm library was initialized. The motor-hat.js code requires and configures the motor-hat library as directed in documentation.
Code Block | ||
---|---|---|
| ||
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.
...