Example program for library X-NUCLEO-IHM12A1 using two low voltage dual brush DC motors

Dependencies:   X_NUCLEO_IHM12A1 mbed

Fork of HelloWorld_IHM12A1 by ST Expansion SW Team

This application provides a simple example of usage of the X-NUCLEO-IHM12A1 Dual Brush DC Motor Control Expansion Board.

It shows how to use 2 bidirectional brush DC motors connected to the board by executing a 13 steps loop:

  • Step 0) Motor 0 runs in forward at 100% of max speed, Motor 1 is inactive
  • Step 1) Motor 0 runs in forward at 75% of max speed, Motor 1 runs in backward at 100% of max speed
  • Step 2) Motor 0 runs in forward at 50% of max speed, Motor 1 runs in backward at 75% of max speed
  • Step 3) Motor 0 runs in forward at 25% of max speed, Motor 1 runs in backward at 50% of max speed
  • Step 4) Motor 0 is stopped, Motor 1 runs in backward at 25% of max speed
  • Step 5) Motor 0 runs in backward at 25% of max speed, Motor 1 is stopped
  • Step 6) Motor 0 runs in backward at 50% of max speed, Motor 1 runs in forward at 25% of max speed
  • Step 7) Motor 0 runs in backward at 75% of max speed, Motor 1 runs in forward at 50% of max speed
  • Step 8) Motor 0 runs in backward at 100% of max speed, Motor 1 runs in forward at 75% of max speed
  • Step 9) Motor 0 runs in backward at 100% of max speed, Motor 1 runs in forward at 100% of max speed
  • Step 10) Stop both motors and disable bridges
  • Step 11) Motor 0 runs in forward at 100% of max speed, Motor 1 runs in forward at 100% of max speed
  • Step 12) Stop both motors and enter standby mode

For the hardware configuration of the expansion board, please refer to the X_NUCLEO_IHM12A1 library web page.

Committer:
Manu_L
Date:
Thu Apr 28 14:03:58 2016 +0000
Revision:
0:773e2a2be16f
Child:
2:1e4061cedf1d
HelloWorld_IHM12A1 creation

Who changed what in which revision?

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