prime funzioni per i motori

Dependencies:   X_NUCLEO_IHM12A1 mbed

Fork of HelloWorld_IHM12A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* Includes ------------------------------------------------------------------*/
00003 
00004 /* mbed specific header files. */
00005 #include "mbed.h"
00006 
00007 /* Component specific header files. */
00008 #include "stspin240_250_class.h"
00009 
00010 /* Variables -----------------------------------------------------------------*/
00011 
00012 /* Initialization parameters of the motor connected to the expansion board. */
00013  Stspin240_250_Init_t initDeviceParameters =
00014  {
00015   20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz             */
00016   20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz             */
00017   20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz              */
00018   50,    /* Duty cycle of PWM used for Ref pin (from 0 to 100)                  */
00019   TRUE   /* Dual Bridge configuration  (FALSE for mono, TRUE for dual brush dc) */
00020  };
00021 
00022 /* Motor Control Component. */
00023 STSPIN240_250 *motor;
00024 
00025 /* Functions -----------------------------------------------------------------*/
00026 
00027 /**
00028  * @brief  This is an example of error handler.
00029  * @param[in] error Number of the error
00030  * @retval None
00031  * @note   If needed, implement it, and then attach it:
00032  *           + motor->AttachErrorHandler(&myErrorHandler);
00033  */
00034 void myErrorHandler(uint16_t error)
00035 {
00036   /* Printing to the console. */
00037   printf("Error %d detected\r\n\n", error);
00038   
00039   /* Infinite loop */
00040   while(1)
00041   {
00042   }    
00043 }
00044 
00045 /**
00046  * @brief  This is an example of user handler for the flag interrupt.
00047  * @param  None
00048  * @retval None
00049  * @note   If needed, implement it, and then attach and enable it:
00050  *           + motor->AttachFlagIRQ(&myFlagIRQHandler);
00051  *           + motor->EnableFlagIRQ();
00052  *         To disable it:
00053  *           + motor->DisbleFlagIRQ();
00054  */
00055 void myFlagIRQHandler(void)
00056 {
00057    /* Code to be customised */
00058   /************************/
00059 
00060   printf("    WARNING: \"FLAG\" interrupt triggered.\r\n");
00061 
00062   /* Get the state of bridge A */
00063   uint16_t bridgeState  = motor->GetBridgeStatus(0);
00064   
00065   if (bridgeState == 0) 
00066   {
00067     if (motor->GetDeviceState(0) != INACTIVE)
00068     {
00069       /* Bridges were disabled due to overcurrent or over temperature */
00070       /* When  motor was running */
00071         myErrorHandler(0XBAD0);
00072     }
00073   }
00074 }
00075 void go (float speed,bool direction);
00076 void turn(bool direction);
00077 void stop();
00078 
00079 /* Main ----------------------------------------------------------------------*/
00080 
00081 int main()
00082 {
00083   
00084   /* Printing to the console. */
00085   printf("STARTING MAIN PROGRAM\r\n");
00086     
00087 //----- Initialization 
00088 
00089   motor = new STSPIN240_250(D2, D9, D6, D7, D5, D4, A0);
00090  
00091   if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE);
00092 
00093   /* Set dual bridge enabled as two motors are used*/
00094   motor->SetDualFullBridgeConfig(1);
00095 
00096   /* Attaching and enabling an interrupt handler. */
00097   motor->AttachFlagIRQ(&myFlagIRQHandler);
00098   motor->EnableFlagIRQ();
00099     
00100   /* Attaching an error handler */
00101   motor->AttachErrorHandler(&myErrorHandler);
00102 
00103   /* Printing to the console. */
00104   printf("Motor Control Application Example for 2 brush DC motors\r\n");
00105 
00106   /* Set PWM Frequency of Ref to 15000 Hz */ 
00107   motor->SetRefPwmFreq(0, 15000); 
00108 
00109   /* Set PWM duty cycle of Ref to 60% */ 
00110   motor->SetRefPwmDc(0, 60);
00111   
00112   /* Set PWM Frequency of bridge A inputs to 10000 Hz */ 
00113   motor->SetBridgeInputPwmFreq(0,10); 
00114   
00115   /* Set PWM Frequency of bridge B inputs to 10000 Hz */ 
00116   motor->SetBridgeInputPwmFreq(1,10000); 
00117   
00118   /* Infinite Loop. */
00119   printf("--> Infinite Loop...\r\n");
00120   
00121   
00122   while (1)
00123   {
00124     go(100,1);
00125 
00126     
00127     
00128   }
00129 }
00130  
00131 void go (float speed,bool direction){
00132     /* direction 1 forward, 0 back*/
00133     motor->SetSpeed(0,speed);
00134     motor->SetSpeed(1,speed);
00135     
00136     if (direction){
00137         motor->Run(0,BDCMotor::FWD);
00138         motor->Run(1,BDCMotor::FWD);
00139         }
00140     else{
00141         motor->Run(0,BDCMotor::BWD);
00142         motor->Run(1,BDCMotor::BWD);
00143         }
00144 }
00145 
00146 void stop(){
00147     motor->SetSpeed(0,0);
00148     motor->SetSpeed(1,0);
00149     }
00150     
00151 void turn(bool direction){
00152     /*dir 1 left - 0 right */
00153     if (direction){
00154         motor->SetSpeed(0,20);
00155         motor->SetSpeed(1,70);
00156         
00157         motor->Run(0,BDCMotor::FWD);
00158         motor->Run(1,BDCMotor::FWD);
00159         
00160         wait_ms(1100);
00161         }
00162     else {
00163         motor->SetSpeed(1,20);
00164         motor->SetSpeed(0,70);
00165         
00166         motor->Run(0,BDCMotor::FWD);
00167         motor->Run(1,BDCMotor::FWD);
00168         
00169         wait_ms(1100);
00170             }
00171     }