concorso

Dependencies:   HCSR04 NetworkSocketAPI Servo X_NUCLEO_53L0A1 X_NUCLEO_IDW01M1v2 X_NUCLEO_IHM02A1 mbed-rtos mbed

Fork of HelloWorld_IHM02A1 by ST

main.cpp

Committer:
d3dfantasy99
Date:
2017-04-29
Revision:
27:4e679fecd547
Parent:
26:caec5f51abe8

File content as of revision 27:4e679fecd547:

// Importo Librerie
#include "mbed.h"
/*#include "hcsr04.h"
#include "SpwfInterface.h"
#include "TCPSocket.h"
*/#include "DevSPI.h"
/*#include "XNucleoIHM02A1.h"
/*#include "x_nucleo_53l0a1.h"
#include <stdio.h>
#include "Servo.h"
#include "rtos.h"*/

#define MPR_1 4
#define STEPS_1 (400 * 128) 
#define DELAY_1 1000
#define DELAY_2 2000
#define DELAY_3 5000
/*
#define VL53L0_I2C_SDA   D14 //sensore fotoni
#define VL53L0_I2C_SCL   D15 //sensore fotoni
*/








DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
static X_NUCLEO_53L0A1 *board=NULL; //sensore fotoni
/*
//Inizializzo le schede
Serial pc(USBTX, USBRX); //seriale
SpwfSAInterface wifi(D8, D2, false); //wifi
Servo servomotore(PB_10); //servomotore
PwmOut motori(PA_8);//D13
DigitalOut sinistra(PA_0);
DigitalOut destra(PA_1);
TCPSocket socket(&wifi);


//Variabili Globali
char * ssid = "TekSmartLab"; //ssid wifi
char * seckey = ""; //password wifi
char * ip_socket = "192.168.1.104"; //ip socket master
int porta_socket = 8000; //porta del socket
int stato_socket = 0;
int stato = 0; // 1 = start - 0 = stop (default stop)
int gradi_servo = 45;
char buffer[0];
int controllo_buffer_in_arrivo = 0;
int contatore_buff = 0;
int invio_dati = 0;
int buca_trovata = 0;
int start_servo = 0;
uint32_t distanza_centrale = 0;
uint32_t distanza_destra = 0;
uint32_t distanza_sinistra = 0;
uint32_t test_distanza = 0;
int misure[2];*/
XNucleoIHM02A1 *x_nucleo_ihm02a1;
L6470_init_t init[L6470DAISYCHAINSIZE] = {
    /* First Motor. */
    {
        9.0,                           /* Motor supply voltage in V. */
        400,                           /* Min number of steps per revolution for the motor. */
        1.7,                           /* Max motor phase voltage in A. */
        3.06,                          /* Max motor phase voltage in V. */
        300.0,                         /* Motor initial speed [step/s]. */
        500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
        500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
        992.0,                         /* Motor maximum speed [step/s]. */
        0.0,                           /* Motor minimum speed [step/s]. */
        602.7,                         /* Motor full-step speed threshold [step/s]. */
        3.06,                          /* Holding kval [V]. */
        3.06,                          /* Constant speed kval [V]. */
        3.06,                          /* Acceleration starting kval [V]. */
        3.06,                          /* Deceleration starting kval [V]. */
        61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
        392.1569e-6,                   /* Start slope [s/step]. */
        643.1372e-6,                   /* Acceleration final slope [s/step]. */
        643.1372e-6,                   /* Deceleration final slope [s/step]. */
        0,                             /* Thermal compensation factor (range [0, 15]). */
        3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
        3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
        StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
        0xFF,                          /* Alarm conditions enable. */
        0x2E88                         /* Ic configuration. */
    },

    /* Second Motor. */
    {
        9.0,                           /* Motor supply voltage in V. */
        400,                           /* Min number of steps per revolution for the motor. */
        1.7,                           /* Max motor phase voltage in A. */
        3.06,                          /* Max motor phase voltage in V. */
        300.0,                         /* Motor initial speed [step/s]. */
        500.0,                         /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
        500.0,                         /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
        992.0,                         /* Motor maximum speed [step/s]. */
        0.0,                           /* Motor minimum speed [step/s]. */
        602.7,                         /* Motor full-step speed threshold [step/s]. */
        3.06,                          /* Holding kval [V]. */
        3.06,                          /* Constant speed kval [V]. */
        3.06,                          /* Acceleration starting kval [V]. */
        3.06,                          /* Deceleration starting kval [V]. */
        61.52,                         /* Intersect speed for bemf compensation curve slope changing [step/s]. */
        392.1569e-6,                   /* Start slope [s/step]. */
        643.1372e-6,                   /* Acceleration final slope [s/step]. */
        643.1372e-6,                   /* Deceleration final slope [s/step]. */
        0,                             /* Thermal compensation factor (range [0, 15]). */
        3.06 * 1000 * 1.10,            /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
        3.06 * 1000 * 1.00,            /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
        StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
        0xFF,                          /* Alarm conditions enable. */
        0x2E88                         /* Ic configuration. */
    }
};

   

/*
      
void controllo_stato_connessione_thread(void const *args) {
    while(1){
    while(stato_socket == 0) {
        

    /* Initializing Motor Control Component. */
   
    
    /* Attaching and enabling interrupt handlers. */
    
/*
        board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D10, D9);
        board->InitBoard();
        DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
        board->sensor_centre->GetDistance(&test_distanza);
        
        servomotore = gradi_servo/100.0;
        destra.write(0);
        sinistra.write(0);
        pc.printf("\r\n Rift Searcher 1.0 \r\n");
  /*      while(!wifi.connect(ssid, seckey, NSAPI_SECURITY_NONE))/*NSAPI_SECURITY_WPA2*/ {      
    /*        pc.printf("\r\n Connessione alla rete wifi corso... Attendere...\r\n");
            wait_ms(2000);    
        }
        //connesso alla rete wifi   
        pc.printf("\r\n Connesso alla rete Wifi\r\n");
    
        //verifico la connessione al socket
        
        while(socket.connect(ip_socket, porta_socket) != 0){
            pc.printf("\r\n Connessione al socket corso... Attendere...\r\n");
            wait_ms(2000);
        }
        //connesso al socket
        pc.printf("\r\n Connessione al socket riuscita\r\n");
        stato_socket = 1;
        }    
    }
}

void controllo_dati_entrata_thread(void const *args) {
    pc.printf("\r\n Controllo dati\r\n");
    while(1){
    if(stato_socket == 1){
        char buffer[1];
        if(socket.recv(buffer, sizeof buffer) != 0){
            int count = 0;
            count = socket.recv(buffer, sizeof buffer);
            if(count > 0){
                buffer [count]='\0';
                if(buffer[0] == '0'){
                    stato = 0;
                    pc.printf("\r\n Stato 0\r\n");
                    }
                else if (buffer[0] == '1'){
                    stato = 1;
                    pc.printf("\r\n Stato 1 - Start\r\n");
                    }
                 else if (buffer[0] == '2'){
                    stato = 2;
                    pc.printf("\r\n Stato 1 - Start\r\n");
                    }          
                } 
            }
        }
    }
}

void misure_invio_dati(void const *args) {
        
        
            
                        
}
void rotazione_passo_passo(void const *args) {
    
}



*/
int main() {
    #ifdef TARGET_STM32F429
    DevSPI dev_spi(D11, D12, D13);
    #else
    DevSPI dev_spi(D11, D12, D3);
    #endif

    x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);

    /* Building a list of motor control components. */
    L6470 **motors = x_nucleo_ihm02a1->get_components();
    
    motors[0]->set_home();

    /* Waiting. */
    wait_ms(DELAY_1);

    /* Getting the current position. */
    int position = motors[0]->get_position();

    /* Printing to the console. */
    printf("--> Getting the current position: %d\r\n", position);

    /* Waiting. */
    wait_ms(DELAY_1);

    /* Printing to the console. */
    printf("--> Moving forward %d steps.\r\n", STEPS_1);

    /* Moving. */
    motors[0]->move(StepperMotor::FWD, STEPS_1);

    /* Waiting while active. */
    motors[0]->wait_while_active();

    /* Getting the current position. */
    position = motors[0]->get_position();
    
    /* Printing to the console. */
    printf("--> Getting the current position: %d\r\n", position);

    /* Printing to the console. */
    printf("--> Marking the current position.\r\n");

    /* Marking the current position. */
    motors[0]->set_mark();

    /* Waiting. */
    wait_ms(DELAY_1);

    
    
    /*
    //inizializzo la connessione seriale
    //inizializzo thread wifi e buffer
    Thread th_wifi(controllo_stato_connessione_thread);
    Thread th_buffer (controllo_dati_entrata_thread);
    Thread th_data (misure_invio_dati);
    Thread th_passo (rotazione_passo_passo);
    //Switch sullo stato
    while(1) {
       switch(stato){
        case 0:
         destra.write(0);
         sinistra.write(0);
         
    /*
    char buffer1[1];
    buffer1[0] = 'a';
    int counta = 0;
    pc.printf("\r\nSending Data\r\n"); 
    counta = sizeof buffer1;
    buffer1 [counta]='\0';
    counta = socket.send(buffer1, sizeof buffer1);
    
    if(counta > 0)
    {
        
        printf("%s\r\n", buffer1);  
    }    
*/
      /*  break;
    
        case 1: //start
        
        motori.period_ms(60);
        motori.pulsewidth(0.05);
        while(stato == 1){
            sinistra.write(1);
            wait_ms(60);
            destra.write(1);
            wait_ms(1500); 
            destra.write(0);
            wait_ms(60);
            sinistra.write(0);
            wait_ms(1000);
        }
        break;
        
        case 2: //test servo e misure
        int temp = 0;
        while(stato == 2){
            
            board->sensor_centre->GetDistance(&distanza_centrale);
                if(distanza_centrale > test_distanza + 5){
                    if(temp < 100){
                    for(temp = gradi_servo; temp<100; temp = temp + 10) {        
                    board->sensor_centre->GetDistance(&distanza_centrale);
                    board->sensor_centre->GetDistance(&distanza_destra);
                    board->sensor_centre->GetDistance(&distanza_sinistra);
                    printf("Distanza centrale : %ld\n Distanza destra : %ld\n Distanza sinistra : %ld\n", distanza_centrale,distanza_destra,distanza_sinistra);
                    wait_ms(500);
                    servomotore = temp/100.0;
                    wait_ms(0.01);
                    }
                    
            }
            else{
                for(temp = gradi_servo; temp<100; temp = temp + 10) {        
                    board->sensor_centre->GetDistance(&distanza_centrale);
                    board->sensor_centre->GetDistance(&distanza_destra);
                    board->sensor_centre->GetDistance(&distanza_sinistra);
                    printf("Distanza centrale : %ld\n Distanza destra : %ld\n Distanza sinistra : %ld\n", distanza_centrale,distanza_destra,distanza_sinistra);
                    wait_ms(500);
                    servomotore = temp/100.0;
                    wait_ms(0.01);
            }           
            
            
            }
    }
    
        break;
        
    }
}