Skip to content

Instantly share code, notes, and snippets.

@zankich
Created May 16, 2014 23:04
Show Gist options
  • Save zankich/911ea6e63f7366b09b48 to your computer and use it in GitHub Desktop.
Save zankich/911ea6e63f7366b09b48 to your computer and use it in GitHub Desktop.
var Cylon = require('cylon');
var rest = require('restler');
var haarcascade = "" + __dirname + "/haarcascade_frontalface_alt.xml";
var DroneRobot = (function(){
function DroneRobot() {}
DroneRobot.prototype.commands = ["faceOff"];
DroneRobot.prototype.connections = [
{ name: 'opencv', adaptor: 'opencv' },
{ name: 'ardrone', adaptor: 'ardrone', port: '192.168.1.1' }
];
DroneRobot.prototype.devices = [
{ name: 'drone', driver: 'ardrone', connection: 'ardrone' },
{ name: 'window', driver: 'window', conneciton: 'opencv' },
{ name: 'mat', driver: 'mat', conneciton: 'opencv' }
];
DroneRobot.prototype.work = function(my) {
console.log("SHOW ME YOUR FACE!");
};
DroneRobot.prototype.faceOff = function() {
var my = this;
var self = this;
self.detect = false;
self.image = null;
my.drone.getPngStream().on('data', function(png) {
my.mat.readImage(png, function(err, img) {
self.image = img;
if (self.detect === false) {
my.window.show(img);
}
});
});
my.mat.on('facesDetected', function(err, im, faces) {
var biggest, center_x, f, face, turn;
biggest = 0;
face = null;
for (var i = 0; i < faces.length; i++) {
f = faces[i];
if (f.width > biggest) {
biggest = f.width;
face = f;
}
}
if (face !== null && (face.width <= 100 && face.width >= 45)) {
rest.get('http://192.168.1.1:3000/robots/digispark/devices/led/commands/ToggleC').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
}
});
rest.get('http://127.0.0.1:3001/robots/mod/commands/WakeUp').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
}
});
im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height],
[0, 255, 0], 2);
center_x = im.width() * 0.5;
turn = -(face.x - center_x) / center_x;
console.log("turning: " + turn);
if (turn < 0) {
my.drone.clockwise(Math.abs(turn * 0.7));
} else {
my.drone.counterClockwise(Math.abs(turn * 0.7));
}
} else {
rest.get('http://192.168.1.1:3000/robots/digispark/devices/led/commands/OffC').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
}
});
rest.get('http://127.0.0.1:3001/robots/mod/commands/Snooze').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result.message);
}
});
}
my.window.show(im);
});
my.drone.takeoff();
after((8).seconds(), function() { my.drone.up(0.5); });
after((10).seconds(), function() { my.drone.hover(); });
after((13).seconds(), function() {
self.detect = true;
every(0.3.seconds(), function() {
my.drone.hover();
my.mat.detectFaces(self.image, haarcascade);
});
after((30).seconds(), function() { my.drone.land(); });
});
};
return DroneRobot;
})();
var robot = new DroneRobot;
robot.name = "drone";
Cylon.robot(robot);
Cylon.api({ host: '192.168.1.3', port: '3000' });
Cylon.start();
package main
import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-digispark"
"github.com/hybridgroup/gobot-gpio"
)
func main() {
master := gobot.GobotMaster()
gobot.Api(master)
digispark := new(gobotDigispark.DigisparkAdaptor)
digispark.Name = "digispark"
led := gobotGPIO.NewLed(digispark)
led.Name = "led"
led.Pin = "2"
master.Robots = append(master.Robots, &gobot.Robot{
Name: "digispark",
Connections: []gobot.Connection{digispark},
Devices: []gobot.Device{red},
})
master.Start()
}
require 'artoo'
require 'http'
connection :pebble, :adaptor => :pebble
device :watch, :driver => :pebble, :name => 'pebble'
api :host => '0.0.0.0', :port => '8080'
name 'pebble'
def button_push(*data)
unless data[1].nil?
puts "Posting to Cylon"
HTTP[:content_type => 'application/json'].get "https://192.168.1.3:3000/robots/drone/commands/faceOff"
p "#{data[1]} button pushed"
end
end
work do
pebble.send_notification("Hello Pebble!")
on pebble, :button => :button_push
end
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot-sphero"
)
var master *gobot.Master
func WakeUp(params map[string]interface{}) bool {
devices := master.FindRobot("mod").GetDevices()
for _, device := range devices {
gobot.Call(device.Driver, "SetRGB", uint8(gobot.Rand(255)), uint8(gobot.Rand(255)), uint8(gobot.Rand(255)))
gobot.Call(device.Driver, "Roll", uint8(70), uint16(gobot.Rand(360)))
}
return true
}
func Snooze(params map[string]interface{}) bool {
devices := master.FindRobot("mod").GetDevices()
for _, device := range devices {
gobot.Call(device.Driver, "SetRGB", uint8(255), uint8(0), uint8(0))
gobot.Call(device.Driver, "Roll", uint8(0), uint16(0))
}
return true
}
func main() {
master = gobot.GobotMaster()
a := gobot.Api(master)
a.Port = "3001"
spheros := map[string]string{
"/dev/rfcomm0": "Thelma",
"/dev/rfcomm1": "Louise",
"/dev/rfcomm2": "Grace",
"/dev/rfcomm3": "Ada",
}
connections := make([]gobot.Connection, 0)
devices := make([]gobot.Device, 0)
for port, name := range spheros {
spheroAdaptor := new(gobotSphero.SpheroAdaptor)
spheroAdaptor.Name = "Sphero"
spheroAdaptor.Port = port
sphero := gobotSphero.NewSphero(spheroAdaptor)
sphero.Name = name
connections = append(connections, spheroAdaptor)
devices = append(devices, sphero)
}
mod := new(gobot.Robot)
mod.Name = "mod"
mod.Commands = map[string]interface{}{"WakeUp": WakeUp, "Snooze": Snooze}
mod.Connections = connections
mod.Devices = devices
mod.Work = func() {
fmt.Println("Ready!")
}
master.Robots = append(master.Robots, mod)
master.Start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment