APS2 MEA Rodrigo Vianna

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
Davidroid
Date:
Fri Oct 16 13:51:31 2015 +0000
Revision:
1:fbf28f3367aa
Parent:
0:e6a49a092e2a
Child:
2:e12e4df7a486
Updated to fit 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 * 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 0:e6a49a092e2a 57 #define ROUND_ANGLE_STEPS 1600
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 0:e6a49a092e2a 84 /*----- Moving forward of N steps. -----*/
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 1:fbf28f3367aa 90 motor->Move(FORWARD, ROUND_ANGLE_STEPS);
Davidroid 0:e6a49a092e2a 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 0:e6a49a092e2a 105 /*----- Moving backward N steps. -----*/
Davidroid 0:e6a49a092e2a 106
Davidroid 0:e6a49a092e2a 107 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 108 printf("--> Moving backward %d steps.\r\n", ROUND_ANGLE_STEPS);
Davidroid 0:e6a49a092e2a 109
Davidroid 0:e6a49a092e2a 110 /* Moving N steps in the backward direction. */
Davidroid 1:fbf28f3367aa 111 motor->Move(BACKWARD, ROUND_ANGLE_STEPS);
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 0:e6a49a092e2a 121
Davidroid 0:e6a49a092e2a 122 /* Setting the current position to be the home position. */
Davidroid 1:fbf28f3367aa 123 motor->SetHome();
Davidroid 0:e6a49a092e2a 124
Davidroid 0:e6a49a092e2a 125 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 126 wait_ms(2000);
Davidroid 0:e6a49a092e2a 127
Davidroid 0:e6a49a092e2a 128
Davidroid 0:e6a49a092e2a 129 /*----- Going to a specified position. -----*/
Davidroid 0:e6a49a092e2a 130
Davidroid 0:e6a49a092e2a 131 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 132 printf("--> Going to position %d.\r\n", ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 133
Davidroid 0:e6a49a092e2a 134 /* Requesting to go to a specified position. */
Davidroid 1:fbf28f3367aa 135 motor->GoTo(ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 136
Davidroid 0:e6a49a092e2a 137 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 138 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 139
Davidroid 0:e6a49a092e2a 140 /* Getting current position. */
Davidroid 1:fbf28f3367aa 141 position = motor->GetPosition();
Davidroid 0:e6a49a092e2a 142
Davidroid 0:e6a49a092e2a 143 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 144 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 145
Davidroid 0:e6a49a092e2a 146 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 147 wait_ms(2000);
Davidroid 0:e6a49a092e2a 148
Davidroid 0:e6a49a092e2a 149
Davidroid 0:e6a49a092e2a 150 /*----- Going Home. -----*/
Davidroid 0:e6a49a092e2a 151
Davidroid 0:e6a49a092e2a 152 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 153 printf("--> Going Home.\r\n");
Davidroid 0:e6a49a092e2a 154
Davidroid 0:e6a49a092e2a 155 /* Requesting to go to home. */
Davidroid 1:fbf28f3367aa 156 motor->GoHome();
Davidroid 0:e6a49a092e2a 157
Davidroid 0:e6a49a092e2a 158 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 159 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 160
Davidroid 0:e6a49a092e2a 161 /* Getting current position. */
Davidroid 1:fbf28f3367aa 162 position = motor->GetPosition();
Davidroid 0:e6a49a092e2a 163
Davidroid 0:e6a49a092e2a 164 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 165 printf(" Position: %d.\r\n", position);
Davidroid 0:e6a49a092e2a 166
Davidroid 0:e6a49a092e2a 167 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 168 wait_ms(2000);
Davidroid 0:e6a49a092e2a 169
Davidroid 0:e6a49a092e2a 170
Davidroid 0:e6a49a092e2a 171 /*----- Moving backward. -----*/
Davidroid 0:e6a49a092e2a 172
Davidroid 0:e6a49a092e2a 173 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 174 printf("--> Moving backward.\r\n");
Davidroid 0:e6a49a092e2a 175
Davidroid 0:e6a49a092e2a 176 /* Requesting to run backward. */
Davidroid 1:fbf28f3367aa 177 motor->Run(BACKWARD);
Davidroid 0:e6a49a092e2a 178
Davidroid 0:e6a49a092e2a 179 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 180 wait_ms(6000);
Davidroid 0:e6a49a092e2a 181
Davidroid 0:e6a49a092e2a 182 /* Getting current speed. */
Davidroid 1:fbf28f3367aa 183 int speed = motor->GetCurrentSpeed();
Davidroid 0:e6a49a092e2a 184
Davidroid 0:e6a49a092e2a 185 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 186 printf(" Speed: %d.\r\n", speed);
Davidroid 0:e6a49a092e2a 187
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 1:fbf28f3367aa 201 speed = motor->GetCurrentSpeed();
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 1:fbf28f3367aa 219 speed = motor->GetCurrentSpeed();
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 /*----- Moving forward. -----*/
Davidroid 0:e6a49a092e2a 226
Davidroid 0:e6a49a092e2a 227 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 228 printf("--> Moving forward.\r\n");
Davidroid 0:e6a49a092e2a 229
Davidroid 0:e6a49a092e2a 230 /* Requesting to run in forward direction. */
Davidroid 1:fbf28f3367aa 231 motor->Run(FORWARD);
Davidroid 0:e6a49a092e2a 232
Davidroid 0:e6a49a092e2a 233 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 234 wait_ms(4000);
Davidroid 0:e6a49a092e2a 235
Davidroid 0:e6a49a092e2a 236
Davidroid 0:e6a49a092e2a 237 /*----- Requiring hard-stop while running. -----*/
Davidroid 0:e6a49a092e2a 238
Davidroid 0:e6a49a092e2a 239 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 240 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 0:e6a49a092e2a 241
Davidroid 0:e6a49a092e2a 242 /* Requesting to immediatly stop. */
Davidroid 1:fbf28f3367aa 243 motor->HardStop();
Davidroid 0:e6a49a092e2a 244
Davidroid 0:e6a49a092e2a 245 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 246 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 247
Davidroid 0:e6a49a092e2a 248 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 249 wait_ms(2000);
Davidroid 0:e6a49a092e2a 250
Davidroid 0:e6a49a092e2a 251
Davidroid 0:e6a49a092e2a 252 /*----- Infinite Loop. -----*/
Davidroid 0:e6a49a092e2a 253
Davidroid 0:e6a49a092e2a 254 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 255 printf("--> Infinite Loop...\r\n");
Davidroid 0:e6a49a092e2a 256
Davidroid 0:e6a49a092e2a 257 /* Setting the current position to be the home position. */
Davidroid 1:fbf28f3367aa 258 motor->SetHome();
Davidroid 0:e6a49a092e2a 259
Davidroid 0:e6a49a092e2a 260 /* Infinite Loop. */
Davidroid 0:e6a49a092e2a 261 while(1)
Davidroid 0:e6a49a092e2a 262 {
Davidroid 0:e6a49a092e2a 263 /* Requesting to go to a specified position. */
Davidroid 1:fbf28f3367aa 264 motor->GoTo(- ROUND_ANGLE_STEPS >> 2);
Davidroid 0:e6a49a092e2a 265
Davidroid 0:e6a49a092e2a 266 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 267 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 268
Davidroid 0:e6a49a092e2a 269 /* Requesting to go to a specified position. */
Davidroid 1:fbf28f3367aa 270 motor->GoTo(ROUND_ANGLE_STEPS >> 2);
Davidroid 0:e6a49a092e2a 271
Davidroid 0:e6a49a092e2a 272 /* Waiting while the motor is active. */
Davidroid 1:fbf28f3367aa 273 motor->WaitWhileActive();
Davidroid 0:e6a49a092e2a 274 }
Davidroid 0:e6a49a092e2a 275 }
Davidroid 0:e6a49a092e2a 276 }