Motor driver library for the AP1017.
AKM Development Platform
AP1017 Motor Driver
Import libraryAP1017
Motor driver library for the AP1017.
AP1017.cpp@1:4d4c77589134, 2017-04-17 (annotated)
- Committer:
- tkstreet
- Date:
- Mon Apr 17 19:08:36 2017 +0000
- Revision:
- 1:4d4c77589134
- Parent:
- 0:a0435a630c5d
- Child:
- 3:f8e70f639ed0
Changed private member types, modified constructor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tkstreet | 0:a0435a630c5d | 1 | #include "AP1017.h" |
tkstreet | 0:a0435a630c5d | 2 | |
tkstreet | 0:a0435a630c5d | 3 | /******************** Constructors & Destructors ****************************/ |
tkstreet | 0:a0435a630c5d | 4 | |
tkstreet | 0:a0435a630c5d | 5 | // Default constructor |
tkstreet | 1:4d4c77589134 | 6 | AP1017::AP1017(void) : motorOn(false), dutyCycle(0.0), freq_hz(0.0), |
tkstreet | 1:4d4c77589134 | 7 | pulseWidth_us(0.0), pwmPeriod_us(0.0) |
tkstreet | 0:a0435a630c5d | 8 | { |
tkstreet | 1:4d4c77589134 | 9 | motor = NULL; |
tkstreet | 1:4d4c77589134 | 10 | inA = NULL; |
tkstreet | 1:4d4c77589134 | 11 | inB = NULL; |
tkstreet | 0:a0435a630c5d | 12 | } |
tkstreet | 0:a0435a630c5d | 13 | |
tkstreet | 0:a0435a630c5d | 14 | // Default destructor |
tkstreet | 0:a0435a630c5d | 15 | AP1017::~AP1017(void) |
tkstreet | 0:a0435a630c5d | 16 | { |
tkstreet | 0:a0435a630c5d | 17 | stopMotor(); |
tkstreet | 1:4d4c77589134 | 18 | delete inA, inB, motor; |
tkstreet | 0:a0435a630c5d | 19 | } |
tkstreet | 0:a0435a630c5d | 20 | |
tkstreet | 0:a0435a630c5d | 21 | /*********************** Member Functions ***********************************/ |
tkstreet | 0:a0435a630c5d | 22 | |
tkstreet | 0:a0435a630c5d | 23 | AP1017::Status AP1017::setDirection(char dir) |
tkstreet | 0:a0435a630c5d | 24 | { |
tkstreet | 0:a0435a630c5d | 25 | |
tkstreet | 0:a0435a630c5d | 26 | switch(dir){ |
tkstreet | 0:a0435a630c5d | 27 | case DIRECTION_CW: |
tkstreet | 0:a0435a630c5d | 28 | if(motorOn == true && dir == DIRECTION_CCW) |
tkstreet | 0:a0435a630c5d | 29 | return ERROR_MOTORON; |
tkstreet | 1:4d4c77589134 | 30 | inA->write(1); |
tkstreet | 1:4d4c77589134 | 31 | inB->write(0); |
tkstreet | 0:a0435a630c5d | 32 | motorOn = true; |
tkstreet | 0:a0435a630c5d | 33 | break; |
tkstreet | 0:a0435a630c5d | 34 | case DIRECTION_CCW: |
tkstreet | 0:a0435a630c5d | 35 | if(motorOn == true && dir == DIRECTION_CW) |
tkstreet | 0:a0435a630c5d | 36 | return ERROR_MOTORON; |
tkstreet | 1:4d4c77589134 | 37 | inA->write(0); |
tkstreet | 1:4d4c77589134 | 38 | inB->write(1); |
tkstreet | 0:a0435a630c5d | 39 | motorOn = true; |
tkstreet | 0:a0435a630c5d | 40 | break; |
tkstreet | 0:a0435a630c5d | 41 | case DIRECTION_BRAKE: |
tkstreet | 1:4d4c77589134 | 42 | inA->write(1); |
tkstreet | 1:4d4c77589134 | 43 | inB->write(1); |
tkstreet | 0:a0435a630c5d | 44 | motorOn = true; |
tkstreet | 0:a0435a630c5d | 45 | break; |
tkstreet | 0:a0435a630c5d | 46 | case DIRECTION_COAST: |
tkstreet | 1:4d4c77589134 | 47 | inA->write(0); |
tkstreet | 1:4d4c77589134 | 48 | inB->write(0); |
tkstreet | 0:a0435a630c5d | 49 | motorOn = false; |
tkstreet | 0:a0435a630c5d | 50 | break; |
tkstreet | 0:a0435a630c5d | 51 | default: |
tkstreet | 0:a0435a630c5d | 52 | return ERROR_DIRECTION; |
tkstreet | 0:a0435a630c5d | 53 | } |
tkstreet | 0:a0435a630c5d | 54 | |
tkstreet | 0:a0435a630c5d | 55 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 56 | } |
tkstreet | 0:a0435a630c5d | 57 | |
tkstreet | 0:a0435a630c5d | 58 | AP1017::Status AP1017::setDutyCycle(float dc) |
tkstreet | 0:a0435a630c5d | 59 | { |
tkstreet | 0:a0435a630c5d | 60 | if((dutyCycle <= 1.00) && (dutyCycle >= 0.0)){ |
tkstreet | 0:a0435a630c5d | 61 | dutyCycle = dc; |
tkstreet | 0:a0435a630c5d | 62 | |
tkstreet | 0:a0435a630c5d | 63 | pulseWidth_us = dutyCycle * pwmPeriod_us; // Override pulse width |
tkstreet | 0:a0435a630c5d | 64 | |
tkstreet | 0:a0435a630c5d | 65 | if(motorOn == true){ |
tkstreet | 1:4d4c77589134 | 66 | motor->write(dutyCycle); // If motor is on, keep it on |
tkstreet | 0:a0435a630c5d | 67 | } |
tkstreet | 0:a0435a630c5d | 68 | |
tkstreet | 0:a0435a630c5d | 69 | } |
tkstreet | 0:a0435a630c5d | 70 | else{ |
tkstreet | 0:a0435a630c5d | 71 | dutyCycle = 0.0; |
tkstreet | 0:a0435a630c5d | 72 | pulseWidth_us = 0.0; |
tkstreet | 0:a0435a630c5d | 73 | |
tkstreet | 0:a0435a630c5d | 74 | return ERROR_DUTY_CYCLE; |
tkstreet | 0:a0435a630c5d | 75 | } |
tkstreet | 0:a0435a630c5d | 76 | |
tkstreet | 0:a0435a630c5d | 77 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 78 | } |
tkstreet | 0:a0435a630c5d | 79 | |
tkstreet | 0:a0435a630c5d | 80 | |
tkstreet | 0:a0435a630c5d | 81 | AP1017::Status AP1017::setFrequency(float freq) |
tkstreet | 0:a0435a630c5d | 82 | { |
tkstreet | 0:a0435a630c5d | 83 | if(freq > MAX_FREQUENCY){ |
tkstreet | 0:a0435a630c5d | 84 | return ERROR_FREQUENCY; |
tkstreet | 0:a0435a630c5d | 85 | } |
tkstreet | 0:a0435a630c5d | 86 | |
tkstreet | 0:a0435a630c5d | 87 | freq_hz = freq; |
tkstreet | 0:a0435a630c5d | 88 | pwmPeriod_us = (1/freq)/0.000001; // Calculate the period |
tkstreet | 0:a0435a630c5d | 89 | |
tkstreet | 0:a0435a630c5d | 90 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 91 | } |
tkstreet | 0:a0435a630c5d | 92 | |
tkstreet | 0:a0435a630c5d | 93 | |
tkstreet | 0:a0435a630c5d | 94 | AP1017::Status AP1017::setPeriod_us(float per) |
tkstreet | 0:a0435a630c5d | 95 | { |
tkstreet | 0:a0435a630c5d | 96 | if(per < 0){ |
tkstreet | 0:a0435a630c5d | 97 | return ERROR_PERIOD; |
tkstreet | 0:a0435a630c5d | 98 | } |
tkstreet | 0:a0435a630c5d | 99 | |
tkstreet | 0:a0435a630c5d | 100 | pwmPeriod_us = per; |
tkstreet | 0:a0435a630c5d | 101 | |
tkstreet | 0:a0435a630c5d | 102 | freq_hz = 1/(per * 0.000001); // Calculate the frequency |
tkstreet | 0:a0435a630c5d | 103 | |
tkstreet | 0:a0435a630c5d | 104 | if(motorOn == true){ // If motor on, keep it on |
tkstreet | 1:4d4c77589134 | 105 | motor->period_us(pwmPeriod_us); |
tkstreet | 0:a0435a630c5d | 106 | } |
tkstreet | 0:a0435a630c5d | 107 | |
tkstreet | 0:a0435a630c5d | 108 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 109 | } |
tkstreet | 0:a0435a630c5d | 110 | |
tkstreet | 0:a0435a630c5d | 111 | |
tkstreet | 0:a0435a630c5d | 112 | AP1017::Status AP1017::setPulseWidth_us(float pw) |
tkstreet | 0:a0435a630c5d | 113 | { |
tkstreet | 0:a0435a630c5d | 114 | if((pw <= 0.0) || (pw > pwmPeriod_us)){ |
tkstreet | 0:a0435a630c5d | 115 | return ERROR_PULSEWIDTH; |
tkstreet | 0:a0435a630c5d | 116 | } |
tkstreet | 0:a0435a630c5d | 117 | |
tkstreet | 0:a0435a630c5d | 118 | dutyCycle = pw/pwmPeriod_us; // Override duty cycle |
tkstreet | 0:a0435a630c5d | 119 | pulseWidth_us = pw; |
tkstreet | 0:a0435a630c5d | 120 | |
tkstreet | 0:a0435a630c5d | 121 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 122 | } |
tkstreet | 0:a0435a630c5d | 123 | |
tkstreet | 0:a0435a630c5d | 124 | |
tkstreet | 0:a0435a630c5d | 125 | AP1017::Status AP1017::startMotor(void) |
tkstreet | 0:a0435a630c5d | 126 | { |
tkstreet | 1:4d4c77589134 | 127 | motor->period_us(pwmPeriod_us); // Set the period |
tkstreet | 1:4d4c77589134 | 128 | motor->pulsewidth_us(pulseWidth_us); // Set the pulse width |
tkstreet | 1:4d4c77589134 | 129 | motor->write(dutyCycle); // Set duty cycle, start motor |
tkstreet | 0:a0435a630c5d | 130 | motorOn = true; // Set ON flag |
tkstreet | 0:a0435a630c5d | 131 | |
tkstreet | 0:a0435a630c5d | 132 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 133 | } |
tkstreet | 0:a0435a630c5d | 134 | |
tkstreet | 0:a0435a630c5d | 135 | |
tkstreet | 0:a0435a630c5d | 136 | AP1017::Status AP1017::stopMotor(void) |
tkstreet | 0:a0435a630c5d | 137 | { |
tkstreet | 1:4d4c77589134 | 138 | motor->write(0.0); // turn motor off (duty cycle saved) |
tkstreet | 0:a0435a630c5d | 139 | motorOn = false; // Set OFF flag |
tkstreet | 0:a0435a630c5d | 140 | |
tkstreet | 0:a0435a630c5d | 141 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 142 | } |
tkstreet | 0:a0435a630c5d | 143 | |
tkstreet | 0:a0435a630c5d | 144 | AP1017::Status AP1017::brakeMotor(void) |
tkstreet | 0:a0435a630c5d | 145 | { |
tkstreet | 0:a0435a630c5d | 146 | setDirection(DIRECTION_BRAKE); |
tkstreet | 0:a0435a630c5d | 147 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 148 | } |
tkstreet | 0:a0435a630c5d | 149 | |
tkstreet | 0:a0435a630c5d | 150 | AP1017::Status AP1017::coastMotor(void) |
tkstreet | 0:a0435a630c5d | 151 | { |
tkstreet | 0:a0435a630c5d | 152 | setDirection(DIRECTION_COAST); |
tkstreet | 0:a0435a630c5d | 153 | return SUCCESS; |
tkstreet | 0:a0435a630c5d | 154 | } |