No Description

l298-stepper.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*jslint node:true, vars:true, bitwise:true, unparam:true */
  2. /*jshint unused:true */
  3. /*
  4. * Author: Zion Orent <zorent@ics.com>
  5. * Copyright (c) 2015 Intel Corporation.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. var HBridge_lib = require('jsupm_l298');
  27. // Instantiate a Stepper motor on a L298 Dual H-Bridge.
  28. // This was tested with the NEMA-17 12V, 350mA, with 200 steps per rev.
  29. var myHBridge_obj = new HBridge_lib.L298(200, 3, 4, 7, 8, 9);
  30. /**************************************
  31. * Instantiate H-bridge stepper object
  32. ***************************************/
  33. myHBridge_obj.goForward = function()
  34. {
  35. myHBridge_obj.setSpeed(10); // 10 RPMs
  36. myHBridge_obj.setDirection(HBridge_lib.L298.DIR_CW);
  37. myHBridge_obj.enable(true);
  38. console.log("Rotating 1 full revolution at 10 RPM speed.");
  39. // move 200 steps, a full rev
  40. myHBridge_obj.stepperSteps(200);
  41. };
  42. myHBridge_obj.reverseDirection = function()
  43. {
  44. console.log("Rotating 1/2 revolution in opposite direction at 10 RPM speed.");
  45. myHBridge_obj.setDirection(HBridge_lib.L298.DIR_CCW);
  46. myHBridge_obj.stepperSteps(100);
  47. };
  48. myHBridge_obj.stop = function()
  49. {
  50. myHBridge_obj.enable(false);
  51. };
  52. myHBridge_obj.quit = function()
  53. {
  54. myHBridge_obj = null;
  55. HBridge_lib.cleanUp();
  56. HBridge_lib = null;
  57. console.log("Exiting");
  58. process.exit(0);
  59. };
  60. /************************
  61. * Run H-bridge stepper!
  62. *************************/
  63. myHBridge_obj.goForward();
  64. setTimeout(myHBridge_obj.reverseDirection, 2000);
  65. setTimeout(function()
  66. {
  67. myHBridge_obj.stop();
  68. myHBridge_obj.quit();
  69. }, 4000);