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

Fork of AP1017 by AKM Development Platform

Committer:
tkstreet
Date:
Fri Apr 14 19:35:08 2017 +0000
Revision:
0:a0435a630c5d
Child:
1:4d4c77589134
Initial commit: basic driver functions for AP1017

Who changed what in which revision?

UserRevisionLine numberNew 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 0:a0435a630c5d 6 AP1017::AP1017(void) : motorOn(false), dutyCycle(0.50), pwmPeriod_us(200),
tkstreet 0:a0435a630c5d 7 motor(ENABLE_PIN), inA(INPUTA_PIN), inB(INPUTB_PIN)
tkstreet 0:a0435a630c5d 8 {
tkstreet 0:a0435a630c5d 9 freq_hz = 0.000001/pwmPeriod_us; // Derive frequency
tkstreet 0:a0435a630c5d 10 pulseWidth_us = dutyCycle * pwmPeriod_us; // Derive pulse width
tkstreet 0:a0435a630c5d 11 stopMotor(); // Keep motor off
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 0:a0435a630c5d 18
tkstreet 0:a0435a630c5d 19 if(ap1017)
tkstreet 0:a0435a630c5d 20 delete ap1017;
tkstreet 0:a0435a630c5d 21 }
tkstreet 0:a0435a630c5d 22
tkstreet 0:a0435a630c5d 23 /*********************** Member Functions ***********************************/
tkstreet 0:a0435a630c5d 24
tkstreet 0:a0435a630c5d 25 AP1017::Status AP1017::setDirection(char dir)
tkstreet 0:a0435a630c5d 26 {
tkstreet 0:a0435a630c5d 27
tkstreet 0:a0435a630c5d 28 switch(dir){
tkstreet 0:a0435a630c5d 29 case DIRECTION_CW:
tkstreet 0:a0435a630c5d 30 if(motorOn == true && dir == DIRECTION_CCW)
tkstreet 0:a0435a630c5d 31 return ERROR_MOTORON;
tkstreet 0:a0435a630c5d 32 inA.write(1);
tkstreet 0:a0435a630c5d 33 inB.write(0);
tkstreet 0:a0435a630c5d 34 motorOn = true;
tkstreet 0:a0435a630c5d 35 break;
tkstreet 0:a0435a630c5d 36 case DIRECTION_CCW:
tkstreet 0:a0435a630c5d 37 if(motorOn == true && dir == DIRECTION_CW)
tkstreet 0:a0435a630c5d 38 return ERROR_MOTORON;
tkstreet 0:a0435a630c5d 39 inA.write(0);
tkstreet 0:a0435a630c5d 40 inB.write(1);
tkstreet 0:a0435a630c5d 41 motorOn = true;
tkstreet 0:a0435a630c5d 42 break;
tkstreet 0:a0435a630c5d 43 case DIRECTION_BRAKE:
tkstreet 0:a0435a630c5d 44 inA.write(1);
tkstreet 0:a0435a630c5d 45 inB.write(1);
tkstreet 0:a0435a630c5d 46 motorOn = true;
tkstreet 0:a0435a630c5d 47 break;
tkstreet 0:a0435a630c5d 48 case DIRECTION_COAST:
tkstreet 0:a0435a630c5d 49 inA.write(0);
tkstreet 0:a0435a630c5d 50 inB.write(0);
tkstreet 0:a0435a630c5d 51 motorOn = false;
tkstreet 0:a0435a630c5d 52 break;
tkstreet 0:a0435a630c5d 53 default:
tkstreet 0:a0435a630c5d 54 return ERROR_DIRECTION;
tkstreet 0:a0435a630c5d 55 }
tkstreet 0:a0435a630c5d 56
tkstreet 0:a0435a630c5d 57 return SUCCESS;
tkstreet 0:a0435a630c5d 58 }
tkstreet 0:a0435a630c5d 59
tkstreet 0:a0435a630c5d 60 AP1017::Status AP1017::setDutyCycle(float dc)
tkstreet 0:a0435a630c5d 61 {
tkstreet 0:a0435a630c5d 62 if((dutyCycle <= 1.00) && (dutyCycle >= 0.0)){
tkstreet 0:a0435a630c5d 63 dutyCycle = dc;
tkstreet 0:a0435a630c5d 64
tkstreet 0:a0435a630c5d 65 pulseWidth_us = dutyCycle * pwmPeriod_us; // Override pulse width
tkstreet 0:a0435a630c5d 66
tkstreet 0:a0435a630c5d 67 if(motorOn == true){
tkstreet 0:a0435a630c5d 68 motor.write(dutyCycle); // If motor is on, keep it on
tkstreet 0:a0435a630c5d 69 }
tkstreet 0:a0435a630c5d 70
tkstreet 0:a0435a630c5d 71 }
tkstreet 0:a0435a630c5d 72 else{
tkstreet 0:a0435a630c5d 73 dutyCycle = 0.0;
tkstreet 0:a0435a630c5d 74 pulseWidth_us = 0.0;
tkstreet 0:a0435a630c5d 75
tkstreet 0:a0435a630c5d 76 return ERROR_DUTY_CYCLE;
tkstreet 0:a0435a630c5d 77 }
tkstreet 0:a0435a630c5d 78
tkstreet 0:a0435a630c5d 79 return SUCCESS;
tkstreet 0:a0435a630c5d 80 }
tkstreet 0:a0435a630c5d 81
tkstreet 0:a0435a630c5d 82
tkstreet 0:a0435a630c5d 83 AP1017::Status AP1017::setFrequency(float freq)
tkstreet 0:a0435a630c5d 84 {
tkstreet 0:a0435a630c5d 85 if(freq > MAX_FREQUENCY){
tkstreet 0:a0435a630c5d 86 return ERROR_FREQUENCY;
tkstreet 0:a0435a630c5d 87 }
tkstreet 0:a0435a630c5d 88
tkstreet 0:a0435a630c5d 89 freq_hz = freq;
tkstreet 0:a0435a630c5d 90 pwmPeriod_us = (1/freq)/0.000001; // Calculate the period
tkstreet 0:a0435a630c5d 91
tkstreet 0:a0435a630c5d 92 return SUCCESS;
tkstreet 0:a0435a630c5d 93 }
tkstreet 0:a0435a630c5d 94
tkstreet 0:a0435a630c5d 95
tkstreet 0:a0435a630c5d 96 AP1017::Status AP1017::setPeriod_us(float per)
tkstreet 0:a0435a630c5d 97 {
tkstreet 0:a0435a630c5d 98 if(per < 0){
tkstreet 0:a0435a630c5d 99 return ERROR_PERIOD;
tkstreet 0:a0435a630c5d 100 }
tkstreet 0:a0435a630c5d 101
tkstreet 0:a0435a630c5d 102 pwmPeriod_us = per;
tkstreet 0:a0435a630c5d 103
tkstreet 0:a0435a630c5d 104 freq_hz = 1/(per * 0.000001); // Calculate the frequency
tkstreet 0:a0435a630c5d 105
tkstreet 0:a0435a630c5d 106 if(motorOn == true){ // If motor on, keep it on
tkstreet 0:a0435a630c5d 107 motor.period_us(pwmPeriod_us);
tkstreet 0:a0435a630c5d 108 }
tkstreet 0:a0435a630c5d 109
tkstreet 0:a0435a630c5d 110 return SUCCESS;
tkstreet 0:a0435a630c5d 111 }
tkstreet 0:a0435a630c5d 112
tkstreet 0:a0435a630c5d 113
tkstreet 0:a0435a630c5d 114 AP1017::Status AP1017::setPulseWidth_us(float pw)
tkstreet 0:a0435a630c5d 115 {
tkstreet 0:a0435a630c5d 116 if((pw <= 0.0) || (pw > pwmPeriod_us)){
tkstreet 0:a0435a630c5d 117 return ERROR_PULSEWIDTH;
tkstreet 0:a0435a630c5d 118 }
tkstreet 0:a0435a630c5d 119
tkstreet 0:a0435a630c5d 120 dutyCycle = pw/pwmPeriod_us; // Override duty cycle
tkstreet 0:a0435a630c5d 121 pulseWidth_us = pw;
tkstreet 0:a0435a630c5d 122
tkstreet 0:a0435a630c5d 123 return SUCCESS;
tkstreet 0:a0435a630c5d 124 }
tkstreet 0:a0435a630c5d 125
tkstreet 0:a0435a630c5d 126
tkstreet 0:a0435a630c5d 127 AP1017::Status AP1017::startMotor(void)
tkstreet 0:a0435a630c5d 128 {
tkstreet 0:a0435a630c5d 129 motor.period_us(pwmPeriod_us); // Set the period
tkstreet 0:a0435a630c5d 130 motor.pulsewidth_us(pulseWidth_us); // Set the pulse width
tkstreet 0:a0435a630c5d 131 motor.write(dutyCycle); // Set duty cycle, start motor
tkstreet 0:a0435a630c5d 132 motorOn = true; // Set ON flag
tkstreet 0:a0435a630c5d 133
tkstreet 0:a0435a630c5d 134 return SUCCESS;
tkstreet 0:a0435a630c5d 135 }
tkstreet 0:a0435a630c5d 136
tkstreet 0:a0435a630c5d 137
tkstreet 0:a0435a630c5d 138 AP1017::Status AP1017::stopMotor(void)
tkstreet 0:a0435a630c5d 139 {
tkstreet 0:a0435a630c5d 140 motor.write(0.0); // turn motor off (duty cycle saved)
tkstreet 0:a0435a630c5d 141 motorOn = false; // Set OFF flag
tkstreet 0:a0435a630c5d 142
tkstreet 0:a0435a630c5d 143 return SUCCESS;
tkstreet 0:a0435a630c5d 144 }
tkstreet 0:a0435a630c5d 145
tkstreet 0:a0435a630c5d 146 AP1017::Status AP1017::brakeMotor(void)
tkstreet 0:a0435a630c5d 147 {
tkstreet 0:a0435a630c5d 148 setDirection(DIRECTION_BRAKE);
tkstreet 0:a0435a630c5d 149 return SUCCESS;
tkstreet 0:a0435a630c5d 150 }
tkstreet 0:a0435a630c5d 151
tkstreet 0:a0435a630c5d 152 AP1017::Status AP1017::coastMotor(void)
tkstreet 0:a0435a630c5d 153 {
tkstreet 0:a0435a630c5d 154 setDirection(DIRECTION_COAST);
tkstreet 0:a0435a630c5d 155 return SUCCESS;
tkstreet 0:a0435a630c5d 156 }