Theis Rotating Platform Demo code

Dependencies:   X_NUCLEO_IHM01A1_Demo_Code mbed

Fork of Demo_IHM01A1_3-Motors by Arkadi Rafalovich

Committer:
Davidroid
Date:
Fri Nov 20 18:03:43 2015 +0000
Revision:
5:a0268a435bb1
Parent:
3:02d9ec4f88b2
Child:
6:10bf302cd369
+ Application updated to reflect addition of FLAG interrupt to the 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 3:02d9ec4f88b2 8 * Motor Control Expansion Board: control of 2 motors.
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:02d9ec4f88b2 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 3:02d9ec4f88b2 63 L6474 *motor1;
Davidroid 3:02d9ec4f88b2 64 L6474 *motor2;
Davidroid 0:e6a49a092e2a 65
Davidroid 0:e6a49a092e2a 66
Davidroid 0:e6a49a092e2a 67 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 68
Davidroid 0:e6a49a092e2a 69 int main()
Davidroid 0:e6a49a092e2a 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:a0268a435bb1 75 motor1 = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 3:02d9ec4f88b2 76 if (motor1->Init(NULL) != COMPONENT_OK)
Davidroid 3:02d9ec4f88b2 77 return false;
Davidroid 5:a0268a435bb1 78 motor2 = new L6474(D2, D8, D4, D3, D10, dev_spi);
Davidroid 3:02d9ec4f88b2 79 if (motor2->Init(NULL) != COMPONENT_OK)
Davidroid 0:e6a49a092e2a 80 return false;
Davidroid 0:e6a49a092e2a 81
Davidroid 0:e6a49a092e2a 82 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 83 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:e6a49a092e2a 84
Davidroid 0:e6a49a092e2a 85 /* Main Loop. */
Davidroid 0:e6a49a092e2a 86 while(true)
Davidroid 0:e6a49a092e2a 87 {
Davidroid 3:02d9ec4f88b2 88 /*----- Moving. -----*/
Davidroid 0:e6a49a092e2a 89
Davidroid 0:e6a49a092e2a 90 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 91 printf("--> Moving forward: M1 %d steps, M2 %d steps.\r\n", ROUND_ANGLE_STEPS >> 1, ROUND_ANGLE_STEPS);
Davidroid 0:e6a49a092e2a 92
Davidroid 0:e6a49a092e2a 93 /* Moving N steps in the forward direction. */
Davidroid 3:02d9ec4f88b2 94 motor1->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS >> 1);
Davidroid 3:02d9ec4f88b2 95 motor2->Move(StepperMotor::FWD, ROUND_ANGLE_STEPS);
Davidroid 3:02d9ec4f88b2 96
Davidroid 0:e6a49a092e2a 97 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 98 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 99 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 100
Davidroid 0:e6a49a092e2a 101 /* Getting current position. */
Davidroid 3:02d9ec4f88b2 102 int position1 = motor1->GetPosition();
Davidroid 3:02d9ec4f88b2 103 int position2 = motor2->GetPosition();
Davidroid 0:e6a49a092e2a 104
Davidroid 0:e6a49a092e2a 105 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 106 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 0:e6a49a092e2a 107
Davidroid 0:e6a49a092e2a 108 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 109 wait_ms(2000);
Davidroid 0:e6a49a092e2a 110
Davidroid 0:e6a49a092e2a 111
Davidroid 3:02d9ec4f88b2 112 /*----- Moving. -----*/
Davidroid 0:e6a49a092e2a 113
Davidroid 0:e6a49a092e2a 114 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 115 printf("--> Moving backward: M1 %d steps, M2 %d steps.\r\n", ROUND_ANGLE_STEPS >> 1, ROUND_ANGLE_STEPS);
Davidroid 3:02d9ec4f88b2 116
Davidroid 0:e6a49a092e2a 117
Davidroid 0:e6a49a092e2a 118 /* Moving N steps in the backward direction. */
Davidroid 3:02d9ec4f88b2 119 motor1->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS >> 1);
Davidroid 3:02d9ec4f88b2 120 motor2->Move(StepperMotor::BWD, ROUND_ANGLE_STEPS);
Davidroid 0:e6a49a092e2a 121
Davidroid 0:e6a49a092e2a 122 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 123 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 124 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 125
Davidroid 0:e6a49a092e2a 126 /* Getting current position. */
Davidroid 3:02d9ec4f88b2 127 position1 = motor1->GetPosition();
Davidroid 3:02d9ec4f88b2 128 position2 = motor2->GetPosition();
Davidroid 0:e6a49a092e2a 129
Davidroid 0:e6a49a092e2a 130 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 131 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 3:02d9ec4f88b2 132 printf("--> Setting Home.\r\n");
Davidroid 0:e6a49a092e2a 133
Davidroid 0:e6a49a092e2a 134 /* Setting the current position to be the home position. */
Davidroid 3:02d9ec4f88b2 135 motor1->SetHome();
Davidroid 3:02d9ec4f88b2 136 motor2->SetHome();
Davidroid 0:e6a49a092e2a 137
Davidroid 0:e6a49a092e2a 138 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 139 wait_ms(2000);
Davidroid 0:e6a49a092e2a 140
Davidroid 0:e6a49a092e2a 141
Davidroid 0:e6a49a092e2a 142 /*----- Going to a specified position. -----*/
Davidroid 0:e6a49a092e2a 143
Davidroid 0:e6a49a092e2a 144 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 145 printf("--> Going to position: M1 %d, M2 %d.\r\n", ROUND_ANGLE_STEPS, ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 146
Davidroid 0:e6a49a092e2a 147 /* Requesting to go to a specified position. */
Davidroid 3:02d9ec4f88b2 148 motor1->GoTo(ROUND_ANGLE_STEPS);
Davidroid 3:02d9ec4f88b2 149 motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 150
Davidroid 0:e6a49a092e2a 151 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 152 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 153 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 154
Davidroid 0:e6a49a092e2a 155 /* Getting current position. */
Davidroid 3:02d9ec4f88b2 156 position1 = motor1->GetPosition();
Davidroid 3:02d9ec4f88b2 157 position2 = motor2->GetPosition();
Davidroid 0:e6a49a092e2a 158
Davidroid 0:e6a49a092e2a 159 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 160 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 0:e6a49a092e2a 161
Davidroid 0:e6a49a092e2a 162 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 163 wait_ms(2000);
Davidroid 0:e6a49a092e2a 164
Davidroid 0:e6a49a092e2a 165
Davidroid 0:e6a49a092e2a 166 /*----- Going Home. -----*/
Davidroid 0:e6a49a092e2a 167
Davidroid 0:e6a49a092e2a 168 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 169 printf("--> Going Home.\r\n");
Davidroid 0:e6a49a092e2a 170
Davidroid 0:e6a49a092e2a 171 /* Requesting to go to home. */
Davidroid 3:02d9ec4f88b2 172 motor1->GoHome();
Davidroid 3:02d9ec4f88b2 173 motor2->GoHome();
Davidroid 0:e6a49a092e2a 174
Davidroid 0:e6a49a092e2a 175 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 176 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 177 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 178
Davidroid 0:e6a49a092e2a 179 /* Getting current position. */
Davidroid 3:02d9ec4f88b2 180 position1 = motor1->GetPosition();
Davidroid 3:02d9ec4f88b2 181 position2 = motor2->GetPosition();
Davidroid 0:e6a49a092e2a 182
Davidroid 0:e6a49a092e2a 183 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 184 printf(" Position: M1 %d, M2 %d.\r\n", position1, position2);
Davidroid 0:e6a49a092e2a 185
Davidroid 0:e6a49a092e2a 186 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 187 wait_ms(2000);
Davidroid 0:e6a49a092e2a 188
Davidroid 0:e6a49a092e2a 189
Davidroid 3:02d9ec4f88b2 190 /*----- Running. -----*/
Davidroid 0:e6a49a092e2a 191
Davidroid 0:e6a49a092e2a 192 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 193 printf("--> M1 running backward, M2 running forward.\r\n");
Davidroid 0:e6a49a092e2a 194
Davidroid 0:e6a49a092e2a 195 /* Requesting to run backward. */
Davidroid 3:02d9ec4f88b2 196 motor1->Run(StepperMotor::BWD);
Davidroid 3:02d9ec4f88b2 197 motor2->Run(StepperMotor::FWD);
Davidroid 0:e6a49a092e2a 198
Davidroid 0:e6a49a092e2a 199 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 200 wait_ms(6000);
Davidroid 0:e6a49a092e2a 201
Davidroid 0:e6a49a092e2a 202 /* Getting current speed. */
Davidroid 3:02d9ec4f88b2 203 int speed1 = motor1->GetSpeed();
Davidroid 3:02d9ec4f88b2 204 int speed2 = motor2->GetSpeed();
Davidroid 0:e6a49a092e2a 205
Davidroid 0:e6a49a092e2a 206 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 207 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 0:e6a49a092e2a 208
Davidroid 0:e6a49a092e2a 209 /*----- Increasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 210
Davidroid 0:e6a49a092e2a 211 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 212 printf("--> Increasing the speed while running.\r\n");
Davidroid 0:e6a49a092e2a 213
Davidroid 0:e6a49a092e2a 214 /* Increasing speed to 2400 step/s. */
Davidroid 3:02d9ec4f88b2 215 motor1->SetMaxSpeed(2400);
Davidroid 3:02d9ec4f88b2 216 motor2->SetMaxSpeed(2400);
Davidroid 0:e6a49a092e2a 217
Davidroid 0:e6a49a092e2a 218 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 219 wait_ms(6000);
Davidroid 0:e6a49a092e2a 220
Davidroid 0:e6a49a092e2a 221 /* Getting current speed. */
Davidroid 3:02d9ec4f88b2 222 speed1 = motor1->GetSpeed();
Davidroid 3:02d9ec4f88b2 223 speed2 = motor2->GetSpeed();
Davidroid 0:e6a49a092e2a 224
Davidroid 0:e6a49a092e2a 225 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 226 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 0:e6a49a092e2a 227
Davidroid 0:e6a49a092e2a 228
Davidroid 0:e6a49a092e2a 229 /*----- Decreasing the speed while running. -----*/
Davidroid 0:e6a49a092e2a 230
Davidroid 0:e6a49a092e2a 231 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 232 printf("--> Decreasing the speed while running.\r\n");
Davidroid 0:e6a49a092e2a 233
Davidroid 0:e6a49a092e2a 234 /* Decreasing speed to 1200 step/s. */
Davidroid 3:02d9ec4f88b2 235 motor1->SetMaxSpeed(1200);
Davidroid 3:02d9ec4f88b2 236 motor2->SetMaxSpeed(1200);
Davidroid 0:e6a49a092e2a 237
Davidroid 0:e6a49a092e2a 238 /* Waiting until delay has expired. */
Davidroid 1:fbf28f3367aa 239 wait_ms(8000);
Davidroid 0:e6a49a092e2a 240
Davidroid 0:e6a49a092e2a 241 /* Getting current speed. */
Davidroid 3:02d9ec4f88b2 242 speed1 = motor1->GetSpeed();
Davidroid 3:02d9ec4f88b2 243 speed2 = motor2->GetSpeed();
Davidroid 0:e6a49a092e2a 244
Davidroid 0:e6a49a092e2a 245 /* Printing to the console. */
Davidroid 3:02d9ec4f88b2 246 printf(" Speed: M1 %d, M2 %d.\r\n", speed1, speed2);
Davidroid 0:e6a49a092e2a 247
Davidroid 0:e6a49a092e2a 248
Davidroid 0:e6a49a092e2a 249 /*----- Requiring hard-stop while running. -----*/
Davidroid 0:e6a49a092e2a 250
Davidroid 0:e6a49a092e2a 251 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 252 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 0:e6a49a092e2a 253
Davidroid 0:e6a49a092e2a 254 /* Requesting to immediatly stop. */
Davidroid 3:02d9ec4f88b2 255 motor1->HardStop();
Davidroid 3:02d9ec4f88b2 256 motor2->HardStop();
Davidroid 0:e6a49a092e2a 257
Davidroid 0:e6a49a092e2a 258 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 259 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 260 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 261
Davidroid 0:e6a49a092e2a 262 /* Waiting 2 seconds. */
Davidroid 0:e6a49a092e2a 263 wait_ms(2000);
Davidroid 0:e6a49a092e2a 264
Davidroid 0:e6a49a092e2a 265
Davidroid 0:e6a49a092e2a 266 /*----- Infinite Loop. -----*/
Davidroid 0:e6a49a092e2a 267
Davidroid 0:e6a49a092e2a 268 /* Printing to the console. */
Davidroid 0:e6a49a092e2a 269 printf("--> Infinite Loop...\r\n");
Davidroid 0:e6a49a092e2a 270
Davidroid 0:e6a49a092e2a 271 /* Setting the current position to be the home position. */
Davidroid 3:02d9ec4f88b2 272 motor1->SetHome();
Davidroid 3:02d9ec4f88b2 273 motor2->SetHome();
Davidroid 0:e6a49a092e2a 274
Davidroid 0:e6a49a092e2a 275 /* Infinite Loop. */
Davidroid 0:e6a49a092e2a 276 while(1)
Davidroid 0:e6a49a092e2a 277 {
Davidroid 0:e6a49a092e2a 278 /* Requesting to go to a specified position. */
Davidroid 3:02d9ec4f88b2 279 motor1->GoTo(ROUND_ANGLE_STEPS >> 1);
Davidroid 3:02d9ec4f88b2 280 motor2->GoTo(- (ROUND_ANGLE_STEPS >> 1));
Davidroid 0:e6a49a092e2a 281
Davidroid 0:e6a49a092e2a 282 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 283 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 284 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 285
Davidroid 0:e6a49a092e2a 286 /* Requesting to go to a specified position. */
Davidroid 3:02d9ec4f88b2 287 motor1->GoTo(- (ROUND_ANGLE_STEPS >> 1));
Davidroid 3:02d9ec4f88b2 288 motor2->GoTo(ROUND_ANGLE_STEPS >> 1);
Davidroid 0:e6a49a092e2a 289
Davidroid 0:e6a49a092e2a 290 /* Waiting while the motor is active. */
Davidroid 3:02d9ec4f88b2 291 motor1->WaitWhileActive();
Davidroid 3:02d9ec4f88b2 292 motor2->WaitWhileActive();
Davidroid 0:e6a49a092e2a 293 }
Davidroid 0:e6a49a092e2a 294 }
Davidroid 0:e6a49a092e2a 295 }