program 1 driver
Dependencies: mbed motorController2
main.cpp@2:50b081df251a, 2016-12-04 (annotated)
- Committer:
- BalB
- Date:
- Sun Dec 04 16:15:40 2016 +0000
- Revision:
- 2:50b081df251a
- Parent:
- 1:3370c513ff5c
- Child:
- 3:e3b61eb0590b
first program controlling 1 driver
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 | 0:1d2060237094 | 6 | // By BalB |
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 | 2:50b081df251a | 38 | |
BalB | 1:3370c513ff5c | 39 | char choice; |
BalB | 2:50b081df251a | 40 | int pos2; |
BalB | 2:50b081df251a | 41 | |
BalB | 2:50b081df251a | 42 | int mitja_esquerra_adalt= 0; //Average of measures of PH |
BalB | 2:50b081df251a | 43 | int mitja_dreta_adalt=0; //Average of measures of PH |
BalB | 2:50b081df251a | 44 | int mitja_esquerra_abaix=0; //Average of measures of PH |
BalB | 2:50b081df251a | 45 | int mitja_dreta_abaix = 0; //Average of measures of PH |
BalB | 2:50b081df251a | 46 | |
BalB | 2:50b081df251a | 47 | int val_top_left [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 48 | int val_top_right [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 49 | int val_bot_left [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 50 | int val_bot_right [N]= {0}; //instantaneous value of measure of PH |
BalB | 2:50b081df251a | 51 | |
BalB | 2:50b081df251a | 52 | double sum_top_left= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 53 | double sum_top_right= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 54 | double sum_bot_left= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 55 | double sum_bot_right= 0; //sumatory of measures of PH |
BalB | 2:50b081df251a | 56 | |
BalB | 2:50b081df251a | 57 | double sum_var_top_left=0; |
BalB | 2:50b081df251a | 58 | double sum_var_top_right=0; |
BalB | 2:50b081df251a | 59 | double sum_var_bot_left=0; |
BalB | 2:50b081df251a | 60 | double sum_var_bot_right=0; |
BalB | 2:50b081df251a | 61 | |
BalB | 2:50b081df251a | 62 | int val_var_top_left=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 63 | int val_var_top_right=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 64 | int val_var_bot_left=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 65 | int val_var_bot_right=0; //Variance Value for this PH |
BalB | 2:50b081df251a | 66 | |
BalB | 2:50b081df251a | 67 | |
BalB | 2:50b081df251a | 68 | |
BalB | 2:50b081df251a | 69 | |
BalB | 1:3370c513ff5c | 70 | |
BalB | 0:1d2060237094 | 71 | //DigitalOut led(LED1); |
BalB | 0:1d2060237094 | 72 | |
BalB | 0:1d2060237094 | 73 | |
BalB | 0:1d2060237094 | 74 | // ATTENTION |
BalB | 0:1d2060237094 | 75 | // The two value below, must be changed in according to the Vcc and ADC |
BalB | 0:1d2060237094 | 76 | // resolution |
BalB | 0:1d2060237094 | 77 | float_t Valim = 3300; // This is the supplay voltage of ADC (or MCU) |
BalB | 0:1d2060237094 | 78 | float_t ADCres = 4096; // This is the ADC resolution that in this case si 12Bit |
BalB | 0:1d2060237094 | 79 | // All NUCLEO boards use an ADC with 12bit resolution |
BalB | 0:1d2060237094 | 80 | |
BalB | 0:1d2060237094 | 81 | int main() |
BalB | 0:1d2060237094 | 82 | { |
BalB | 0:1d2060237094 | 83 | pc.printf("\n\r\n\rSTART program\n\r"); |
BalB | 1:3370c513ff5c | 84 | stepper1.setSpeed(50); |
BalB | 1:3370c513ff5c | 85 | stepper1.setAcceleration(5000); |
BalB | 1:3370c513ff5c | 86 | stepper1.setCurrentPosition(0); |
BalB | 1:3370c513ff5c | 87 | stepper2.setSpeed(50); |
BalB | 1:3370c513ff5c | 88 | stepper2.setAcceleration(5000); |
BalB | 1:3370c513ff5c | 89 | stepper2.setCurrentPosition(0); |
BalB | 0:1d2060237094 | 90 | while(1) |
BalB | 1:3370c513ff5c | 91 | { |
BalB | 1:3370c513ff5c | 92 | if(i==0) |
BalB | 1:3370c513ff5c | 93 | { |
BalB | 1:3370c513ff5c | 94 | pc.printf("Que vols fer, anar seguent grau?? (1)...Retrocedir ?? (2)"); |
BalB | 1:3370c513ff5c | 95 | while(!pc.readable()); |
BalB | 1:3370c513ff5c | 96 | choice=pc.getc(); |
BalB | 1:3370c513ff5c | 97 | pc.printf("char= %d" ,choice-48); |
BalB | 1:3370c513ff5c | 98 | pc.printf(" ha leido"); |
BalB | 1:3370c513ff5c | 99 | stepper2.moveTo(rint(-60*micro_steps2));//moveTo Sets target position |
BalB | 1:3370c513ff5c | 100 | stepper2.runToPosition();//Gives the order to go to that target position |
BalB | 1:3370c513ff5c | 101 | wait_ms(1500); |
BalB | 1:3370c513ff5c | 102 | } |
BalB | 2:50b081df251a | 103 | pos2=rint(micro_steps2*(i-60)); |
BalB | 2:50b081df251a | 104 | stepper2.moveTo(pos2);//moveTo Sets target position |
BalB | 2:50b081df251a | 105 | stepper2.runToPosition();//Gives the order to go to that target position |
BalB | 2:50b081df251a | 106 | wait_ms(1000); |
BalB | 2:50b081df251a | 107 | |
BalB | 2:50b081df251a | 108 | sum_top_left=0; |
BalB | 2:50b081df251a | 109 | sum_top_right=0; |
BalB | 2:50b081df251a | 110 | sum_bot_left=0; |
BalB | 2:50b081df251a | 111 | sum_bot_right=0; |
BalB | 2:50b081df251a | 112 | |
BalB | 2:50b081df251a | 113 | sum_var_top_left=0; |
BalB | 2:50b081df251a | 114 | sum_var_top_right=0; |
BalB | 2:50b081df251a | 115 | sum_var_bot_left=0; |
BalB | 2:50b081df251a | 116 | sum_var_bot_right=0; |
BalB | 2:50b081df251a | 117 | |
BalB | 2:50b081df251a | 118 | while(j<N) |
BalB | 2:50b081df251a | 119 | { |
BalB | 2:50b081df251a | 120 | val_top_left[j]=top_left.read_u16(); //Taking N measures |
BalB | 2:50b081df251a | 121 | val_top_right[j]=top_right.read_u16(); //Taking N measures |
BalB | 2:50b081df251a | 122 | val_bot_left[j]=bot_left.read_u16(); //Taking N measures |
BalB | 2:50b081df251a | 123 | val_bot_right[j]=bot_right.read_u16(); //Taking N measures |
BalB | 2:50b081df251a | 124 | |
BalB | 2:50b081df251a | 125 | |
BalB | 2:50b081df251a | 126 | |
BalB | 2:50b081df251a | 127 | sum_top_left = sum_top_left + val_top_left[j]; //Making the sum of the measure for each PH |
BalB | 2:50b081df251a | 128 | sum_top_right = sum_top_right + val_top_right[j]; //Making the sum of the measure for each PH |
BalB | 2:50b081df251a | 129 | sum_bot_left = sum_bot_left + val_bot_left[j]; //Making the sum of the measure for each PH |
BalB | 2:50b081df251a | 130 | sum_bot_right = sum_bot_right + val_bot_right[j]; //Making the sum of the measure for each PH |
BalB | 2:50b081df251a | 131 | |
BalB | 2:50b081df251a | 132 | j++; |
BalB | 2:50b081df251a | 133 | |
BalB | 2:50b081df251a | 134 | } |
BalB | 2:50b081df251a | 135 | |
BalB | 2:50b081df251a | 136 | mitja_esquerra_adalt=double(rint((sum_top_left/N))); //Computiong the average |
BalB | 2:50b081df251a | 137 | mitja_dreta_adalt=double(rint((sum_top_right/N))); //Computiong the average |
BalB | 2:50b081df251a | 138 | mitja_esquerra_abaix=double(rint((sum_bot_left/N))); //Computiong the average |
BalB | 2:50b081df251a | 139 | mitja_dreta_abaix=double(rint((sum_bot_right/N))); //Computiong the average |
BalB | 2:50b081df251a | 140 | |
BalB | 2:50b081df251a | 141 | for (j=0;j<N;j++) |
BalB | 2:50b081df251a | 142 | { |
BalB | 2:50b081df251a | 143 | 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 | 2:50b081df251a | 144 | 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 | 2:50b081df251a | 145 | 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 | 2:50b081df251a | 146 | 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 | 2:50b081df251a | 147 | |
BalB | 2:50b081df251a | 148 | } |
BalB | 2:50b081df251a | 149 | val_var_top_left=double(rint(sqrt(sum_var_top_left/N))); //Obtaining Variance Value for this PH |
BalB | 2:50b081df251a | 150 | val_var_top_right=double(rint(sqrt(sum_var_top_right/N))); //Obtaining Variance Value for this PH |
BalB | 2:50b081df251a | 151 | val_var_bot_left=double(rint(sqrt(sum_var_bot_left/N))); //Obtaining Variance Value for this PH |
BalB | 2:50b081df251a | 152 | val_var_bot_right=double(rint(sqrt(sum_var_bot_right/N))); //Obtaining Variance Value for this PH |
BalB | 2:50b081df251a | 153 | |
BalB | 2:50b081df251a | 154 | |
BalB | 2:50b081df251a | 155 | //QUEDA PRINTEJAR RESULTATS |
BalB | 2:50b081df251a | 156 | |
BalB | 2:50b081df251a | 157 | if(i==121) |
BalB | 2:50b081df251a | 158 | { |
BalB | 2:50b081df251a | 159 | i=0; |
BalB | 2:50b081df251a | 160 | stepper2.moveTo (-60*micro_steps2); |
BalB | 2:50b081df251a | 161 | stepper2.runToPosition(); |
BalB | 2:50b081df251a | 162 | |
BalB | 2:50b081df251a | 163 | } |
BalB | 2:50b081df251a | 164 | else if(i!=121) |
BalB | 2:50b081df251a | 165 | { |
BalB | 2:50b081df251a | 166 | i++; |
BalB | 2:50b081df251a | 167 | } |
BalB | 2:50b081df251a | 168 | /* |
BalB | 2:50b081df251a | 169 | |
BalB | 0:1d2060237094 | 170 | // Read the analog input value |
BalB | 0:1d2060237094 | 171 | measTL = top_left.read_u16(); |
BalB | 0:1d2060237094 | 172 | measTR = top_right.read_u16(); |
BalB | 0:1d2060237094 | 173 | measBL = bot_left.read_u16(); |
BalB | 0:1d2060237094 | 174 | measBR = bot_right.read_u16(); |
BalB | 0:1d2060237094 | 175 | |
BalB | 0:1d2060237094 | 176 | // Display the result via Virtual COM |
BalB | 0:1d2060237094 | 177 | pc.printf("TOP_LEFT == %d || TOP_RIGHT == %d || BOT_LEFT == %d || BOT_RIGHT == %d ", measTL, measTR , measBL, measBR); |
BalB | 0:1d2060237094 | 178 | |
BalB | 1:3370c513ff5c | 179 | wait_ms(800); // 800 ms |
BalB | 2:50b081df251a | 180 | */ |
BalB | 2:50b081df251a | 181 | |
BalB | 2:50b081df251a | 182 | |
BalB | 0:1d2060237094 | 183 | } |
BalB | 0:1d2060237094 | 184 | } |
BalB | 0:1d2060237094 | 185 |