AP1017 library for the Rev.E hardware with expanded capabilities.

Fork of AP1017 by AKM Development Platform

Committer:
tkstreet
Date:
Fri Aug 10 17:31:09 2018 +0000
Revision:
13:1bea9a9412cb
Parent:
11:ac867fa4aa10
Removed calls to debug library.

Who changed what in which revision?

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