Motor driver library for the AP1017.

/media/uploads/tkstreet/akm_name_logo.png

AKM Development Platform

AP1017 Motor Driver

Import libraryAP1017

Motor driver library for the AP1017.

Committer:
tkstreet
Date:
Mon Apr 17 19:17:25 2017 +0000
Revision:
2:8a644b1066c4
Parent:
1:4d4c77589134
Child:
3:f8e70f639ed0
Fixed documentation formatting

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 #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 0:a0435a630c5d 48 DIRECTION_COAST = 0x03, /**< Release motor to coast */
tkstreet 0:a0435a630c5d 49 DIRECTION_BRAKE = 0x04 /**< Brake motor */
tkstreet 0:a0435a630c5d 50 } Rotation;
tkstreet 0:a0435a630c5d 51
tkstreet 0:a0435a630c5d 52 float getFrequency(void);
tkstreet 0:a0435a630c5d 53 float getDutyCycle(void);
tkstreet 0:a0435a630c5d 54 Rotation getDirection(void);
tkstreet 0:a0435a630c5d 55
tkstreet 2:8a644b1066c4 56 /**
tkstreet 2:8a644b1066c4 57 * Sets the direction to clockwise, counterclockwise, brake or coast.
tkstreet 2:8a644b1066c4 58 * Changing between clockwise and counterclockwise may only be performed
tkstreet 2:8a644b1066c4 59 * when motor is off.
tkstreet 0:a0435a630c5d 60 *
tkstreet 2:8a644b1066c4 61 * @param dir Rotation type: DIRECTION_CW, DIRECTION_CCW, DIRECTION_COAST,
tkstreet 0:a0435a630c5d 62 * or DIRECTION_BRAKE
tkstreet 2:8a644b1066c4 63 * @return Returns successful termination, ERROR_MOTORON for invalid
tkstreet 0:a0435a630c5d 64 * direction switching, or ERROR_DIRECTION for invalid direction.
tkstreet 0:a0435a630c5d 65 */
tkstreet 0:a0435a630c5d 66 Status setDirection(char dir);
tkstreet 0:a0435a630c5d 67
tkstreet 2:8a644b1066c4 68 /**
tkstreet 2:8a644b1066c4 69 * Sets the duty cycle in percentage. Also sets the pulse width in
tkstreet 2:8a644b1066c4 70 * microseconds by calculation using current value of the period.
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 0:a0435a630c5d 75 Status setDutyCycle(float dc);
tkstreet 0:a0435a630c5d 76
tkstreet 2:8a644b1066c4 77 /**
tkstreet 2:8a644b1066c4 78 * Sets the frequency of the motor in Hertz.
tkstreet 2:8a644b1066c4 79 *
tkstreet 2:8a644b1066c4 80 * @param f Frequency (Hertz).
tkstreet 2:8a644b1066c4 81 * @return Returns successful termination or frequency error.
tkstreet 0:a0435a630c5d 82 */
tkstreet 0:a0435a630c5d 83 Status setFrequency(float freq);
tkstreet 0:a0435a630c5d 84
tkstreet 2:8a644b1066c4 85 /**
tkstreet 2:8a644b1066c4 86 * Sets the period in microseconds. Also sets the pulse width by
tkstreet 2:8a644b1066c4 87 * calculation with the current value of the duty cycle. Period will be
tkstreet 2:8a644b1066c4 88 * changed even if motor is currently running.
tkstreet 0:a0435a630c5d 89 *
tkstreet 2:8a644b1066c4 90 * @param per PWM Period in microseconds.
tkstreet 2:8a644b1066c4 91 * @return Returns successful termination or period error.
tkstreet 0:a0435a630c5d 92 */
tkstreet 0:a0435a630c5d 93 Status setPeriod_us(float per);
tkstreet 0:a0435a630c5d 94
tkstreet 2:8a644b1066c4 95 /**
tkstreet 2:8a644b1066c4 96 * Sets the pulse with in microseconds. Also sets the duty cycle by
tkstreet 2:8a644b1066c4 97 * calculation with the current value of the period. Pulse width will be
tkstreet 2:8a644b1066c4 98 * changed even if motor is currently running.
tkstreet 0:a0435a630c5d 99 *
tkstreet 2:8a644b1066c4 100 * @param pw Pulse width in microseconds.
tkstreet 2:8a644b1066c4 101 * @return Returns successful termination or pulse width error.
tkstreet 0:a0435a630c5d 102 */
tkstreet 0:a0435a630c5d 103 Status setPulseWidth_us(float pw);
tkstreet 0:a0435a630c5d 104
tkstreet 2:8a644b1066c4 105 /**
tkstreet 2:8a644b1066c4 106 * Engages the motor.
tkstreet 2:8a644b1066c4 107 *
tkstreet 2:8a644b1066c4 108 * @return Returns successful termination or pulse width error.
tkstreet 0:a0435a630c5d 109 */
tkstreet 0:a0435a630c5d 110 Status startMotor(void);
tkstreet 0:a0435a630c5d 111
tkstreet 2:8a644b1066c4 112 /**
tkstreet 2:8a644b1066c4 113 * Stops forced rotation of the motor.
tkstreet 2:8a644b1066c4 114 *
tkstreet 2:8a644b1066c4 115 * @return Returns successful termination or pulse width error.
tkstreet 0:a0435a630c5d 116 */
tkstreet 0:a0435a630c5d 117 Status stopMotor(void);
tkstreet 0:a0435a630c5d 118
tkstreet 2:8a644b1066c4 119 /**
tkstreet 2:8a644b1066c4 120 * Applies forced braking of motor.
tkstreet 2:8a644b1066c4 121 *
tkstreet 2:8a644b1066c4 122 * @return Returns successful termination or pulse width error.
tkstreet 0:a0435a630c5d 123 */
tkstreet 0:a0435a630c5d 124 Status brakeMotor(void);
tkstreet 0:a0435a630c5d 125
tkstreet 0:a0435a630c5d 126 /**
tkstreet 2:8a644b1066c4 127 * Removes force from the motor and allows it to spin freely.
tkstreet 2:8a644b1066c4 128 *
tkstreet 2:8a644b1066c4 129 * @return Returns successful termination or pulse width error.
tkstreet 0:a0435a630c5d 130 */
tkstreet 0:a0435a630c5d 131 Status coastMotor(void);
tkstreet 0:a0435a630c5d 132
tkstreet 0:a0435a630c5d 133 private:
tkstreet 0:a0435a630c5d 134
tkstreet 0:a0435a630c5d 135 bool motorOn; // Status flag for the motor
tkstreet 0:a0435a630c5d 136 float dutyCycle; // Given as proportion: 0.00 to 1.00
tkstreet 0:a0435a630c5d 137 float freq_hz; // PWM frequency
tkstreet 0:a0435a630c5d 138 float pulseWidth_us; // Pulse width in microseconds
tkstreet 0:a0435a630c5d 139 float pwmPeriod_us; // PWM period in microseconds
tkstreet 0:a0435a630c5d 140
tkstreet 1:4d4c77589134 141 PwmOut *motor; // Motor object
tkstreet 0:a0435a630c5d 142
tkstreet 1:4d4c77589134 143 DigitalOut *inA; // Directions setting pin A
tkstreet 1:4d4c77589134 144 DigitalOut *inB; // Direction setting pin B
tkstreet 0:a0435a630c5d 145 /* inA=L, inB=L -> Standby (Coast)
tkstreet 0:a0435a630c5d 146 * inA=H, inB=L -> Forward (CW)
tkstreet 0:a0435a630c5d 147 * inA=L, inB=H -> Reverse (CCW)
tkstreet 0:a0435a630c5d 148 * inA=H, inB=H -> Brake
tkstreet 0:a0435a630c5d 149 */
tkstreet 0:a0435a630c5d 150
tkstreet 0:a0435a630c5d 151 //Status initMotors(void);
tkstreet 0:a0435a630c5d 152
tkstreet 0:a0435a630c5d 153 };
tkstreet 0:a0435a630c5d 154
tkstreet 0:a0435a630c5d 155 #endif