AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.h@1:4d4c77589134, 2017-04-17 (annotated)
- Committer:
- tkstreet
- Date:
- Mon Apr 17 19:08:36 2017 +0000
- Revision:
- 1:4d4c77589134
- Parent:
- 0:a0435a630c5d
- Child:
- 2:8a644b1066c4
Changed private member types, modified constructor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tkstreet | 0:a0435a630c5d | 1 | #ifndef __AP1017_H |
tkstreet | 0:a0435a630c5d | 2 | #define __AP1017_H |
tkstreet | 0:a0435a630c5d | 3 | |
tkstreet | 0:a0435a630c5d | 4 | #include "mbed.h" |
tkstreet | 0:a0435a630c5d | 5 | |
tkstreet | 0:a0435a630c5d | 6 | #define MAX_FREQUENCY 5000 |
tkstreet | 0:a0435a630c5d | 7 | #define ENABLE_PIN P0_10 |
tkstreet | 1:4d4c77589134 | 8 | #define INA_PIN P0_11 |
tkstreet | 1:4d4c77589134 | 9 | #define INB_PIN P0_9 |
tkstreet | 0:a0435a630c5d | 10 | |
tkstreet | 0:a0435a630c5d | 11 | /** |
tkstreet | 0:a0435a630c5d | 12 | * This is a device driver for the AP1017 with pulse width modulation. |
tkstreet | 0:a0435a630c5d | 13 | * |
tkstreet | 0:a0435a630c5d | 14 | * @note AP1017 is a 12V Single Channel H-Bridge Motor Driver IC manufactured by AKM. |
tkstreet | 0:a0435a630c5d | 15 | * |
tkstreet | 0:a0435a630c5d | 16 | * Example: |
tkstreet | 0:a0435a630c5d | 17 | * @code |
tkstreet | 0:a0435a630c5d | 18 | * #include "mbed.h" |
tkstreet | 0:a0435a630c5d | 19 | * #include "AP1017.h" |
tkstreet | 0:a0435a630c5d | 20 | * |
tkstreet | 0:a0435a630c5d | 21 | * int main() { |
tkstreet | 0:a0435a630c5d | 22 | * AP1017 motorA; |
tkstreet | 0:a0435a630c5d | 23 | * |
tkstreet | 0:a0435a630c5d | 24 | * } |
tkstreet | 0:a0435a630c5d | 25 | * @endcode |
tkstreet | 0:a0435a630c5d | 26 | */ |
tkstreet | 0:a0435a630c5d | 27 | class AP1017 |
tkstreet | 0:a0435a630c5d | 28 | { |
tkstreet | 0:a0435a630c5d | 29 | public: |
tkstreet | 0:a0435a630c5d | 30 | |
tkstreet | 0:a0435a630c5d | 31 | /** Default constructor creates motors with PWM initial duty cycle of 0%. |
tkstreet | 0:a0435a630c5d | 32 | * Motor EN pin connected to D2, INA connected to D0, INB connected to D1. |
tkstreet | 0:a0435a630c5d | 33 | */ |
tkstreet | 0:a0435a630c5d | 34 | AP1017(void); |
tkstreet | 0:a0435a630c5d | 35 | |
tkstreet | 0:a0435a630c5d | 36 | /** Disables PWM for the motors. |
tkstreet | 0:a0435a630c5d | 37 | */ |
tkstreet | 0:a0435a630c5d | 38 | ~AP1017(void); |
tkstreet | 0:a0435a630c5d | 39 | |
tkstreet | 0:a0435a630c5d | 40 | /** Return status enumeration for debugging. |
tkstreet | 0:a0435a630c5d | 41 | */ |
tkstreet | 0:a0435a630c5d | 42 | typedef enum { |
tkstreet | 0:a0435a630c5d | 43 | SUCCESS = 0x00, /**< Successful termination */ |
tkstreet | 0:a0435a630c5d | 44 | ERROR_FREQUENCY = 0x01, /**< Frequency out of bounds */ |
tkstreet | 0:a0435a630c5d | 45 | ERROR_DUTY_CYCLE = 0x02, /**< Invalid duty cycle */ |
tkstreet | 0:a0435a630c5d | 46 | ERROR_DIRECTION = 0x03, /**< Invalid direction */ |
tkstreet | 0:a0435a630c5d | 47 | ERROR_PERIOD = 0x04, /**< Invalid period */ |
tkstreet | 0:a0435a630c5d | 48 | ERROR_PULSEWIDTH = 0x05, /**< Invalid pulse width */ |
tkstreet | 0:a0435a630c5d | 49 | ERROR_MOTORON = 0x06 /**< Direction switched while motor on */ |
tkstreet | 0:a0435a630c5d | 50 | } Status; |
tkstreet | 0:a0435a630c5d | 51 | |
tkstreet | 0:a0435a630c5d | 52 | /** Motor directions. |
tkstreet | 0:a0435a630c5d | 53 | */ |
tkstreet | 0:a0435a630c5d | 54 | typedef enum { |
tkstreet | 0:a0435a630c5d | 55 | DIRECTION_CW = 0x00, /**< Clockwise motor rotation */ |
tkstreet | 0:a0435a630c5d | 56 | DIRECTION_CCW = 0x01, /**< Counterclockwise motor rotation */ |
tkstreet | 0:a0435a630c5d | 57 | DIRECTION_COAST = 0x03, /**< Release motor to coast */ |
tkstreet | 0:a0435a630c5d | 58 | DIRECTION_BRAKE = 0x04 /**< Brake motor */ |
tkstreet | 0:a0435a630c5d | 59 | } Rotation; |
tkstreet | 0:a0435a630c5d | 60 | |
tkstreet | 0:a0435a630c5d | 61 | float getFrequency(void); |
tkstreet | 0:a0435a630c5d | 62 | float getDutyCycle(void); |
tkstreet | 0:a0435a630c5d | 63 | Rotation getDirection(void); |
tkstreet | 0:a0435a630c5d | 64 | |
tkstreet | 0:a0435a630c5d | 65 | /** Sets the direction to clockwise, counterclockwise, brake or coast. |
tkstreet | 0:a0435a630c5d | 66 | * Changing between clockwise and counterclockwise may only be performed |
tkstreet | 0:a0435a630c5d | 67 | * when motor is off. |
tkstreet | 0:a0435a630c5d | 68 | * |
tkstreet | 0:a0435a630c5d | 69 | * @param dir Rotation type: DIRECTION_CW, DIRECTION_CCW, DIRECTION_COAST, |
tkstreet | 0:a0435a630c5d | 70 | * or DIRECTION_BRAKE |
tkstreet | 0:a0435a630c5d | 71 | * @return Returns successful termination, ERROR_MOTORON for invalid |
tkstreet | 0:a0435a630c5d | 72 | * direction switching, or ERROR_DIRECTION for invalid direction. |
tkstreet | 0:a0435a630c5d | 73 | */ |
tkstreet | 0:a0435a630c5d | 74 | Status setDirection(char dir); |
tkstreet | 0:a0435a630c5d | 75 | |
tkstreet | 0:a0435a630c5d | 76 | /** Sets the duty cycle in percentage. Also sets the pulse width in |
tkstreet | 0:a0435a630c5d | 77 | * microseconds by calculation using current value of the period. |
tkstreet | 0:a0435a630c5d | 78 | * |
tkstreet | 0:a0435a630c5d | 79 | * @param dc Duty cycle as a proportion (0.0 to 1.0). |
tkstreet | 0:a0435a630c5d | 80 | * @return Returns successful termination or dutyc cyle error. |
tkstreet | 0:a0435a630c5d | 81 | */ |
tkstreet | 0:a0435a630c5d | 82 | Status setDutyCycle(float dc); |
tkstreet | 0:a0435a630c5d | 83 | |
tkstreet | 0:a0435a630c5d | 84 | /** Sets the frequency of the motor in Hertz. |
tkstreet | 0:a0435a630c5d | 85 | * @param f Frequency (Hertz). |
tkstreet | 0:a0435a630c5d | 86 | * @return Returns successful termination or frequency error. |
tkstreet | 0:a0435a630c5d | 87 | */ |
tkstreet | 0:a0435a630c5d | 88 | Status setFrequency(float freq); |
tkstreet | 0:a0435a630c5d | 89 | |
tkstreet | 0:a0435a630c5d | 90 | /** Sets the period in microseconds. Also sets the pulse width by |
tkstreet | 0:a0435a630c5d | 91 | * calculation with the current value of the duty cycle. Period will be |
tkstreet | 0:a0435a630c5d | 92 | * changed even if motor is currently running. |
tkstreet | 0:a0435a630c5d | 93 | * |
tkstreet | 0:a0435a630c5d | 94 | * @param per PWM Period in microseconds. |
tkstreet | 0:a0435a630c5d | 95 | * @return Returns successful termination or period error. |
tkstreet | 0:a0435a630c5d | 96 | */ |
tkstreet | 0:a0435a630c5d | 97 | Status setPeriod_us(float per); |
tkstreet | 0:a0435a630c5d | 98 | |
tkstreet | 0:a0435a630c5d | 99 | /** Sets the pulse with in microseconds. Also sets the duty cycle by |
tkstreet | 0:a0435a630c5d | 100 | * calculation with the current value of the period. Pulse width will be |
tkstreet | 0:a0435a630c5d | 101 | * changed even if motor is currently running. |
tkstreet | 0:a0435a630c5d | 102 | * |
tkstreet | 0:a0435a630c5d | 103 | * @param pw Pulse width in microseconds. |
tkstreet | 0:a0435a630c5d | 104 | * @return Returns successful termination or pulse width error. |
tkstreet | 0:a0435a630c5d | 105 | */ |
tkstreet | 0:a0435a630c5d | 106 | Status setPulseWidth_us(float pw); |
tkstreet | 0:a0435a630c5d | 107 | |
tkstreet | 0:a0435a630c5d | 108 | /** Engages the motor. |
tkstreet | 0:a0435a630c5d | 109 | */ |
tkstreet | 0:a0435a630c5d | 110 | Status startMotor(void); |
tkstreet | 0:a0435a630c5d | 111 | |
tkstreet | 0:a0435a630c5d | 112 | /** Stops forced rotation of the motor. |
tkstreet | 0:a0435a630c5d | 113 | */ |
tkstreet | 0:a0435a630c5d | 114 | Status stopMotor(void); |
tkstreet | 0:a0435a630c5d | 115 | |
tkstreet | 0:a0435a630c5d | 116 | /** Applies forced braking of motor. |
tkstreet | 0:a0435a630c5d | 117 | */ |
tkstreet | 0:a0435a630c5d | 118 | Status brakeMotor(void); |
tkstreet | 0:a0435a630c5d | 119 | |
tkstreet | 0:a0435a630c5d | 120 | /** |
tkstreet | 0:a0435a630c5d | 121 | */ |
tkstreet | 0:a0435a630c5d | 122 | Status coastMotor(void); |
tkstreet | 0:a0435a630c5d | 123 | |
tkstreet | 0:a0435a630c5d | 124 | private: |
tkstreet | 0:a0435a630c5d | 125 | |
tkstreet | 0:a0435a630c5d | 126 | bool motorOn; // Status flag for the motor |
tkstreet | 0:a0435a630c5d | 127 | float dutyCycle; // Given as proportion: 0.00 to 1.00 |
tkstreet | 0:a0435a630c5d | 128 | float freq_hz; // PWM frequency |
tkstreet | 0:a0435a630c5d | 129 | float pulseWidth_us; // Pulse width in microseconds |
tkstreet | 0:a0435a630c5d | 130 | float pwmPeriod_us; // PWM period in microseconds |
tkstreet | 0:a0435a630c5d | 131 | |
tkstreet | 1:4d4c77589134 | 132 | PwmOut *motor; // Motor object |
tkstreet | 0:a0435a630c5d | 133 | |
tkstreet | 1:4d4c77589134 | 134 | DigitalOut *inA; // Directions setting pin A |
tkstreet | 1:4d4c77589134 | 135 | DigitalOut *inB; // Direction setting pin B |
tkstreet | 0:a0435a630c5d | 136 | /* inA=L, inB=L -> Standby (Coast) |
tkstreet | 0:a0435a630c5d | 137 | * inA=H, inB=L -> Forward (CW) |
tkstreet | 0:a0435a630c5d | 138 | * inA=L, inB=H -> Reverse (CCW) |
tkstreet | 0:a0435a630c5d | 139 | * inA=H, inB=H -> Brake |
tkstreet | 0:a0435a630c5d | 140 | */ |
tkstreet | 0:a0435a630c5d | 141 | |
tkstreet | 0:a0435a630c5d | 142 | //Status initMotors(void); |
tkstreet | 0:a0435a630c5d | 143 | |
tkstreet | 0:a0435a630c5d | 144 | }; |
tkstreet | 0:a0435a630c5d | 145 | |
tkstreet | 0:a0435a630c5d | 146 | #endif |