Skip to content
firepick1 (localhost) edited this page Feb 5, 2016 · 4 revisions

Overview

RestClient is a small REST client that you can use to write JavaScript programs that control a firenodejs robot remotely via REST.

Example

rest-demo.js shows how to use RestClient. In this demo, we do two things:

  • Step 1. home FPD
  • Step 2. move FPD to (x:50,y:50}

In the firenodejs directory, type the following bash command, substituting PI2 with the hostname or ip address of the firenodejs server:

node js/rest-demo.js -h PI2

Your FPD should move and you'll see something like the following:

START	: rest-demo.js
INFO : Step 1. Homing...
HTTP	: POST http://localhost:8080/firestep
HTTP	: <= [{"hom":""},{"mpo":""}]
END : rest-demo.js
HTTP	: => {"s":0,"r":{"mpo":{"1":0,"2":0,"3":0,"x":0,"y":0,"z":0,"xn":0,"yn":0,"zn":0}},"t":0.001}
INFO : FPD is at position x: 0 y: 0 z: 0
INFO : Step 2. Move to (50,50)...
HTTP	: POST http://localhost:8080/firestep
HTTP	: <= [{"mov":{"x":50,"y":50}},{"mpo":""}]
HTTP	: => {"s":0,"r":{"mpo":{"1":1654,"2":-1025,"3":957,"x":49.999,"y":49.998,"z":-0.002,"xn":50,"yn":50,"zn":0}},"t":0.001}
INFO : FPD is at position x: 49.999 y: 49.998 z: -0.002

Walkthrough

We first include everything we need and set up defaults:

varshould=require("should");varRestClient=require("./RestClient");console.log("START\t: rest-demo.js");varrestOptions={protocol: "http://",host: "localhost",port: "8080",};

Now we read command line arguments:

for(vari=1;i<process.argv.length;i++){vararg=process.argv[i];if(arg==="-h"){(++i).should.below(process.argv.length);restOptions.host=process.argv[i];}}

We create a RestClient with our updated options:

varrest=newRestClient(restOptions);

And declare the actions we wish to perform:

functionstep1(){console.log("Step 1. Homing...");rest.post("/firestep",[{hom:""},{mpo:""}],function(data){// Step 1 http response callbackconsole.log("FPD is at position x:",data.r.mpo.x," y:",data.r.mpo.y," z:",data.r.mpo.z);step2();// do AFTER step1 is done});}functionstep2(){console.log("Step 2. Move to (50,50)...");rest.post("/firestep",[{mov:{x:50,y:50}},{mpo:""}],function(data){// Step 2 http response callbackconsole.log("FPD is at position x:",data.r.mpo.x," y:",data.r.mpo.y," z:",data.r.mpo.z);});}

Now we kick off step1() and sit back till it's done. Notice that we do not call step2() directly--we have to WAIT until step1 is done:

step1();// step2(); <= DANGER! we can't just put step2 here because it will run in PARALLEL!console.log("END\t: rest-demo.js");

Summary

Using RestClient, you can orchestrate the behavior of one or more FPD(s), doing arbitrary calculations with the returned REST results.

Have fun!

Clone this wiki locally