Created
December 19, 2014 00:03
-
-
Save dudleyjosh/14e4f2d72575550f7905 to your computer and use it in GitHub Desktop.
Code for a Tessel automated rotary welder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tessel = require('tessel'); | |
var led = require('tessel-led'); | |
var _ = require('underscore'); | |
// Setup the Relay module on Port A | |
var relaylib = require('relay-mono'); | |
var relay = relaylib.use(tessel.port['A']); | |
// Setup all switches & sensors to the GPIO | |
var gpio = tessel.port['GPIO']; | |
var swTurnTable = gpio.pin['G1']; | |
var btnWeld = gpio.pin['G2']; | |
var btnHome = gpio.pin['G3']; | |
var btnJog = gpio.pin['G6']; | |
// Add/Set 'Active' state for each GPIO object | |
swTurnTable.pressed = false; | |
btnWeld.pressed = false; | |
btnHome.pressed = false; | |
btnJog.pressed = false; | |
// Set pulldown switches/sensors (Tessel 'pull' functionality wasn't working when I did this project so I hardwired some physical pulldown resistors inline with my wiring) | |
//swTurnTable.pull('pulldown'); | |
//btnWeld.pull('pulldown'); | |
//btnHome.pull('pulldown'); | |
//btnJog.pull('pulldown'); | |
// Set all switches & sensors to input | |
swTurnTable.input(); | |
btnWeld.input(); | |
btnHome.input(); | |
btnJog.input(); | |
// underscore debounce function time | |
var timeDebounce = 100; | |
// Wait for the relay module to connect | |
relay.on('ready', function() { | |
console.log('Ready to weld...'); | |
// Check for btnWeld... then start welding | |
btnWeld.on('rise', _.debounce(function(time, type) { | |
if ( btnWeld.read() ) { | |
console.log('btnWeld was pressed!', type, time); | |
btnWeld.pressed = true; | |
weld(); | |
} | |
}, debounceTime)); | |
// Check for btnHome... then home the turn table w/o welding | |
btnHome.on('rise', _.debounce(function(time, type) { | |
if ( btnHome.read() ) { | |
console.log('btnHome was pressed!', type, time); | |
btnHome.pressed = true; | |
weld(); | |
} | |
}, timeDebounce)); | |
// Check for btnJog... then jog the turn table w/o welding | |
btnJog.on('change', _.debounce(function(time, type) { | |
if ( btnJog.read() ) { | |
console.log('btnJog was pressed!', type, time); | |
btnJog.pressed = true; | |
jog(); | |
} | |
if ( !btnJog.read() && btnJog.pressed ) { | |
console.log('btnJog was released!', type, time); | |
btnJog.pressed = false; | |
jog(); | |
} | |
}, timeDebounce)); | |
}); | |
// When a relay 'latch' state is changed, it emits the 'latch' event | |
relay.on('latch', function(channel, value) { | |
if ( channel == 1 ) { | |
console.log('latch on turn table relay (channel ' + channel + ') switched to', value); | |
} | |
if ( channel == 2 ) { | |
console.log('latch on welder relay (channel ' + channel + ') switched to', value); | |
} | |
}); | |
// Function to weld or home the turn table | |
function weld() { | |
swTurnTable.on('rise', _.debounce(function(time, type) { | |
if ( swTurnTable.read() && !swTurnTable.pressed ) { | |
swTurnTable.pressed = true; | |
console.log('on ' + type + ': ' + swTurnTable.read()); | |
swTurnTable.once('low', _.debounce(function(time, type) { | |
led.green.hide(); | |
led.blue.hide(); | |
console.log('once ' + type + ': ' + swTurnTable.read()); | |
if ( btnWeld.pressed || btnHome.pressed ) { | |
relay.turnOff(1, function(err) { | |
if (err) console.log('Err turning off turn table relay 1', err); | |
btnHome.pressed = false; | |
}); | |
} | |
if ( btnWeld.pressed ) { | |
relay.turnOff(2, function(err) { | |
if (err) console.log('Err turning off welder relay 2', err); | |
btnWeld.pressed = false; | |
}); | |
} | |
swTurnTable.removeAllListeners(); | |
swTurnTable.pressed = false; | |
}, timeDebounce)); | |
} | |
}, timeDebounce)); | |
if ( btnWeld.pressed || btnHome.pressed ) { | |
relay.turnOn(1, function(err) { | |
if ( err ) { | |
console.log('Err turning on turn table relay 1', err); | |
} | |
else { | |
if ( btnWeld.pressed ) { | |
led.green.show(); | |
} | |
if ( btnHome.pressed ) { | |
led.blue.show(); | |
} | |
} | |
}); | |
} | |
if ( btnWeld.pressed ) { | |
relay.turnOn(2, function(err) { | |
if ( err ) { | |
console.log('Err turning on welder relay 2', err); | |
} | |
}); | |
} | |
} | |
// Function to jog the weld turn table while btnJog is pressed | |
function jog() { | |
if ( btnJog.pressed ) { | |
relay.turnOn(1, function(err) { | |
if (err) console.log("Err turning on relay 1", err); | |
led.blue.show(); | |
}); | |
} | |
else { | |
relay.turnOff(1, function(err) { | |
if (err) console.log("Err turning off relay 1", err); | |
led.blue.hide(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment