RC Car JavaScript Code
window.app = app;
app.heading = 0;
app.beta = 0;
app.gamma = 0;
app.rc = {
// CONSTANTS
SCREEN_ID_GearP: 7828228,
SCREEN_ID_GearR: 7828240,
SCREEN_ID_GearN: 7828260,
SCREEN_ID_GearD: 7828548,
steeringCentre: 0,
currentHeading: 0,
currentSteering: “”,
currentSpeed: 0,
currentGear: “”,
apiSteering: “”,
apiSpeed: “”,
apiGear: “”,
valAPISteering: “”,
valAPISpeed: 0,
valAPIGear: “”,
accelerationX: 0,
accelerationY: 0,
accelerationZ: 0,
DeviceMotionSupported: false,
driveTimeout: 150,
updateAPITimeout: 1000,
watchDirectionTimeout: 150,
watchAPITimeout: 1000,
watchSpeedTimeout: 150,
log: “”,
apiViewDirection: ‘https://storage-extension.appshed.com/api/ffa59e04-5e59-11e5-9f41-12313b0e6970’,
apiSetDirection: “https://storage-extension.appshed.com/api/cfaf7e40-5e59-11e5-ae47-12313b0e6970”,
apiDeleteDirection: “https://storage-extension.appshed.com/api/d91ce742-5e59-11e5-a87d-12313b0e6970”,
apiViewSpeed: ‘https://storage-extension.appshed.com/api/6fb8c72c-5e5d-11e5-8179-12313b0e6970’,
apiSetSpeed: ‘https://storage-extension.appshed.com/api/61f938ce-5e5d-11e5-839b-12313b0e6970’,
apiDeleteSpeed: ‘https://storage-extension.appshed.com/api/68a00cfc-5e5d-11e5-a2bb-12313b0e6970’,
apiViewGear: ‘https://storage-extension.appshed.com/api/ac6b3cb0-66df-11e5-9337-12313b0e6970’,
apiSetGear: ‘https://storage-extension.appshed.com/api/9c1d5dd4-66df-11e5-95c6-12313b0e6970’,
apiDeleteGear: ‘https://storage-extension.appshed.com/api/a3ea8ce4-66df-11e5-a404-12313b0e6970’,
writelog: function(msg){
this.log = msg+”n”+this.log
app.setVariable(“log”,this.log)
},
startSteeringApp: function(){
this.watchMotion();
this.watchDirection();
this.updateAPIDirection();
alert(“Steering control has started.”)
setTimeout(“app.rc.calibrateSteering()”,2000);
},
startSpeedApp: function(){
this.watchSpeed();
this.updateAPISpeed();
alert(“Speed control has started.”)
},
startDrivingApp: function(){
this.watchAPIs();
this.drive();
},
watchMotion: function(){
window.addEventListener(‘deviceorientation’, function(eventData) {
app.heading = eventData.alpha
app.beta = eventData.beta
app.gamma = eventData.gamma
app.setVariable(“gamma”,eventData.gamma)
app.setVariable(“beta”,eventData.beta)
app.setVariable(“alpha”,eventData.alpha)
}, false);
if (window.DeviceMotionEvent != undefined) {
this.DeviceMotionSupported = true;
app.setVariable(“DeviceMotionSupported”,this.DeviceMotionSupported)
window.ondevicemotion = function(e) {
app.rc.accelerationX = e.accelerationIncludingGravity.x;
app.rc.accelerationY = e.accelerationIncludingGravity.y;
app.rc.accelerationZ = e.accelerationIncludingGravity.z;
app.setVariable(“accelerationX”,e.accelerationIncludingGravity.x);
app.setVariable(“accelerationY”,e.accelerationIncludingGravity.y);
app.setVariable(“accelerationZ”,e.accelerationIncludingGravity.z);
}
}
},
setDirection: function(x){
this.currentHeading = x;
app.setVariable(“fieldDirection”,x)
this.setCurrentSteering()
},
setSpeed: function(spd){
this.currentSpeed = spd;
app.setVariable(“currentSpeed”,spd);
},
setGear: function(gear,navigate){
this.currentGear = gear;
app.setVariable(“currentGear”,gear);
this.setAPIGear(gear)
if(navigate){
var id = app.rc[“SCREEN_ID_Gear”+gear]
if(id)
app.showScreen(id)
}
},
setSteering: function(steering){
this.currentSteering = steering
app.setVariable(“currentSteering”,steering)
},
calibrateSteering: function(){
this.steeringCentre = this.currentHeading
app.setVariable(“steeringCentre”,this.steeringCentre)
},
getAPIDirection: function(){
var response;
app.ajaxRequest(this.apiViewDirection, ‘get’, ‘json’, null, function(response) {
app.rc.apiSteering = response[0][‘direction’]
app.setVariable(“apiSteering”,app.rc.apiSteering)
return app.rc.apiSteering
})
},
getAPIGear: function(){
var response;
app.ajaxRequest(app.rc.apiViewGear, ‘get’, ‘json’, null, function(response) {
app.rc.apiGear = response[0][‘gear’]
app.setVariable(“apiGear”,app.rc.apiGear)
return app.rc.apiGear
})
},
getAPISpeed: function(){
var response;
app.ajaxRequest(this.apiViewSpeed, ‘get’, ‘json’, null, function(response) {
app.rc.apiSpeed = response[0][‘speed’]
app.setVariable(“apiSpeed”,app.rc.apiSpeed)
return app.rc.apiSpeed
})
},
setAPIDirection: function(drn){
// if the api value has not changed, don’t do anything
if(this.valAPISteering == drn)
return
this.valAPISteering = drn;
var response;
app.ajaxRequest(app.rc.apiDeleteDirection, ‘post’, ‘json’, null, function(response) {
app.ajaxRequest(app.rc.apiSetDirection, ‘post’, ‘json’, { “direction”:drn }, function(response) {
}, function(response) {
})
}, function(response) {
})
},
setAPIGear: function(gear){
// if the api value has not changed, don’t do anything
if(this.valAPIGear == gear)
return
this.valAPIGear = gear;
var response;
app.ajaxRequest(app.rc.apiDeleteGear, ‘post’, ‘json’, null, function(response) {
app.ajaxRequest(app.rc.apiSetGear, ‘post’, ‘json’, { “gear”:gear }, function(response) {
}, function(response) {})
}, function(response) {
})
},
setAPISpeed: function(spd){
// if the api value has not changed, don’t do anything
if(this.valAPISpeed == spd)
return
this.valAPISpeed = spd;
var response;
app.ajaxRequest(app.rc.apiDeleteSpeed, ‘post’, ‘json’, null, function(response) {
app.ajaxRequest(app.rc.apiSetSpeed, ‘post’, ‘json’, { “speed”:spd }, function(response) {
}, function(response) {
})
}, function(response) {
})
},
clearAPIs: function(){
var response;
app.ajaxRequest(app.rc.apiDeleteGear, ‘post’, ‘json’, null, function(response) {}, function(response) {})
app.ajaxRequest(app.rc.apiDeleteSpeed, ‘post’, ‘json’, null, function(response) {}, function(response) {})
app.ajaxRequest(app.rc.apiDeleteDirection, ‘post’, ‘json’, null, function(response) {}, function(response) {})
},
setCurrentSteering: function(){
if(this.DeviceMotionSupported)
this.setCurrentSteeringRoll()
else
this.setCurrentSteeringCompass()
},
setCurrentSteeringRoll: function(){
var x = this.accelerationX;
var steer = “”;
if(x>3)
steer = “left”
if(x>5)
steer = “hardleft”
if(x>7)
steer = “veryhardleft”
if(x<-3)
steer = “right”
if(x<-5)
steer = “hardright”
if(x<-7)
steer = “veryhardright”
this.setSteering(steer)
},
setCurrentSteeringCompass: function(){
var adjustedHeading;
var currentHeading = this.currentHeading;
var adjustment = 180 – this.steeringCentre
// if steeringCentre < 180, at some point it crosses 0 to 360
// work out by how much
if(this.steeringCentre < 180){
// values that go past 360 by this amount must be converted into negative
var adjustmentLimit = 360 – adjustment;
if(this.currentHeading > adjustmentLimit){
var distanceFromZero = 360 – this.currentHeading
currentHeading = 0 – distanceFromZero
}
adjustedHeading = currentHeading + adjustment
}
if(this.steeringCentre > 180){
var adjustmentLimit = 0 – adjustment;
if(this.currentHeading < adjustmentLimit){
currentHeading = 360 + currentHeading
}
adjustedHeading = currentHeading + adjustment
}
app.setVariable(“adjustedHeading”,adjustedHeading)
// work with 180 as the centre point
// if the adjustedHeading is from 200 to 260, turn left
// if from 160-100, turn right
if(adjustedHeading >= 200)
this.currentSteering = “left”
if(adjustedHeading >= 220)
this.currentSteering = “hardleft”
if(adjustedHeading >= 240)
this.currentSteering = “veryhardleft”
if(adjustedHeading <= 160)
this.currentSteering = “right”
if(adjustedHeading <= 140)
this.currentSteering = “hardright”
if(adjustedHeading <= 120)
this.currentSteering = “veryhardright”
if(adjustedHeading > 160 && adjustedHeading < 200)
this.currentSteering = “”
this.setSteering(this.currentSteering)
},
watchDirection: function(){
// only do this if using motiondriving
if(app.getVariable(“motiondriving”))
this.setDirection(app.heading);
setTimeout(“app.rc.watchDirection()”,this.watchDirectionTimeout)
},
watchSpeed: function(){
if(this.DeviceMotionSupported)
this.watchSpeedMotion()
else
this.watchSpeedCompass()
},
watchSpeedMotion: function(){
var speed = 0;
var y = this.accelerationY;
if(y < 5)
speed = 20
if(y < 4)
speed = 40
if(y < 3)
speed = 60
if(y < 2)
speed = 80
if(y < 1)
speed = 100
if(app.getVariable(“motiondriving”))
this.setSpeed(speed)
setTimeout(“app.rc.watchSpeed()”,this.watchSpeedTimeout)
},
watchSpeedCompass: function(){
var beta = app.beta
if(beta < 0) beta = 0;
if(beta > 30) beta = 30;
var speed = (30 – beta)*3.3333
if(app.getVariable(“motiondriving”))
this.setSpeed(speed)
setTimeout(“app.rc.watchSpeed()”,this.watchSpeedTimeout)
},
watchAPIs: function(){
this.getAPIDirection()
this.getAPISpeed()
this.getAPIGear()
setTimeout(“app.rc.watchAPIs()”,this.watchAPITimeout)
},
updateAPIDirection: function(){
this.setAPIDirection(this.currentSteering);
setTimeout(“app.rc.updateAPIDirection()”,this.updateAPITimeout)
},
updateAPISpeed: function(){
this.setAPISpeed(this.currentSpeed)
setTimeout(“app.rc.updateAPISpeed()”,this.updateAPITimeout)
},
move: function(speed,gear){
if(isNaN(speed))
speed = 100;
app.setPin(“pwma”,speed)
app.setPin(“pwmb”,speed)
if(gear == “R”){
app.setPin(“a1a”,true)
app.setPin(“a1b”,false);
app.setPin(“b1a”,true)
app.setPin(“b1b”,false);
} else if(gear == “D”) {
app.setPin(“a1a”,false)
app.setPin(“a1b”,true);
app.setPin(“b1a”,false)
app.setPin(“b1b”,true);
} else {
app.setPin(“a1a”,false)
app.setPin(“a1b”,false);
app.setPin(“b1a”,false)
app.setPin(“b1b”,false);
}
},
stop: function(){
app.setPin(“a1a”,false)
app.setPin(“a1b”,false);
app.setPin(“b1a”,false)
app.setPin(“b1b”,false);
},
turn: function(direction,speed,gear){
if(!gear)
gear = “D”
if(direction == “R”){
app.setPin(“pwma”,0)
app.setPin(“pwmb”,speed)
app.setPin(“a1a”,false)
app.setPin(“a1b”,false);
if(gear == “D”){
app.setPin(“b1a”,false)
app.setPin(“b1b”,true);
} else {
app.setPin(“b1a”,true)
app.setPin(“b1b”,false);
}
}
if(direction == “L”){
app.setPin(“pwma”,speed)
app.setPin(“pwmb”,0)
if(gear == “D”){
app.setPin(“a1a”,false)
app.setPin(“a1b”,true);
} else {
app.setPin(“a1a”,true)
app.setPin(“a1b”,false);
}
app.setPin(“b1a”,false)
app.setPin(“b1b”,false);
}
},
drive: function(){
var steering = this.currentSteering;
var speed = this.currentSpeed;
var gear = this.currentGear;
if(app.getVariable(“apiMode”) == “1”){
steering = this.apiSteering;
speed = this.apiSpeed;
gear = this.apiGear
}
if(app.getVariable(“pwm”) != “1”){
if(speed > 50)
speed = 100;
else
speed = 0;
}
if(gear == “” || speed == 0)
this.stop();
else if(String(steering).match(/left/))
this.turn(“L”,speed, gear)
else if(String(steering).match(/right/))
this.turn(“R”,speed, gear)
else if(speed > “”)
this.move(speed,gear);
setTimeout(“app.rc.drive()”,this.driveTimeout)
},
control: function(gear, speed, steer){
this.setGear(gear)
this.setSpeed(speed)
this.setSteering(steer)
}
}