Skip to content

Commit

Permalink
Sonar examples
Browse files Browse the repository at this point in the history
I'm assuming these are removed from programs.json because Sonar has been dropped in favor of proximity.
  • Loading branch information
dtex authored and rwaldron committed Aug 23, 2019
1 parent ff4da5f commit 17b4f41
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 65 deletions.
83 changes: 39 additions & 44 deletions eg/sonar-scan.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,61 @@
var five = require("../lib/johnny-five.js"),
board;

board = new five.Board();

board.on("ready", function() {
var center, collision, degrees, step, facing,
range, redirect, look, isScanning, scanner, sonar, servos;
const {Board, Servo, Servos, Sonar} = require("../lib/johnny-five.js");
const board = new Board();

board.on("ready", () => {

// Collision distance (inches)
collision = 6;

// Starting scanner scanning position (degrees)
degrees = 90;

// Servo scanning steps (degrees)
step = 10;

// Current facing direction
facing = "";
const collision = 6;

// Scanning range (degrees)
range = [0, 170];
const range = [0, 170];

// Servo center point (degrees)
center = range[1] / 2;
const center = range[1] / 2;

// Redirection map
redirect = {
const redirect = {
left: "right",
right: "left"
};

// Direction to look after releasing scanner lock (degrees)
look = {
const look = {
forward: center,
left: 130,
right: 40
};

// Scanning state
isScanning = true;

// Sonar instance (distance detection)
sonar = new five.Sonar("A2");
const sonar = new Sonar("A2");

// Servo instance (panning)
scanner = new five.Servo({
const scanner = new Servo({
pin: 12,
range: range
range
});

servos = {
right: new five.Servo({
const servos = {
right: new Servo({
pin: 10,
type: "continuous"
}),
left: new five.Servo({
left: new Servo({
pin: 11,
type: "continuous"
})
};

// Starting scanner scanning position (degrees)
let degrees = 90;

// Servo scanning steps (degrees)
let step = 10;

// Current facing direction
let facing = "";

// Scanning state
let isScanning = true;

// Initialize the scanner at it's center point
// Will be exactly half way between the range's
Expand All @@ -69,8 +66,8 @@ board.on("ready", function() {
servos.left.to(90);

// Scanner/Panning loop
this.loop(100, function() {
var bounds;
board.loop(100, () => {
let bounds;

bounds = {
left: center + 10,
Expand Down Expand Up @@ -110,30 +107,28 @@ board.on("ready", function() {
// [2] Sonar "change" events are emitted when the value of a
// distance reading has changed since the previous reading
//
sonar.on("change", function() {
var turnTo;
sonar.on("change", () => {
let turnTo;

// Detect collision
if (Math.abs(this.inches) < collision && isScanning) {
if (Math.abs(sonar.inches) < collision && isScanning) {

// Scanning lock will prevent multiple collision detections
// of the same obstacle
isScanning = false;
turnTo = redirect[facing] || Object.keys(redirect)[Date.now() % 2];

// Log collision detection to REPL
console.log(
[Date.now(),
"Collision detected " + this.inches + " inches away.",
"Turning " + turnTo.toUpperCase() + " to avoid"
].join("\n")
);

console.log(`${Date.now()}
Collision detected ${sonar.inches} inches away.
Turning ${turnTo.toUpperCase()} to avoid`);

// Override the next scan position (degrees)
// degrees = look[ turnTo ];

// [1] Allow 1000ms to pass and release the scanning lock
// by setting isScanning state to true.
board.wait(1500, function() {
board.wait(1500, () => {
console.log("Release Scanner Lock");
isScanning = true;
});
Expand Down
19 changes: 7 additions & 12 deletions eg/sonar-srf10.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
var five = require("../lib/johnny-five.js");
var board = new five.Board();
const {Board, Sonar} = require("../lib/johnny-five.js");
var board = new Board();

board.on("ready", function() {
board.on("ready", () => {

var sonar = new five.Sonar({
var sonar = new Sonar({
device: "SRF10"
});

function display(type, value, unit) {
console.log("%s event: object is %d %s away", type, value, unit);
console.log(`${type} event: object is ${value} ${unit} away`);
}

sonar.on("data", function() {
display("data", this.inches, "inches");
});

sonar.on("change", function() {
display("data", this.inches, "inches");
});
sonar.on("data", () => display("data", sonar.inches, "inches"));
sonar.on("change", () => display("data", sonar.inches, "inches"));
});
16 changes: 7 additions & 9 deletions eg/sonar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
var five = require("../lib/johnny-five.js"),
board, sonar;
const {Board, Sonar} = require("../lib/johnny-five.js");
const board = new Board();

board = new five.Board();

board.on("ready", function() {
board.on("ready", () => {

// Create a new `sonar` hardware instance.
sonar = new five.Sonar("A2");
const sonar = new Sonar("A2");

// Sonar Properties

Expand All @@ -30,21 +28,21 @@ board.on("ready", function() {
//
// "data" fired continuously
//
sonar.on("data", function() {
sonar.on("data", () => {
/*
this.voltage - raw voltage reading
this.inches - calculated distance, inches
this.cm - calculated distance, centimeters
*/
console.log("data", "Object is " + this.inches + "inches away");
console.log("data", "Object is " + sonar.inches + "inches away");
});

//
// "change" fired when distance reading changes
//
sonar.on("change", function() {
console.log("change", "Object is " + this.inches + "inches away");
console.log("change", "Object is " + sonar.inches + "inches away");
});
});

0 comments on commit 17b4f41

Please sign in to comment.