Codice per pilotare il motore IHM01A1

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
Davidroid
Date:
Wed Nov 18 18:45:35 2015 +0000
Revision:
3:fffa53c7aed2
Parent:
2:e12e4df7a486
Child:
5:8065b587ade0
+ Program updated to reflect the modifications to the "StepperMotor" abstract class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /**
Davidroid 0:e6a49a092e2a 2 ******************************************************************************
Davidroid 0:e6a49a092e2a 3 * @file main.cpp
Davidroid 0:e6a49a092e2a 4 * @author Davide Aliprandi / AST
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 0:e6a49a092e2a 7 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
Davidroid 0:e6a49a092e2a 8 * Motor Control Expansion Board: control of 1 motor.
Davidroid 0:e6a49a092e2a 9 * This application makes use of a C++ component architecture obtained
Davidroid 0:e6a49a092e2a 10 * from the C component architecture through the Stm32CubeTOO tool.
Davidroid 0:e6a49a092e2a 11 ******************************************************************************
Davidroid 0:e6a49a092e2a 12 * @attention
Davidroid 0:e6a49a092e2a 13 *
Davidroid 0:e6a49a092e2a 14 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:e6a49a092e2a 15 *
Davidroid 0:e6a49a092e2a 16 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:e6a49a092e2a 17 * are permitted provided that the following conditions are met:
Davidroid 0:e6a49a092e2a 18 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:e6a49a092e2a 19 * this list of conditions and the following disclaimer.
Davidroid 0:e6a49a092e2a 20 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:e6a49a092e2a 21 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:e6a49a092e2a 22 * and/or other materials provided with the distribution.
Davidroid 0:e6a49a092e2a 23 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:e6a49a092e2a 24 * may be used to endorse or promote products derived from this software
Davidroid 0:e6a49a092e2a 25 * without specific prior written permission.
Davidroid 0:e6a49a092e2a 26 *
Davidroid 0:e6a49a092e2a 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:e6a49a092e2a 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:e6a49a092e2a 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:e6a49a092e2a 30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:e6a49a092e2a 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:e6a49a092e2a 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:e6a49a092e2a 33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:e6a49a092e2a 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:e6a49a092e2a 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:e6a49a092e2a 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:e6a49a092e2a 37 *
Davidroid 0:e6a49a092e2a 38 ******************************************************************************
Davidroid 0:e6a49a092e2a 39 */
Davidroid 0:e6a49a092e2a 40
Davidroid 0:e6a49a092e2a 41
Davidroid 0:e6a49a092e2a 42 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 43
Davidroid 0:e6a49a092e2a 44 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 45 #include "mbed.h"
Davidroid 0:e6a49a092e2a 46
Davidroid 0:e6a49a092e2a 47 /* Helper header files. */
Davidroid 0:e6a49a092e2a 48 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 49
Davidroid 0:e6a49a092e2a 50 /* Component specific header files. */
Davidroid 0:e6a49a092e2a 51 #include "l6474_class.h"
Davidroid 0:e6a49a092e2a 52
Davidroid 0:e6a49a092e2a 53
Davidroid 0:e6a49a092e2a 54 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 55
Davidroid 0:e6a49a092e2a 56 /* Number of steps corresponding to one round angle of the motor. */
Davidroid 3:fffa53c7aed2 57 #define ROUND_ANGLE_STEPS 3200
Davidroid 0:e6a49a092e2a 58
Davidroid 0:e6a49a092e2a 59
Davidroid 0:e6a49a092e2a 60 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 61
Davidroid 0:e6a49a092e2a 62 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 63 L6474 *motor;
Davidroid 0:e6a49a092e2a 64
Davidroid 0:e6a49a092e2a 65
Davidroid 0:e6a49a092e2a 66 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 67
Davidroid 0:e6a49a092e2a 68 int main()
Davidroid 0:e6a49a092e2a 69 {
Davidroid 0:e6a49a092e2a 70 /* Initializing SPI bus. */
Davidroid 0:e6a49a092e2a 71 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 72
Davidroid 0:e6a49a092e2a 73 /* Initializing Motor Control Component. */
Davidroid 1:fbf28f3367aa 74 motor = new L6474(D8, D7, D9, D10, dev_spi);
Davidroid 1:fbf28f3367aa 75 if (motor->Init(NULL) != COMPONENT_OK)
Davidroid 0:e6a49a092e2a 76 return false;
Davidroid 0:e6a49a092e2a 77
Davidroid 0:e6a49a092e2a 78 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 79 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:e6a49a092e2a 80
Davidroid 0:e6a49a092e2a 81 /* Main Loop. */
Davidroid 0:e6a49a092e2a 82 while(true)
Davidroid 0:e6a49a092e2a 83 {
Davidroid 3:fffa53c7aed2 84 /*----- Moving. -----*/
Davidroid 0:e6a49a092e2a 85
Davidroid 0:e6a49a092e2a 86 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 87 printf("--> Moving forward %d steps.\r\n", ROUND_ANGLE_STEPS);
Davidroid 0:e6a49a092e2a 88
Davidroid 0:e6a49a092e2a 89 /* Moving N steps in the forward direction. */
Davidroid 3:fffa53c7aed2 90 motor->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS);
Davidroid 3:fffa53c7aed2 91
Davidroid 0:e6a49a092e2a 92 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 93 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 94
Davidroid 0:e6a49a092e2a 95 /* Getting current position. */
Davidroid 1:fbf28f3367aa 96 int position = motor->GetPosition();
Davidroid 0:e6a49a092e2a 97
Davidroid 0:e6a49a092e2a 98 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 99 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 100
Davidroid 0:e6a49a092e2a 101 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 102 wait_ms(2000);
Davidroid 0:e6a49a092e2a 103
Davidroid 0:e6a49a092e2a 104
Davidroid 3:fffa53c7aed2 105 /*----- Moving. -----*/
Davidroid 0:e6a49a092e2a 106
Davidroid 0:e6a49a092e2a 107 /* Printing to the console. */
Davidroid 3:fffa53c7aed2 108 printf("--> Moving backward %d steps.\r\n", ROUND_ANGLE_STEPS >> 1);
Davidroid 3:fffa53c7aed2 109
Davidroid 0:e6a49a092e2a 110 /* Moving N steps in the backward direction. */
Davidroid 3:fffa53c7aed2 111 motor->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 112
Davidroid 0:e6a49a092e2a 113 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 114 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 115
Davidroid 0:e6a49a092e2a 116 /* Getting current position. */
Davidroid 1:fbf28f3367aa 117 position = motor->GetPosition();
Davidroid 0:e6a49a092e2a 118
Davidroid 0:e6a49a092e2a 119 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 120 printf(" Position: %d.\r\n", position);
Davidroid 3:fffa53c7aed2 121 printf("--> Setting Home.\r\n");
Davidroid 0:e6a49a092e2a 122
Davidroid 0:e6a49a092e2a 123 /* Setting the current position to be the home position. */
Davidroid 1:fbf28f3367aa 124 motor->SetHome();
Davidroid 0:e6a49a092e2a 125
Davidroid 0:e6a49a092e2a 126 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 127 wait_ms(2000);
Davidroid 0:e6a49a092e2a 128
Davidroid 0:e6a49a092e2a 129
Davidroid 0:e6a49a092e2a 130 /*----- Going to a specified position. -----*/
Davidroid 0:e6a49a092e2a 131
Davidroid 0:e6a49a092e2a 132 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 133 printf("--> Going to position %d.\r\n", ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 134
Davidroid 0:e6a49a092e2a 135 /* Requesting to go to a specified position. */
Davidroid 1:fbf28f3367aa 136 motor->GoTo(ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 137
Davidroid 0:e6a49a092e2a 138 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 139 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 140
Davidroid 0:e6a49a092e2a 141 /* Getting current position. */
Davidroid 1:fbf28f3367aa 142 position = motor->GetPosition();
Davidroid 0:e6a49a092e2a 143
Davidroid 0:e6a49a092e2a 144 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 145 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 146
Davidroid 0:e6a49a092e2a 147 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 148 wait_ms(2000);
Davidroid 0:e6a49a092e2a 149
Davidroid 0:e6a49a092e2a 150
Davidroid 0:e6a49a092e2a 151 /*----- Going Home. -----*/
Davidroid 0:e6a49a092e2a 152
Davidroid 0:e6a49a092e2a 153 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 154 printf("--> Going Home.\r\n");
Davidroid 0:e6a49a092e2a 155
Davidroid 0:e6a49a092e2a 156 /* Requesting to go to home. */
Davidroid 1:fbf28f3367aa 157 motor->GoHome();
Davidroid 0:e6a49a092e2a 158
Davidroid 0:e6a49a092e2a 159 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 160 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 161
Davidroid 0:e6a49a092e2a 162 /* Getting current position. */
Davidroid 1:fbf28f3367aa 163 position = motor->GetPosition();
Davidroid 0:e6a49a092e2a 164
Davidroid 0:e6a49a092e2a 165 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 166 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 167
Davidroid 0:e6a49a092e2a 168 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 169 wait_ms(2000);
Davidroid 0:e6a49a092e2a 170
Davidroid 0:e6a49a092e2a 171
Davidroid 3:fffa53c7aed2 172 /*----- Running. -----*/
Davidroid 0:e6a49a092e2a 173
Davidroid 0:e6a49a092e2a 174 /* Printing to the console. */
Davidroid 3:fffa53c7aed2 175 printf("--> Running backward.\r\n");
Davidroid 0:e6a49a092e2a 176
Davidroid 0:e6a49a092e2a 177 /* Requesting to run backward. */
Davidroid 3:fffa53c7aed2 178 motor->Run(StepperMotor::BWD);
Davidroid 0:e6a49a092e2a 179
Davidroid 0:e6a49a092e2a 180 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 181 wait_ms(6000);
Davidroid 0:e6a49a092e2a 182
Davidroid 0:e6a49a092e2a 183 /* Getting current speed. */
Davidroid 2:e12e4df7a486 184 int speed = motor->GetSpeed();
Davidroid 0:e6a49a092e2a 185
Davidroid 0:e6a49a092e2a 186 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 187 printf(" Speed: %d.\r\n", speed);
Davidroid 0:e6a49a092e2a 188
Davidroid 0:e6a49a092e2a 189 /*----- Increasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 190
Davidroid 0:e6a49a092e2a 191 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 192 printf("--> Increasing the speed while running.\r\n");
Davidroid 0:e6a49a092e2a 193
Davidroid 0:e6a49a092e2a 194 /* Increasing speed to 2400 step/s. */
Davidroid 1:fbf28f3367aa 195 motor->SetMaxSpeed(2400);
Davidroid 0:e6a49a092e2a 196
Davidroid 0:e6a49a092e2a 197 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 198 wait_ms(6000);
Davidroid 0:e6a49a092e2a 199
Davidroid 0:e6a49a092e2a 200 /* Getting current speed. */
Davidroid 2:e12e4df7a486 201 speed = motor->GetSpeed();
Davidroid 0:e6a49a092e2a 202
Davidroid 0:e6a49a092e2a 203 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 204 printf(" Speed: %d.\r\n", speed);
Davidroid 0:e6a49a092e2a 205
Davidroid 0:e6a49a092e2a 206
Davidroid 0:e6a49a092e2a 207 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 208
Davidroid 0:e6a49a092e2a 209 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 210 printf("--> Decreasing the speed while running.\r\n");
Davidroid 0:e6a49a092e2a 211
Davidroid 0:e6a49a092e2a 212 /* Decreasing speed to 1200 step/s. */
Davidroid 1:fbf28f3367aa 213 motor->SetMaxSpeed(1200);
Davidroid 0:e6a49a092e2a 214
Davidroid 0:e6a49a092e2a 215 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 216 wait_ms(8000);
Davidroid 0:e6a49a092e2a 217
Davidroid 0:e6a49a092e2a 218 /* Getting current speed. */
Davidroid 2:e12e4df7a486 219 speed = motor->GetSpeed();
Davidroid 0:e6a49a092e2a 220
Davidroid 0:e6a49a092e2a 221 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 222 printf(" Speed: %d.\r\n", speed);
Davidroid 0:e6a49a092e2a 223
Davidroid 0:e6a49a092e2a 224
Davidroid 0:e6a49a092e2a 225 /*----- Requiring hard-stop while running. -----*/
Davidroid 0:e6a49a092e2a 226
Davidroid 0:e6a49a092e2a 227 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 228 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 0:e6a49a092e2a 229
Davidroid 0:e6a49a092e2a 230 /* Requesting to immediatly stop. */
Davidroid 1:fbf28f3367aa 231 motor->HardStop();
Davidroid 0:e6a49a092e2a 232
Davidroid 0:e6a49a092e2a 233 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 234 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 235
Davidroid 0:e6a49a092e2a 236 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 237 wait_ms(2000);
Davidroid 0:e6a49a092e2a 238
Davidroid 0:e6a49a092e2a 239
Davidroid 0:e6a49a092e2a 240 /*----- Infinite Loop. -----*/
Davidroid 0:e6a49a092e2a 241
Davidroid 0:e6a49a092e2a 242 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 243 printf("--> Infinite Loop...\r\n");
Davidroid 0:e6a49a092e2a 244
Davidroid 0:e6a49a092e2a 245 /* Setting the current position to be the home position. */
Davidroid 1:fbf28f3367aa 246 motor->SetHome();
Davidroid 0:e6a49a092e2a 247
Davidroid 0:e6a49a092e2a 248 /* Infinite Loop. */
Davidroid 0:e6a49a092e2a 249 while(1)
Davidroid 0:e6a49a092e2a 250 {
Davidroid 0:e6a49a092e2a 251 /* Requesting to go to a specified position. */
Davidroid 3:fffa53c7aed2 252 motor->GoTo(ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 253
Davidroid 0:e6a49a092e2a 254 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 255 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 256
Davidroid 0:e6a49a092e2a 257 /* Requesting to go to a specified position. */
Davidroid 3:fffa53c7aed2 258 motor->GoTo(- (ROUND_ANGLE_STEPS >> 1));
Davidroid 0:e6a49a092e2a 259
Davidroid 0:e6a49a092e2a 260 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 261 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 262 }
Davidroid 0:e6a49a092e2a 263 }
Davidroid 0:e6a49a092e2a 264 }