|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file X_NUCLEO_IHM12A1_HelloWorld.ino |
| 4 | + * @author STMicroelectronics |
| 5 | + * @version V1.0.0 |
| 6 | + * @date 29 July 2019 |
| 7 | + * @brief Arduino test application for the STMicroelectronics X-NUCLEO-IHM12A1 |
| 8 | + * Motor Control Expansion Board: control of 2 DC Brush motors. |
| 9 | + ****************************************************************************** |
| 10 | + * @attention |
| 11 | + * |
| 12 | + * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> |
| 13 | + * |
| 14 | + * Redistribution and use in source and binary forms, with or without modification, |
| 15 | + * are permitted provided that the following conditions are met: |
| 16 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 17 | + * this list of conditions and the following disclaimer. |
| 18 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 | + * this list of conditions and the following disclaimer in the documentation |
| 20 | + * and/or other materials provided with the distribution. |
| 21 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 22 | + * may be used to endorse or promote products derived from this software |
| 23 | + * without specific prior written permission. |
| 24 | + * |
| 25 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 28 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 29 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 32 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 33 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + * |
| 36 | + ****************************************************************************** |
| 37 | + */ |
| 38 | + |
| 39 | +/* Includes ------------------------------------------------------------------*/ |
| 40 | + |
| 41 | +/* Arduino specific header files. */ |
| 42 | +#include "Arduino.h" |
| 43 | + |
| 44 | +/* Component specific header files. */ |
| 45 | +#include "STSpin240_250.h" |
| 46 | + |
| 47 | +/* Definitions ---------------------------------------------------------------*/ |
| 48 | + |
| 49 | +#define SerialPort Serial |
| 50 | + |
| 51 | +/* Variables -----------------------------------------------------------------*/ |
| 52 | + |
| 53 | +/* Initialization parameters of the motor connected to the expansion board. */ |
| 54 | +STSpin240_250_init_t init_s = |
| 55 | +{ |
| 56 | + 20000, /* Frequency of PWM of Input Bridge A in Hz up to 100000Hz */ |
| 57 | + 20000, /* Frequency of PWM of Input Bridge B in Hz up to 100000Hz */ |
| 58 | + 20000, /* Frequency of PWM used for Ref pin in Hz up to 100000Hz */ |
| 59 | + 50, /* Duty cycle of PWM used for Ref pin (from 0 to 100) */ |
| 60 | + TRUE /* Dual Bridge configuration (FALSE for mono, TRUE for dual brush dc) */ |
| 61 | +}; |
| 62 | + |
| 63 | +/* Motor Control Component. */ |
| 64 | +STSpin240_250 *motor; |
| 65 | + |
| 66 | +/* Functions -----------------------------------------------------------------*/ |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief This is an example of error handler. |
| 70 | + * @param[in] error Number of the error |
| 71 | + * @retval None |
| 72 | + * @note If needed, implement it, and then attach it: |
| 73 | + * + motor->attach_error_handler(&my_error_handler); |
| 74 | + */ |
| 75 | +void my_error_handler(uint16_t error) |
| 76 | +{ |
| 77 | + /* Printing to the console. */ |
| 78 | + char report[256]; |
| 79 | + sprintf(report, "Error 0x%x detected\r\n\n", error); |
| 80 | + SerialPort.print(report); |
| 81 | + |
| 82 | + /* Infinite loop */ |
| 83 | + while (true) |
| 84 | + { |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief This is an example of user handler for the flag interrupt. |
| 90 | + * @param None |
| 91 | + * @retval None |
| 92 | + * @note If needed, implement it, and then attach and enable it: |
| 93 | + * + motor->attach_flag_irq(&my_flag_irq_handler); |
| 94 | + * + motor->enable_flag_irq(); |
| 95 | + * To disable it: |
| 96 | + * + motor->DisbleFlagIRQ(); |
| 97 | + */ |
| 98 | +void my_flag_irq_handler(void) |
| 99 | +{ |
| 100 | + /* Code to be customised */ |
| 101 | + /************************/ |
| 102 | + |
| 103 | + SerialPort.print(" WARNING: \"FLAG\" interrupt triggered.\r\n"); |
| 104 | + |
| 105 | + /* Get the state of bridge A */ |
| 106 | + uint16_t bridgeState = motor->get_bridge_status(0); |
| 107 | + |
| 108 | + if (bridgeState == 0) |
| 109 | + { |
| 110 | + if (motor->get_device_state(0) != INACTIVE || motor->get_device_state(1) != INACTIVE) |
| 111 | + { |
| 112 | + /* Bridges were disabled due to overcurrent or over temperature */ |
| 113 | + /* When motor was running */ |
| 114 | + my_error_handler(0xBAD0); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/* setup ----------------------------------------------------------------------*/ |
| 120 | + |
| 121 | +void setup() |
| 122 | +{ |
| 123 | + /* Initialize Serial Port */ |
| 124 | + SerialPort.begin(115200); |
| 125 | + |
| 126 | + /* Printing to the console. */ |
| 127 | + SerialPort.println("STARTING DEMO PROGRAM"); |
| 128 | + |
| 129 | + /* Initialization of the motor driver*/ |
| 130 | + /* Please, be careful that pins can change if you change the Nucleo board */ |
| 131 | + /* Give a look at the DataSheet of the Nucleo to find a correct configuration */ |
| 132 | + motor = new STSpin240_250(D2, D9, D6, D7, D5, D4, A0); |
| 133 | + |
| 134 | + if (motor->init(&init_s) != COMPONENT_OK) exit(EXIT_FAILURE); |
| 135 | + |
| 136 | + /* Set dual bridge enabled as two motors are used*/ |
| 137 | + motor->set_dual_full_bridge_config(1); |
| 138 | + |
| 139 | + /* Attaching and enabling an interrupt handler. */ |
| 140 | + motor->attach_flag_irq(&my_flag_irq_handler); |
| 141 | + |
| 142 | + /* Attaching an error handler */ |
| 143 | + motor->attach_error_handler(&my_error_handler); |
| 144 | + |
| 145 | + /* Printing to the console. */ |
| 146 | + SerialPort.print("Motor Control Application Example for 2 brush DC motors\r\n"); |
| 147 | + |
| 148 | + /* Set PWM Frequency of Ref to 15000 Hz */ |
| 149 | + motor->set_ref_pwm_freq(0, 15000); |
| 150 | + |
| 151 | + /* Set PWM duty cycle of Ref to 60% */ |
| 152 | + motor->set_ref_pwm_dc(0, 60); |
| 153 | + |
| 154 | + /* Set PWM Frequency of bridge A inputs to 10000 Hz */ |
| 155 | + motor->set_bridge_input_pwm_freq(0,10000); |
| 156 | + |
| 157 | + /* Set PWM Frequency of bridge B inputs to 10000 Hz */ |
| 158 | + motor->set_bridge_input_pwm_freq(1,10000); |
| 159 | +} |
| 160 | + |
| 161 | +/* loop ----------------------------------------------------------------------*/ |
| 162 | + |
| 163 | +void loop() |
| 164 | +{ |
| 165 | + uint8_t demoStep = 0; |
| 166 | + |
| 167 | + while (demoStep <= 12) |
| 168 | + { |
| 169 | + switch (demoStep) |
| 170 | + { |
| 171 | + case 0: |
| 172 | + { |
| 173 | + SerialPort.print("STEP 0: Motor(0) FWD Speed=100%% - Motor(1) Inactive\r\n"); |
| 174 | + /* Set speed of motor 0 to 100 % */ |
| 175 | + motor->set_speed(0,100); |
| 176 | + /* start motor 0 to run forward*/ |
| 177 | + /* if chip is in standby mode */ |
| 178 | + /* it is automatically awakened */ |
| 179 | + motor->run(0, BDCMotor::FWD); |
| 180 | + break; |
| 181 | + } |
| 182 | + case 1: |
| 183 | + { |
| 184 | + SerialPort.print("STEP 1: Motor(0) FWD Speed=75%% - Motor(1) BWD Speed=100%%\r\n"); |
| 185 | + /* Set speed of motor 0 to 75 % */ |
| 186 | + motor->set_speed(0,75); |
| 187 | + /* Set speed of motor 1 to 100 % */ |
| 188 | + motor->set_speed(1,100); |
| 189 | + /* start motor 1 to run backward */ |
| 190 | + motor->run(1, BDCMotor::BWD); |
| 191 | + break; |
| 192 | + } |
| 193 | + case 2: |
| 194 | + { |
| 195 | + SerialPort.print("STEP 2: Motor(0) FWD Speed=50%% - Motor(1) BWD Speed=75%%\r\n"); |
| 196 | + /* Set speed of motor 0 to 50 % */ |
| 197 | + motor->set_speed(0,50); |
| 198 | + /* Set speed of motor 1 to 75% */ |
| 199 | + motor->set_speed(1,75); |
| 200 | + break; |
| 201 | + } |
| 202 | + case 3: |
| 203 | + { |
| 204 | + SerialPort.print("STEP 3: Motor(0) FWD Speed=25%% - Motor(1) BWD Speed=50%%\r\n"); |
| 205 | + /* Set speed of motor 0 to 25 % */ |
| 206 | + motor->set_speed(0,25); |
| 207 | + /* Set speed of motor 1 to 50% */ |
| 208 | + motor->set_speed(1,50); |
| 209 | + break; |
| 210 | + } |
| 211 | + case 4: |
| 212 | + { |
| 213 | + SerialPort.print("STEP 4: Motor(0) Stopped - Motor(1) BWD Speed=25%%\r\n"); |
| 214 | + /* Stop Motor 0 */ |
| 215 | + motor->hard_stop(0); |
| 216 | + /* Set speed of motor 1 to 25% */ |
| 217 | + motor->set_speed(1,25); |
| 218 | + break; |
| 219 | + } |
| 220 | + case 5: |
| 221 | + { |
| 222 | + SerialPort.print("STEP 5: Motor(0) BWD Speed=25%% - Motor(1) Stopped\r\n"); |
| 223 | + /* Set speed of motor 0 to 25 % */ |
| 224 | + motor->set_speed(0,25); |
| 225 | + /* start motor 0 to run backward */ |
| 226 | + motor->run(0, BDCMotor::BWD); |
| 227 | + /* Stop Motor 1 */ |
| 228 | + motor->hard_stop(1); |
| 229 | + break; |
| 230 | + } |
| 231 | + case 6: |
| 232 | + { |
| 233 | + SerialPort.print("STEP 6: Motor(0) BWD Speed=50%% - Motor(1) FWD Speed=25%%\r\n"); |
| 234 | + /* Set speed of motor 0 to 50 % */ |
| 235 | + motor->set_speed(0,50); |
| 236 | + /* Set speed of motor 1 to 25 % */ |
| 237 | + motor->set_speed(1,25); |
| 238 | + /* start motor 1 to run backward */ |
| 239 | + motor->run(1, BDCMotor::FWD); |
| 240 | + break; |
| 241 | + } |
| 242 | + case 7: |
| 243 | + { |
| 244 | + SerialPort.print("STEP 7: Motor(0) BWD Speed=75%% - Motor(1) FWD Speed=50%%\r\n"); |
| 245 | + /* Set speed of motor 0 to 75 % */ |
| 246 | + motor->set_speed(0,75); |
| 247 | + /* Set speed of motor 1 to 50 % */ |
| 248 | + motor->set_speed(1,50); |
| 249 | + break; |
| 250 | + } |
| 251 | + case 8: |
| 252 | + { |
| 253 | + SerialPort.print("STEP 8: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=75%%\r\n"); |
| 254 | + /* Set speed of motor 0 to 100 % */ |
| 255 | + motor->set_speed(0,100); |
| 256 | + /* Set speed of motor 1 to 75 % */ |
| 257 | + motor->set_speed(1,75); |
| 258 | + break; |
| 259 | + } |
| 260 | + case 9: |
| 261 | + { |
| 262 | + SerialPort.print("STEP 9: Motor(0) BWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n"); |
| 263 | + /* Set speed of motor 1 to 100 % */ |
| 264 | + motor->set_speed(1,100); |
| 265 | + break; |
| 266 | + } |
| 267 | + case 10: |
| 268 | + { |
| 269 | + SerialPort.print("STEP 10: Stop both motors and disable bridges\r\n"); |
| 270 | + /* Stop both motors and disable bridge */ |
| 271 | + motor->hard_hiz(0); |
| 272 | + motor->hard_hiz(1); |
| 273 | + break; |
| 274 | + } |
| 275 | + case 11: |
| 276 | + { |
| 277 | + SerialPort.print("STEP 11: Motor(0) FWD Speed=100%% - Motor(1) FWD Speed=100%%\r\n"); |
| 278 | + /* Start both motors to go forward*/ |
| 279 | + motor->run(0,BDCMotor::FWD); |
| 280 | + motor->run(1,BDCMotor::FWD); |
| 281 | + break; |
| 282 | + } |
| 283 | + case 12: |
| 284 | + default: |
| 285 | + { |
| 286 | + SerialPort.print("STEP 12: Stop both motors and enter standby mode\r\n"); |
| 287 | + /* Stop both motors and put chip in standby mode */ |
| 288 | + motor->reset(); |
| 289 | + break; |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + /* Wait for 2 seconds */ |
| 294 | + delay(2000); |
| 295 | + |
| 296 | + /* Increment demostep*/ |
| 297 | + demoStep++; |
| 298 | + } |
| 299 | +} |
0 commit comments