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