servo.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Controlling hobby servo motors
  2. ==============================
  3. There are 4 dedicated connection points on the pyboard for connecting up
  4. hobby servo motors (see eg
  5. [Wikipedia](http://en.wikipedia.org/wiki/Servo_%28radio_control%29)).
  6. These motors have 3 wires: ground, power and signal. On the pyboard you
  7. can connect them in the bottom right corner, with the signal pin on the
  8. far right. Pins X1, X2, X3 and X4 are the 4 dedicated servo signal pins.
  9. .. image:: img/pyboard_servo.jpg
  10. In this picture there are male-male double adaptors to connect the servos
  11. to the header pins on the pyboard.
  12. The ground wire on a servo is usually the darkest coloured one, either
  13. black or dark brown. The power wire will most likely be red.
  14. The power pin for the servos (labelled VIN) is connected directly to the
  15. input power source of the pyboard. When powered via USB, VIN is powered
  16. through a diode by the 5V USB power line. Connect to USB, the pyboard can
  17. power at least 4 small to medium sized servo motors.
  18. If using a battery to power the pyboard and run servo motors, make sure it
  19. is not greater than 6V, since this is the maximum voltage most servo motors
  20. can take. (Some motors take only up to 4.8V, so check what type you are
  21. using.)
  22. Creating a Servo object
  23. -----------------------
  24. Plug in a servo to position 1 (the one with pin X1) and create a servo object
  25. using::
  26. >>> servo1 = pyb.Servo(1)
  27. To change the angle of the servo use the ``angle`` method::
  28. >>> servo1.angle(45)
  29. >>> servo1.angle(-60)
  30. The angle here is measured in degrees, and ranges from about -90 to +90,
  31. depending on the motor. Calling ``angle`` without parameters will return
  32. the current angle::
  33. >>> servo1.angle()
  34. -60
  35. Note that for some angles, the returned angle is not exactly the same as
  36. the angle you set, due to rounding errors in setting the pulse width.
  37. You can pass a second parameter to the ``angle`` method, which specifies how
  38. long to take (in milliseconds) to reach the desired angle. For example, to
  39. take 1 second (1000 milliseconds) to go from the current position to 50 degrees,
  40. use ::
  41. >>> servo1.angle(50, 1000)
  42. This command will return straight away and the servo will continue to move
  43. to the desired angle, and stop when it gets there. You can use this feature
  44. as a speed control, or to synchronise 2 or more servo motors. If we have
  45. another servo motor (``servo2 = pyb.Servo(2)``) then we can do ::
  46. >>> servo1.angle(-45, 2000); servo2.angle(60, 2000)
  47. This will move the servos together, making them both take 2 seconds to
  48. reach their final angles.
  49. Note: the semicolon between the 2 expressions above is used so that they
  50. are executed one after the other when you press enter at the REPL prompt.
  51. In a script you don't need to do this, you can just write them one line
  52. after the other.
  53. Continuous rotation servos
  54. --------------------------
  55. So far we have been using standard servos that move to a specific angle
  56. and stay at that angle. These servo motors are useful to create joints
  57. of a robot, or things like pan-tilt mechanisms. Internally, the motor
  58. has a variable resistor (potentiometer) which measures the current angle
  59. and applies power to the motor proportional to how far it is from the
  60. desired angle. The desired angle is set by the width of a high-pulse on
  61. the servo signal wire. A pulse width of 1500 microsecond corresponds
  62. to the centre position (0 degrees). The pulses are sent at 50 Hz, ie
  63. 50 pulses per second.
  64. You can also get **continuous rotation** servo motors which turn
  65. continuously clockwise or counterclockwise. The direction and speed of
  66. rotation is set by the pulse width on the signal wire. A pulse width
  67. of 1500 microseconds corresponds to a stopped motor. A pulse width
  68. smaller or larger than this means rotate one way or the other, at a
  69. given speed.
  70. On the pyboard, the servo object for a continuous rotation motor is
  71. the same as before. In fact, using ``angle`` you can set the speed. But
  72. to make it easier to understand what is intended, there is another method
  73. called ``speed`` which sets the speed::
  74. >>> servo1.speed(30)
  75. ``speed`` has the same functionality as ``angle``: you can get the speed,
  76. set it, and set it with a time to reach the final speed. ::
  77. >>> servo1.speed()
  78. 30
  79. >>> servo1.speed(-20)
  80. >>> servo1.speed(0, 2000)
  81. The final command above will set the motor to stop, but take 2 seconds
  82. to do it. This is essentially a control over the acceleration of the
  83. continuous servo.
  84. A servo speed of 100 (or -100) is considered maximum speed, but actually
  85. you can go a bit faster than that, depending on the particular motor.
  86. The only difference between the ``angle`` and ``speed`` methods (apart from
  87. the name) is the way the input numbers (angle or speed) are converted to
  88. a pulse width.
  89. Calibration
  90. -----------
  91. The conversion from angle or speed to pulse width is done by the servo
  92. object using its calibration values. To get the current calibration,
  93. use ::
  94. >>> servo1.calibration()
  95. (640, 2420, 1500, 2470, 2200)
  96. There are 5 numbers here, which have meaning:
  97. 1. Minimum pulse width; the smallest pulse width that the servo accepts.
  98. 2. Maximum pulse width; the largest pulse width that the servo accepts.
  99. 3. Centre pulse width; the pulse width that puts the servo at 0 degrees
  100. or 0 speed.
  101. 4. The pulse width corresponding to 90 degrees. This sets the conversion
  102. in the method ``angle`` of angle to pulse width.
  103. 5. The pulse width corresponding to a speed of 100. This sets the conversion
  104. in the method ``speed`` of speed to pulse width.
  105. You can recalibrate the servo (change its default values) by using::
  106. >>> servo1.calibration(700, 2400, 1510, 2500, 2000)
  107. Of course, you would change the above values to suit your particular
  108. servo motor.