d

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

Fork of HelloWorld_IHM01A1 by ST

Revision:
38:2fdc20bbb354
Parent:
35:2b44ed4ec7a0
--- a/main.cpp	Mon Mar 13 17:46:46 2017 +0000
+++ b/main.cpp	Sat Apr 29 06:34:44 2017 +0000
@@ -1,57 +1,16 @@
-/**
- ******************************************************************************
- * @file    main.cpp
- * @author  Davide Aliprandi, STMicroelectronics
- * @version V1.0.0
- * @date    October 14th, 2015
- * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
- *          Motor Control Expansion Board: control of 1 motor.
- ******************************************************************************
- * @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 "L6474.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"
-
-/* Component specific header files. */
-#include "L6474.h"
-
-
-/* Definitions ---------------------------------------------------------------*/
-
-/* Number of steps. */
 #define STEPS_1 (400 * 8)   /* 1 revolution given a 400 steps motor configured at 1/8 microstep mode. */
 
 /* Delay in milliseconds. */
@@ -97,7 +56,7 @@
 };
 
 /* Motor Control Component. */
-L6474 *motor;
+
 
 
 /* Functions -----------------------------------------------------------------*/
@@ -130,14 +89,145 @@
     motor->isr_flag = FALSE;
 }
 
+/*
+#define VL53L0_I2C_SDA   D14 //sensore fotoni
+#define VL53L0_I2C_SCL   D15 //sensore fotoni
+*/
 
-/* Main ----------------------------------------------------------------------*/
+
+
+
+
+
+
+
+/*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];*/
+
+    /* Second Motor. */
+
+   
+
+/*
+      
+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. */
+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) {
+    
+}
+
+
+
+*/
+ 
+
+    /* Attaching and enabling interrupt handlers. */
+    
+
+int main() {
+    
     DevSPI dev_spi(D11, D12, D13);
 
     /* Initializing Motor Control Component. */
@@ -145,11 +235,8 @@
     if (motor->init(&init) != COMPONENT_OK) {
         exit(EXIT_FAILURE);
     }
-
-    /* Attaching and enabling interrupt handlers. */
-    motor->attach_flag_irq(&flag_irq_handler);
+   motor->attach_flag_irq(&flag_irq_handler);
     motor->enable_flag_irq();
-
     /* Printing to the console. */
     printf("Motor Control Application Example for 1 Motor\r\n\n");
 
@@ -168,199 +255,87 @@
     /* Getting current position. */
     int position = motor->get_position();
     
-    /* Printing to the console. */
-    printf("    Position: %d.\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-
-    /*----- Changing the motor setting. -----*/
-
-    /* Printing to the console. */
-    printf("--> Setting Torque Regulation Current to 500[mA].\r\n");
-
-    /* Increasing the torque regulation current to 500[mA]. */
-    motor->set_parameter(L6474_TVAL, 500);
-
-    /* Printing to the console. */
-    printf("--> Doubling the microsteps.\r\n");
-
-    /* Doubling the microsteps. */
-    if (!motor->set_step_mode((StepperMotor::step_mode_t) STEP_MODE_1_16)) {
-        printf("    Step Mode not allowed.\r\n");
-    }
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-    /* Printing to the console. */
-    printf("--> Setting Home.\r\n");
-
-    /* Setting the current position to be the home position. */
-    motor->set_home();
-
-    /* Getting current position. */
-    position = motor->get_position();
-    
-    /* Printing to the console. */
-    printf("    Position: %d.\r\n", position);
-    
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- Moving. -----*/
+    /*
+    //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("--> Moving backward %d steps.\r\n", STEPS_1);
-
-    /* Moving N steps in the backward direction. */
-    motor->move(StepperMotor::BWD, STEPS_1);
-    
-    /* Waiting while the motor is active. */
-    motor->wait_while_active();
-
-    /* Getting current position. */
-    position = motor->get_position();
-    
-    /* Printing to the console. */
-    printf("    Position: %d.\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_1);
-
-
-    /*----- Going to a specified position. -----*/
-
-    /* Printing to the console. */
-    printf("--> Going to position %d.\r\n", STEPS_1);
-    
-    /* Requesting to go to a specified position. */
-    motor->go_to(STEPS_1);
-    
-    /* Waiting while the motor is active. */
-    motor->wait_while_active();
-
-    /* Getting current position. */
-    position = motor->get_position();
-    
-    /* Printing to the console. */
-    printf("    Position: %d.\r\n", position);
-    
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-    
-    /*----- Going Home. -----*/
-
-    /* Printing to the console. */
-    printf("--> Going Home.\r\n");
-    
-    /* Requesting to go to home. */
-    motor->go_home();
+    if(counta > 0)
+    {
+        
+        printf("%s\r\n", buffer1);  
+    }    
+*/
+      /*  break;
     
-    /* Waiting while the motor is active. */
-    motor->wait_while_active();
-
-    /* Getting current position. */
-    position = motor->get_position();
-
-    /* Printing to the console. */
-    printf("    Position: %d.\r\n", position);
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- Running. -----*/
-
-    /* Printing to the console. */
-    printf("--> Running backward for %d seconds.\r\n", DELAY_3 / 1000);
-
-    /* Requesting to run backward. */
-    motor->run(StepperMotor::BWD);
-
-    /* Waiting. */
-    wait_ms(DELAY_3);
-
-    /* Getting current speed. */
-    int speed = motor->get_speed();
-
-    /* Printing to the console. */
-    printf("    Speed: %d.\r\n", speed);
-
-    /*----- Increasing the speed while running. -----*/
-
-    /* Printing to the console. */
-    printf("--> Increasing the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
-
-    /* Increasing the speed. */
-    motor->set_max_speed(SPEED_1);
-
-    /* Waiting. */
-    wait_ms(DELAY_3);
-
-    /* Getting current speed. */
-    speed = motor->get_speed();
-
-    /* Printing to the console. */
-    printf("    Speed: %d.\r\n", speed);
-
-
-    /*----- Decreasing the speed while running. -----*/
-
-    /* Printing to the console. */
-    printf("--> Decreasing the speed while running again for %d seconds.\r\n", DELAY_4 / 1000);
-
-    /* Decreasing the speed. */
-    motor->set_max_speed(SPEED_2);
-
-    /* Waiting. */
-    wait_ms(DELAY_4);
-
-    /* Getting current speed. */
-    speed = motor->get_speed();
-
-    /* Printing to the console. */
-    printf("    Speed: %d.\r\n", speed);
-
-
-    /*----- Hard Stop. -----*/
-
-    /* Printing to the console. */
-    printf("--> Hard Stop.\r\n");
-
-    /* Requesting to immediatly stop. */
-    motor->hard_stop();
-
-    /* Waiting while the motor is active. */
-    motor->wait_while_active();
-
-    /* Waiting. */
-    wait_ms(DELAY_2);
-
-
-    /*----- Infinite Loop. -----*/
-
-    /* Printing to the console. */
-    printf("--> Infinite Loop...\r\n");
-
-    /* Setting the current position to be the home position. */
-    motor->set_home();
-
-    /* Infinite Loop. */
-    while (true) {
-        /* Requesting to go to a specified position. */
-        motor->go_to(STEPS_1 >> 1);
-
-        /* Waiting while the motor is active. */
-        motor->wait_while_active();
-
-        /* Requesting to go to a specified position. */
-        motor->go_to(- (STEPS_1 >> 1));
-
-        /* Waiting while the motor is active. */
-        motor->wait_while_active();
+        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;
+        
+    }*/
+}
\ No newline at end of file