7 · Sensing and acting
An aggregate program reads the world through sensors and changes it through actuators. Neither lives inside the program: the configuration connects them.
Inputs: sense and nbrSense
sense(name) returns a value the device reads at the start of each round. Where it comes from depends on where the program runs:
- in the simulator, from config.js:
source,obstacle,x,y, plus whatever itssensors(id, x, y, round)returns; - on a device, from the
sensors()function you pass tostart: a button, a light sensor, a battery level, a robot's distance sensor.
nbrSense(name) is a field: one reading per neighbour, measured by the platform. nbrSense('range') is the distance to each neighbour: exact in the simulator, from positions over WiFi, a rough estimate from signal strength over Bluetooth, where nbrSense('rssi') also gives the raw dBm.
Here config.js provides a temp sensor: a heat source moving around the network. Each node checks whether it is the warmest among its neighbours. Open the config.js tab to see the environment.
Outputs: the program's value
The program returns a value every round, and onResult(value, id) turns it into action: an LED, a motor, a message on the console. Keep the program free of side effects and let onResult act: the same program then runs unchanged in the simulator, and a round that is computed again does no harm.
start(program, {
target: 'ble',
period: 500,
sensors: () => ({ source: BTN1.read(), light: Puck.light() }),
onResult: (hops) => LED1.write(hops === 0)
});
When the output has several parts, return an object and use each part in onResult: { led: ..., speed: ... }.
A robot example
A swarm of robots on WiFi, each one knowing its position. They gather around the robot whose button was pressed: each robot moves towards the neighbour that is closer to the target.
var program = () => {
var dist = distanceTo(sense('target'));
var next = minId(nbr(dist), dist); // the neighbour closer to the target
var where = nbr(sense('pos')); // everyone shares its position
return { dist: dist, towards: next === null ? null : where.at(next) };
};
start(program, {
target: 'udp',
wifi: { ssid: 'robots', password: '...' },
position: () => odometry.position(), // [x, y], from the robot's own localisation
range: 1.5, // neighbours within 1.5 m
sensors: () => ({ target: BTN1.read(), pos: odometry.position() }),
onResult: (r) => r.towards ? drive.towards(r.towards) : drive.stop()
});
odometry and drive stand for your robot's code: NbrJS decides where to go, your robot knows how to get there.
Reacting to events
Rounds run every period milliseconds. For a quicker reaction, call a round yourself when something happens:
var net = start(program, { target: 'ble', period: 2000, sensors: () => ({ source: BTN1.read() }) });
setWatch(() => net.round(), BTN1, { edge: 'both', repeat: true, debounce: 30 });
Try it
1. Change the environment in config.js: two heat sources instead of one.
2. Return the hops from the warmest node instead of a boolean.
Solutions
sensors: (id, x, y, round) => {
var a = 30 - Math.hypot(x - 2, y - 2) * 3, b = 30 - Math.hypot(x - 6, y - 5) * 3;
return { temp: Math.round(Math.max(a, b)) };
}
var program = () => hopsTo(sense('temp') >= nbr(sense('temp')).max());