1 /*jslint node:true, vars:true, bitwise:true, unparam:true */ 2 /*jshint unused:true */ 3 4 /* 5 * Author: Zion Orent <zorent (at) ics.com> 6 * Copyright (c) 2015 Intel Corporation. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining 9 * a copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sublicense, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 28 var groveMotorDriver_lib = require('jsupm_grovemd'); 29 30 function start() 31 { 32 if (my_MotorDriver_obj) 33 { 34 // set direction to CW and set speed to 50% 35 console.log("Spin M1 and M2 at half speed for 3 seconds"); 36 my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CW, 37 groveMotorDriver_lib.GroveMD.DIR_CW); 38 my_MotorDriver_obj.setMotorSpeeds(127, 127); 39 } 40 } 41 42 function reverse() 43 { 44 if (my_MotorDriver_obj) 45 { 46 // counter clockwise 47 console.log("Reversing M1 and M2 for 3 seconds"); 48 my_MotorDriver_obj.setMotorDirections(groveMotorDriver_lib.GroveMD.DIR_CCW, 49 groveMotorDriver_lib.GroveMD.DIR_CCW); 50 } 51 } 52 53 function end() 54 { 55 if (my_MotorDriver_obj) 56 { 57 console.log("Stopping motors"); 58 my_MotorDriver_obj.setMotorSpeeds(0, 0); 59 } 60 exit(); 61 } 62 63 // When exiting: clear memory and print exit message 64 function exit() 65 { 66 if (my_MotorDriver_obj) 67 { 68 my_MotorDriver_obj = null; 69 groveMotorDriver_lib.cleanUp(); 70 } 71 groveMotorDriver_lib = null; 72 console.log("Exiting"); 73 process.exit(0); 74 } 75 76 77 // Instantiate an I2C Grove Motor Driver on I2C bus 0 78 var my_MotorDriver_obj = new groveMotorDriver_lib.GroveMD( 79 groveMotorDriver_lib.GROVEMD_I2C_BUS, 80 groveMotorDriver_lib.GROVEMD_DEFAULT_I2C_ADDR); 81 82 start(); 83 84 setTimeout(function() 85 { 86 reverse(); 87 setTimeout(end, 3000); 88 }, 3000); 89 90 91 process.on('SIGINT', function() 92 { 93 exit(); 94 }); 95