program 1 driver
Dependencies: mbed motorController2
main.cpp@3:e3b61eb0590b, 2016-12-09 (annotated)
- Committer:
- BalB
- Date:
- Fri Dec 09 17:50:58 2016 +0000
- Revision:
- 3:e3b61eb0590b
- Parent:
- 2:50b081df251a
- Child:
- 4:6956b568c810
1r programa con los 2 drivers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BalB | 0:1d2060237094 | 1 | #include "mbed.h" |
BalB | 1:3370c513ff5c | 2 | #include "AccelStepper.h" |
BalB | 1:3370c513ff5c | 3 | #include "math.h" |
BalB | 0:1d2060237094 | 4 | |
BalB | 0:1d2060237094 | 5 | // |
BalB | 3:e3b61eb0590b | 6 | // By BalB 9-12-16 |
BalB | 0:1d2060237094 | 7 | // |
BalB | 0:1d2060237094 | 8 | #include "mbed.h" |
BalB | 0:1d2060237094 | 9 | AnalogIn top_left(A0); // Analog InPut |
BalB | 0:1d2060237094 | 10 | AnalogIn top_right(A1); // Analog InPut |
BalB | 0:1d2060237094 | 11 | AnalogIn bot_left(A2); // Analog InPut |
BalB | 0:1d2060237094 | 12 | AnalogIn bot_right(A3); // Analog InPut |
BalB | 1:3370c513ff5c | 13 | AccelStepper stepper1(1 , PA_10, PB_3); //(mode DRIVER, pin1, pin2) STEPPER DE DENTRO DE LA MAQUINA (STEPS DE 1.8) |
BalB | 1:3370c513ff5c | 14 | AccelStepper stepper2(1 , PB_5, PB_4); //(mode DRIVER, pin1, pin2) STEPPER DE FUERA (EL QUE TIENE ENGRANAJES Y STEPS DE 0.6) |
BalB | 1:3370c513ff5c | 15 | const float stp1 = 1.8; |
BalB | 1:3370c513ff5c | 16 | const float stp2 =0.6; |
BalB | 1:3370c513ff5c | 17 | const int range1 = 32; |
BalB | 1:3370c513ff5c | 18 | const int range2 = 32; |
BalB | 1:3370c513ff5c | 19 | const float micro_steps1 = 1/(stp1/range1);//sale que hara 18 steps |
BalB | 1:3370c513ff5c | 20 | const float micro_steps2 = 1/(stp2/range2);//saln que hara 53 steps |
BalB | 1:3370c513ff5c | 21 | |
BalB | 0:1d2060237094 | 22 | |
BalB | 2:50b081df251a | 23 | const int N =100; //Number of measures x position |
BalB | 2:50b081df251a | 24 | |
BalB | 2:50b081df251a | 25 | |
BalB | 0:1d2060237094 | 26 | |
BalB | 0:1d2060237094 | 27 | DigitalOut myled(LED1); // Digital OutPut |
BalB | 0:1d2060237094 | 28 | Serial pc(SERIAL_TX, SERIAL_RX); // Default USART is: 8N1 NO FlowControl |
BalB | 1:3370c513ff5c | 29 | |
BalB | 1:3370c513ff5c | 30 | |
BalB | 0:1d2060237094 | 31 | // Variables -------------------------------------------------------------- |
BalB | 1:3370c513ff5c | 32 | uint16_t measTL = 0; |
BalB | 1:3370c513ff5c | 33 | uint16_t measTR = 0; |
BalB | 1:3370c513ff5c | 34 | uint16_t measBL = 0; |
BalB | 1:3370c513ff5c | 35 | uint16_t measBR = 0; |
BalB | 2:50b081df251a | 36 | int i=0; //iterator 1 |
BalB | 2:50b081df251a | 37 | int j=0; //iteerator 2 |
BalB | 3:e3b61eb0590b | 38 | int k=0; //iterator 3 |
BalB | 2:50b081df251a | 39 | |
BalB | 1:3370c513ff5c | 40 | char choice; |
BalB | 3:e3b61eb0590b | 41 | int pos2; // position of stepper2 (outside stepper) |
BalB | 3:e3b61eb0590b | 42 | int pos1; // position of stepper1 (inside stepper) |
BalB | 2:50b081df251a | 43 | |
BalB | 2:50b081df251a | 44 | int mitja_esquerra_adalt= 0; //Average of measures of PH |
BalB | 2:50b081df251a | 45 | int mitja_dreta_adalt=0; //Average of measures of PH |
BalB | 2:50b081df251a | 46 | int mitja_esquerra_abaix=0; //Average of measures of PH |
BalB | 2:50b081df251a | 47 | int mitja_dreta_abaix = 0; //Average of measures of PH |
BalB | 2:50b081df251a | 48 | |
BalB | 2:50b081df251a | 49 | int val_top_left [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 50 | int val_top_right [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 51 | int val_bot_left [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 52 | int val_bot_right [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 53 | |
BalB | 2:50b081df251a | 54 | double sum_top_left= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 55 | double sum_top_right= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 56 | double sum_bot_left= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 57 | double sum_bot_right= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 58 | |
BalB | 2:50b081df251a | 59 | double sum_var_top_left=0; |
BalB | 2:50b081df251a | 60 | double sum_var_top_right=0; |
BalB | 2:50b081df251a | 61 | double sum_var_bot_left=0; |
BalB | 2:50b081df251a | 62 | double sum_var_bot_right=0; |
BalB | 2:50b081df251a | 63 | |
BalB | 2:50b081df251a | 64 | int val_var_top_left=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 65 | int val_var_top_right=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 66 | int val_var_bot_left=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 67 | int val_var_bot_right=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 68 | |
BalB | 2:50b081df251a | 69 | |
BalB | 2:50b081df251a | 70 | |
BalB | 2:50b081df251a | 71 | |
BalB | 1:3370c513ff5c | 72 | |
BalB | 0:1d2060237094 | 73 | //DigitalOut led(LED1); |
BalB | 0:1d2060237094 | 74 | |
BalB | 0:1d2060237094 | 75 | |
BalB | 0:1d2060237094 | 76 | // ATTENTION |
BalB | 0:1d2060237094 | 77 | // The two value below, must be changed in according to the Vcc and ADC |
BalB | 0:1d2060237094 | 78 | // resolution |
BalB | 0:1d2060237094 | 79 | float_t Valim = 3300; // This is the supplay voltage of ADC (or MCU) |
BalB | 0:1d2060237094 | 80 | float_t ADCres = 4096; // This is the ADC resolution that in this case si 12Bit |
BalB | 0:1d2060237094 | 81 | // All NUCLEO boards use an ADC with 12bit resolution |
BalB | 0:1d2060237094 | 82 | |
BalB | 0:1d2060237094 | 83 | int main() |
BalB | 0:1d2060237094 | 84 | { |
BalB | 0:1d2060237094 | 85 | pc.printf("\n\r\n\rSTART program\n\r"); |
BalB | 1:3370c513ff5c | 86 | stepper1.setSpeed(50); |
BalB | 1:3370c513ff5c | 87 | stepper1.setAcceleration(5000); |
BalB | 1:3370c513ff5c | 88 | stepper1.setCurrentPosition(0); |
BalB | 1:3370c513ff5c | 89 | stepper2.setSpeed(50); |
BalB | 1:3370c513ff5c | 90 | stepper2.setAcceleration(5000); |
BalB | 1:3370c513ff5c | 91 | stepper2.setCurrentPosition(0); |
BalB | 0:1d2060237094 | 92 | while(1) |
BalB | 1:3370c513ff5c | 93 | { |
BalB | 3:e3b61eb0590b | 94 | while (k<180){ |
BalB | 3:e3b61eb0590b | 95 | pos1=rint(micro_steps1*k); |
BalB | 3:e3b61eb0590b | 96 | stepper1.moveTo(pos1);//moveTo Sets target position |
BalB | 3:e3b61eb0590b | 97 | stepper1.runToPosition();//Gives the order to go to that target position |
BalB | 3:e3b61eb0590b | 98 | i=0; |
BalB | 3:e3b61eb0590b | 99 | while(i<121){ |
BalB | 3:e3b61eb0590b | 100 | |
BalB | 3:e3b61eb0590b | 101 | |
BalB | 3:e3b61eb0590b | 102 | |
BalB | 3:e3b61eb0590b | 103 | if(i==0) |
BalB | 3:e3b61eb0590b | 104 | { |
BalB | 3:e3b61eb0590b | 105 | pc.printf("Que vols fer, anar seguent grau?? (1)...Retrocedir ?? (2)"); |
BalB | 3:e3b61eb0590b | 106 | while(!pc.readable()); |
BalB | 3:e3b61eb0590b | 107 | choice=pc.getc(); |
BalB | 3:e3b61eb0590b | 108 | pc.printf("char= %d" ,choice-48); |
BalB | 3:e3b61eb0590b | 109 | pc.printf(": ha leido"); |
BalB | 3:e3b61eb0590b | 110 | stepper2.moveTo(rint(-60*micro_steps2));//moveTo Sets target position |
BalB | 3:e3b61eb0590b | 111 | stepper2.runToPosition();//Gives the order to go to that target position |
BalB | 3:e3b61eb0590b | 112 | wait_ms(1500); |
BalB | 3:e3b61eb0590b | 113 | } |
BalB | 3:e3b61eb0590b | 114 | pos2=rint(micro_steps2*(i-60)); |
BalB | 3:e3b61eb0590b | 115 | stepper2.moveTo(pos2);//moveTo Sets target position |
BalB | 3:e3b61eb0590b | 116 | stepper2.runToPosition();//Gives the order to go to that target position |
BalB | 3:e3b61eb0590b | 117 | wait_ms(1000); |
BalB | 2:50b081df251a | 118 | |
BalB | 3:e3b61eb0590b | 119 | sum_top_left=0; |
BalB | 3:e3b61eb0590b | 120 | sum_top_right=0; |
BalB | 3:e3b61eb0590b | 121 | sum_bot_left=0; |
BalB | 3:e3b61eb0590b | 122 | sum_bot_right=0; |
BalB | 2:50b081df251a | 123 | |
BalB | 3:e3b61eb0590b | 124 | sum_var_top_left=0; |
BalB | 3:e3b61eb0590b | 125 | sum_var_top_right=0; |
BalB | 3:e3b61eb0590b | 126 | sum_var_bot_left=0; |
BalB | 3:e3b61eb0590b | 127 | sum_var_bot_right=0; |
BalB | 2:50b081df251a | 128 | |
BalB | 3:e3b61eb0590b | 129 | while(j<N) |
BalB | 3:e3b61eb0590b | 130 | { |
BalB | 3:e3b61eb0590b | 131 | val_top_left[j]=top_left.read_u16(); //Taking N measures |
BalB | 3:e3b61eb0590b | 132 | val_top_right[j]=top_right.read_u16(); //Taking N measures |
BalB | 3:e3b61eb0590b | 133 | val_bot_left[j]=bot_left.read_u16(); //Taking N measures |
BalB | 3:e3b61eb0590b | 134 | val_bot_right[j]=bot_right.read_u16(); //Taking N measures |
BalB | 2:50b081df251a | 135 | |
BalB | 2:50b081df251a | 136 | |
BalB | 2:50b081df251a | 137 | |
BalB | 3:e3b61eb0590b | 138 | sum_top_left = sum_top_left + val_top_left[j]; //Making the sum of the measure for each PH |
BalB | 3:e3b61eb0590b | 139 | sum_top_right = sum_top_right + val_top_right[j]; //Making the sum of the measure for each PH |
BalB | 3:e3b61eb0590b | 140 | sum_bot_left = sum_bot_left + val_bot_left[j]; //Making the sum of the measure for each PH |
BalB | 3:e3b61eb0590b | 141 | sum_bot_right = sum_bot_right + val_bot_right[j]; //Making the sum of the measure for each PH |
BalB | 2:50b081df251a | 142 | |
BalB | 3:e3b61eb0590b | 143 | j++; |
BalB | 2:50b081df251a | 144 | |
BalB | 3:e3b61eb0590b | 145 | } |
BalB | 2:50b081df251a | 146 | |
BalB | 3:e3b61eb0590b | 147 | mitja_esquerra_adalt=double(rint((sum_top_left/N))); //Computiong the average |
BalB | 3:e3b61eb0590b | 148 | mitja_dreta_adalt=double(rint((sum_top_right/N))); //Computiong the average |
BalB | 3:e3b61eb0590b | 149 | mitja_esquerra_abaix=double(rint((sum_bot_left/N))); //Computiong the average |
BalB | 3:e3b61eb0590b | 150 | mitja_dreta_abaix=double(rint((sum_bot_right/N))); //Computiong the average |
BalB | 2:50b081df251a | 151 | |
BalB | 3:e3b61eb0590b | 152 | for (j=0;j<N;j++) |
BalB | 3:e3b61eb0590b | 153 | { |
BalB | 3:e3b61eb0590b | 154 | sum_var_top_left= ((val_top_left[j]-mitja_esquerra_adalt)*(val_top_left[j]-mitja_esquerra_adalt)) + sum_var_top_left; //Variance for each measure |
BalB | 3:e3b61eb0590b | 155 | sum_var_top_right= ((val_top_right[j]-mitja_dreta_adalt)*(val_top_right[j]-mitja_dreta_adalt)) + sum_var_top_right; //Variance for each measure |
BalB | 3:e3b61eb0590b | 156 | sum_var_bot_left= ((val_bot_left[j]-mitja_esquerra_abaix)*(val_bot_left[j]-mitja_esquerra_abaix)) + sum_var_bot_left; //Variance for each measure |
BalB | 3:e3b61eb0590b | 157 | sum_var_bot_right= ((val_bot_right[j]-mitja_dreta_abaix)*(val_bot_right[j]-mitja_dreta_abaix)) + sum_var_bot_right; //Variance for each measure |
BalB | 3:e3b61eb0590b | 158 | |
BalB | 3:e3b61eb0590b | 159 | } |
BalB | 3:e3b61eb0590b | 160 | val_var_top_left=double(rint(sqrt(sum_var_top_left/N))); //Obtaining Variance Value for this PH |
BalB | 3:e3b61eb0590b | 161 | val_var_top_right=double(rint(sqrt(sum_var_top_right/N))); //Obtaining Variance Value for this PH |
BalB | 3:e3b61eb0590b | 162 | val_var_bot_left=double(rint(sqrt(sum_var_bot_left/N))); //Obtaining Variance Value for this PH |
BalB | 3:e3b61eb0590b | 163 | val_var_bot_right=double(rint(sqrt(sum_var_bot_right/N))); //Obtaining Variance Value for this PH |
BalB | 2:50b081df251a | 164 | |
BalB | 2:50b081df251a | 165 | |
BalB | 3:e3b61eb0590b | 166 | //QUEDA PRINTEJAR RESULTATS |
BalB | 3:e3b61eb0590b | 167 | i++; |
BalB | 3:e3b61eb0590b | 168 | |
BalB | 3:e3b61eb0590b | 169 | } |
BalB | 2:50b081df251a | 170 | /* |
BalB | 2:50b081df251a | 171 | |
BalB | 0:1d2060237094 | 172 | // Read the analog input value |
BalB | 0:1d2060237094 | 173 | measTL = top_left.read_u16(); |
BalB | 0:1d2060237094 | 174 | measTR = top_right.read_u16(); |
BalB | 0:1d2060237094 | 175 | measBL = bot_left.read_u16(); |
BalB | 0:1d2060237094 | 176 | measBR = bot_right.read_u16(); |
BalB | 0:1d2060237094 | 177 | |
BalB | 0:1d2060237094 | 178 | // Display the result via Virtual COM |
BalB | 0:1d2060237094 | 179 | pc.printf("TOP_LEFT == %d || TOP_RIGHT == %d || BOT_LEFT == %d || BOT_RIGHT == %d ", measTL, measTR , measBL, measBR); |
BalB | 0:1d2060237094 | 180 | |
BalB | 1:3370c513ff5c | 181 | wait_ms(800); // 800 ms |
BalB | 2:50b081df251a | 182 | */ |
BalB | 3:e3b61eb0590b | 183 | k++; |
BalB | 3:e3b61eb0590b | 184 | } |
BalB | 0:1d2060237094 | 185 | } |
BalB | 0:1d2060237094 | 186 | } |
BalB | 0:1d2060237094 | 187 |