Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: QEI mbed-rtos mbed
main.cpp@3:967aee5fed5b, 2013-12-02 (annotated)
- Committer:
- jaoramos
- Date:
- Mon Dec 02 02:22:37 2013 +0000
- Revision:
- 3:967aee5fed5b
- Parent:
- 2:011e6115c77a
- Child:
- 4:8fcaff7801b0
added currents direction and a voltage function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaoramos | 0:9f2b0ea63eac | 1 | #include "mbed.h" |
jaoramos | 0:9f2b0ea63eac | 2 | #include "rtos.h" |
jaoramos | 0:9f2b0ea63eac | 3 | #include "QEI.h" |
jaoramos | 2:011e6115c77a | 4 | #include <fstream> |
jaoramos | 2:011e6115c77a | 5 | #include <iomanip> |
jaoramos | 1:5c05e0d08e61 | 6 | |
jaoramos | 1:5c05e0d08e61 | 7 | #define MOTOR_PPR 1200 |
jaoramos | 1:5c05e0d08e61 | 8 | #define ENCODER_PPR 1024 |
jaoramos | 1:5c05e0d08e61 | 9 | |
jaoramos | 1:5c05e0d08e61 | 10 | #define QUADRATURE_TYPE 2 |
jaoramos | 1:5c05e0d08e61 | 11 | #define OUR_PI 3.141592653589793 |
jaoramos | 1:5c05e0d08e61 | 12 | #define DATA_COLS 6 |
jaoramos | 1:5c05e0d08e61 | 13 | #define NR_SAMPLES 1500 |
jaoramos | 1:5c05e0d08e61 | 14 | #define buffer_size 3500 |
jaoramos | 1:5c05e0d08e61 | 15 | #define MAX_VOLTAGE 3.3 |
jaoramos | 1:5c05e0d08e61 | 16 | #define VOLTS_PER_AMP 0.14 |
jaoramos | 1:5c05e0d08e61 | 17 | |
jaoramos | 0:9f2b0ea63eac | 18 | |
jaoramos | 0:9f2b0ea63eac | 19 | Serial pc(USBTX, USBRX); |
jaoramos | 0:9f2b0ea63eac | 20 | |
jaoramos | 1:5c05e0d08e61 | 21 | QEI encoder(p29, p30, NC, ENCODER_PPR); |
jaoramos | 1:5c05e0d08e61 | 22 | QEI motor(p25, p26, NC, MOTOR_PPR); |
jaoramos | 0:9f2b0ea63eac | 23 | Timer T; |
jaoramos | 0:9f2b0ea63eac | 24 | |
jaoramos | 1:5c05e0d08e61 | 25 | //Curent Measurement |
jaoramos | 1:5c05e0d08e61 | 26 | AnalogIn aIn(p16); //pin 15 set as analog input. Pins 15-20 can be used as analog inputs. |
jaoramos | 1:5c05e0d08e61 | 27 | |
jaoramos | 2:011e6115c77a | 28 | //Motor direction and PWM |
jaoramos | 2:011e6115c77a | 29 | DigitalOut dOut1(p5); |
jaoramos | 2:011e6115c77a | 30 | DigitalOut dOut2(p7); |
jaoramos | 2:011e6115c77a | 31 | PwmOut pwmOut(p21); |
jaoramos | 1:5c05e0d08e61 | 32 | |
jaoramos | 0:9f2b0ea63eac | 33 | // open a file for data logger |
jaoramos | 0:9f2b0ea63eac | 34 | LocalFileSystem local("local"); |
jaoramos | 1:5c05e0d08e61 | 35 | //const int buffer_size = DATA_COLS * NR_SAMPLES; |
jaoramos | 1:5c05e0d08e61 | 36 | float theta1, theta2, dtheta1, dtheta2; |
jaoramos | 1:5c05e0d08e61 | 37 | float mCurrent = 0.0; |
jaoramos | 1:5c05e0d08e61 | 38 | //int pulses0 = 0; |
jaoramos | 1:5c05e0d08e61 | 39 | //int deltaPulses; |
jaoramos | 0:9f2b0ea63eac | 40 | float t0 = 0.0; |
jaoramos | 1:5c05e0d08e61 | 41 | float t = 0.0, dt; |
jaoramos | 1:5c05e0d08e61 | 42 | |
jaoramos | 1:5c05e0d08e61 | 43 | float encoder_conv = 2*OUR_PI/(float(ENCODER_PPR)*float(QUADRATURE_TYPE)); |
jaoramos | 1:5c05e0d08e61 | 44 | float motor_conv = 2*OUR_PI/(float(MOTOR_PPR)*float(QUADRATURE_TYPE)); |
jaoramos | 1:5c05e0d08e61 | 45 | |
jaoramos | 0:9f2b0ea63eac | 46 | float* buffer; |
jaoramos | 2:011e6115c77a | 47 | float lambda1 = 30, lambda2 = 30, lambda3 = 15; |
jaoramos | 0:9f2b0ea63eac | 48 | int index; |
jaoramos | 0:9f2b0ea63eac | 49 | |
jaoramos | 0:9f2b0ea63eac | 50 | void saving(void const *args) { |
jaoramos | 0:9f2b0ea63eac | 51 | index = 0; |
jaoramos | 0:9f2b0ea63eac | 52 | while (true) { |
jaoramos | 1:5c05e0d08e61 | 53 | buffer[index] = theta1; |
jaoramos | 1:5c05e0d08e61 | 54 | buffer[index+1] = theta2; |
jaoramos | 1:5c05e0d08e61 | 55 | buffer[index+2] = dtheta1; |
jaoramos | 1:5c05e0d08e61 | 56 | buffer[index+3] = dtheta2; |
jaoramos | 1:5c05e0d08e61 | 57 | buffer[index+4] = mCurrent; |
jaoramos | 1:5c05e0d08e61 | 58 | buffer[index+5] = t; |
jaoramos | 1:5c05e0d08e61 | 59 | |
jaoramos | 1:5c05e0d08e61 | 60 | index = index+DATA_COLS; |
jaoramos | 1:5c05e0d08e61 | 61 | Thread::wait(20); |
jaoramos | 0:9f2b0ea63eac | 62 | } |
jaoramos | 0:9f2b0ea63eac | 63 | } |
jaoramos | 0:9f2b0ea63eac | 64 | |
jaoramos | 3:967aee5fed5b | 65 | void setVoltage(float inputVoltage) |
jaoramos | 3:967aee5fed5b | 66 | { |
jaoramos | 3:967aee5fed5b | 67 | if(inputVoltage<0.0) { |
jaoramos | 3:967aee5fed5b | 68 | inputVoltage = -inputVoltage; |
jaoramos | 3:967aee5fed5b | 69 | dOut1=0; |
jaoramos | 3:967aee5fed5b | 70 | dOut2=1; |
jaoramos | 3:967aee5fed5b | 71 | } else { |
jaoramos | 3:967aee5fed5b | 72 | dOut1=1; |
jaoramos | 3:967aee5fed5b | 73 | dOut2=0; |
jaoramos | 3:967aee5fed5b | 74 | } |
jaoramos | 3:967aee5fed5b | 75 | float dutyCycle = inputVoltage/MAX_VOLTAGE; |
jaoramos | 3:967aee5fed5b | 76 | dutyCycle = (dutyCycle > 1.0)? 1.0 : dutyCycle; |
jaoramos | 3:967aee5fed5b | 77 | pwmOut.write(dutyCycle); |
jaoramos | 3:967aee5fed5b | 78 | } |
jaoramos | 3:967aee5fed5b | 79 | |
jaoramos | 0:9f2b0ea63eac | 80 | void computing(void const *args) { |
jaoramos | 2:011e6115c77a | 81 | float z1 = 0.0, z2 = 0.0, dz1 = 0.0, dz2 = 0.0, z3 = 0.0, dz3 = 0.0; |
jaoramos | 3:967aee5fed5b | 82 | float freq = 2.0, inputVoltage; |
jaoramos | 1:5c05e0d08e61 | 83 | int pulsesPend, pulsesMot; |
jaoramos | 0:9f2b0ea63eac | 84 | while (true) { |
jaoramos | 0:9f2b0ea63eac | 85 | t = T.read(); |
jaoramos | 1:5c05e0d08e61 | 86 | |
jaoramos | 2:011e6115c77a | 87 | //set pwm |
jaoramos | 3:967aee5fed5b | 88 | inputVoltage = MAX_VOLTAGE * (sin(freq*t)); |
jaoramos | 2:011e6115c77a | 89 | //pc.printf("Duty%f\n\r",dutyCycle); |
jaoramos | 3:967aee5fed5b | 90 | setVoltage(inputVoltage); |
jaoramos | 2:011e6115c77a | 91 | |
jaoramos | 2:011e6115c77a | 92 | //read current |
jaoramos | 1:5c05e0d08e61 | 93 | mCurrent = aIn.read()*MAX_VOLTAGE/VOLTS_PER_AMP; |
jaoramos | 3:967aee5fed5b | 94 | if(dOut1 == 0) |
jaoramos | 3:967aee5fed5b | 95 | mCurrent = -mCurrent; |
jaoramos | 1:5c05e0d08e61 | 96 | pulsesPend = -encoder.getPulses(); |
jaoramos | 1:5c05e0d08e61 | 97 | pulsesMot = motor.getPulses(); |
jaoramos | 1:5c05e0d08e61 | 98 | |
jaoramos | 1:5c05e0d08e61 | 99 | dt = t - t0; //time difference |
jaoramos | 1:5c05e0d08e61 | 100 | theta2 = float(pulsesPend)*encoder_conv; |
jaoramos | 1:5c05e0d08e61 | 101 | theta1 = float(pulsesMot)*motor_conv; |
jaoramos | 0:9f2b0ea63eac | 102 | |
jaoramos | 1:5c05e0d08e61 | 103 | //calculate dtheta1 |
jaoramos | 2:011e6115c77a | 104 | dz1 = - lambda1 * z1 + lambda1 * theta1; |
jaoramos | 1:5c05e0d08e61 | 105 | z1 = z1 + dz1 * dt; |
jaoramos | 1:5c05e0d08e61 | 106 | dtheta1 = dz1; |
jaoramos | 0:9f2b0ea63eac | 107 | |
jaoramos | 1:5c05e0d08e61 | 108 | //calculate dtheta2 |
jaoramos | 2:011e6115c77a | 109 | dz2 = - lambda2 * z2 + lambda2 * theta2; |
jaoramos | 1:5c05e0d08e61 | 110 | z2 = z2 + dz2 * dt; |
jaoramos | 1:5c05e0d08e61 | 111 | dtheta2 = dz2; |
jaoramos | 2:011e6115c77a | 112 | |
jaoramos | 2:011e6115c77a | 113 | //filter current |
jaoramos | 2:011e6115c77a | 114 | dz3 = -lambda3 * z3 + lambda3 * mCurrent; |
jaoramos | 2:011e6115c77a | 115 | z3 = z3 + dz3 * dt; |
jaoramos | 2:011e6115c77a | 116 | mCurrent = z3; |
jaoramos | 2:011e6115c77a | 117 | |
jaoramos | 0:9f2b0ea63eac | 118 | t0 = t; |
jaoramos | 0:9f2b0ea63eac | 119 | Thread::wait(1); |
jaoramos | 0:9f2b0ea63eac | 120 | } |
jaoramos | 0:9f2b0ea63eac | 121 | } |
jaoramos | 0:9f2b0ea63eac | 122 | |
jaoramos | 0:9f2b0ea63eac | 123 | void saveToFile () |
jaoramos | 0:9f2b0ea63eac | 124 | { |
jaoramos | 0:9f2b0ea63eac | 125 | FILE *fp = fopen("/local/data.csv", "w"); |
jaoramos | 0:9f2b0ea63eac | 126 | if (!fp) { |
jaoramos | 0:9f2b0ea63eac | 127 | fprintf(stderr, "File could not be openend \n\r"); |
jaoramos | 0:9f2b0ea63eac | 128 | exit(1); |
jaoramos | 0:9f2b0ea63eac | 129 | } |
jaoramos | 0:9f2b0ea63eac | 130 | |
jaoramos | 0:9f2b0ea63eac | 131 | wait(2.0); |
jaoramos | 0:9f2b0ea63eac | 132 | |
jaoramos | 1:5c05e0d08e61 | 133 | for (int i=0; i < index; i=i+DATA_COLS) |
jaoramos | 1:5c05e0d08e61 | 134 | { |
jaoramos | 1:5c05e0d08e61 | 135 | for (int j = 0; j< DATA_COLS; j++) |
jaoramos | 1:5c05e0d08e61 | 136 | { |
jaoramos | 1:5c05e0d08e61 | 137 | fprintf(fp,"%f,", buffer[i+j]); |
jaoramos | 1:5c05e0d08e61 | 138 | } |
jaoramos | 1:5c05e0d08e61 | 139 | fprintf(fp,"\n"); |
jaoramos | 0:9f2b0ea63eac | 140 | } |
jaoramos | 0:9f2b0ea63eac | 141 | pc.printf("closing file\n\r"); |
jaoramos | 0:9f2b0ea63eac | 142 | fclose(fp); |
jaoramos | 0:9f2b0ea63eac | 143 | wait(2.0);; |
jaoramos | 0:9f2b0ea63eac | 144 | } |
jaoramos | 0:9f2b0ea63eac | 145 | |
jaoramos | 0:9f2b0ea63eac | 146 | int main() { |
jaoramos | 0:9f2b0ea63eac | 147 | //allocate memory for the buffer |
jaoramos | 1:5c05e0d08e61 | 148 | pc.printf("creating buffer!\r\n"); |
jaoramos | 1:5c05e0d08e61 | 149 | buffer = new float[buffer_size]; |
jaoramos | 1:5c05e0d08e61 | 150 | pc.printf("done creating buffer!\r\n"); |
jaoramos | 0:9f2b0ea63eac | 151 | T.start(); |
jaoramos | 0:9f2b0ea63eac | 152 | Thread thrd2(computing,NULL,osPriorityRealtime); |
jaoramos | 0:9f2b0ea63eac | 153 | Thread thrd3(saving,NULL,osPriorityNormal); |
jaoramos | 0:9f2b0ea63eac | 154 | |
jaoramos | 2:011e6115c77a | 155 | //Run forward |
jaoramos | 2:011e6115c77a | 156 | pwmOut.period(0.0001); |
jaoramos | 2:011e6115c77a | 157 | dOut1=1; |
jaoramos | 2:011e6115c77a | 158 | dOut2=0; |
jaoramos | 2:011e6115c77a | 159 | |
jaoramos | 2:011e6115c77a | 160 | |
jaoramos | 0:9f2b0ea63eac | 161 | pc.printf("Start!\r\n"); |
jaoramos | 0:9f2b0ea63eac | 162 | pc.printf("Time: %f\r\n", t); |
jaoramos | 0:9f2b0ea63eac | 163 | while (t < 10.0) |
jaoramos | 0:9f2b0ea63eac | 164 | { |
jaoramos | 0:9f2b0ea63eac | 165 | pc.printf("Time: %f\r\n", t); |
jaoramos | 0:9f2b0ea63eac | 166 | Thread::wait(1000); |
jaoramos | 0:9f2b0ea63eac | 167 | } |
jaoramos | 2:011e6115c77a | 168 | pc.printf("Done at Index: %d\r\n",index); |
jaoramos | 2:011e6115c77a | 169 | pwmOut.write(0.0); |
jaoramos | 0:9f2b0ea63eac | 170 | thrd2.terminate(); |
jaoramos | 0:9f2b0ea63eac | 171 | thrd3.terminate(); |
jaoramos | 0:9f2b0ea63eac | 172 | saveToFile(); |
jaoramos | 0:9f2b0ea63eac | 173 | } |