dfhjk

Dependencies:   X_NUCLEO_IHM12A1 X_NUCLEO_IKS01A2 mbed

Fork of HelloWorld_IHM12A1 by ST

Revision:
5:1808ddbbef21
Parent:
2:1e4061cedf1d
--- a/main.cpp	Wed Nov 23 09:19:34 2016 +0000
+++ b/main.cpp	Thu Feb 16 12:20:59 2017 +0000
@@ -1,42 +1,3 @@
-/**
- ******************************************************************************
- * @file    main.cpp
- * @author  IPC Rennes
- * @version V1.0.0
- * @date    April 25th, 2016
- * @brief   mbed simple application for the STMicroelectronics X-NUCLEO-IHM12A1
- *          Motor Control Expansion Board: control of 2 Brush DC motors.
- ******************************************************************************
- * @attention
- *
- * <h2><center>&copy; COPYRIGHT(c) 2016 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.
- *
- ******************************************************************************
- */
-
-/* Includes ------------------------------------------------------------------*/
 
 /* mbed specific header files. */
 #include "mbed.h"
@@ -47,226 +8,90 @@
 /* Variables -----------------------------------------------------------------*/
 
 /* Initialization parameters of the motor connected to the expansion board. */
- Stspin240_250_Init_t initDeviceParameters =
- {
-  20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz             */
-  20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz             */
-  20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz              */
-  50,    /* Duty cycle of PWM used for Ref pin (from 0 to 100)                  */
-  TRUE   /* Dual Bridge configuration  (FALSE for mono, TRUE for dual brush dc) */
- };
+Stspin240_250_Init_t initDeviceParameters = {
+    20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz             */
+    20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz             */
+    20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz              */
+    50,    /* Duty cycle of PWM used for Ref pin (from 0 to 100)                  */
+    TRUE   /* Dual Bridge configuration  (FALSE for mono, TRUE for dual brush dc) */
+};
 
 /* Motor Control Component. */
 STSPIN240_250 *motor;
 
-/* Functions -----------------------------------------------------------------*/
-
-/**
- * @brief  This is an example of error handler.
- * @param[in] error Number of the error
- * @retval None
- * @note   If needed, implement it, and then attach it:
- *           + motor->AttachErrorHandler(&myErrorHandler);
- */
-void myErrorHandler(uint16_t error)
-{
-  /* Printing to the console. */
-  printf("Error %d detected\r\n\n", error);
-  
-  /* Infinite loop */
-  while(1)
-  {
-  }    
-}
-
-/**
- * @brief  This is an example of user handler for the flag interrupt.
- * @param  None
- * @retval None
- * @note   If needed, implement it, and then attach and enable it:
- *           + motor->AttachFlagIRQ(&myFlagIRQHandler);
- *           + motor->EnableFlagIRQ();
- *         To disable it:
- *           + motor->DisbleFlagIRQ();
- */
-void myFlagIRQHandler(void)
-{
-   /* Code to be customised */
-  /************************/
-
-  printf("    WARNING: \"FLAG\" interrupt triggered.\r\n");
-
-  /* Get the state of bridge A */
-  uint16_t bridgeState  = motor->GetBridgeStatus(0);
-  
-  if (bridgeState == 0) 
-  {
-    if (motor->GetDeviceState(0) != INACTIVE)
-    {
-      /* Bridges were disabled due to overcurrent or over temperature */
-      /* When  motor was running */
-        myErrorHandler(0XBAD0);
-    }
-  }
-}
 
 /* Main ----------------------------------------------------------------------*/
 
 int main()
 {
-  uint8_t demoStep = 0;
-  
-  /* Printing to the console. */
-  printf("STARTING MAIN PROGRAM\r\n");
-    
-//----- Initialization 
-  
-  /* Initializing Motor Control Component. */
-  #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8)
-  motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A2);
-  #elif (defined TARGET_NUCLEO_L152RE)
-  motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A3);
-  #else
-  motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0);
-  #endif
-  if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE);
+
+    uint8_t demoStep = 0;
+   // riv
+    /* Initializing Motor Control Component. */
+    motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0 );           //Chiamo i l costruttore per inizializzare l'oggetto motor
+
+    /* Set dual bridge enabled as two motors are used*/
+    motor->SetDualFullBridgeConfig(1);
 
-  /* Set dual bridge enabled as two motors are used*/
-  motor->SetDualFullBridgeConfig(1);
+    /* Set PWM Frequency of Ref to 15000 Hz */
+    motor->SetRefPwmFreq(0, 15000);       //frequenza clock
+
+    /* Set PWM duty cycle of Ref to 60% */
+    motor->SetRefPwmDc(0, 60);
 
-  /* Attaching and enabling an interrupt handler. */
-  motor->AttachFlagIRQ(&myFlagIRQHandler);
-  motor->EnableFlagIRQ();
-    
-  /* Attaching an error handler */
-  motor->AttachErrorHandler(&myErrorHandler);
+    /* Set PWM Frequency of bridge A inputs to 10000 Hz */
+    motor->SetBridgeInputPwmFreq(0,10000);
 
-  /* Printing to the console. */
-  printf("Motor Control Application Example for 2 brush DC motors\r\n");
-
-  /* Set PWM Frequency of Ref to 15000 Hz */ 
-  motor->SetRefPwmFreq(0, 15000); 
+    /* Set PWM Frequency of bridge B inputs to 10000 Hz */
+    motor->SetBridgeInputPwmFreq(1,10000);
 
-  /* Set PWM duty cycle of Ref to 60% */ 
-  motor->SetRefPwmDc(0, 60); 
-  
-  /* Set PWM Frequency of bridge A inputs to 10000 Hz */ 
-  motor->SetBridgeInputPwmFreq(0,10000); 
-  
-  /* Set PWM Frequency of bridge B inputs to 10000 Hz */ 
-  motor->SetBridgeInputPwmFreq(1,10000); 
-  
-  /* Infinite Loop. */
-  printf("--> Infinite Loop...\r\n");
-  while (1)
-  {
-    switch (demoStep)
-    {  
-        case 0:
-          printf("STEP 0: Motor(0) FWD Speed=100%% - Motor(1) Inactive\r\n");
-          /* Set speed of motor 0 to 100 % */
-          motor->SetSpeed(0,100); 
-          /* start motor 0 to run forward*/
-          /* if chip is in standby mode */
-          /* it is automatically awakened */
-          motor->Run(0, BDCMotor::FWD);
-          break;
-         case 1:
-          printf("STEP 1: Motor(0) FWD Speed=75%% - Motor(1) BWD Speed=100%%\r\n");
-          /* Set speed of motor 0 to 75 % */
-          motor->SetSpeed(0,75); 
-          /* Set speed of motor 1 to 100 % */
-          motor->SetSpeed(1,100); 
-          /* start motor 1 to run backward */
-          motor->Run(1, BDCMotor::BWD);
-          break;
-        case 2:
-          printf("STEP 2: Motor(0) FWD Speed=50%% - Motor(1) BWD Speed=75%%\r\n");
-          /* Set speed of motor 0 to 50 % */
-          motor->SetSpeed(0,50);   
-         /* Set speed of motor 1 to 75% */
-          motor->SetSpeed(1,75); 
-          break;      
-        case 3:
-          printf("STEP 3: Motor(0) FWD Speed=25%% - Motor(1) BWD Speed=50%%\r\n");
-          /* Set speed of motor 0 to 25 % */
-          motor->SetSpeed(0,25);  
-          /* Set speed of motor 1 to 50% */
-          motor->SetSpeed(1,50);          
-          break;  
-        case 4:
-          printf("STEP 4: Motor(0) Stopped - Motor(1) BWD Speed=25%%\r\n");
-          /* Stop Motor 0 */
-          motor->HardStop(0);   
-          /* Set speed of motor 1 to 25% */
-          motor->SetSpeed(1,25);      
-          break;         
-         case 5:
-          printf("STEP 5: Motor(0) BWD Speed=25%% - Motor(1) Stopped\r\n");
-          /* Set speed of motor 0 to 25 % */
-          motor->SetSpeed(0,25); 
-          /* start motor 0 to run backward */
-          motor->Run(0, BDCMotor::BWD);
-          /* Stop Motor 1 */
-          motor->HardStop(1);   
-          break;
-         case 6:
-          printf("STEP 6: Motor(0) BWD Speed=50%% - Motor(1) FWD Speed=25%%\r\n");
-          /* Set speed of motor 0 to 50 % */
-          motor->SetSpeed(0,50); 
-          /* Set speed of motor 1 to 25 % */
-          motor->SetSpeed(1,25); 
-          /* start motor 1 to run backward */
-          motor->Run(1, BDCMotor::FWD);
-          break;
-        case 7:
-          printf("STEP 7: Motor(0) BWD Speed=75%% - Motor(1) FWD Speed=50%%\r\n");
-          /* Set speed of motor 0 to 75 % */
-          motor->SetSpeed(0,75);   
-          /* Set speed of motor 1 to 50 % */
-          motor->SetSpeed(1,50);             
-          break;      
-        case 8:
-          printf("STEP 8: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=75%%\r\n");
-          /* Set speed of motor 0 to 100 % */
-          motor->SetSpeed(0,100);   
-          /* Set speed of motor 1 to 75 % */
-          motor->SetSpeed(1,75);   
-          break;  
-        case 9:
-          printf("STEP 9: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n");
-          /* Set speed of motor 1 to 100 % */
-          motor->SetSpeed(1,100);    
-          break;   
-        case 10:
-          printf("STEP 10\r\n: Stop both motors and disable bridges\r\n");
-          /* Stop both motors and disable bridge */
-          motor->HardHiZ(0);    
-          motor->HardHiZ(1);    
-          break;           
-        case 11:
-          printf("STEP 11: Motor(0) FWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n");
-          /* Start both motors to go forward*/
-          motor->Run(0,BDCMotor::FWD);    
-          motor->Run(1,BDCMotor::FWD);    
-          break;                 
-        case 12:
-        default:
-          printf("STEP 12: Stop both motors and enter standby mode\r\n");
-          /* Stop both motors and put chip in standby mode */
-          motor->Reset();    
-          break;            
-    }
-  
-    /* Wait for 5 seconds */  
-    wait_ms(5000);
+    
     
-    /* Increment demostep*/  
-    demoStep++;
-    if (demoStep > 12)
-    {
-      demoStep = 0;
-    }
-  } 
+               //INIZIALIZZAZIONI, s1 e s2 conterranno i valori di velocità da dare ai singoli motori
+      
+                int s0=50;
+                int s1=50;
+                
+                //Definisco due oggetti della classe AnalogIn che chiamo ten1 e ten2, chiamo il costruttore per inizializzarli 
+                AnalogIn ten0(A1);  //indico che vorrò leggere la tensione analogica su questi due pin, 
+                AnalogIn ten1(A2);  //è la stessa di quella che ho sulle due ruote
+                    
+                /**** SETTO I DUE MOTORI ALLA STESSA VELOCITA, 50%, E ALLA STESSA DIREZIONE ****/
+                
+                motor->SetSpeed(0,s0);                          //SETTO LA VELOCITà DEL MOTORE 0 AL 50%
+                motor->SetSpeed(1,s1);                          //RIDUCO LA VELOCITA DEL MOTORE 0
+                motor->Run(1, BDCMotor::FWD);                   //FACCIO ANDARE AVANTI IL MOTORE 1, per farlo andare indietro basta mettere BWD invece di FWD
+                motor->Run(0, BDCMotor::FWD);                   //MOTORE 0 IN AVANTI
+                
+                
+                 while (1)
+                 {   
+                
+                    if(ten1.read() - ten0.read() >0.1)   //la tensione sul motore 0 è 10 volte quella dell'altro motore
+                    {
+                        s1+=10;
+                        motor->SetSpeed(1,s1);      //aumento la velocità del 5%                         
+                       // motor->Run(1, BDCMotor::FWD);     
+                       
+                       printf(" Velocita S1 %6ld\n", s1);
+                    }
+                    
+                    else if (ten0.read() - ten1.read() >0.1)   //la tensione sul motore 0 è 10 volte quella dell'altro motore
+                    {
+                        
+                        s0 +=10;
+                        motor->SetSpeed(0,s0);      //aumento la velocità del 5%                         
+                       // motor->Run(0, BDCMotor::FWD);     
+                       
+                       printf(" Velocita S0 %6ld\n", s0);
+                                      
+                           
+                       
+                    
+                    }
+                    
+                    wait_ms(250);  // 250 ms
+                }
+            
+            
 }
-/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/