No Description

adafruitms1438-stepper.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/python
  2. # Author: Zion Orent <zorent@ics.com>
  3. # Copyright (c) 2015 Intel Corporation.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining
  6. # a copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish,
  9. # distribute, sublicense, and/or sell copies of the Software, and to
  10. # permit persons to whom the Software is furnished to do so, subject to
  11. # the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. import time, sys, signal, atexit
  24. import pyupm_adafruitms1438 as upmAdafruitms1438
  25. # Import header values
  26. I2CBus = upmAdafruitms1438.ADAFRUITMS1438_I2C_BUS
  27. I2CAddr = upmAdafruitms1438.ADAFRUITMS1438_DEFAULT_I2C_ADDR
  28. M12Motor = upmAdafruitms1438.AdafruitMS1438.STEPMOTOR_M12
  29. MotorDirCW = upmAdafruitms1438.AdafruitMS1438.DIR_CW
  30. MotorDirCCW = upmAdafruitms1438.AdafruitMS1438.DIR_CCW
  31. # Instantiate an Adafruit MS 1438 on I2C bus 0
  32. myMotorShield = upmAdafruitms1438.AdafruitMS1438(I2CBus, I2CAddr)
  33. ## Exit handlers ##
  34. # This stops python from printing a stacktrace when you hit control-C
  35. def SIGINTHandler(signum, frame):
  36. raise SystemExit
  37. # This function lets you run code on exit,
  38. # including functions from myMotorShield
  39. def exitHandler():
  40. myMotorShield.disableStepper(M12Motor)
  41. print "Exiting"
  42. sys.exit(0)
  43. # Register exit handlers
  44. atexit.register(exitHandler)
  45. signal.signal(signal.SIGINT, SIGINTHandler)
  46. # Setup for use with a stepper motor connected to the M1 & M2 ports
  47. # set a PWM period of 50Hz
  48. # disable first, to be safe
  49. myMotorShield.disableStepper(M12Motor)
  50. # configure for a NEMA-17, 200 steps per revolution
  51. myMotorShield.stepConfig(M12Motor, 200)
  52. # set speed at 10 RPM's
  53. myMotorShield.setStepperSpeed(M12Motor, 10);
  54. myMotorShield.setStepperDirection(M12Motor, MotorDirCW)
  55. # enable
  56. print "Enabling..."
  57. myMotorShield.enableStepper(M12Motor)
  58. print "Rotating 1 full revolution at 10 RPM speed."
  59. myMotorShield.stepperSteps(M12Motor, 200)
  60. print "Sleeping for 2 seconds..."
  61. time.sleep(2)
  62. print "Rotating 1/2 revolution in opposite direction at 10 RPM speed."
  63. myMotorShield.setStepperDirection(M12Motor, MotorDirCCW)
  64. myMotorShield.stepperSteps(M12Motor, 100)
  65. print "Disabling..."
  66. # exitHandler runs automatically