Simple program featuring a few API functions usage of the X_NUCLEO_IHM04A1 library.

Dependencies:   X_NUCLEO_IHM04A1 mbed

Dependents:   SimplePIDBot

Fork of HelloWorld_IHM04A1 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    May 16, 2016
00007   * @brief   This example shows how to use 1 IHM04A1 expansion board with 
00008   * 4 unidirectionnal Brush DC motors.
00009   * Each motor has one lead connected to one of the bridge output, 
00010   * the other lead to the ground. The input bridges are not parallelised.
00011   * The demo sequence starts when the user button is pressed.
00012   * Each time, the user button is pressed, one new motor is activated
00013   ******************************************************************************
00014   * @attention
00015   *
00016   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00017   *
00018   * Redistribution and use in source and binary forms, with or without modification,
00019   * are permitted provided that the following conditions are met:
00020   *   1. Redistributions of source code must retain the above copyright notice,
00021   *      this list of conditions and the following disclaimer.
00022   *   2. Redistributions in binary form must reproduce the above copyright notice,
00023   *      this list of conditions and the following disclaimer in the documentation
00024   *      and/or other materials provided with the distribution.
00025   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00026   *      may be used to endorse or promote products derived from this software
00027   *      without specific prior written permission.
00028   *
00029   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00030   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00031   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00032   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00033   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00034   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00035   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00037   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039   *
00040   ******************************************************************************
00041   */
00042 
00043 
00044 /* Includes ------------------------------------------------------------------*/
00045 
00046 /* mbed specific header files. */
00047 #include "mbed.h"
00048 
00049 /* Component specific header files. */
00050 #include "L6206.h"
00051 
00052 
00053 /* Definitions ---------------------------------------------------------------*/
00054 
00055 #define MAX_MOTOR (4)
00056 
00057 
00058 /* Variables -----------------------------------------------------------------*/
00059 
00060 static volatile uint16_t gLastError;
00061 static volatile uint8_t gStep = 0;
00062 
00063 
00064 /* Variables -----------------------------------------------------------------*/
00065 
00066 /* Initialization parameters. */
00067 L6206_init_t init =
00068 {
00069     L6206_CONF_PARAM_PARALLE_BRIDGES,
00070     {L6206_CONF_PARAM_FREQ_PWM1A, L6206_CONF_PARAM_FREQ_PWM2A, L6206_CONF_PARAM_FREQ_PWM1B, L6206_CONF_PARAM_FREQ_PWM2B},
00071     {100,100,100,100},
00072     {FORWARD,FORWARD,BACKWARD,FORWARD},
00073     {INACTIVE,INACTIVE,INACTIVE,INACTIVE},
00074     {FALSE,FALSE}
00075 };
00076 
00077 /* Motor Control Component. */
00078 L6206 *motor;
00079 
00080 /* User button on Nucleo board */
00081 InterruptIn my_button_irq(USER_BUTTON);
00082 
00083 
00084 /* Functions -----------------------------------------------------------------*/
00085 
00086 /**
00087   * @brief  This function is executed in case of error occurrence.
00088   * @param  error number of the error
00089   * @retval None
00090   */
00091 void my_error_handler(uint16_t error)
00092 {
00093   /* Backup error number */
00094   gLastError = error;
00095   
00096   /* Enter your own code here */
00097 }
00098 
00099 /**
00100 * @brief  This function is the User handler for the flag interrupt
00101 * @param  None
00102 * @retval None
00103 * @note   If needed, implement it, and then attach and enable it:
00104 *           + motor->attach_flag_interrupt(my_flag_irq_handler);
00105 */
00106 void my_flag_irq_handler(void)
00107 {
00108   /* Code to be customised */
00109   /************************/
00110   /* Get the state of bridge A */
00111   uint16_t bridgeState  = motor->get_bridge_status(0);
00112   
00113   if (bridgeState == 0) {
00114     if ((motor->get_device_state(0) != INACTIVE)||
00115         (motor->get_device_state(1) != INACTIVE)) {
00116       /* Bridge A was disabling due to overcurrent or over temperature */
00117       /* When at least on of its  motor was running */
00118         my_error_handler(0XBAD0);
00119     }
00120   }
00121   
00122   /* Get the state of bridge B */
00123   bridgeState  = motor->get_bridge_status(1);
00124   
00125   if (bridgeState == 0)  {
00126     if ((motor->get_device_state(2) != INACTIVE)||
00127         (motor->get_device_state(3) != INACTIVE)) {
00128       /* Bridge A was disabling due to overcurrent or over temperature */
00129       /* When at least on of its  motor was running */
00130         my_error_handler(0XBAD1);
00131     }
00132   }  
00133 }
00134 
00135 
00136 /* Private functions ---------------------------------------------------------*/
00137 
00138 /**
00139   * @brief  Button Irq
00140   * @param  None
00141   * @retval None
00142   */
00143 
00144 void my_button_pressed(void)
00145 {
00146     my_button_irq.disable_irq();
00147     gStep++;
00148     if (gStep > MAX_MOTOR) {
00149         gStep = 0;
00150     }
00151     wait_ms(200);
00152     my_button_irq.enable_irq();
00153 }
00154 
00155 
00156 /**
00157   * @brief  Main program
00158   * @param  None
00159   * @retval None
00160   */
00161 int main(void)
00162 {
00163     /*----- Initialization. -----*/
00164 
00165     /* Initializing Motor Control Component. */
00166 #ifdef TARGET_STM32F429
00167     motor = new L6206(D2, A4, PB_4, PC_7, PA_15, PB_3);
00168 #else
00169     motor = new L6206(D2, A4, D5, D4, A0, A1);
00170 #endif
00171 
00172     /* When init method is called with NULL pointer, the L6206 parameters are set   */
00173     /* with the predefined values from file l6206_target_config.h, otherwise the    */
00174     /* parameters are set using the init structure values.          */
00175     if (motor->init(&init) != COMPONENT_OK) {
00176         exit(EXIT_FAILURE);
00177     }
00178 
00179     /* Attach the function my_flag_irq_handler (defined below) to the flag interrupt */
00180     motor->attach_flag_interrupt(my_flag_irq_handler);
00181 
00182     /* Attach the function my_error_handler (defined below) to the error Handler*/
00183     motor->attach_error_handler(my_error_handler);
00184 
00185     /* Printing to the console. */
00186     printf("Motor Control Application Example for 4 Motors\r\n\n");
00187 
00188     /* Select the configuration with no bridge paralleling, two unidirectionnal motors on bridge A 
00189        and two unidirectionnal motors on bridge B */
00190     motor->set_dual_full_bridge_config(PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B);
00191  
00192     /* Set PWM Frequency of bridge A inputs to 1000 Hz */ 
00193     motor->set_bridge_input_pwm_freq(0,1000);
00194   
00195     /* Set PWM Frequency of bridge B inputs to 2000 Hz */ 
00196     motor->set_bridge_input_pwm_freq(1,2000);
00197   
00198     // Attach my_button_pressed function to Irq
00199     my_button_irq.fall(&my_button_pressed);
00200 
00201     /* Infinite loop */
00202     while (true) {
00203 
00204         if (gStep > 0) {
00205             printf("Running motor 0 at 5%% of the maximum speed\r\n");
00206             /* Set speed of motor 0 to 5% */
00207             motor->set_speed(0,5);
00208             /* start motor 0 */
00209             motor->run(0, BDCMotor::FWD);
00210         }
00211 
00212         if (gStep > 1) {
00213             printf("Running motor 1 at 10%% of the maximum speed\r\n");
00214             /* Set speed of motor 1 to 10 % */
00215             motor->set_speed(1,10);
00216             /* start motor 1 */
00217             motor->run(1, BDCMotor::FWD);
00218         }
00219 
00220         if (gStep > 2) {
00221             printf("Running motor 2 at 15%% of the maximum speed\r\n");
00222             /* Set speed of motor 2 to 15 % */
00223             motor->set_speed(2,15);
00224             /* start motor 2 */
00225             motor->run(2, BDCMotor::FWD);
00226         }
00227 
00228         if (gStep > 3)  {
00229             printf("Running motor 3 at 20%% of the maximum speed\r\n");
00230             /* Set speed of motor 3 to 20 % */
00231             motor->set_speed(3,20);
00232             /* start motor 3 */
00233             motor->run(3, BDCMotor::FWD);      
00234         }
00235 
00236         if (gStep > 0) {
00237             wait_ms(1000);
00238         
00239             motor->hard_hiz(0);   
00240             motor->hard_hiz(1);   
00241             motor->hard_hiz(2);   
00242             motor->hard_hiz(3);
00243             
00244             wait_ms(1000);
00245         }
00246     }
00247 }
00248 
00249 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/