AP1017 library for the Rev.E hardware with expanded capabilities.
Fork of AP1017 by
Diff: AP1017.h
- Revision:
- 0:a0435a630c5d
- Child:
- 1:4d4c77589134
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AP1017.h Fri Apr 14 19:35:08 2017 +0000 @@ -0,0 +1,146 @@ +#ifndef __AP1017_H +#define __AP1017_H + +#include "mbed.h" + +#define MAX_FREQUENCY 5000 +#define ENABLE_PIN P0_10 +#define INPUTA_PIN P0_11 +#define INPUTB_PIN P0_9 + +/** + * This is a device driver for the AP1017 with pulse width modulation. + * + * @note AP1017 is a 12V Single Channel H-Bridge Motor Driver IC manufactured by AKM. + * + * Example: + * @code + * #include "mbed.h" + * #include "AP1017.h" + * + * int main() { + * AP1017 motorA; + * + * } + * @endcode + */ +class AP1017 +{ +public: + + /** Default constructor creates motors with PWM initial duty cycle of 0%. + * Motor EN pin connected to D2, INA connected to D0, INB connected to D1. + */ + AP1017(void); + + /** Disables PWM for the motors. + */ + ~AP1017(void); + + /** Return status enumeration for debugging. + */ + typedef enum { + SUCCESS = 0x00, /**< Successful termination */ + ERROR_FREQUENCY = 0x01, /**< Frequency out of bounds */ + ERROR_DUTY_CYCLE = 0x02, /**< Invalid duty cycle */ + ERROR_DIRECTION = 0x03, /**< Invalid direction */ + ERROR_PERIOD = 0x04, /**< Invalid period */ + ERROR_PULSEWIDTH = 0x05, /**< Invalid pulse width */ + ERROR_MOTORON = 0x06 /**< Direction switched while motor on */ + } Status; + + /** Motor directions. + */ + typedef enum { + DIRECTION_CW = 0x00, /**< Clockwise motor rotation */ + DIRECTION_CCW = 0x01, /**< Counterclockwise motor rotation */ + DIRECTION_COAST = 0x03, /**< Release motor to coast */ + DIRECTION_BRAKE = 0x04 /**< Brake motor */ + } Rotation; + + float getFrequency(void); + float getDutyCycle(void); + Rotation getDirection(void); + + /** Sets the direction to clockwise, counterclockwise, brake or coast. + * Changing between clockwise and counterclockwise may only be performed + * when motor is off. + * + * @param dir Rotation type: DIRECTION_CW, DIRECTION_CCW, DIRECTION_COAST, + * or DIRECTION_BRAKE + * @return Returns successful termination, ERROR_MOTORON for invalid + * direction switching, or ERROR_DIRECTION for invalid direction. + */ + Status setDirection(char dir); + + /** Sets the duty cycle in percentage. Also sets the pulse width in + * microseconds by calculation using current value of the period. + * + * @param dc Duty cycle as a proportion (0.0 to 1.0). + * @return Returns successful termination or dutyc cyle error. + */ + Status setDutyCycle(float dc); + + /** Sets the frequency of the motor in Hertz. + * @param f Frequency (Hertz). + * @return Returns successful termination or frequency error. + */ + Status setFrequency(float freq); + + /** Sets the period in microseconds. Also sets the pulse width by + * calculation with the current value of the duty cycle. Period will be + * changed even if motor is currently running. + * + * @param per PWM Period in microseconds. + * @return Returns successful termination or period error. + */ + Status setPeriod_us(float per); + + /** Sets the pulse with in microseconds. Also sets the duty cycle by + * calculation with the current value of the period. Pulse width will be + * changed even if motor is currently running. + * + * @param pw Pulse width in microseconds. + * @return Returns successful termination or pulse width error. + */ + Status setPulseWidth_us(float pw); + + /** Engages the motor. + */ + Status startMotor(void); + + /** Stops forced rotation of the motor. + */ + Status stopMotor(void); + + /** Applies forced braking of motor. + */ + Status brakeMotor(void); + + /** + */ + Status coastMotor(void); + +private: + + bool motorOn; // Status flag for the motor + float dutyCycle; // Given as proportion: 0.00 to 1.00 + float freq_hz; // PWM frequency + float pulseWidth_us; // Pulse width in microseconds + float pwmPeriod_us; // PWM period in microseconds + + PwmOut motor; // Motor object + + DigitalOut inA; // Directions setting pin A + DigitalOut inB; // Direction setting pin B + /* inA=L, inB=L -> Standby (Coast) + * inA=H, inB=L -> Forward (CW) + * inA=L, inB=H -> Reverse (CCW) + * inA=H, inB=H -> Brake + */ + + //Status initMotors(void); + +}; + +#endif \ No newline at end of file