Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. var tessel = require('tessel');
  2. var led = require('tessel-led');
  3. var _ = require('underscore');
  4.  
  5. // Setup the Relay module on Port A
  6. var relaylib = require('relay-mono');
  7. var relay = relaylib.use(tessel.port['A']);
  8.  
  9. // Setup all switches & sensors to the GPIO
  10. var gpio = tessel.port['GPIO'];
  11. var swTurnTable = gpio.pin['G1'];
  12. var btnWeld = gpio.pin['G2'];
  13. var btnHome = gpio.pin['G3'];
  14. var btnJog = gpio.pin['G6'];
  15.  
  16. // Add/Set 'Active' state for each GPIO object
  17. swTurnTable.pressed = false;
  18. btnWeld.pressed = false;
  19. btnHome.pressed = false;
  20. btnJog.pressed = false;
  21.  
  22. // 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)
  23. //swTurnTable.pull('pulldown');
  24. //btnWeld.pull('pulldown');
  25. //btnHome.pull('pulldown');
  26. //btnJog.pull('pulldown');
  27.  
  28. // Set all switches & sensors to input
  29. swTurnTable.input();
  30. btnWeld.input();
  31. btnHome.input();
  32. btnJog.input();
  33.  
  34. // underscore debounce function time
  35. var timeDebounce = 100;
  36.  
  37. // Wait for the relay module to connect
  38. relay.on('ready', function() {
  39.  
  40. console.log('Ready to weld...');
  41.  
  42. // Check for btnWeld... then start welding
  43. btnWeld.on('rise', _.debounce(function(time, type) {
  44. if ( btnWeld.read() ) {
  45. console.log('btnWeld was pressed!', type, time);
  46. btnWeld.pressed = true;
  47. weld();
  48. }
  49. }, debounceTime));
  50.  
  51. // Check for btnHome... then home the turn table w/o welding
  52. btnHome.on('rise', _.debounce(function(time, type) {
  53. if ( btnHome.read() ) {
  54. console.log('btnHome was pressed!', type, time);
  55. btnHome.pressed = true;
  56. weld();
  57. }
  58. }, timeDebounce));
  59.  
  60. // Check for btnJog... then jog the turn table w/o welding
  61. btnJog.on('change', _.debounce(function(time, type) {
  62. if ( btnJog.read() ) {
  63. console.log('btnJog was pressed!', type, time);
  64. btnJog.pressed = true;
  65. jog();
  66. }
  67. if ( !btnJog.read() && btnJog.pressed ) {
  68. console.log('btnJog was released!', type, time);
  69. btnJog.pressed = false;
  70. jog();
  71. }
  72. }, timeDebounce));
  73.  
  74. });
  75.  
  76. // When a relay 'latch' state is changed, it emits the 'latch' event
  77. relay.on('latch', function(channel, value) {
  78. if ( channel == 1 ) {
  79. console.log('latch on turn table relay (channel ' + channel + ') switched to', value);
  80. }
  81. if ( channel == 2 ) {
  82. console.log('latch on welder relay (channel ' + channel + ') switched to', value);
  83. }
  84. });
  85.  
  86.  
  87. // Function to weld or home the turn table
  88. function weld() {
  89.  
  90. swTurnTable.on('rise', _.debounce(function(time, type) {
  91.  
  92. if ( swTurnTable.read() && !swTurnTable.pressed ) {
  93.  
  94. swTurnTable.pressed = true;
  95.  
  96. console.log('on ' + type + ': ' + swTurnTable.read());
  97.  
  98. swTurnTable.once('low', _.debounce(function(time, type) {
  99.  
  100. led.green.hide();
  101. led.blue.hide();
  102.  
  103. console.log('once ' + type + ': ' + swTurnTable.read());
  104.  
  105. if ( btnWeld.pressed || btnHome.pressed ) {
  106. relay.turnOff(1, function(err) {
  107. if (err) console.log('Err turning off turn table relay 1', err);
  108. btnHome.pressed = false;
  109. });
  110. }
  111. if ( btnWeld.pressed ) {
  112. relay.turnOff(2, function(err) {
  113. if (err) console.log('Err turning off welder relay 2', err);
  114. btnWeld.pressed = false;
  115. });
  116. }
  117.  
  118. swTurnTable.removeAllListeners();
  119. swTurnTable.pressed = false;
  120.  
  121. }, timeDebounce));
  122.  
  123. }
  124.  
  125. }, timeDebounce));
  126.  
  127. if ( btnWeld.pressed || btnHome.pressed ) {
  128. relay.turnOn(1, function(err) {
  129. if ( err ) {
  130. console.log('Err turning on turn table relay 1', err);
  131. }
  132. else {
  133. if ( btnWeld.pressed ) {
  134. led.green.show();
  135. }
  136. if ( btnHome.pressed ) {
  137. led.blue.show();
  138. }
  139. }
  140. });
  141. }
  142.  
  143. if ( btnWeld.pressed ) {
  144. relay.turnOn(2, function(err) {
  145. if ( err ) {
  146. console.log('Err turning on welder relay 2', err);
  147. }
  148. });
  149. }
  150.  
  151. }
  152.  
  153.  
  154. // Function to jog the weld turn table while btnJog is pressed
  155. function jog() {
  156. if ( btnJog.pressed ) {
  157. relay.turnOn(1, function(err) {
  158. if (err) console.log("Err turning on relay 1", err);
  159. led.blue.show();
  160. });
  161. }
  162. else {
  163. relay.turnOff(1, function(err) {
  164. if (err) console.log("Err turning off relay 1", err);
  165. led.blue.hide();
  166. });
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement