Arduino Drone 0.01

A very early implementation of what possible things the Arduino drone will need to do / be aware of and how to react. Note: You'll need an understanding of physics to calculate velocities, speeds, altitudes and all that.

by Sam Fereday

JavaScript

// Just some world variables
var world = {
	floor: {
    	x: 0,
        y: 10
    }
};

// Drone Core
var Drone = function(){};
Drone.prototype = {
	modules: [],
   	powerCommitment: 0,
    registerModule: function(Cls) {
        var newMod = new Cls();
        newMod.init(this);
        this.powerCommitment += newMod.powerCost;
        if(typeof newMod.update !== "function") throw "Module requires an update method.";
    	this.modules.push(newMod);
        return this;
    },
    update: function() {
    	_.each(this.modules, function(mod){
            mod.update();
        });
    },
    tell: function(evstr) {
        switch(evstr){
            case "lowpower":
            this.process("land");
            break;
            case "landed":
            this.process("charge");
            break;
        }
    },
    process: function(evstr) {
    	_.each(this.modules, function(mod){
            if(mod.recognizes(evstr)) mod.perform(evstr);
        });
    },
    getStatus: function() {
     	// ...   
    }
};

// Modules
var Mod_Navigation = function(){};
Mod_Navigation.prototype = {
    x: 0,
    y: 250,
    cmPerSecond: 2,
    onFloor: false,
    powerCost: 10,
    init: function(h){
    	this.host = h;
    },
	update: function(){
    	// ...
    },
    recognizes: function(ev){
    	if(typeof this[ev] === "function") return true;
    },
    perform: function(fstr) {
        // Not sure if this is really allowed.
     	(this[fstr])();
    },
    // Custom methods belonging to this module
    land: function(){
        this.onFloor = false;
        if(this.y > world.floor.y) {
        	this.y -= this.cmPerSecond;
        } else {
            this.onFloor = true;
            this.host.tell("landed");
        }
    }
};

var Mod_Solar = function(){};
Mod_Solar.prototype = {
    charged: true,
    host: null,
    powerCost: 10,
    currentCharge: 0,
    initialCharge: 1000,
    unitsPerSecond: 0,
    init: function(h){
    	this.host = h;
       ...