APS2 MEA Rodrigo Vianna

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
Davidroid
Date:
Wed Nov 25 12:09:18 2015 +0000
Revision:
6:32166bfc04b0
Parent:
5:8065b587ade0
Child:
7:1d8a73b24eba
+ Updated with the new X_NUCLEO_IHM01A1 library.

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