diff --git a/books/beaglebone-cookbook/04motors/motors.rst b/books/beaglebone-cookbook/04motors/motors.rst index 9bfc351f2ca3d45309246f199a22fd9fc45d3a96..0879e86ca56d259fcca805012fc64bb9397f54fa 100644 --- a/books/beaglebone-cookbook/04motors/motors.rst +++ b/books/beaglebone-cookbook/04motors/motors.rst @@ -7,11 +7,11 @@ Motors .. |ohm| replace:: Ω -.. |deg| replace:: ° - -.. todo - Figure out degrees - +.. |deg| replace:: ° + +.. todo + Figure out degrees + One of the many fun things about embedded computers is that you can move physical things with motors. But there are so many different kinds of motors (``servo``, ``stepper``, ``DC``), so how do you select the right one? @@ -19,10 +19,10 @@ The type of motor you use depends on the type of motion you want: - R/C or hobby servo motor Can be quickly positioned at various absolute angles, but some don't spin. In fact, many can turn only about 180{deg}. -- Stepper motor - Spins and can also rotate in precise relative angles, such as turning 45°. Stepper motors come in two types: ``bipolar`` (which has four wires) and ``unipolar`` (which has five or six wires). -- DC motor - Spins either clockwise or counter-clockwise and can have the greatest speed of the three. But a DC motor can't easily be made to turn to a given angle. +- Stepper motor + Spins and can also rotate in precise relative angles, such as turning 45°. Stepper motors come in two types: ``bipolar`` (which has four wires) and ``unipolar`` (which has five or six wires). +- DC motor + Spins either clockwise or counter-clockwise and can have the greatest speed of the three. But a DC motor can't easily be made to turn to a given angle. When you know which type of motor to use, interfacing is easy. This chapter shows how to interface with each of these motors. @@ -38,7 +38,7 @@ When you know which type of motor to use, interfacing is easy. This chapter show .. note:: All the examples in the book assume you have cloned the Cookbook repository on - git.beagleboard.org. Go here :ref:`basics_repo` for instructions. + git.beagleboard.org. Go here :ref:`basics_repo` for instructions. .. _motors_servo: @@ -87,25 +87,33 @@ in :ref:`py_servoMotor_code`. You need to configure the pin for PWM. .. code-block:: bash - bone$ cd ~/beaglebone-cookbook-code/04motors - bone$ config-pin P9_16 pwm - bone$ ./servoMotor.py + bone$ cd ~/beaglebone-cookbook-code/04motors + bone$ config-pin P9_16 pwm + bone$ ./servoMotor.py -.. _py_servoMotor_code: +.. tabs:: -.. literalinclude:: ../code/04motors/servoMotor.py - :caption: Code for driving a servo motor (servoMotor.py) - :linenos: + .. group-tab:: Python -:download:`servoMotor.py <../code/04motors/servoMotor.py>` + .. _py_servoMotor_code: -.. _motors_servoMotor_code: + .. literalinclude:: ../code/04motors/servoMotor.py + :caption: Code for driving a servo motor (servoMotor.py) + :language: Python + :linenos: -.. literalinclude:: ../code/04motors/servoMotor.js - :caption: Code for driving a servo motor (servoMotor.js) - :linenos: + :download:`servoMotor.py <../code/04motors/servoMotor.py>` + + .. group-tab:: JavaScript + + .. _motors_servoMotor_code: -:download:`servoMotor.js <../code/04motors/servoMotor.js>` + .. literalinclude:: ../code/04motors/servoMotor.js + :caption: Code for driving a servo motor (servoMotor.js) + :language: JavaScript + :linenos: + + :download:`servoMotor.js <../code/04motors/servoMotor.js>` Running the code causes the motor to move back and forth, progressing to successive @@ -127,10 +135,10 @@ Combine the code from :ref:`digital_rotaryEncoder_js` and :ref:`motors_servo`. .. code-block:: bash - bone$ config-pin P9_16 pwm - bone$ config-pin P8_11 eqep - bone$ config-pin P8_12 eqep - bone$ ./servoEncoder.py + bone$ config-pin P9_16 pwm + bone$ config-pin P8_11 eqep + bone$ config-pin P8_12 eqep + bone$ ./servoEncoder.py .. _py_servoEncoder_code: @@ -158,7 +166,7 @@ but it won't. Most motors require more current than the GPIO ports on the Bone Our solution is to use a transistor to control the current to the bone. Here we configure the encoder to returns value between 0 and 180 inclusive. This value is then -mapped to a value between *min* (0.6 ms) and *max* (2.5 ms). This number is converted from +mapped to a value between *min* (0.6 ms) and *max* (2.5 ms). This number is converted from milliseconds and nanoseconds (time 1000000) and sent to the servo motor via the pwm. @@ -184,23 +192,31 @@ Wire your breadboard as shown in :ref:`motors_dcMotor_fig`. Wiring a DC motor to spin one direction -Use the code in :ref:`motors_dcMotor_code` (``dcMotor.js``) to run the motor. +Use the code in :ref:`py_dcMotor_code` to run the motor. -.. _py_dcMotor_code: +.. tabs:: -.. literalinclude:: ../code/04motors/dcMotor.py - :caption: Driving a DC motor in one direction (dcMotor.py) - :linenos: + .. group-tab:: Python -:download:`dcMotor.py <../code/04motors/dcMotor.py>` + .. _py_dcMotor_code: -.. _motors_dcMotor_code: + .. literalinclude:: ../code/04motors/dcMotor.py + :caption: Driving a DC motor in one direction (dcMotor.py) + :language: Python + :linenos: -.. literalinclude:: ../code/04motors/dcMotor.js - :caption: Driving a DC motor in one direction (dcMotor.js) - :linenos: + :download:`dcMotor.py <../code/04motors/dcMotor.py>` + + .. group-tab:: JavaScript + + .. _motors_dcMotor_code: + + .. literalinclude:: ../code/04motors/dcMotor.js + :caption: Driving a DC motor in one direction (dcMotor.js) + :language: JavaScript + :linenos: -:download:`dcMotor.js <../code/04motors/dcMotor.js>` + :download:`dcMotor.js <../code/04motors/dcMotor.js>` See Also ========= @@ -323,12 +339,12 @@ Wire, as shown in :ref:`motors_unipolar_fig`. The IC in :ref:`motors_unipolar_fig` is illustrated upside down from the way it is usually displayed. - That is, the notch for pin 1 is on the bottom. This made drawing the diagram much cleaner. + That is, the notch for pin 1 is on the bottom. This made drawing the diagram much cleaner. - Also, notice the ``banded`` wire running the *P9_7* (5 V) to the UL2003A. - The stepper motor I'm using runs better at 5 V, so I'm using the Bone's 5 V power supply. - The signal coming from the GPIO pins is 3.3 V, but the U2003A will step them up to 5 V to drive - the motor. + Also, notice the ``banded`` wire running the *P9_7* (5 V) to the UL2003A. + The stepper motor I'm using runs better at 5 V, so I'm using the Bone's 5 V power supply. + The signal coming from the GPIO pins is 3.3 V, but the U2003A will step them up to 5 V to drive + the motor. .. _motors_unipolar_fig: @@ -362,4 +378,4 @@ so :ref:`motors_unistepperMotor_code` shows only the lines that you need to chan The code in this example makes the following changes: * The *states* are different. Here, we have two pins high at a time. -* The time between steps (*ms*) is shorter, and the number of steps per direction (*max*) is bigger. The unipolar stepper I'm using has many more steps per rotation, so I need more steps to make it go around. +* The time between steps (*ms*) is shorter, and the number of steps per direction (*max*) is bigger. The unipolar stepper I'm using has many more steps per rotation, so I need more steps to make it go around.