ST / Mbed 2 deprecated HelloWorld_IHM12A1

Dependencies:   X_NUCLEO_IHM12A1 mbed

Fork of HelloWorld_IHM12A1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  IPC Rennes
00005  * @version V1.0.0
00006  * @date    April 25th, 2016
00007  * @brief   mbed simple application for the STMicroelectronics X-NUCLEO-IHM12A1
00008  *          Motor Control Expansion Board: control of 2 Brush DC motors.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 /* Includes ------------------------------------------------------------------*/
00040 
00041 /* mbed specific header files. */
00042 #include "mbed.h"
00043 
00044 /* Component specific header files. */
00045 #include "STSpin240_250.h"
00046 
00047 /* Variables -----------------------------------------------------------------*/
00048 
00049 /* Initialization parameters of the motor connected to the expansion board. */
00050  STSpin240_250_init_t init =
00051  {
00052   20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz             */
00053   20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz             */
00054   20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz              */
00055   50,    /* Duty cycle of PWM used for Ref pin (from 0 to 100)                  */
00056   TRUE   /* Dual Bridge configuration  (FALSE for mono, TRUE for dual brush dc) */
00057  };
00058 
00059 /* Motor Control Component. */
00060 STSpin240_250 *motor;
00061 
00062 /* Functions -----------------------------------------------------------------*/
00063 
00064 /**
00065  * @brief  This is an example of error handler.
00066  * @param[in] error Number of the error
00067  * @retval None
00068  * @note   If needed, implement it, and then attach it:
00069  *           + motor->attach_error_handler(&my_error_handler);
00070  */
00071 void my_error_handler(uint16_t error)
00072 {
00073   /* Printing to the console. */
00074   printf("Error %d detected\r\n\n", error);
00075   
00076   /* Infinite loop */
00077   while (true) {
00078   }    
00079 }
00080 
00081 /**
00082  * @brief  This is an example of user handler for the flag interrupt.
00083  * @param  None
00084  * @retval None
00085  * @note   If needed, implement it, and then attach and enable it:
00086  *           + motor->attach_flag_irq(&my_flag_irq_handler);
00087  *           + motor->enable_flag_irq();
00088  *         To disable it:
00089  *           + motor->DisbleFlagIRQ();
00090  */
00091 void my_flag_irq_handler(void)
00092 {
00093    /* Code to be customised */
00094   /************************/
00095 
00096   printf("    WARNING: \"FLAG\" interrupt triggered.\r\n");
00097 
00098   /* Get the state of bridge A */
00099   uint16_t bridgeState  = motor->get_bridge_status(0);
00100   
00101   if (bridgeState == 0)  {
00102     if (motor->get_device_state(0) != INACTIVE) {
00103       /* Bridges were disabled due to overcurrent or over temperature */
00104       /* When  motor was running */
00105       my_error_handler(0XBAD0);
00106     }
00107   }
00108 }
00109 
00110 /* Main ----------------------------------------------------------------------*/
00111 
00112 int main()
00113 {
00114   uint8_t demoStep = 0;
00115   
00116   /* Printing to the console. */
00117   printf("STARTING MAIN PROGRAM\r\n");
00118     
00119 //----- Initialization 
00120   
00121   /* Initializing Motor Control Component. */
00122   #if (defined TARGET_NUCLEO_F030R8)||(defined TARGET_NUCLEO_F334R8)
00123   motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A2);
00124   #elif (defined TARGET_NUCLEO_L152RE)
00125   motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A3);
00126   #elif (defined TARGET_NUCLEO_F429ZI)
00127   motor = new STSpin240_250(D2, D9, D6, D7, D5, D3, A0);
00128   #else
00129   motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A0);
00130   #endif
00131   if (motor->init(&init) != COMPONENT_OK) exit(EXIT_FAILURE);
00132 
00133   /* Set dual bridge enabled as two motors are used*/
00134   motor->set_dual_full_bridge_config(1);
00135 
00136   /* Attaching and enabling an interrupt handler. */
00137   motor->attach_flag_irq(&my_flag_irq_handler);
00138   motor->enable_flag_irq();
00139     
00140   /* Attaching an error handler */
00141   motor->attach_error_handler(&my_error_handler);
00142 
00143   /* Printing to the console. */
00144   printf("Motor Control Application Example for 2 brush DC motors\r\n");
00145 
00146   /* Set PWM Frequency of Ref to 15000 Hz */ 
00147   motor->set_ref_pwm_freq(0, 15000); 
00148 
00149   /* Set PWM duty cycle of Ref to 60% */ 
00150   motor->set_ref_pwm_dc(0, 60); 
00151   
00152   /* Set PWM Frequency of bridge A inputs to 10000 Hz */ 
00153   motor->set_bridge_input_pwm_freq(0,10000); 
00154   
00155   /* Set PWM Frequency of bridge B inputs to 10000 Hz */ 
00156   motor->set_bridge_input_pwm_freq(1,10000); 
00157   
00158   /* Infinite Loop. */
00159   printf("--> Infinite Loop...\r\n");
00160   while (true) {
00161     switch (demoStep) {
00162         case 0: {
00163           printf("STEP 0: Motor(0) FWD Speed=100%% - Motor(1) Inactive\r\n");
00164           /* Set speed of motor 0 to 100 % */
00165           motor->set_speed(0,100); 
00166           /* start motor 0 to run forward*/
00167           /* if chip is in standby mode */
00168           /* it is automatically awakened */
00169           motor->run(0, BDCMotor::FWD);
00170           break;
00171         }
00172         case 1: {
00173           printf("STEP 1: Motor(0) FWD Speed=75%% - Motor(1) BWD Speed=100%%\r\n");
00174           /* Set speed of motor 0 to 75 % */
00175           motor->set_speed(0,75); 
00176           /* Set speed of motor 1 to 100 % */
00177           motor->set_speed(1,100); 
00178           /* start motor 1 to run backward */
00179           motor->run(1, BDCMotor::BWD);
00180           break;
00181         }
00182         case 2: {
00183           printf("STEP 2: Motor(0) FWD Speed=50%% - Motor(1) BWD Speed=75%%\r\n");
00184           /* Set speed of motor 0 to 50 % */
00185           motor->set_speed(0,50);   
00186          /* Set speed of motor 1 to 75% */
00187           motor->set_speed(1,75); 
00188           break;
00189         }     
00190         case 3: {
00191           printf("STEP 3: Motor(0) FWD Speed=25%% - Motor(1) BWD Speed=50%%\r\n");
00192           /* Set speed of motor 0 to 25 % */
00193           motor->set_speed(0,25);  
00194           /* Set speed of motor 1 to 50% */
00195           motor->set_speed(1,50);          
00196           break;
00197         } 
00198         case 4: {
00199           printf("STEP 4: Motor(0) Stopped - Motor(1) BWD Speed=25%%\r\n");
00200           /* Stop Motor 0 */
00201           motor->hard_stop(0);   
00202           /* Set speed of motor 1 to 25% */
00203           motor->set_speed(1,25);      
00204           break;
00205         }        
00206         case 5: {
00207           printf("STEP 5: Motor(0) BWD Speed=25%% - Motor(1) Stopped\r\n");
00208           /* Set speed of motor 0 to 25 % */
00209           motor->set_speed(0,25); 
00210           /* start motor 0 to run backward */
00211           motor->run(0, BDCMotor::BWD);
00212           /* Stop Motor 1 */
00213           motor->hard_stop(1);   
00214           break;
00215         }
00216         case 6: {
00217           printf("STEP 6: Motor(0) BWD Speed=50%% - Motor(1) FWD Speed=25%%\r\n");
00218           /* Set speed of motor 0 to 50 % */
00219           motor->set_speed(0,50); 
00220           /* Set speed of motor 1 to 25 % */
00221           motor->set_speed(1,25); 
00222           /* start motor 1 to run backward */
00223           motor->run(1, BDCMotor::FWD);
00224           break;
00225         }
00226         case 7: {
00227           printf("STEP 7: Motor(0) BWD Speed=75%% - Motor(1) FWD Speed=50%%\r\n");
00228           /* Set speed of motor 0 to 75 % */
00229           motor->set_speed(0,75);   
00230           /* Set speed of motor 1 to 50 % */
00231           motor->set_speed(1,50);             
00232           break;
00233         }
00234         case 8: {
00235           printf("STEP 8: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=75%%\r\n");
00236           /* Set speed of motor 0 to 100 % */
00237           motor->set_speed(0,100);   
00238           /* Set speed of motor 1 to 75 % */
00239           motor->set_speed(1,75);   
00240           break;
00241         } 
00242         case 9: {
00243           printf("STEP 9: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n");
00244           /* Set speed of motor 1 to 100 % */
00245           motor->set_speed(1,100);    
00246           break;
00247         }  
00248         case 10: {
00249           printf("STEP 10\r\n: Stop both motors and disable bridges\r\n");
00250           /* Stop both motors and disable bridge */
00251           motor->hard_hiz(0);    
00252           motor->hard_hiz(1);    
00253           break;
00254         }         
00255         case 11: {
00256           printf("STEP 11: Motor(0) FWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n");
00257           /* Start both motors to go forward*/
00258           motor->run(0,BDCMotor::FWD);    
00259           motor->run(1,BDCMotor::FWD);    
00260           break;
00261         }               
00262         case 12:
00263         default: {
00264           printf("STEP 12: Stop both motors and enter standby mode\r\n");
00265           /* Stop both motors and put chip in standby mode */
00266           motor->reset();    
00267           break;
00268         }
00269     }
00270   
00271     /* Wait for 5 seconds */  
00272     wait_ms(5000);
00273     
00274     /* Increment demostep*/  
00275     demoStep++;
00276     if (demoStep > 12) {
00277       demoStep = 0;
00278     }
00279   } 
00280 }
00281 
00282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/