Emmanuel L. / Mbed 2 deprecated HelloWorld_IHM04A1

Dependencies:   X_NUCLEO_IHM04A1 mbed

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

00001 /**
00002   ******************************************************************************
00003   * @file    Multi/Examples/MotionControl/IHM04A1_ExampleFor4UniDirMotors/Src/main.c 
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 /* Includes ------------------------------------------------------------------*/
00044 
00045 /* mbed specific header files. */
00046 #include "mbed.h"
00047 
00048 /* Component specific header files. */
00049 #include "l6206_class.h"
00050 
00051 
00052 
00053 /* Definitions ---------------------------------------------------------------*/
00054 
00055 
00056 #define MAX_MOTOR (4)
00057 
00058 
00059 
00060 /* Variables -----------------------------------------------------------------*/
00061 
00062 static volatile uint16_t gLastError;
00063 static volatile uint8_t gStep = 0;
00064 
00065 
00066 
00067 /* Variables -----------------------------------------------------------------*/
00068 
00069 /* Initialization parameters. */
00070 L6206_Init_t init =
00071 {
00072     L6206_CONF_PARAM_PARALLE_BRIDGES,
00073     {L6206_CONF_PARAM_FREQ_PWM1A, L6206_CONF_PARAM_FREQ_PWM2A, L6206_CONF_PARAM_FREQ_PWM1B, L6206_CONF_PARAM_FREQ_PWM2B},
00074     {100,100,100,100},
00075     {FORWARD,FORWARD,BACKWARD,FORWARD},
00076     {INACTIVE,INACTIVE,INACTIVE,INACTIVE},
00077     {FALSE,FALSE}
00078 };
00079 
00080 /* Motor Control Component. */
00081 L6206 *motor;
00082 
00083 /* User button on Nucleo board */
00084 InterruptIn mybutton_irq(USER_BUTTON);
00085 
00086 
00087 
00088 /* Functions -----------------------------------------------------------------*/
00089 
00090 /**
00091   * @brief  This function is executed in case of error occurrence.
00092   * @param  error number of the error
00093   * @retval None
00094   */
00095 void Error_Handler(uint16_t error)
00096 {
00097   /* Backup error number */
00098   gLastError = error;
00099   
00100   /* Enter your own code here */
00101 }
00102 
00103 /**
00104 * @brief  This function is the User handler for the flag interrupt
00105 * @param  None
00106 * @retval None
00107 * @note   If needed, implement it, and then attach and enable it:
00108 *           + motor->AttachFlagInterrupt(MyFlagInterruptHandler);
00109 */
00110 void MyFlagInterruptHandler(void)
00111 {
00112   /* Code to be customised */
00113   /************************/
00114   /* Get the state of bridge A */
00115   uint16_t bridgeState  = motor->GetBridgeStatus(0);
00116   
00117   if (bridgeState == 0) 
00118   {
00119     if ((motor->GetDeviceState(0) != INACTIVE)||
00120         (motor->GetDeviceState(1) != INACTIVE))
00121     {
00122       /* Bridge A was disabling due to overcurrent or over temperature */
00123       /* When at least on of its  motor was running */
00124         Error_Handler(0XBAD0);
00125     }
00126   }
00127   
00128   /* Get the state of bridge B */
00129   bridgeState  = motor->GetBridgeStatus(1);
00130   
00131   if (bridgeState == 0) 
00132   {
00133     if ((motor->GetDeviceState(2) != INACTIVE)||
00134         (motor->GetDeviceState(3) != INACTIVE))
00135     {
00136       /* Bridge A was disabling due to overcurrent or over temperature */
00137       /* When at least on of its  motor was running */
00138         Error_Handler(0XBAD1);
00139     }
00140   }  
00141  }
00142 
00143 
00144 /* Private functions ---------------------------------------------------------*/
00145 
00146 /**
00147   * @brief  Button Irq
00148   * @param  None
00149   * @retval None
00150   */
00151 
00152 void button_pressed(void)
00153 {
00154     mybutton_irq.disable_irq();
00155     gStep++;
00156     if (gStep > MAX_MOTOR)
00157     {
00158         gStep = 0;
00159     }
00160     wait_ms(200);
00161     mybutton_irq.enable_irq();
00162 
00163 }
00164 
00165 
00166 /**
00167   * @brief  Main program
00168   * @param  None
00169   * @retval None
00170   */
00171 int main(void)
00172 {
00173     /*----- Initialization. -----*/
00174 
00175     /* Initializing Motor Control Component. */
00176     motor = new L6206( D2, A4, D5, D4, A0, A1);
00177 
00178     /* When Init method is called with NULL pointer, the L6206 parameters are set   */
00179     /* with the predefined values from file l6206_target_config.h, otherwise the    */
00180     /* parameters are set using the initDeviceParameters structure values.          */
00181     if (motor->Init(&init) != COMPONENT_OK)
00182         exit(EXIT_FAILURE);
00183 
00184     /* Attach the function MyFlagInterruptHandler (defined below) to the flag interrupt */
00185     motor->AttachFlagInterrupt(MyFlagInterruptHandler);
00186 
00187     /* Attach the function Error_Handler (defined below) to the error Handler*/
00188     motor->AttachErrorHandler(Error_Handler);
00189 
00190     /* Printing to the console. */
00191     printf("Motor Control Application Example for 4 Motor\r\n\n");
00192 
00193 
00194     /* Select the configuration with no bridge paralleling, two unidirectionnal motors on bridge A 
00195        and two unidirectionnal motors on bridge B */
00196     motor->SetDualFullBridgeConfig(PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B);
00197  
00198     /* Set PWM Frequency of bridge A inputs to 1000 Hz */ 
00199     motor->SetBridgeInputPwmFreq(0,1000);
00200   
00201     /* Set PWM Frequency of bridge B inputs to 2000 Hz */ 
00202     motor->SetBridgeInputPwmFreq(1,2000);
00203   
00204     // Attach button_pressed function to Irq
00205     mybutton_irq.fall(&button_pressed);
00206 
00207     /* Infinite loop */
00208     while(1)
00209     {
00210 
00211         if (gStep > 0)
00212         {
00213             printf("Run motor 0 at 20%% of the maximum speed\n");
00214             /* Set speed of motor 0 to 20% */
00215             motor->SetSpeed(0,20);
00216             /* start motor 0 */
00217             motor->Run(0, BDCMotor::FWD);
00218         }
00219 
00220         if (gStep > 1)
00221         {
00222             printf("Run motor 1 at 30%% of the maximum speed\n");
00223             /* Set speed of motor 1 to 30 % */
00224             motor->SetSpeed(1,30);
00225             /* start motor 1 */
00226             motor->Run(1, BDCMotor::FWD);
00227         }
00228 
00229         if (gStep > 2)
00230         {
00231             printf("Run motor 2 at 40%% of the maximum speed\n");
00232             /* Set speed of motor 2 to 40 % */
00233             motor->SetSpeed(2,40);
00234             /* start motor 2 */
00235             motor->Run(2, BDCMotor::FWD);
00236         }
00237 
00238         if (gStep > 3)
00239         {
00240             printf("Run motor 3 at 50%% of the maximum speed\n");
00241             /* Set speed of motor 3 to 50 % */
00242             motor->SetSpeed(3,50);
00243             /* start motor 3 */
00244             motor->Run(3, BDCMotor::FWD);      
00245         }
00246 
00247         if (gStep > 0)
00248         {
00249             wait_ms(1000);
00250         
00251             motor->HardHiZ(0);   
00252             motor->HardHiZ(1);   
00253             motor->HardHiZ(2);   
00254             motor->HardHiZ(3);
00255             
00256             wait_ms(1000);
00257         }
00258     }
00259 }
00260 
00261 
00262 
00263 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00264