program 1 driver

Dependencies:   mbed motorController2

main.cpp

Committer:
BalB
Date:
2016-12-04
Revision:
2:50b081df251a
Parent:
1:3370c513ff5c
Child:
3:e3b61eb0590b

File content as of revision 2:50b081df251a:

#include "mbed.h"
#include "AccelStepper.h"
#include "math.h"
 
//
// By BalB
//
#include "mbed.h"
AnalogIn top_left(A0); // Analog InPut
AnalogIn top_right(A1); // Analog InPut
AnalogIn bot_left(A2); // Analog InPut
AnalogIn bot_right(A3); // Analog InPut
AccelStepper stepper1(1 , PA_10, PB_3); //(mode DRIVER, pin1, pin2) STEPPER DE DENTRO DE LA MAQUINA (STEPS DE 1.8)
AccelStepper stepper2(1 , PB_5, PB_4); //(mode DRIVER, pin1, pin2) STEPPER DE FUERA (EL QUE TIENE ENGRANAJES Y STEPS DE 0.6)
const float stp1 = 1.8;
const float stp2 =0.6;
const int range1 = 32;
const int range2 = 32;
const float micro_steps1 = 1/(stp1/range1);//sale que hara 18 steps
const float micro_steps2 = 1/(stp2/range2);//saln que hara 53 steps


const int N =100; //Number of measures x position



DigitalOut myled(LED1); // Digital OutPut
Serial pc(SERIAL_TX, SERIAL_RX); // Default USART is: 8N1 NO FlowControl


// Variables --------------------------------------------------------------
uint16_t measTL = 0;
uint16_t measTR = 0;
uint16_t measBL = 0;
uint16_t measBR = 0;
int i=0; //iterator 1
int j=0; //iteerator 2

char choice;
int pos2;

int mitja_esquerra_adalt= 0; //Average of measures of PH
int mitja_dreta_adalt=0;     //Average of measures of PH
int mitja_esquerra_abaix=0;  //Average of measures of PH
int mitja_dreta_abaix = 0;   //Average of measures of PH

int val_top_left [N]= {0};  //instantaneous value of measure of PH
int val_top_right [N]= {0}; //instantaneous value of measure of PH
int val_bot_left [N]= {0};  //instantaneous value of measure of PH
int val_bot_right [N]= {0}; //instantaneous value of measure of PH

double sum_top_left= 0;  //sumatory of measures of PH
double sum_top_right= 0; //sumatory of measures of PH
double sum_bot_left= 0;  //sumatory of measures of PH
double sum_bot_right= 0; //sumatory of measures of PH

double sum_var_top_left=0;
double sum_var_top_right=0;
double sum_var_bot_left=0;
double sum_var_bot_right=0;

int val_var_top_left=0;     //Variance Value for this PH
int val_var_top_right=0;    //Variance Value for this PH
int val_var_bot_left=0;     //Variance Value for this PH
int val_var_bot_right=0;    //Variance Value for this PH





//DigitalOut led(LED1);


// ATTENTION
// The two value below, must be changed in according to the Vcc and ADC
// resolution
float_t Valim = 3300; // This is the supplay voltage of ADC (or MCU)
float_t ADCres = 4096; // This is the ADC resolution that in this case si 12Bit
 // All NUCLEO boards use an ADC with 12bit resolution

int main()
{
    pc.printf("\n\r\n\rSTART program\n\r");
    stepper1.setSpeed(50);
    stepper1.setAcceleration(5000);
    stepper1.setCurrentPosition(0);
    stepper2.setSpeed(50);
    stepper2.setAcceleration(5000);
    stepper2.setCurrentPosition(0); 
    while(1)
    {   
        if(i==0)
        {
            pc.printf("Que vols fer, anar seguent grau?? (1)...Retrocedir ?? (2)");
            while(!pc.readable());
            choice=pc.getc();
            pc.printf("char= %d" ,choice-48);
            pc.printf("  ha leido");
            stepper2.moveTo(rint(-60*micro_steps2));//moveTo Sets target position
            stepper2.runToPosition();//Gives the order to go to that target position
            wait_ms(1500);
        }
        pos2=rint(micro_steps2*(i-60));
        stepper2.moveTo(pos2);//moveTo Sets target position
        stepper2.runToPosition();//Gives the order to go to that target position        
        wait_ms(1000);
        
        sum_top_left=0;
        sum_top_right=0;
        sum_bot_left=0;
        sum_bot_right=0;
        
        sum_var_top_left=0;
        sum_var_top_right=0;
        sum_var_bot_left=0;
        sum_var_bot_right=0;
        
        while(j<N)
         {
            val_top_left[j]=top_left.read_u16();    //Taking N measures 
            val_top_right[j]=top_right.read_u16();  //Taking N measures 
            val_bot_left[j]=bot_left.read_u16();    //Taking N measures     
            val_bot_right[j]=bot_right.read_u16();  //Taking N measures 

            
            
            sum_top_left = sum_top_left + val_top_left[j];      //Making the sum of the measure for each PH
            sum_top_right = sum_top_right + val_top_right[j];   //Making the sum of the measure for each PH
            sum_bot_left = sum_bot_left + val_bot_left[j];      //Making the sum of the measure for each PH
            sum_bot_right = sum_bot_right + val_bot_right[j];   //Making the sum of the measure for each PH

            j++;

         }        
        
         mitja_esquerra_adalt=double(rint((sum_top_left/N))); //Computiong the average
         mitja_dreta_adalt=double(rint((sum_top_right/N)));   //Computiong the average
         mitja_esquerra_abaix=double(rint((sum_bot_left/N))); //Computiong the average
         mitja_dreta_abaix=double(rint((sum_bot_right/N)));   //Computiong the average
           
         for (j=0;j<N;j++)
         {
            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
            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
            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 
            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

         }
         val_var_top_left=double(rint(sqrt(sum_var_top_left/N)));    //Obtaining Variance Value for this PH
         val_var_top_right=double(rint(sqrt(sum_var_top_right/N)));  //Obtaining Variance Value for this PH
         val_var_bot_left=double(rint(sqrt(sum_var_bot_left/N)));    //Obtaining Variance Value for this PH    
         val_var_bot_right=double(rint(sqrt(sum_var_bot_right/N)));  //Obtaining Variance Value for this PH
         
         
         //QUEDA PRINTEJAR RESULTATS
         
         if(i==121)
         {
            i=0;
            stepper2.moveTo (-60*micro_steps2);
            stepper2.runToPosition();
  
         }
         else if(i!=121)
         { 
           i++;
         }
         /*
        
        // Read the analog input value
        measTL = top_left.read_u16();
        measTR = top_right.read_u16();
        measBL = bot_left.read_u16();
        measBR = bot_right.read_u16();

        // Display the result via Virtual COM
        pc.printf("TOP_LEFT == %d || TOP_RIGHT == %d || BOT_LEFT == %d || BOT_RIGHT == %d ", measTL, measTR , measBL, measBR);

        wait_ms(800); // 800 ms
        */
    
    
    }
}