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

Fork of AP1017 by AKM Development Platform

Committer:
tkstreet
Date:
Tue May 01 21:28:57 2018 +0000
Revision:
11:ac867fa4aa10
Parent:
10:16d45e3f4be3
Child:
13:1bea9a9412cb
Removed old hardware dependence to prepare for new hardware.

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