Skip to content

Instantly share code, notes, and snippets.

@grantges
Created June 23, 2016 22:50
Show Gist options
  • Save grantges/cc02a6c771773b15c7d8b40343372950 to your computer and use it in GitHub Desktop.
Save grantges/cc02a6c771773b15c7d8b40343372950 to your computer and use it in GitHub Desktop.
An Appcelerator Hyperloop module (Android) for working with the Sphere robot
var ConvenienceRobot = require('com.orbotix.ConvenienceRobot');
var DualStackDiscoveryAgent = require('com.orbotix.DualStackDiscoveryAgent');
var Robot = require('com.orbotix.common.Robot');
var DiscoveryException = require('com.orbotix.common.DiscoveryException');
var RobotChangedStateListener = require('com.orbotix.common.RobotChangedStateListener');
var RobotLE = require('com.orbotix.le.RobotLE');
var Context = require('android.content.Context');
var DeviceSensorAsyncMessage = require('com.orbotix.async.DeviceSensorAsyncMessage');
var ConfigureLocatorCommand = require('com.orbotix.command.ConfigureLocatorCommand');
var ResponseListener = require('com.orbotix.common.ResponseListener');
var DeviceResponse = require('com.orbotix.common.internal.DeviceResponse');
var AsyncMessage = require('com.orbotix.common.internal.AsyncMessage');
var SensorFlag = require('com.orbotix.common.sensor.SensorFlag');
var SensorControl = require('com.orbotix.subsystem.SensorControl');
var Activity = require('android.app.Activity');
var Sphero = {
robot: null,
currentHeading: 0,
currentVelocity: 0,
isDriving: false,
connectedCallback: null,
finishedCallback: null,
init: function _init(connectedCallback, finishedCallback){
Sphero.connectedCallback = connectedCallback;
Sphero.finishedCallback = finishedCallback;
DualStackDiscoveryAgent.getInstance().addRobotStateListener(new RobotChangedStateListener({
handleRobotChangedState: function (robot, type) {
if (type == 'Online') {
Ti.API.info('Connected');
Sphero.connectedCallback && Sphero.connectedCallback();
Sphero.robot = new ConvenienceRobot(robot);
var sensorFlag = SensorFlag.LOCATOR.longValue();
Sphero.robot.enableSensors(sensorFlag, SensorControl.StreamingRate.STREAMING_RATE10);
Sphero.configureLocator();
/** Setup Response Listener for Feedback Messaging **/
Sphero.robot.addResponseListener(new ResponseListener({
handleAsyncMessage: function (asyncMessage, robot) {
}
}));
}
}
}));
},
startDiscovery: function _startDiscovery(currentActivity) {
var activity = currentActivity || new Activity(Ti.Android.currentActivity);
//If the DiscoveryAgent is not already looking for robots, start discovery.
if( !DualStackDiscoveryAgent.getInstance().isDiscovering() ) {
DualStackDiscoveryAgent.getInstance().startDiscovery(activity);
}
},
configureLocator: function _configureLocator() {
//flag, x, y, z
Sphero.robot && Sphero.robot.sendCommand(new ConfigureLocatorCommand(1, 0, 0, 0));
},
forward: function _moveForward() {
console.log("_moveForward");
Sphero.currentVelocity = Sphero.currentVelocity+.1;
if(Sphero.currentVelocity >=1){
Sphero.currentVelocity = 1;
}
console.log(Sphero.currentVelocity);
Sphero.robot && Sphero.robot.drive(Sphero.currentHeading, Sphero.currentVelocity);
Sphero.isDriving = true;
},
right: function _moveRight() {
console.log("_moveRight");
Sphero.currentHeading+=20;
Sphero.robot && Sphero.robot.drive( Sphero.currentHeading, Sphero.currentVelocity);
Sphero.isDriving = true;
},
reverse: function _moveReverse() {
console.log("_moveReverse");
Sphero.currentVelocity = Sphero.currentVelocity-.1;
Sphero.robot && Sphero.robot.drive( Sphero.currentHeading, Sphero.currentVelocity);
Sphero.isDriving = true;
},
left: function _moveLeft(){
console.log("_moveLeft");
Sphero.currentHeading-=20;
Sphero.robot && Sphero.robot.drive( Sphero.currentHeading, Sphero.currentVelocity)
Sphero.isDriving = true;
},
stop: function _stop(){
console.log("_stop");
Sphero.robot && Sphero.robot.stop();
Sphero.isDriving = false;
}
}
module.exports = Sphero;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment