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

This is an example of how the MotorHat module is used in KRL.  This example comes from wheels.krl.

Code Block
languagejs
titleKRL example
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 Library Configuration

MotorHat module takes advantage of the motor-hat npm library. The npm library was installed manually but could be listed as a new dependency of the engine.
The motorHat module 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 the motor-hat documentation.

...

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

...