AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
AP1017.h@4:c36159701cde, 2017-04-21 (annotated)
- Committer:
- tkstreet
- Date:
- Fri Apr 21 20:18:32 2017 +0000
- Revision:
- 4:c36159701cde
- Parent:
- 3:f8e70f639ed0
- Child:
- 6:d4d3bc82d446
Debugged motor functions. Added debugging serial messages. Added isMotorOn() function.
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 | class AP1017 |
tkstreet | 0:a0435a630c5d | 15 | { |
tkstreet | 0:a0435a630c5d | 16 | public: |
tkstreet | 0:a0435a630c5d | 17 | |
tkstreet | 2:8a644b1066c4 | 18 | /** |
tkstreet | 2:8a644b1066c4 | 19 | * Default constructor creates motors with PWM initial duty cycle of 0%. |
tkstreet | 2:8a644b1066c4 | 20 | * Motor EN pin connected to D2, INA connected to D0, INB connected to D1. |
tkstreet | 2:8a644b1066c4 | 21 | */ |
tkstreet | 0:a0435a630c5d | 22 | AP1017(void); |
tkstreet | 0:a0435a630c5d | 23 | |
tkstreet | 2:8a644b1066c4 | 24 | /** |
tkstreet | 2:8a644b1066c4 | 25 | * Disables PWM for the motors. |
tkstreet | 0:a0435a630c5d | 26 | */ |
tkstreet | 0:a0435a630c5d | 27 | ~AP1017(void); |
tkstreet | 0:a0435a630c5d | 28 | |
tkstreet | 2:8a644b1066c4 | 29 | /** |
tkstreet | 2:8a644b1066c4 | 30 | * Return status enumeration for debugging. |
tkstreet | 0:a0435a630c5d | 31 | */ |
tkstreet | 0:a0435a630c5d | 32 | typedef enum { |
tkstreet | 0:a0435a630c5d | 33 | SUCCESS = 0x00, /**< Successful termination */ |
tkstreet | 0:a0435a630c5d | 34 | ERROR_FREQUENCY = 0x01, /**< Frequency out of bounds */ |
tkstreet | 0:a0435a630c5d | 35 | ERROR_DUTY_CYCLE = 0x02, /**< Invalid duty cycle */ |
tkstreet | 0:a0435a630c5d | 36 | ERROR_DIRECTION = 0x03, /**< Invalid direction */ |
tkstreet | 0:a0435a630c5d | 37 | ERROR_PERIOD = 0x04, /**< Invalid period */ |
tkstreet | 0:a0435a630c5d | 38 | ERROR_PULSEWIDTH = 0x05, /**< Invalid pulse width */ |
tkstreet | 0:a0435a630c5d | 39 | ERROR_MOTORON = 0x06 /**< Direction switched while motor on */ |
tkstreet | 0:a0435a630c5d | 40 | } Status; |
tkstreet | 0:a0435a630c5d | 41 | |
tkstreet | 2:8a644b1066c4 | 42 | /** |
tkstreet | 2:8a644b1066c4 | 43 | * Motor directions. |
tkstreet | 0:a0435a630c5d | 44 | */ |
tkstreet | 0:a0435a630c5d | 45 | typedef enum { |
tkstreet | 0:a0435a630c5d | 46 | DIRECTION_CW = 0x00, /**< Clockwise motor rotation */ |
tkstreet | 0:a0435a630c5d | 47 | DIRECTION_CCW = 0x01, /**< Counterclockwise motor rotation */ |
tkstreet | 4:c36159701cde | 48 | DIRECTION_COAST = 0x02, /**< Release motor to coast */ |
tkstreet | 4:c36159701cde | 49 | DIRECTION_BRAKE = 0x03 /**< Brake motor */ |
tkstreet | 0:a0435a630c5d | 50 | } Rotation; |
tkstreet | 0:a0435a630c5d | 51 | |
tkstreet | 0:a0435a630c5d | 52 | |
tkstreet | 2:8a644b1066c4 | 53 | /** |
tkstreet | 2:8a644b1066c4 | 54 | * Sets the direction to clockwise, counterclockwise, brake or coast. |
tkstreet | 2:8a644b1066c4 | 55 | * Changing between clockwise and counterclockwise may only be performed |
tkstreet | 2:8a644b1066c4 | 56 | * when motor is off. |
tkstreet | 0:a0435a630c5d | 57 | * |
tkstreet | 2:8a644b1066c4 | 58 | * @param dir Rotation type: DIRECTION_CW, DIRECTION_CCW, DIRECTION_COAST, |
tkstreet | 0:a0435a630c5d | 59 | * or DIRECTION_BRAKE |
tkstreet | 2:8a644b1066c4 | 60 | * @return Returns successful termination, ERROR_MOTORON for invalid |
tkstreet | 0:a0435a630c5d | 61 | * direction switching, or ERROR_DIRECTION for invalid direction. |
tkstreet | 0:a0435a630c5d | 62 | */ |
tkstreet | 3:f8e70f639ed0 | 63 | Status setDirection(Rotation dir); |
tkstreet | 3:f8e70f639ed0 | 64 | |
tkstreet | 3:f8e70f639ed0 | 65 | /** |
tkstreet | 3:f8e70f639ed0 | 66 | * Returns the currently set direction. |
tkstreet | 3:f8e70f639ed0 | 67 | */ |
tkstreet | 3:f8e70f639ed0 | 68 | Rotation getDirection(void); |
tkstreet | 0:a0435a630c5d | 69 | |
tkstreet | 2:8a644b1066c4 | 70 | /** |
tkstreet | 3:f8e70f639ed0 | 71 | * Sets the speed via setting the duty cycle. Duty cycle given |
tkstreet | 3:f8e70f639ed0 | 72 | * as a percentage. |
tkstreet | 0:a0435a630c5d | 73 | * |
tkstreet | 2:8a644b1066c4 | 74 | * @param dc Duty cycle as a proportion (0.0 to 1.0). |
tkstreet | 2:8a644b1066c4 | 75 | * @return Returns successful termination or dutyc cyle error. |
tkstreet | 0:a0435a630c5d | 76 | */ |
tkstreet | 3:f8e70f639ed0 | 77 | Status setSpeed(float dc); |
tkstreet | 0:a0435a630c5d | 78 | |
tkstreet | 2:8a644b1066c4 | 79 | /** |
tkstreet | 3:f8e70f639ed0 | 80 | * Returns the currently set speed as a percentage. |
tkstreet | 0:a0435a630c5d | 81 | */ |
tkstreet | 3:f8e70f639ed0 | 82 | float getSpeed(void); |
tkstreet | 0:a0435a630c5d | 83 | |
tkstreet | 2:8a644b1066c4 | 84 | /** |
tkstreet | 2:8a644b1066c4 | 85 | * Engages the motor. |
tkstreet | 2:8a644b1066c4 | 86 | * |
tkstreet | 2:8a644b1066c4 | 87 | * @return Returns successful termination or pulse width error. |
tkstreet | 0:a0435a630c5d | 88 | */ |
tkstreet | 3:f8e70f639ed0 | 89 | Status start(void); |
tkstreet | 0:a0435a630c5d | 90 | |
tkstreet | 2:8a644b1066c4 | 91 | /** |
tkstreet | 2:8a644b1066c4 | 92 | * Stops forced rotation of the motor. |
tkstreet | 2:8a644b1066c4 | 93 | * |
tkstreet | 2:8a644b1066c4 | 94 | * @return Returns successful termination or pulse width error. |
tkstreet | 0:a0435a630c5d | 95 | */ |
tkstreet | 3:f8e70f639ed0 | 96 | Status stop(void); |
tkstreet | 0:a0435a630c5d | 97 | |
tkstreet | 2:8a644b1066c4 | 98 | /** |
tkstreet | 2:8a644b1066c4 | 99 | * Applies forced braking of motor. |
tkstreet | 2:8a644b1066c4 | 100 | * |
tkstreet | 2:8a644b1066c4 | 101 | * @return Returns successful termination or pulse width error. |
tkstreet | 0:a0435a630c5d | 102 | */ |
tkstreet | 3:f8e70f639ed0 | 103 | Status brake(void); |
tkstreet | 0:a0435a630c5d | 104 | |
tkstreet | 0:a0435a630c5d | 105 | /** |
tkstreet | 2:8a644b1066c4 | 106 | * Removes force from the motor and allows it to spin freely. |
tkstreet | 2:8a644b1066c4 | 107 | * |
tkstreet | 2:8a644b1066c4 | 108 | * @return Returns successful termination or pulse width error. |
tkstreet | 0:a0435a630c5d | 109 | */ |
tkstreet | 3:f8e70f639ed0 | 110 | Status coast(void); |
tkstreet | 4:c36159701cde | 111 | |
tkstreet | 4:c36159701cde | 112 | /** |
tkstreet | 4:c36159701cde | 113 | * Checks if the motor is currently running. |
tkstreet | 4:c36159701cde | 114 | * |
tkstreet | 4:c36159701cde | 115 | * @return TRUE if motor is on, FALSE if not. |
tkstreet | 4:c36159701cde | 116 | */ |
tkstreet | 4:c36159701cde | 117 | bool isMotorOn(void); |
tkstreet | 0:a0435a630c5d | 118 | |
tkstreet | 0:a0435a630c5d | 119 | private: |
tkstreet | 0:a0435a630c5d | 120 | |
tkstreet | 3:f8e70f639ed0 | 121 | bool motorOn; // Status flag for the motor |
tkstreet | 3:f8e70f639ed0 | 122 | float dutyCycle; // Given as proportion: 0.00 to 1.00 |
tkstreet | 3:f8e70f639ed0 | 123 | Rotation direction; |
tkstreet | 0:a0435a630c5d | 124 | |
tkstreet | 3:f8e70f639ed0 | 125 | PwmOut *motor; // Motor object |
tkstreet | 3:f8e70f639ed0 | 126 | |
tkstreet | 3:f8e70f639ed0 | 127 | DigitalOut *inA; // Directions setting pin A |
tkstreet | 3:f8e70f639ed0 | 128 | DigitalOut *inB; // Direction setting pin B |
tkstreet | 0:a0435a630c5d | 129 | /* inA=L, inB=L -> Standby (Coast) |
tkstreet | 0:a0435a630c5d | 130 | * inA=H, inB=L -> Forward (CW) |
tkstreet | 0:a0435a630c5d | 131 | * inA=L, inB=H -> Reverse (CCW) |
tkstreet | 0:a0435a630c5d | 132 | * inA=H, inB=H -> Brake |
tkstreet | 0:a0435a630c5d | 133 | */ |
tkstreet | 0:a0435a630c5d | 134 | |
tkstreet | 0:a0435a630c5d | 135 | }; |
tkstreet | 0:a0435a630c5d | 136 | |
tkstreet | 0:a0435a630c5d | 137 | #endif |