concorso

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

Fork of HelloWorld_IHM02A1 by ST

Revision:
27:4e679fecd547
Parent:
26:caec5f51abe8
--- a/main.cpp	Mon Mar 13 17:53:12 2017 +0000
+++ b/main.cpp	Sat Apr 29 06:15:01 2017 +0000
@@ -1,75 +1,65 @@
-/**
- ******************************************************************************
- * @file    main.cpp
- * @author  Davide Aliprandi, STMicroelectronics
- * @version V1.0.0
- * @date    November 4th, 2015
- * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM02A1
- *          Motor Control Expansion Board: control of 2 motors.
- ******************************************************************************
- * @attention
- *
- * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *   1. Redistributions of source code must retain the above copyright notice,
- *      this list of conditions and the following disclaimer.
- *   2. Redistributions in binary form must reproduce the above copyright notice,
- *      this list of conditions and the following disclaimer in the documentation
- *      and/or other materials provided with the distribution.
- *   3. Neither the name of STMicroelectronics nor the names of its contributors
- *      may be used to endorse or promote products derived from this software
- *      without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
+// 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"*/
 
-
-/* Includes ------------------------------------------------------------------*/
-
-/* mbed specific header files. */
-#include "mbed.h"
-
-/* Helper header files. */
-#include "DevSPI.h"
-
-/* Expansion Board specific header files. */
-#include "XNucleoIHM02A1.h"
-
-
-/* Definitions ---------------------------------------------------------------*/
-
-/* Number of movements per revolution. */
 #define MPR_1 4
-
-/* Number of steps. */
-#define STEPS_1 (400 * 128)   /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
-#define STEPS_2 (STEPS_1 * 2)
-
-/* Delay in milliseconds. */
+#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
+*/
+
+
+
+
+
+
 
 
-/* Variables -----------------------------------------------------------------*/
+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);
+
 
-/* Motor Control Expansion Board. */
+//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;
-
-/* Initialization parameters of the motors connected to the expansion board. */
 L6470_init_t init[L6470DAISYCHAINSIZE] = {
     /* First Motor. */
     {
@@ -128,36 +118,103 @@
     }
 };
 
+   
 
-/* Main ----------------------------------------------------------------------*/
+/*
+      
+void controllo_stato_connessione_thread(void const *args) {
+    while(1){
+    while(stato_socket == 0) {
+        
 
-int main()
-{
-    /*----- Initialization. -----*/
+    /* 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;
+        }    
+    }
+}
 
-    /* Initializing SPI bus. */
-#ifdef TARGET_STM32F429
+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
+    #else
     DevSPI dev_spi(D11, D12, D3);
-#endif
+    #endif
 
-    /* Initializing Motor Control Expansion Board. */
     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();
-
-    /* Printing to the console. */
-    printf("Motor Control Application Example for 2 Motors\r\n\n");
-
-
-    /*----- Setting home and marke positions, getting positions, and going to positions. -----*/
-
-    /* Printing to the console. */
-    printf("--> Setting home position.\r\n");
-
-    /* Setting the home position. */
+    
     motors[0]->set_home();
 
     /* Waiting. */
@@ -196,239 +253,89 @@
     /* Waiting. */
     wait_ms(DELAY_1);
 
-    /* Printing to the console. */
-    printf("--> Moving backward %d steps.\r\n", STEPS_2);
-
-    /* Moving. */
-    motors[0]->move(StepperMotor::BWD, STEPS_2);
-
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    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("--> Going to marked position.\r\n");
-
-    /* Going to marked position. */
-    motors[0]->go_mark();
-    
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    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("--> Going to home position.\r\n");
-
-    /* Going to home position. */
-    motors[0]->go_home();
     
-    /* Waiting while active. */
-    motors[0]->wait_while_active();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    position = motors[0]->get_position();
+    /*
+    //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);
     
-    /* Printing to the console. */
-    printf("--> Getting the current position: %d\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Halving the microsteps.\r\n");
-
-    /* Halving the microsteps. */
-    init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel -  1 : init[0].step_sel);
-    if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel)) {
-        printf("    Step Mode not allowed.\r\n");
-    }
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Setting home position.\r\n");
-
-    /* Setting the home position. */
-    motors[0]->set_home();
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Getting the current position. */
-    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();
+    if(counta > 0)
+    {
+        
+        printf("%s\r\n", buffer1);  
+    }    
+*/
+      /*  break;
     
-    /* 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_2);
-
-
-    /*----- Running together for a certain amount of time. -----*/
-
-    /* Printing to the console. */
-    printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_run(StepperMotor::BWD, 400);
-    }
-
-    /* Performing the action on each motor at the same time. */
-    x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_3);
-
-
-    /*----- Increasing the speed while running. -----*/
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_get_speed();
-    }
-
-    /* Performing the action on each motor at the same time. */
-    uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Printing to the console. */
-    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
-
-    /* Printing to the console. */
-    printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1);
-    }
-
-    /* Performing the action on each motor at the same time. */
-    results = x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_3);
-
-    /* Preparing each motor to perform a run at a specified speed. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_get_speed();
+        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);
+            }           
+            
+            
+            }
     }
-
-    /* Performing the action on each motor at the same time. */
-    results = x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Printing to the console. */
-    printf("    Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-
-    /*----- Hard Stop. -----*/
-
-    /* Printing to the console. */
-    printf("--> Hard Stop.\r\n");
-
-    /* Preparing each motor to perform a hard stop. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_hard_stop();
+    
+        break;
+        
     }
-
-    /* Performing the action on each motor at the same time. */
-    x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- Doing a full revolution on each motor, one after the other. -----*/
-
-    /* Printing to the console. */
-    printf("--> Doing a full revolution on each motor, one after the other.\r\n");
-
-    /* Doing a full revolution on each motor, one after the other. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        for (int i = 0; i < MPR_1; i++) {
-            /* Computing the number of steps. */
-            int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1);
-
-            /* Moving. */
-            motors[m]->move(StepperMotor::FWD, steps);
-            
-            /* Waiting while active. */
-            motors[m]->wait_while_active();
-
-            /* Waiting. */
-            wait_ms(DELAY_1);
-        }
-    }
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- High Impedance State. -----*/
-
-    /* Printing to the console. */
-    printf("--> High Impedance State.\r\n");
-
-    /* Preparing each motor to set High Impedance State. */
-    for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
-        motors[m]->prepare_hard_hiz();
-    }
-
-    /* Performing the action on each motor at the same time. */
-    x_nucleo_ihm02a1->perform_prepared_actions();
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-}
+}
\ No newline at end of file