This is a program to drive a stepper servomotor from SerialUSB without any other interrution but the serial one.

Dependencies:   mbed

Committer:
Yo_Robot
Date:
Mon Apr 09 03:10:30 2012 +0000
Revision:
0:da3eb35a2787
version0.2  PTO using Timer2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yo_Robot 0:da3eb35a2787 1 #ifndef L293D_H
Yo_Robot 0:da3eb35a2787 2 #define L293d_H
Yo_Robot 0:da3eb35a2787 3
Yo_Robot 0:da3eb35a2787 4 #include "mbed.h"
Yo_Robot 0:da3eb35a2787 5
Yo_Robot 0:da3eb35a2787 6 /**@brief Class to handle L293D in two modes, Pwm or Digital.
Yo_Robot 0:da3eb35a2787 7 * Pwm Mode requires constructing an object with two PwmOut'puts to handle Speed.
Yo_Robot 0:da3eb35a2787 8 * Enable, or Digital Mode requires One DigitalOut'put to Enable or Disable both sides
Yo_Robot 0:da3eb35a2787 9 * of the L293D driver.
Yo_Robot 0:da3eb35a2787 10 *
Yo_Robot 0:da3eb35a2787 11 * This of course requires an 'special' wiring of the driver, using one transistor for each side.
Yo_Robot 0:da3eb35a2787 12 * The transistor is effectively a NOT gate, assuring that the direction of the current of the
Yo_Robot 0:da3eb35a2787 13 * driver output will be controlled using just one pin.
Yo_Robot 0:da3eb35a2787 14 * _____________
Yo_Robot 0:da3eb35a2787 15 * Enable/ Pwm --->[1]
Yo_Robot 0:da3eb35a2787 16 * _5V__ | L
Yo_Robot 0:da3eb35a2787 17 * | _______________ [2]
Yo_Robot 0:da3eb35a2787 18 * _|_ / | 2
Yo_Robot 0:da3eb35a2787 19 * | | | ---------[3]
Yo_Robot 0:da3eb35a2787 20 * |1k| | | | 9
Yo_Robot 0:da3eb35a2787 21 * |__| | | (GND)[4]
Yo_Robot 0:da3eb35a2787 22 * /_________/ ( ~ ) | 3
Yo_Robot 0:da3eb35a2787 23 * _left |/ | (GND)[5]
Yo_Robot 0:da3eb35a2787 24 * -.---|470ohm|----|\ (Q 2N3904) | |
Yo_Robot 0:da3eb35a2787 25 * | | \ | -------[6] D
Yo_Robot 0:da3eb35a2787 26 * | |_(GND) |
Yo_Robot 0:da3eb35a2787 27 * | |
Yo_Robot 0:da3eb35a2787 28 * \___________________________________________ [7]
Yo_Robot 0:da3eb35a2787 29 * |
Yo_Robot 0:da3eb35a2787 30 * 5~15V-->[8]_____________
Yo_Robot 0:da3eb35a2787 31 *
Yo_Robot 0:da3eb35a2787 32 * Hope you get my ASCII Art. same goes for the other side, thus allowing a complete control of the driver
Yo_Robot 0:da3eb35a2787 33 * with a few pins.
Yo_Robot 0:da3eb35a2787 34 *
Yo_Robot 0:da3eb35a2787 35 * Also for Sake of Simplicity SetSpeedLeft/Right will control Motor Speed and Direction, from -1.0 to 1.0
Yo_Robot 0:da3eb35a2787 36 * Reverse if < 0.
Yo_Robot 0:da3eb35a2787 37 */
Yo_Robot 0:da3eb35a2787 38 class L293D{
Yo_Robot 0:da3eb35a2787 39
Yo_Robot 0:da3eb35a2787 40 public:
Yo_Robot 0:da3eb35a2787 41
Yo_Robot 0:da3eb35a2787 42 /**@brief
Yo_Robot 0:da3eb35a2787 43 * Two PWM for speed Cpntrol & two digital outs needed for setting direction
Yo_Robot 0:da3eb35a2787 44 * on each side of the L293D.
Yo_Robot 0:da3eb35a2787 45 * @param left: Output to the left side of the driver
Yo_Robot 0:da3eb35a2787 46 * @param right: Output to the right side of the driver
Yo_Robot 0:da3eb35a2787 47 * @param pwmLeft: Left side PWM.
Yo_Robot 0:da3eb35a2787 48 * @param pwmRight: Right side PWM.
Yo_Robot 0:da3eb35a2787 49 */
Yo_Robot 0:da3eb35a2787 50 L293D( PwmOut pwmLeft, PwmOut right, DigitalOut left, DigitalOut right );
Yo_Robot 0:da3eb35a2787 51
Yo_Robot 0:da3eb35a2787 52
Yo_Robot 0:da3eb35a2787 53 /**@brief
Yo_Robot 0:da3eb35a2787 54 * Set the left PWM to current speed and Direction.
Yo_Robot 0:da3eb35a2787 55 * Values from -1.0 to 1.0, to control direction and speed,
Yo_Robot 0:da3eb35a2787 56 * if Enable mode, only use -1.0 & 1.0
Yo_Robot 0:da3eb35a2787 57 */
Yo_Robot 0:da3eb35a2787 58 void setSpeedLeft( float speed );
Yo_Robot 0:da3eb35a2787 59
Yo_Robot 0:da3eb35a2787 60
Yo_Robot 0:da3eb35a2787 61 /**@brief
Yo_Robot 0:da3eb35a2787 62 * Set the right PWM to current speed and direction.
Yo_Robot 0:da3eb35a2787 63 * Values from -1.0 to 1.0, to control direction and speed,
Yo_Robot 0:da3eb35a2787 64 * if Enable mode, only use -1.0 & 1.0
Yo_Robot 0:da3eb35a2787 65 * Only to use when PWM mode is declared
Yo_Robot 0:da3eb35a2787 66 */
Yo_Robot 0:da3eb35a2787 67 void setSpeedRight( float speed );
Yo_Robot 0:da3eb35a2787 68
Yo_Robot 0:da3eb35a2787 69
Yo_Robot 0:da3eb35a2787 70 /**@brief
Yo_Robot 0:da3eb35a2787 71 * Read the left PWM current speed & direction
Yo_Robot 0:da3eb35a2787 72 * Ranges from -1.0 to 1.0, as being set.
Yo_Robot 0:da3eb35a2787 73 * @return Current PWM duty cycle.
Yo_Robot 0:da3eb35a2787 74 */
Yo_Robot 0:da3eb35a2787 75 float getSpeedLeft();
Yo_Robot 0:da3eb35a2787 76
Yo_Robot 0:da3eb35a2787 77
Yo_Robot 0:da3eb35a2787 78 /**@brief
Yo_Robot 0:da3eb35a2787 79 * Read the left PWM current speed & direction.
Yo_Robot 0:da3eb35a2787 80 * Ranges from -1.0 to 1.0 as being set.
Yo_Robot 0:da3eb35a2787 81 * @return Current PWM duty cycle.
Yo_Robot 0:da3eb35a2787 82 */
Yo_Robot 0:da3eb35a2787 83 float getSpeedRight();
Yo_Robot 0:da3eb35a2787 84
Yo_Robot 0:da3eb35a2787 85
Yo_Robot 0:da3eb35a2787 86 private:
Yo_Robot 0:da3eb35a2787 87
Yo_Robot 0:da3eb35a2787 88 DigitalOut _left;
Yo_Robot 0:da3eb35a2787 89 DigitalOut _right;
Yo_Robot 0:da3eb35a2787 90 PwmOut _pwmLeft;
Yo_Robot 0:da3eb35a2787 91 PwmOut _pwmRight;
Yo_Robot 0:da3eb35a2787 92
Yo_Robot 0:da3eb35a2787 93 float _speedLeft;
Yo_Robot 0:da3eb35a2787 94 float _speedRight;
Yo_Robot 0:da3eb35a2787 95
Yo_Robot 0:da3eb35a2787 96 };
Yo_Robot 0:da3eb35a2787 97
Yo_Robot 0:da3eb35a2787 98 #endif