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