Motor driver library for the AP1017.
AKM Development Platform
AP1017 Motor Driver
Import libraryAP1017
Motor driver library for the AP1017.
AP1017.h
- Committer:
- tkstreet
- Date:
- 2017-04-17
- Revision:
- 2:8a644b1066c4
- Parent:
- 1:4d4c77589134
- Child:
- 3:f8e70f639ed0
File content as of revision 2:8a644b1066c4:
#ifndef __AP1017_H #define __AP1017_H #include "mbed.h" #define MAX_FREQUENCY 5000 #define ENABLE_PIN P0_10 #define INA_PIN P0_11 #define INB_PIN P0_9 /** * This is a device driver for the AP1017 with pulse width modulation. */ 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. * * @return Returns successful termination or pulse width error. */ Status startMotor(void); /** * Stops forced rotation of the motor. * * @return Returns successful termination or pulse width error. */ Status stopMotor(void); /** * Applies forced braking of motor. * * @return Returns successful termination or pulse width error. */ Status brakeMotor(void); /** * Removes force from the motor and allows it to spin freely. * * @return Returns successful termination or pulse width error. */ 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