Monday, October 27, 2014

Interacting with robot (Sphero) through Node.js

 Sphero is a sophisticated and programmable robot housed in a polycarbonate sphere shell.

The cylon-sphero adaptor makes it easy to interact with Sphero using Node.js. Once you have your Sphero setup and connected to your computer, you can start writing code to make Sphero move, change direction, speed and colors, or detect Sphero events and execute some code when they occur.

We have created a interactive game using three Spheros. Controlling multiple Spheros , Spheros will roll in a random direction and change their colors when it has a collision.


For this Cylon example, we're going to set up a Sphero such that it will change it's color and roll in a random direction when it has a collision.
Before we get started, make sure you have the cylon-sphero module installed.
First, let's load up Cylon:
var Cylon = require('cylon');
With that loaded, we can begin defining our robot.
Cylon.robot({
We're going to have one connection, and one device for this robot, both the same Sphero.
  connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
  device: { name: 'sphero', driver: 'sphero' },
With the necessary hardware defined, we can now start telling Cylon about the work our robot will be performing.
  work: function(me) {
We'll assign some variables here for later, a basic color for our sphero and a variable we'll use for a bitwise math operation later.
    var color = 0x00FF00,
        bitFilter = 0xFFFF00;
When our Sphero emits the 'connect' event, we're going to hook up collision detection, make sure it's not moving, and set a color.
    me.sphero.on('connect', function() {
      console.log("Setting up Collision Detection...");
      me.sphero.detectCollisions();
      me.sphero.setRGB(color);
      me.sphero.stop();
    });
And when our Sphero detects a collision, we want to notify the user of this via the console.
    me.sphero.on('collision', function(data) {
      console.log("Collision:");
We get the new color for a Sphero by doing a bitwise XOR operation on it, using the bitfilter above.
      color = color ^ bitFilter;
With our new color in hand, we can let the user know what color we're using now, and change the Sphero to that color. We'll also tell the Sphero to roll in a random direction, at speed 90.
      console.log("Color: " + (color.toString(16)) + " ");
      me.sphero.setRGB(color);
      me.sphero.roll(90, Math.floor(Math.random() * 360));
    });
  }
And with all that said and done, we can now start the robot.
}).start();

Code snippet :


var Cylon = require('cylon'); Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0' },
device: { name: 'sphero', driver: 'sphero' },
work: function(me) {
var color = 0x00FF00,
bitFilter = 0xFFFF00;
after((1).seconds(), function() {
console.log("Setting up Collision Detection...");
me.sphero.detectCollisions();
me.sphero.setRGB(color);
me.sphero.stop();
});
me.sphero.on('collision', function(data) {
console.log("Collision:");
color = color ^ bitFilter;
console.log("Color: " + (color.toString(16)) + " ");
me.sphero.setRGB(color);
me.sphero.roll(90, Math.floor(Math.random() * 360));
});
}
}).start();

Tuesday, July 8, 2014

Micro services

Micro services - everyone talks about them nowadays. There's no common understanding however what they are. I've been researching this topic for one of the new initiative currently we are working.  

Thanks to PAWEL PACANA for consolidating all the Articles and presentations related micro services. 

Link

Wednesday, May 21, 2014

longjohn

Using longjohn couldn't get any easier. You just need to require('longjohn') and you are all set. The magic that I mentioned above consists of replacing functions like setTimeout and nextTick with patched versions that keep track of the stack as it unfolds and as author suggests, this probably shouldn't be used in production.
MoreInfo
npm reference

Monday, May 19, 2014

NGINX as a WebSockets Proxy

The WebSocket protocol provides a way of creating web applications that support realtime bi-directional communications between clients and servers.  Part of HTML5, WebSockets makes it much easier to develop these types of applications then the methods previously available.  Most modern browsers support WebSockets including Firefox, Internet Explorer, Chrome, Safari and Opera and more and more server application frameworks are now supporting WebSockets as well.
For enterprise production use, where multiple WebSocket servers are needed for performance and high availability, a load balancing layer that understands the WebSocket protocol is required, and NGINX has supported WebSockets since NGINX 1.3 and can act as a reverse proxy and do load balancing of WebSocket applications.
The WebSocket protocol is different than the HTTP protocol, but the WebSocket handshake is compatible with HTTP, using the HTTP Upgrade facility to upgrade the connection from HTTP to WebSocket.  This allows WebSocket applications to more easily fit into existing infrastructures.  For example, WebSocket applications can use the standard HTTP ports 80 and 443, thus allowing the use of existing firewall rules.

Monday, May 5, 2014

Optimization killers in Node.js

This  link will contain advice to avoid writing code that will perform significantly worse than expected. Specifically those patterns that cause V8 (relevant to Node.JS, Opera, Chromium...) to refuse to optimize the affected function.

Some V8 background

In V8 there is no interpreter but there are 2 different compilers: generic and optimizing. That means your Javascript is always compiled and run directly as native code. That means it's fast, right? Wrong. Code being compiled to native code by itself isn't hugely significant for performance. It just removes interpreter overhead but the code will still be slow if not optimized.

Sunday, May 4, 2014

How-to: Heap Snapshots and Handling Node.js Memory Leaks

Node memory leaks happen. Usually they occur in production where processes experience the weight of their purpose and a mandate for up time. Thankfully, the core of Node is becoming more and more resilient to leaks. However, between our code and the modules we include, leaks still happen and it’s good to be prepared to handle them ...  More Info


Monday, April 14, 2014

Deep inside Node.js memory leak issue by TJ Fontaine

A few weeks ago, Eran Hammer of Walmart labs came to the Node.js core team complaining of a memory leak he had been tracking down for months. By expending a lot of effort over those few months he had taken the memory leak from a couple hundred megabytes a day, down to a mere eight megabytes a day. That in and of itself is an impressive feat, however, the source and final portion of the leak was elusive.

More info: http://www.joyent.com/blog/walmart-node-js-memory-leak