No Description

grovemd.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 groveMotorDriver_lib = require('jsupm_grovemd');
  27. function start()
  28. {
  29. if (my_MotorDriver_obj)
  30. {
  31. // set direction to CW and set speed to 50%
  32. console.log("Spin M1 and M2 at half speed for 3 seconds");
  33. my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CW,
  34. groveMotorDriver_lib.GroveMD.DIR_CW);
  35. my_MotorDriver_obj.setMotorSpeeds(127, 127);
  36. }
  37. }
  38. function reverse()
  39. {
  40. if (my_MotorDriver_obj)
  41. {
  42. // counter clockwise
  43. console.log("Reversing M1 and M2 for 3 seconds");
  44. my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CCW,
  45. groveMotorDriver_lib.GroveMD.DIR_CCW);
  46. }
  47. }
  48. function end()
  49. {
  50. if (my_MotorDriver_obj)
  51. {
  52. console.log("Stopping motors");
  53. my_MotorDriver_obj.setMotorSpeeds(0, 0);
  54. }
  55. exit();
  56. }
  57. // When exiting: clear memory and print exit message
  58. function exit()
  59. {
  60. if (my_MotorDriver_obj)
  61. {
  62. my_MotorDriver_obj = null;
  63. groveMotorDriver_lib.cleanUp();
  64. }
  65. groveMotorDriver_lib = null;
  66. console.log("Exiting");
  67. process.exit(0);
  68. }
  69. // Instantiate an I2C Grove Motor Driver on I2C bus 0
  70. var my_MotorDriver_obj = new groveMotorDriver_lib.GroveMD(
  71. groveMotorDriver_lib.GROVEMD_I2C_BUS,
  72. groveMotorDriver_lib.GROVEMD_DEFAULT_I2C_ADDR);
  73. start();
  74. setTimeout(function()
  75. {
  76. reverse();
  77. setTimeout(end, 3000);
  78. }, 3000);
  79. process.on('SIGINT', function()
  80. {
  81. exit();
  82. });