Hans Meier / Mbed 2 deprecated MyHelloWorld_IHM04A1

Dependencies:   FastPWM mbed ST_INTERFACES

Fork of HelloWorld_IHM04A1 by ST

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     {0,0,0,0},
00072     {FORWARD,FORWARD,FORWARD,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   printf("Error: %u\r\n\n",error);
00098 }
00099 
00100 /**
00101 * @brief  This function is the User handler for the flag interrupt
00102 * @param  None
00103 * @retval None
00104 * @note   If needed, implement it, and then attach and enable it:
00105 *           + motor->attach_flag_interrupt(my_flag_irq_handler);
00106 */
00107 void my_flag_irq_handler(void)
00108 {
00109   /* Code to be customised */
00110   /************************/
00111   /* Get the state of bridge A */
00112   uint16_t bridgeState  = motor->get_bridge_status(0);
00113   
00114   if (bridgeState == 0) {
00115     if ((motor->get_device_state(0) != INACTIVE)||
00116         (motor->get_device_state(1) != INACTIVE)) {
00117       /* Bridge A was disabling due to overcurrent or over temperature */
00118       /* When at least on of its  motor was running */
00119         my_error_handler(0XBAD0);
00120     }
00121   }
00122   
00123   /* Get the state of bridge B */
00124   bridgeState  = motor->get_bridge_status(1);
00125   
00126   if (bridgeState == 0)  {
00127     if ((motor->get_device_state(2) != INACTIVE)||
00128         (motor->get_device_state(3) != INACTIVE)) {
00129       /* Bridge A was disabling due to overcurrent or over temperature */
00130       /* When at least on of its  motor was running */
00131         my_error_handler(0XBAD1);
00132     }
00133   }  
00134 }
00135 
00136 
00137 /* Private functions ---------------------------------------------------------*/
00138 
00139 /**
00140   * @brief  Button Irq
00141   * @param  None
00142   * @retval None
00143   */
00144 
00145 void my_button_pressed(void)
00146 {
00147     my_button_irq.disable_irq();
00148     gStep++;
00149     if (gStep > MAX_MOTOR) {
00150         gStep = 0;
00151     }
00152     wait_ms(200);
00153     my_button_irq.enable_irq();
00154 }
00155 
00156 
00157 /**
00158   * @brief  Main program
00159   * @param  None
00160   * @retval None
00161   */
00162 int main(void)
00163 {
00164     /*----- Initialization. -----*/
00165 
00166     /* Initializing Motor Control Component. */
00167     motor = new L6206( D2, A4, D5, D4, A0, A1);
00168     DigitalIn Pin_d4(D4);
00169     DigitalIn Pin_a0(A0);
00170     DigitalIn Pin_a1(A1);
00171     
00172     Pin_d4.mode(PullNone);
00173     Pin_a0.mode(PullNone);
00174     Pin_a1.mode(PullNone);
00175     
00176     /* When init method is called with NULL pointer, the L6206 parameters are set   */
00177     /* with the predefined values from file l6206_target_config.h, otherwise the    */
00178     /* parameters are set using the init structure values.  */        
00179     if (motor->init(&init) != COMPONENT_OK) {
00180         exit(EXIT_FAILURE);
00181     }
00182     
00183     /* Attach the function my_flag_irq_handler (defined below) to the flag interrupt */
00184     motor->attach_flag_interrupt(my_flag_irq_handler);
00185 
00186     /* Attach the function my_error_handler (defined below) to the error Handler*/
00187     motor->attach_error_handler(my_error_handler);
00188 
00189     /* Printing to the console. */
00190     printf("Motor Control Application Example for 4 Motors\r\n\n");
00191 
00192     /* Select the configuration with no bridge paralleling, two unidirectionnal motors on bridge A 
00193        and two unidirectionnal motors on bridge B */
00194     //motor->set_dual_full_bridge_config(PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B);
00195     motor->set_dual_full_bridge_config(PARALLELING_ALL_WITH_IN1A___1_UNDIR_MOTOR);
00196  
00197     /* Set PWM Frequency of bridge A inputs to 1000 Hz */ 
00198    // motor->set_bridge_input_pwm_freq(0,10000);
00199   
00200     /* Set PWM Frequency of bridge B inputs to 2000 Hz */ 
00201     //motor->set_bridge_input_pwm_freq(1,2000);
00202     motor->set_bridge_input_pwm_freq(0,1000);
00203     //motor->set_bridge_input_pwm_freq(1,50000);
00204     Pin_d4.mode(PullNone);
00205     Pin_a0.mode(PullNone);
00206     Pin_a1.mode(PullNone);
00207   
00208     // Attach my_button_pressed function to Irq
00209     my_button_irq.fall(&my_button_pressed);
00210 
00211     /* Infinite loop */
00212     while (true) {
00213         
00214         if (gStep == 0) {
00215             printf("stopping motor 0\r\n");
00216             /* Set speed of motor 0 to 5% */
00217             motor->hard_hiz(0);  
00218         }
00219 
00220         
00221         
00222         if (gStep == 1) {
00223             printf("Running motor 0 at 1%% of the maximum speed\r\n");
00224             /* Set speed of motor 0 to 5% */
00225             motor->set_speed(0,100);
00226             /* start motor 0 */
00227             motor->run(0, BDCMotor::FWD);
00228 
00229         }
00230 
00231         if (gStep == 2) {
00232             printf("Running motor 0 at 5%% of the maximum speed\r\n");
00233             /* Set speed of motor 1 to 10 % */
00234             motor->set_speed(0,500);
00235             /* start motor 1 */
00236             //motor->run(1, BDCMotor::FWD);
00237             motor->run(0, BDCMotor::FWD);
00238         }
00239 
00240         if (gStep == 3) {
00241             printf("Running motor 0 at 10%% of the maximum speed\r\n");
00242             /* Set speed of motor 2 to 15 % */
00243             motor->set_speed(0,1000);
00244             /* start motor 2 */
00245             //motor->run(2, BDCMotor::FWD);
00246             motor->run(0, BDCMotor::FWD);
00247         }
00248 
00249         if (gStep == 4)  {
00250             printf("Running motor 0 at 50%% of the maximum speed\r\n");
00251             /* Set speed of motor 3 to 20 % */
00252             motor->set_speed(0,5000);
00253             /* start motor 3 */
00254             //motor->run(3, BDCMotor::FWD);      
00255             motor->run(0, BDCMotor::FWD);
00256         }
00257 
00258         if (gStep >= 0) {
00259             wait_ms(1000);
00260         
00261             //motor->hard_hiz(0);   
00262             //motor->hard_hiz(1);   
00263             //motor->hard_hiz(2);   
00264             //motor->hard_hiz(3);
00265             
00266             //wait_ms(1000);
00267         }
00268     }
00269 }
00270 
00271 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00272