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@4:8fcaff7801b0, 2013-12-02 (annotated)
- Committer:
- jaoramos
- Date:
- Mon Dec 02 04:38:30 2013 +0000
- Revision:
- 4:8fcaff7801b0
- Parent:
- 3:967aee5fed5b
- Child:
- 5:d41998e421ed
first run on system
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 | 4:8fcaff7801b0 | 7 | #define MOTOR_PPR 300 |
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 | 4:8fcaff7801b0 | 42 | float k1 = -0.0316, k2 = 9.7076, k3 = -0.4095, k4 = 1.2340, k5 = 0.0410; |
jaoramos | 1:5c05e0d08e61 | 43 | |
jaoramos | 1:5c05e0d08e61 | 44 | float encoder_conv = 2*OUR_PI/(float(ENCODER_PPR)*float(QUADRATURE_TYPE)); |
jaoramos | 1:5c05e0d08e61 | 45 | float motor_conv = 2*OUR_PI/(float(MOTOR_PPR)*float(QUADRATURE_TYPE)); |
jaoramos | 1:5c05e0d08e61 | 46 | |
jaoramos | 0:9f2b0ea63eac | 47 | float* buffer; |
jaoramos | 2:011e6115c77a | 48 | float lambda1 = 30, lambda2 = 30, lambda3 = 15; |
jaoramos | 0:9f2b0ea63eac | 49 | int index; |
jaoramos | 4:8fcaff7801b0 | 50 | int pulsesPend, pulsesMot; |
jaoramos | 0:9f2b0ea63eac | 51 | |
jaoramos | 0:9f2b0ea63eac | 52 | void saving(void const *args) { |
jaoramos | 0:9f2b0ea63eac | 53 | index = 0; |
jaoramos | 4:8fcaff7801b0 | 54 | while (index < buffer_size) { |
jaoramos | 1:5c05e0d08e61 | 55 | buffer[index] = theta1; |
jaoramos | 1:5c05e0d08e61 | 56 | buffer[index+1] = theta2; |
jaoramos | 1:5c05e0d08e61 | 57 | buffer[index+2] = dtheta1; |
jaoramos | 1:5c05e0d08e61 | 58 | buffer[index+3] = dtheta2; |
jaoramos | 1:5c05e0d08e61 | 59 | buffer[index+4] = mCurrent; |
jaoramos | 1:5c05e0d08e61 | 60 | buffer[index+5] = t; |
jaoramos | 4:8fcaff7801b0 | 61 | index = index + DATA_COLS; |
jaoramos | 1:5c05e0d08e61 | 62 | Thread::wait(20); |
jaoramos | 0:9f2b0ea63eac | 63 | } |
jaoramos | 0:9f2b0ea63eac | 64 | } |
jaoramos | 0:9f2b0ea63eac | 65 | |
jaoramos | 3:967aee5fed5b | 66 | void setVoltage(float inputVoltage) |
jaoramos | 3:967aee5fed5b | 67 | { |
jaoramos | 3:967aee5fed5b | 68 | if(inputVoltage<0.0) { |
jaoramos | 3:967aee5fed5b | 69 | inputVoltage = -inputVoltage; |
jaoramos | 3:967aee5fed5b | 70 | dOut1=0; |
jaoramos | 3:967aee5fed5b | 71 | dOut2=1; |
jaoramos | 3:967aee5fed5b | 72 | } else { |
jaoramos | 3:967aee5fed5b | 73 | dOut1=1; |
jaoramos | 3:967aee5fed5b | 74 | dOut2=0; |
jaoramos | 3:967aee5fed5b | 75 | } |
jaoramos | 3:967aee5fed5b | 76 | float dutyCycle = inputVoltage/MAX_VOLTAGE; |
jaoramos | 3:967aee5fed5b | 77 | dutyCycle = (dutyCycle > 1.0)? 1.0 : dutyCycle; |
jaoramos | 3:967aee5fed5b | 78 | pwmOut.write(dutyCycle); |
jaoramos | 3:967aee5fed5b | 79 | } |
jaoramos | 3:967aee5fed5b | 80 | |
jaoramos | 0:9f2b0ea63eac | 81 | void computing(void const *args) { |
jaoramos | 2:011e6115c77a | 82 | float z1 = 0.0, z2 = 0.0, dz1 = 0.0, dz2 = 0.0, z3 = 0.0, dz3 = 0.0; |
jaoramos | 4:8fcaff7801b0 | 83 | float inputVoltage; |
jaoramos | 4:8fcaff7801b0 | 84 | |
jaoramos | 0:9f2b0ea63eac | 85 | while (true) { |
jaoramos | 0:9f2b0ea63eac | 86 | t = T.read(); |
jaoramos | 1:5c05e0d08e61 | 87 | |
jaoramos | 2:011e6115c77a | 88 | //set pwm |
jaoramos | 4:8fcaff7801b0 | 89 | // ADD A SANITY CHECK ON THETA |
jaoramos | 4:8fcaff7801b0 | 90 | inputVoltage = -(k1*theta1 + k2*theta2 + k3*dtheta1 + k4*dtheta2 + k5*mCurrent); |
jaoramos | 4:8fcaff7801b0 | 91 | if (cos(theta2) < 0.98) |
jaoramos | 4:8fcaff7801b0 | 92 | inputVoltage = 0.0; |
jaoramos | 3:967aee5fed5b | 93 | setVoltage(inputVoltage); |
jaoramos | 2:011e6115c77a | 94 | |
jaoramos | 2:011e6115c77a | 95 | //read current |
jaoramos | 1:5c05e0d08e61 | 96 | mCurrent = aIn.read()*MAX_VOLTAGE/VOLTS_PER_AMP; |
jaoramos | 3:967aee5fed5b | 97 | if(dOut1 == 0) |
jaoramos | 3:967aee5fed5b | 98 | mCurrent = -mCurrent; |
jaoramos | 1:5c05e0d08e61 | 99 | pulsesPend = -encoder.getPulses(); |
jaoramos | 1:5c05e0d08e61 | 100 | pulsesMot = motor.getPulses(); |
jaoramos | 1:5c05e0d08e61 | 101 | |
jaoramos | 1:5c05e0d08e61 | 102 | dt = t - t0; //time difference |
jaoramos | 4:8fcaff7801b0 | 103 | theta2 = float(pulsesPend)*encoder_conv + OUR_PI; |
jaoramos | 1:5c05e0d08e61 | 104 | theta1 = float(pulsesMot)*motor_conv; |
jaoramos | 0:9f2b0ea63eac | 105 | |
jaoramos | 1:5c05e0d08e61 | 106 | //calculate dtheta1 |
jaoramos | 2:011e6115c77a | 107 | dz1 = - lambda1 * z1 + lambda1 * theta1; |
jaoramos | 1:5c05e0d08e61 | 108 | z1 = z1 + dz1 * dt; |
jaoramos | 1:5c05e0d08e61 | 109 | dtheta1 = dz1; |
jaoramos | 0:9f2b0ea63eac | 110 | |
jaoramos | 1:5c05e0d08e61 | 111 | //calculate dtheta2 |
jaoramos | 2:011e6115c77a | 112 | dz2 = - lambda2 * z2 + lambda2 * theta2; |
jaoramos | 1:5c05e0d08e61 | 113 | z2 = z2 + dz2 * dt; |
jaoramos | 1:5c05e0d08e61 | 114 | dtheta2 = dz2; |
jaoramos | 2:011e6115c77a | 115 | |
jaoramos | 2:011e6115c77a | 116 | //filter current |
jaoramos | 2:011e6115c77a | 117 | dz3 = -lambda3 * z3 + lambda3 * mCurrent; |
jaoramos | 2:011e6115c77a | 118 | z3 = z3 + dz3 * dt; |
jaoramos | 2:011e6115c77a | 119 | mCurrent = z3; |
jaoramos | 2:011e6115c77a | 120 | |
jaoramos | 0:9f2b0ea63eac | 121 | t0 = t; |
jaoramos | 0:9f2b0ea63eac | 122 | Thread::wait(1); |
jaoramos | 0:9f2b0ea63eac | 123 | } |
jaoramos | 0:9f2b0ea63eac | 124 | } |
jaoramos | 0:9f2b0ea63eac | 125 | |
jaoramos | 0:9f2b0ea63eac | 126 | void saveToFile () |
jaoramos | 0:9f2b0ea63eac | 127 | { |
jaoramos | 0:9f2b0ea63eac | 128 | FILE *fp = fopen("/local/data.csv", "w"); |
jaoramos | 0:9f2b0ea63eac | 129 | if (!fp) { |
jaoramos | 0:9f2b0ea63eac | 130 | fprintf(stderr, "File could not be openend \n\r"); |
jaoramos | 0:9f2b0ea63eac | 131 | exit(1); |
jaoramos | 0:9f2b0ea63eac | 132 | } |
jaoramos | 0:9f2b0ea63eac | 133 | |
jaoramos | 0:9f2b0ea63eac | 134 | wait(2.0); |
jaoramos | 0:9f2b0ea63eac | 135 | |
jaoramos | 1:5c05e0d08e61 | 136 | for (int i=0; i < index; i=i+DATA_COLS) |
jaoramos | 1:5c05e0d08e61 | 137 | { |
jaoramos | 1:5c05e0d08e61 | 138 | for (int j = 0; j< DATA_COLS; j++) |
jaoramos | 1:5c05e0d08e61 | 139 | { |
jaoramos | 1:5c05e0d08e61 | 140 | fprintf(fp,"%f,", buffer[i+j]); |
jaoramos | 1:5c05e0d08e61 | 141 | } |
jaoramos | 1:5c05e0d08e61 | 142 | fprintf(fp,"\n"); |
jaoramos | 0:9f2b0ea63eac | 143 | } |
jaoramos | 0:9f2b0ea63eac | 144 | pc.printf("closing file\n\r"); |
jaoramos | 0:9f2b0ea63eac | 145 | fclose(fp); |
jaoramos | 0:9f2b0ea63eac | 146 | wait(2.0);; |
jaoramos | 0:9f2b0ea63eac | 147 | } |
jaoramos | 0:9f2b0ea63eac | 148 | |
jaoramos | 0:9f2b0ea63eac | 149 | int main() { |
jaoramos | 0:9f2b0ea63eac | 150 | //allocate memory for the buffer |
jaoramos | 1:5c05e0d08e61 | 151 | pc.printf("creating buffer!\r\n"); |
jaoramos | 1:5c05e0d08e61 | 152 | buffer = new float[buffer_size]; |
jaoramos | 1:5c05e0d08e61 | 153 | pc.printf("done creating buffer!\r\n"); |
jaoramos | 0:9f2b0ea63eac | 154 | T.start(); |
jaoramos | 0:9f2b0ea63eac | 155 | Thread thrd2(computing,NULL,osPriorityRealtime); |
jaoramos | 0:9f2b0ea63eac | 156 | Thread thrd3(saving,NULL,osPriorityNormal); |
jaoramos | 0:9f2b0ea63eac | 157 | |
jaoramos | 2:011e6115c77a | 158 | //Run forward |
jaoramos | 2:011e6115c77a | 159 | pwmOut.period(0.0001); |
jaoramos | 2:011e6115c77a | 160 | dOut1=1; |
jaoramos | 2:011e6115c77a | 161 | dOut2=0; |
jaoramos | 2:011e6115c77a | 162 | |
jaoramos | 0:9f2b0ea63eac | 163 | pc.printf("Start!\r\n"); |
jaoramos | 0:9f2b0ea63eac | 164 | pc.printf("Time: %f\r\n", t); |
jaoramos | 0:9f2b0ea63eac | 165 | while (t < 10.0) |
jaoramos | 0:9f2b0ea63eac | 166 | { |
jaoramos | 4:8fcaff7801b0 | 167 | //pc.printf("Time: %f\r\n", t); |
jaoramos | 0:9f2b0ea63eac | 168 | Thread::wait(1000); |
jaoramos | 0:9f2b0ea63eac | 169 | } |
jaoramos | 2:011e6115c77a | 170 | pc.printf("Done at Index: %d\r\n",index); |
jaoramos | 2:011e6115c77a | 171 | pwmOut.write(0.0); |
jaoramos | 0:9f2b0ea63eac | 172 | thrd2.terminate(); |
jaoramos | 0:9f2b0ea63eac | 173 | thrd3.terminate(); |
jaoramos | 0:9f2b0ea63eac | 174 | saveToFile(); |
jaoramos | 0:9f2b0ea63eac | 175 | } |