Library for H-Bridge Motor Driver Using Bipolar Transistors
motorlib.h@2:c1f9f9d74f35, 2013-01-26 (annotated)
- Committer:
- prabhuvd
- Date:
- Sat Jan 26 15:00:23 2013 +0000
- Revision:
- 2:c1f9f9d74f35
- Parent:
- 1:b690c5eb9469
LM293D and PWM based drive support added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prabhuvd | 0:155daae8a9fa | 1 | /*mbed motorlib Library for H-Bridge Motor Driver Using Bipolar Transistors |
prabhuvd | 0:155daae8a9fa | 2 | * |
prabhuvd | 0:155daae8a9fa | 3 | * mbed simple H-bridge motor controller |
prabhuvd | 0:155daae8a9fa | 4 | * Copyright (c) 2007-2012, Prabhu Desai |
prabhuvd | 0:155daae8a9fa | 5 | * |
prabhuvd | 0:155daae8a9fa | 6 | |
prabhuvd | 0:155daae8a9fa | 7 | */ |
prabhuvd | 0:155daae8a9fa | 8 | |
prabhuvd | 0:155daae8a9fa | 9 | #ifndef MBED_MOTOR_H |
prabhuvd | 0:155daae8a9fa | 10 | #define MBED_MOTOR_H |
prabhuvd | 0:155daae8a9fa | 11 | |
prabhuvd | 0:155daae8a9fa | 12 | #include "mbed.h" |
prabhuvd | 0:155daae8a9fa | 13 | |
prabhuvd | 2:c1f9f9d74f35 | 14 | #define LM293D_MOTOR_CTRL |
prabhuvd | 0:155daae8a9fa | 15 | /** Interface to control a standard DC motor |
prabhuvd | 0:155daae8a9fa | 16 | * |
prabhuvd | 0:155daae8a9fa | 17 | */ |
prabhuvd | 0:155daae8a9fa | 18 | class Motor { |
prabhuvd | 0:155daae8a9fa | 19 | public: |
prabhuvd | 2:c1f9f9d74f35 | 20 | /** Motor Control Type */ |
prabhuvd | 2:c1f9f9d74f35 | 21 | enum MCtrlType { |
prabhuvd | 2:c1f9f9d74f35 | 22 | L293D_DISCRETE /**< 0 or 100% duty cycle with L293D */ |
prabhuvd | 2:c1f9f9d74f35 | 23 | , L293D_PWM /**< PWM based voltage control with L293D */ |
prabhuvd | 2:c1f9f9d74f35 | 24 | , COMPS_DISCRETE /**< 0 or 100% duty cycle with transistor circuit*/ |
prabhuvd | 2:c1f9f9d74f35 | 25 | , COMPS_PWM /**< PWM based voltage control with transistor circuit*/ |
prabhuvd | 2:c1f9f9d74f35 | 26 | |
prabhuvd | 2:c1f9f9d74f35 | 27 | }; |
prabhuvd | 0:155daae8a9fa | 28 | |
prabhuvd | 0:155daae8a9fa | 29 | /** Create a motor control interface |
prabhuvd | 0:155daae8a9fa | 30 | * |
prabhuvd | 0:155daae8a9fa | 31 | * @param r1 DigitalOut, driving H-bridge NPN transistor on the BOTTOm right |
prabhuvd | 1:b690c5eb9469 | 32 | * @param r2 DigitalOut, driving H-bridge PNP transistor on the TOP right |
prabhuvd | 0:155daae8a9fa | 33 | * @param r3 DigitalOut, driving H-bridge NPN transistor on the BOTTOm left. |
prabhuvd | 1:b690c5eb9469 | 34 | * @param r4 DigitalOut, driving H-bridge PNP transistor on the TOP left |
prabhuvd | 2:c1f9f9d74f35 | 35 | * @param mctype DigitalOut, driving H-bridge PNP transistor on the TOP left |
prabhuvd | 0:155daae8a9fa | 36 | */ |
prabhuvd | 2:c1f9f9d74f35 | 37 | Motor(PinName r1, PinName r4,MCtrlType mctype =L293D_PWM); |
prabhuvd | 2:c1f9f9d74f35 | 38 | Motor(PinName r1, PinName r2, PinName r3, PinName r4,MCtrlType mctype =L293D_PWM); |
prabhuvd | 0:155daae8a9fa | 39 | |
prabhuvd | 0:155daae8a9fa | 40 | /** Set the motor to coast/roll/off |
prabhuvd | 0:155daae8a9fa | 41 | * |
prabhuvd | 0:155daae8a9fa | 42 | * @param void |
prabhuvd | 0:155daae8a9fa | 43 | * @return void |
prabhuvd | 0:155daae8a9fa | 44 | */ |
prabhuvd | 0:155daae8a9fa | 45 | void coast(void); |
prabhuvd | 0:155daae8a9fa | 46 | |
prabhuvd | 0:155daae8a9fa | 47 | /** Rotate motor in clockwise direction. |
prabhuvd | 0:155daae8a9fa | 48 | * |
prabhuvd | 0:155daae8a9fa | 49 | * @param void |
prabhuvd | 0:155daae8a9fa | 50 | * @return void |
prabhuvd | 0:155daae8a9fa | 51 | */ |
prabhuvd | 0:155daae8a9fa | 52 | void forward(void); |
prabhuvd | 0:155daae8a9fa | 53 | |
prabhuvd | 0:155daae8a9fa | 54 | /** /** Rotate motor in Anti-clockwise direction. |
prabhuvd | 0:155daae8a9fa | 55 | * |
prabhuvd | 0:155daae8a9fa | 56 | * @param void |
prabhuvd | 0:155daae8a9fa | 57 | * @return void |
prabhuvd | 0:155daae8a9fa | 58 | */ |
prabhuvd | 0:155daae8a9fa | 59 | |
prabhuvd | 0:155daae8a9fa | 60 | void backward(void); |
prabhuvd | 0:155daae8a9fa | 61 | |
prabhuvd | 0:155daae8a9fa | 62 | /** Stop the motor. |
prabhuvd | 0:155daae8a9fa | 63 | * |
prabhuvd | 0:155daae8a9fa | 64 | * @param void |
prabhuvd | 0:155daae8a9fa | 65 | * @return void |
prabhuvd | 0:155daae8a9fa | 66 | */ |
prabhuvd | 0:155daae8a9fa | 67 | |
prabhuvd | 0:155daae8a9fa | 68 | void stop(void); |
prabhuvd | 0:155daae8a9fa | 69 | |
prabhuvd | 0:155daae8a9fa | 70 | protected: |
prabhuvd | 2:c1f9f9d74f35 | 71 | #ifdef LM293D_MOTOR_CTRL |
prabhuvd | 2:c1f9f9d74f35 | 72 | PwmOut _r1; |
prabhuvd | 2:c1f9f9d74f35 | 73 | PwmOut _r4; |
prabhuvd | 2:c1f9f9d74f35 | 74 | #else |
prabhuvd | 0:155daae8a9fa | 75 | DigitalOut _r1; |
prabhuvd | 0:155daae8a9fa | 76 | DigitalOut _r2; |
prabhuvd | 0:155daae8a9fa | 77 | DigitalOut _r3; |
prabhuvd | 2:c1f9f9d74f35 | 78 | DigitalOut _r4; |
prabhuvd | 2:c1f9f9d74f35 | 79 | #endif |
prabhuvd | 2:c1f9f9d74f35 | 80 | MCtrlType _mctype; |
prabhuvd | 0:155daae8a9fa | 81 | |
prabhuvd | 0:155daae8a9fa | 82 | }; |
prabhuvd | 0:155daae8a9fa | 83 | |
prabhuvd | 0:155daae8a9fa | 84 | #endif |