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@5:d41998e421ed, 2013-12-03 (annotated)
- Committer:
- jaoramos
- Date:
- Tue Dec 03 16:16:14 2013 +0000
- Revision:
- 5:d41998e421ed
- Parent:
- 4:8fcaff7801b0
- Child:
- 6:16da0de99a8c
stable control with rattling on x2 encoding and second iteration on the gains
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 | 5:d41998e421ed | 12 | #define DATA_COLS 7 |
jaoramos | 5:d41998e421ed | 13 | #define BUFFER_SIZE 4200 |
jaoramos | 1:5c05e0d08e61 | 14 | #define MAX_VOLTAGE 3.3 |
jaoramos | 1:5c05e0d08e61 | 15 | #define VOLTS_PER_AMP 0.14 |
jaoramos | 1:5c05e0d08e61 | 16 | |
jaoramos | 0:9f2b0ea63eac | 17 | |
jaoramos | 0:9f2b0ea63eac | 18 | Serial pc(USBTX, USBRX); |
jaoramos | 0:9f2b0ea63eac | 19 | |
jaoramos | 1:5c05e0d08e61 | 20 | QEI encoder(p29, p30, NC, ENCODER_PPR); |
jaoramos | 1:5c05e0d08e61 | 21 | QEI motor(p25, p26, NC, MOTOR_PPR); |
jaoramos | 0:9f2b0ea63eac | 22 | Timer T; |
jaoramos | 0:9f2b0ea63eac | 23 | |
jaoramos | 1:5c05e0d08e61 | 24 | //Curent Measurement |
jaoramos | 1:5c05e0d08e61 | 25 | AnalogIn aIn(p16); //pin 15 set as analog input. Pins 15-20 can be used as analog inputs. |
jaoramos | 1:5c05e0d08e61 | 26 | |
jaoramos | 2:011e6115c77a | 27 | //Motor direction and PWM |
jaoramos | 2:011e6115c77a | 28 | DigitalOut dOut1(p5); |
jaoramos | 2:011e6115c77a | 29 | DigitalOut dOut2(p7); |
jaoramos | 2:011e6115c77a | 30 | PwmOut pwmOut(p21); |
jaoramos | 1:5c05e0d08e61 | 31 | |
jaoramos | 0:9f2b0ea63eac | 32 | // open a file for data logger |
jaoramos | 0:9f2b0ea63eac | 33 | LocalFileSystem local("local"); |
jaoramos | 1:5c05e0d08e61 | 34 | float theta1, theta2, dtheta1, dtheta2; |
jaoramos | 1:5c05e0d08e61 | 35 | float mCurrent = 0.0; |
jaoramos | 5:d41998e421ed | 36 | float inputVoltage = 0.0; |
jaoramos | 1:5c05e0d08e61 | 37 | //int pulses0 = 0; |
jaoramos | 1:5c05e0d08e61 | 38 | //int deltaPulses; |
jaoramos | 0:9f2b0ea63eac | 39 | float t0 = 0.0; |
jaoramos | 1:5c05e0d08e61 | 40 | float t = 0.0, dt; |
jaoramos | 5:d41998e421ed | 41 | |
jaoramos | 5:d41998e421ed | 42 | //Controller gains - Full-state Feedback |
jaoramos | 5:d41998e421ed | 43 | //float k1 = -0.0316, k2 = 9.7076, k3 = -0.4095, k4 = 1.2340, k5 = 0.0410; |
jaoramos | 5:d41998e421ed | 44 | float k1 = -0.3162, k2 = 18.278, k3 = -0.8964, k4 = 2.4441, k5 = 0.1843; |
jaoramos | 1:5c05e0d08e61 | 45 | |
jaoramos | 1:5c05e0d08e61 | 46 | float encoder_conv = 2*OUR_PI/(float(ENCODER_PPR)*float(QUADRATURE_TYPE)); |
jaoramos | 1:5c05e0d08e61 | 47 | float motor_conv = 2*OUR_PI/(float(MOTOR_PPR)*float(QUADRATURE_TYPE)); |
jaoramos | 1:5c05e0d08e61 | 48 | |
jaoramos | 0:9f2b0ea63eac | 49 | float* buffer; |
jaoramos | 2:011e6115c77a | 50 | float lambda1 = 30, lambda2 = 30, lambda3 = 15; |
jaoramos | 0:9f2b0ea63eac | 51 | int index; |
jaoramos | 4:8fcaff7801b0 | 52 | int pulsesPend, pulsesMot; |
jaoramos | 0:9f2b0ea63eac | 53 | |
jaoramos | 0:9f2b0ea63eac | 54 | void saving(void const *args) { |
jaoramos | 0:9f2b0ea63eac | 55 | index = 0; |
jaoramos | 5:d41998e421ed | 56 | while (index < BUFFER_SIZE) { |
jaoramos | 1:5c05e0d08e61 | 57 | buffer[index] = theta1; |
jaoramos | 1:5c05e0d08e61 | 58 | buffer[index+1] = theta2; |
jaoramos | 1:5c05e0d08e61 | 59 | buffer[index+2] = dtheta1; |
jaoramos | 1:5c05e0d08e61 | 60 | buffer[index+3] = dtheta2; |
jaoramos | 1:5c05e0d08e61 | 61 | buffer[index+4] = mCurrent; |
jaoramos | 5:d41998e421ed | 62 | buffer[index+5] = inputVoltage; |
jaoramos | 5:d41998e421ed | 63 | buffer[index+6] = t; |
jaoramos | 4:8fcaff7801b0 | 64 | index = index + DATA_COLS; |
jaoramos | 1:5c05e0d08e61 | 65 | Thread::wait(20); |
jaoramos | 0:9f2b0ea63eac | 66 | } |
jaoramos | 0:9f2b0ea63eac | 67 | } |
jaoramos | 0:9f2b0ea63eac | 68 | |
jaoramos | 3:967aee5fed5b | 69 | void setVoltage(float inputVoltage) |
jaoramos | 3:967aee5fed5b | 70 | { |
jaoramos | 3:967aee5fed5b | 71 | if(inputVoltage<0.0) { |
jaoramos | 3:967aee5fed5b | 72 | inputVoltage = -inputVoltage; |
jaoramos | 3:967aee5fed5b | 73 | dOut1=0; |
jaoramos | 3:967aee5fed5b | 74 | dOut2=1; |
jaoramos | 3:967aee5fed5b | 75 | } else { |
jaoramos | 3:967aee5fed5b | 76 | dOut1=1; |
jaoramos | 3:967aee5fed5b | 77 | dOut2=0; |
jaoramos | 3:967aee5fed5b | 78 | } |
jaoramos | 3:967aee5fed5b | 79 | float dutyCycle = inputVoltage/MAX_VOLTAGE; |
jaoramos | 3:967aee5fed5b | 80 | dutyCycle = (dutyCycle > 1.0)? 1.0 : dutyCycle; |
jaoramos | 3:967aee5fed5b | 81 | pwmOut.write(dutyCycle); |
jaoramos | 3:967aee5fed5b | 82 | } |
jaoramos | 3:967aee5fed5b | 83 | |
jaoramos | 0:9f2b0ea63eac | 84 | void computing(void const *args) { |
jaoramos | 5:d41998e421ed | 85 | float z1 = 0.0, z2 = 0.0, dz1 = 0.0, dz2 = 0.0, z3 = 0.0, dz3 = 0.0; |
jaoramos | 4:8fcaff7801b0 | 86 | |
jaoramos | 0:9f2b0ea63eac | 87 | while (true) { |
jaoramos | 0:9f2b0ea63eac | 88 | t = T.read(); |
jaoramos | 1:5c05e0d08e61 | 89 | |
jaoramos | 2:011e6115c77a | 90 | //set pwm |
jaoramos | 4:8fcaff7801b0 | 91 | // ADD A SANITY CHECK ON THETA |
jaoramos | 4:8fcaff7801b0 | 92 | inputVoltage = -(k1*theta1 + k2*theta2 + k3*dtheta1 + k4*dtheta2 + k5*mCurrent); |
jaoramos | 4:8fcaff7801b0 | 93 | if (cos(theta2) < 0.98) |
jaoramos | 4:8fcaff7801b0 | 94 | inputVoltage = 0.0; |
jaoramos | 3:967aee5fed5b | 95 | setVoltage(inputVoltage); |
jaoramos | 2:011e6115c77a | 96 | |
jaoramos | 2:011e6115c77a | 97 | //read current |
jaoramos | 1:5c05e0d08e61 | 98 | mCurrent = aIn.read()*MAX_VOLTAGE/VOLTS_PER_AMP; |
jaoramos | 3:967aee5fed5b | 99 | if(dOut1 == 0) |
jaoramos | 3:967aee5fed5b | 100 | mCurrent = -mCurrent; |
jaoramos | 1:5c05e0d08e61 | 101 | pulsesPend = -encoder.getPulses(); |
jaoramos | 1:5c05e0d08e61 | 102 | pulsesMot = motor.getPulses(); |
jaoramos | 1:5c05e0d08e61 | 103 | |
jaoramos | 1:5c05e0d08e61 | 104 | dt = t - t0; //time difference |
jaoramos | 4:8fcaff7801b0 | 105 | theta2 = float(pulsesPend)*encoder_conv + OUR_PI; |
jaoramos | 1:5c05e0d08e61 | 106 | theta1 = float(pulsesMot)*motor_conv; |
jaoramos | 0:9f2b0ea63eac | 107 | |
jaoramos | 1:5c05e0d08e61 | 108 | //calculate dtheta1 |
jaoramos | 2:011e6115c77a | 109 | dz1 = - lambda1 * z1 + lambda1 * theta1; |
jaoramos | 1:5c05e0d08e61 | 110 | z1 = z1 + dz1 * dt; |
jaoramos | 1:5c05e0d08e61 | 111 | dtheta1 = dz1; |
jaoramos | 0:9f2b0ea63eac | 112 | |
jaoramos | 1:5c05e0d08e61 | 113 | //calculate dtheta2 |
jaoramos | 2:011e6115c77a | 114 | dz2 = - lambda2 * z2 + lambda2 * theta2; |
jaoramos | 1:5c05e0d08e61 | 115 | z2 = z2 + dz2 * dt; |
jaoramos | 1:5c05e0d08e61 | 116 | dtheta2 = dz2; |
jaoramos | 2:011e6115c77a | 117 | |
jaoramos | 2:011e6115c77a | 118 | //filter current |
jaoramos | 2:011e6115c77a | 119 | dz3 = -lambda3 * z3 + lambda3 * mCurrent; |
jaoramos | 2:011e6115c77a | 120 | z3 = z3 + dz3 * dt; |
jaoramos | 2:011e6115c77a | 121 | mCurrent = z3; |
jaoramos | 2:011e6115c77a | 122 | |
jaoramos | 0:9f2b0ea63eac | 123 | t0 = t; |
jaoramos | 0:9f2b0ea63eac | 124 | Thread::wait(1); |
jaoramos | 0:9f2b0ea63eac | 125 | } |
jaoramos | 0:9f2b0ea63eac | 126 | } |
jaoramos | 0:9f2b0ea63eac | 127 | |
jaoramos | 0:9f2b0ea63eac | 128 | void saveToFile () |
jaoramos | 0:9f2b0ea63eac | 129 | { |
jaoramos | 0:9f2b0ea63eac | 130 | FILE *fp = fopen("/local/data.csv", "w"); |
jaoramos | 0:9f2b0ea63eac | 131 | if (!fp) { |
jaoramos | 0:9f2b0ea63eac | 132 | fprintf(stderr, "File could not be openend \n\r"); |
jaoramos | 0:9f2b0ea63eac | 133 | exit(1); |
jaoramos | 0:9f2b0ea63eac | 134 | } |
jaoramos | 0:9f2b0ea63eac | 135 | |
jaoramos | 0:9f2b0ea63eac | 136 | wait(2.0); |
jaoramos | 0:9f2b0ea63eac | 137 | |
jaoramos | 5:d41998e421ed | 138 | for (int i=0; i < index; i = i + DATA_COLS) |
jaoramos | 1:5c05e0d08e61 | 139 | { |
jaoramos | 5:d41998e421ed | 140 | for (int j = 0; j < DATA_COLS; j++) |
jaoramos | 1:5c05e0d08e61 | 141 | { |
jaoramos | 1:5c05e0d08e61 | 142 | fprintf(fp,"%f,", buffer[i+j]); |
jaoramos | 1:5c05e0d08e61 | 143 | } |
jaoramos | 1:5c05e0d08e61 | 144 | fprintf(fp,"\n"); |
jaoramos | 0:9f2b0ea63eac | 145 | } |
jaoramos | 0:9f2b0ea63eac | 146 | pc.printf("closing file\n\r"); |
jaoramos | 0:9f2b0ea63eac | 147 | fclose(fp); |
jaoramos | 0:9f2b0ea63eac | 148 | wait(2.0);; |
jaoramos | 0:9f2b0ea63eac | 149 | } |
jaoramos | 0:9f2b0ea63eac | 150 | |
jaoramos | 0:9f2b0ea63eac | 151 | int main() { |
jaoramos | 0:9f2b0ea63eac | 152 | //allocate memory for the buffer |
jaoramos | 1:5c05e0d08e61 | 153 | pc.printf("creating buffer!\r\n"); |
jaoramos | 5:d41998e421ed | 154 | buffer = new float[BUFFER_SIZE]; |
jaoramos | 1:5c05e0d08e61 | 155 | pc.printf("done creating buffer!\r\n"); |
jaoramos | 0:9f2b0ea63eac | 156 | T.start(); |
jaoramos | 5:d41998e421ed | 157 | pwmOut.period(0.0001); |
jaoramos | 0:9f2b0ea63eac | 158 | |
jaoramos | 5:d41998e421ed | 159 | Thread thrd2(computing,NULL,osPriorityRealtime); |
jaoramos | 5:d41998e421ed | 160 | pc.printf("started computing thread!\r\n"); |
jaoramos | 5:d41998e421ed | 161 | Thread thrd3(saving,NULL,osPriorityNormal); |
jaoramos | 5:d41998e421ed | 162 | pc.printf("started saving thread!\r\n"); |
jaoramos | 5:d41998e421ed | 163 | |
jaoramos | 2:011e6115c77a | 164 | |
jaoramos | 0:9f2b0ea63eac | 165 | pc.printf("Start!\r\n"); |
jaoramos | 0:9f2b0ea63eac | 166 | pc.printf("Time: %f\r\n", t); |
jaoramos | 0:9f2b0ea63eac | 167 | while (t < 10.0) |
jaoramos | 0:9f2b0ea63eac | 168 | { |
jaoramos | 4:8fcaff7801b0 | 169 | //pc.printf("Time: %f\r\n", t); |
jaoramos | 0:9f2b0ea63eac | 170 | Thread::wait(1000); |
jaoramos | 0:9f2b0ea63eac | 171 | } |
jaoramos | 2:011e6115c77a | 172 | pc.printf("Done at Index: %d\r\n",index); |
jaoramos | 2:011e6115c77a | 173 | pwmOut.write(0.0); |
jaoramos | 0:9f2b0ea63eac | 174 | thrd2.terminate(); |
jaoramos | 0:9f2b0ea63eac | 175 | thrd3.terminate(); |
jaoramos | 0:9f2b0ea63eac | 176 | saveToFile(); |
jaoramos | 0:9f2b0ea63eac | 177 | } |