Robo l minuscolo
Dependencies: X_NUCLEO_IHM04A1 mbed
Fork of HelloWorld_IHM04A1 by
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>© 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 00174 printf("ESTRONCIO\n"); 00175 /*Con il codice 00176 00177 motor->run(0, BDCMotor::FWD); // Forward 00178 00179 fai andare avanti. 00180 00181 00182 00183 Se vuoi cambiare il verso devi usare questo codice 00184 00185 motor->run(0, BDCMotor::BWD); // Backword 00186 00187 00188 00189 Lo 0 indica il motore 0, per il secondo motore puoi usare 1 collegandolo opportunamente. 00190 00191 00192 00193 Quindi ad esempio se devi fare andare tutti e due avanti, usi questo codice 00194 00195 motor->run(0, BDCMotor::FWD); // Forward 00196 00197 motor->run(1, BDCMotor::FWD); // Forward 00198 00199 00200 00201 Per farli andare indietro 00202 00203 motor->run(0, BDCMotor::BWD); // Forward 00204 00205 motor->run(1, BDCMotor::BWD); // Forward 00206 */ 00207 /*----- Initialization. -----*/ 00208 00209 /* Initializing Motor Control Component. */ 00210 motor = new L6206( D2, A4, D5, D4, A0, A1); 00211 00212 /* When Init method is called with NULL pointer, the L6206 parameters are set */ 00213 /* with the predefined values from file l6206_target_config.h, otherwise the */ 00214 /* parameters are set using the initDeviceParameters structure values. */ 00215 // if (motor->Init(&init) != COMPONENT_OK) 00216 // exit(EXIT_FAILURE); 00217 00218 /* Attach the function MyFlagInterruptHandler (defined below) to the flag interrupt */ 00219 // motor->AttachFlagInterrupt(MyFlagInterruptHandler); 00220 00221 /* Attach the function Error_Handler (defined below) to the error Handler*/ 00222 // motor->AttachErrorHandler(Error_Handler); 00223 00224 /* Printing to the console. */ 00225 printf("Motor Control Application Example for 4 Motor\r\n\n"); 00226 00227 00228 /* Select the configuration with no bridge paralleling, two unidirectionnal motors on bridge A 00229 and two unidirectionnal motors on bridge B */ 00230 00231 // motor->SetDualFullBridgeConfig(PARALLELING_NONE___2_UNDIR_MOTOR_BRIDGE_A__2_UNDIR_MOTOR_BRIDGE_B); 00232 motor->SetDualFullBridgeConfig(PARALLELING_NONE___1_BIDIR_MOTOR_BRIDGE_A__1_BIDIR_MOTOR_BRIDGE_B); 00233 00234 /* Set PWM Frequency of bridge A inputs to 1000 Hz */ 00235 motor->SetBridgeInputPwmFreq(0,2000); 00236 00237 /* Set PWM Frequency of bridge B inputs to 2000 Hz */ 00238 motor->SetBridgeInputPwmFreq(1,2000); 00239 00240 // // Attach button_pressed function to Irq 00241 // mybutton_irq.fall(&button_pressed); 00242 00243 /* Infinite loop */ 00244 while(1) 00245 { 00246 00247 // if (gStep > 0) 00248 // { 00249 printf("ESTRONCIOLETTO\n"); 00250 // printf("Run motor 0 at 20%% of the maximum speed\n"); 00251 // /* Set speed of motor 0 to 20% */ 00252 // motor->SetSpeed(0,40); 00253 /* start motor 0 */ 00254 // motor->Run(0, BDCMotor::FWD); 00255 // } 00256 00257 // if (gStep > 1) 00258 // { 00259 printf("Run motor 1 e 2 at 50% avanti\n"); 00260 /* Set speed of motor 1 to 30 % */ 00261 motor->SetSpeed(0,100); 00262 motor->SetSpeed(1,100); 00263 /* start motor 1 */ 00264 motor->Run(1, BDCMotor::FWD); 00265 motor->Run(0, BDCMotor::FWD); // Avanti 00266 printf("Motori 1 e 2 FERMI\n"); 00267 wait_ms(5000); 00268 motor->HardHiZ(0); 00269 motor->HardHiZ(1); 00270 // printf("Motori 1 e 2 FERMI\n"); 00271 wait_ms(1000); 00272 printf("Run motor 1 e 2 at 50% indietro\n"); 00273 motor->Run(1, BDCMotor::BWD); 00274 motor->Run(0, BDCMotor::BWD); // Indietro 00275 wait_ms(5000); 00276 motor->HardHiZ(0); 00277 motor->HardHiZ(1); 00278 printf("Motori 1 e 2 FERMI\n"); 00279 wait_ms(1000); 00280 // } 00281 00282 // if (gStep > 2) 00283 // { 00284 // printf("Run motor 2 at 40%% of the maximum speed\n"); 00285 // /* Set speed of motor 2 to 40 % */ 00286 // motor->SetSpeed(2,100); 00287 // /* start motor 2 */ 00288 // motor->Run(2, BDCMotor::FWD); 00289 // } 00290 00291 // if (gStep > 3) 00292 // { 00293 // printf("Run motor 3 at 50%% of the maximum speed\n"); 00294 // /* Set speed of motor 3 to 50 % */ 00295 // motor->SetSpeed(3,100); 00296 // /* start motor 3 */ 00297 // motor->Run(3, BDCMotor::FWD); 00298 // } 00299 00300 // if (gStep > 0) 00301 // { 00302 // wait_ms(1000); 00303 00304 // motor->HardHiZ(0); 00305 // motor->HardHiZ(1); 00306 // motor->HardHiZ(2); 00307 // motor->HardHiZ(3); 00308 00309 // wait_ms(1000); 00310 // } 00311 00312 } 00313 } 00314 00315 00316 00317 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 00318
Generated on Sat Jul 30 2022 02:56:22 by 1.7.2