Geen omschrijving

adafruitms1438-stepper.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. function exit()
  27. {
  28. console.log("Exiting");
  29. myMotorShield_obj = null;
  30. if (MotorShield_lib)
  31. {
  32. MotorShield_lib.cleanUp();
  33. MotorShield_lib = null;
  34. }
  35. process.exit(0);
  36. }
  37. var MotorShield_lib = require('jsupm_adafruitms1438');
  38. /* Import header values */
  39. var I2CBus = MotorShield_lib.ADAFRUITMS1438_I2C_BUS;
  40. var I2CAddr = MotorShield_lib.ADAFRUITMS1438_DEFAULT_I2C_ADDR;
  41. var M12motor = MotorShield_lib.AdafruitMS1438.STEPMOTOR_M12;
  42. var MotorDirCW = MotorShield_lib.AdafruitMS1438.DIR_CW;
  43. var MotorDirCCW = MotorShield_lib.AdafruitMS1438.DIR_CCW;
  44. // Instantiate an Adafruit MS 1438 on I2C bus 0
  45. var myMotorShield_obj = new MotorShield_lib.AdafruitMS1438(I2CBus, I2CAddr);
  46. // Setup for use with a stepper motor connected to the M1 & M2 ports
  47. // disable first, to be safe
  48. myMotorShield_obj.disableStepper(M12motor);
  49. // configure for a NEMA-17, 200 steps per revolution
  50. myMotorShield_obj.stepConfig(M12motor, 200);
  51. // set speed at 10 RPM's
  52. myMotorShield_obj.setStepperSpeed(M12motor, 10);
  53. myMotorShield_obj.setStepperDirection(M12motor, MotorDirCW);
  54. console.log("Enabling...");
  55. myMotorShield_obj.enableStepper(M12motor);
  56. console.log("Rotating 1 full revolution at 10 RPM speed.");
  57. myMotorShield_obj.stepperSteps(M12motor, 200);
  58. console.log("Sleeping for 2 seconds...");
  59. setTimeout(function()
  60. {
  61. console.log("Rotating 1/2 revolution in opposite direction at 10 RPM speed.");
  62. myMotorShield_obj.setStepperDirection(M12motor, MotorDirCCW);
  63. myMotorShield_obj.stepperSteps(M12motor, 100);
  64. console.log("Disabling...");
  65. myMotorShield_obj.disableStepper(M12motor);
  66. exit();
  67. }, 2000);
  68. process.on('SIGINT', function()
  69. {
  70. exit();
  71. });