200117StepMotorControl

Dependencies:   X_NUCLEO_IHM02A1 TextLCD

Committer:
Davidroid
Date:
Mon Jul 03 11:53:02 2017 +0000
Revision:
27:b25816ce6043
Parent:
26:caec5f51abe8
Child:
28:ed18e436f437
mbed OS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:5148e9486cf2 1 /**
Davidroid 0:5148e9486cf2 2 ******************************************************************************
Davidroid 0:5148e9486cf2 3 * @file main.cpp
Davidroid 12:5be6dd48b94a 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:5148e9486cf2 5 * @version V1.0.0
Davidroid 0:5148e9486cf2 6 * @date November 4th, 2015
Davidroid 12:5be6dd48b94a 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM02A1
Davidroid 1:9f1974b0960d 8 * Motor Control Expansion Board: control of 2 motors.
Davidroid 0:5148e9486cf2 9 ******************************************************************************
Davidroid 0:5148e9486cf2 10 * @attention
Davidroid 0:5148e9486cf2 11 *
Davidroid 0:5148e9486cf2 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:5148e9486cf2 13 *
Davidroid 0:5148e9486cf2 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:5148e9486cf2 15 * are permitted provided that the following conditions are met:
Davidroid 0:5148e9486cf2 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:5148e9486cf2 17 * this list of conditions and the following disclaimer.
Davidroid 0:5148e9486cf2 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:5148e9486cf2 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:5148e9486cf2 20 * and/or other materials provided with the distribution.
Davidroid 0:5148e9486cf2 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:5148e9486cf2 22 * may be used to endorse or promote products derived from this software
Davidroid 0:5148e9486cf2 23 * without specific prior written permission.
Davidroid 0:5148e9486cf2 24 *
Davidroid 0:5148e9486cf2 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:5148e9486cf2 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:5148e9486cf2 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:5148e9486cf2 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:5148e9486cf2 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:5148e9486cf2 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:5148e9486cf2 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:5148e9486cf2 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:5148e9486cf2 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:5148e9486cf2 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:5148e9486cf2 35 *
Davidroid 0:5148e9486cf2 36 ******************************************************************************
Davidroid 0:5148e9486cf2 37 */
Davidroid 0:5148e9486cf2 38
Davidroid 0:5148e9486cf2 39
Davidroid 0:5148e9486cf2 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 41
Davidroid 0:5148e9486cf2 42 /* mbed specific header files. */
Davidroid 0:5148e9486cf2 43 #include "mbed.h"
Davidroid 27:b25816ce6043 44 #include "rtos.h"
Davidroid 0:5148e9486cf2 45
Davidroid 0:5148e9486cf2 46 /* Helper header files. */
Davidroid 0:5148e9486cf2 47 #include "DevSPI.h"
Davidroid 0:5148e9486cf2 48
Davidroid 0:5148e9486cf2 49 /* Expansion Board specific header files. */
Davidroid 26:caec5f51abe8 50 #include "XNucleoIHM02A1.h"
Davidroid 0:5148e9486cf2 51
Davidroid 0:5148e9486cf2 52
Davidroid 0:5148e9486cf2 53 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 54
Davidroid 0:5148e9486cf2 55 /* Number of movements per revolution. */
Davidroid 0:5148e9486cf2 56 #define MPR_1 4
Davidroid 1:9f1974b0960d 57
Davidroid 1:9f1974b0960d 58 /* Number of steps. */
Davidroid 18:591a007effc2 59 #define STEPS_1 (400 * 128) /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
Davidroid 18:591a007effc2 60 #define STEPS_2 (STEPS_1 * 2)
Davidroid 0:5148e9486cf2 61
Davidroid 0:5148e9486cf2 62 /* Delay in milliseconds. */
Davidroid 1:9f1974b0960d 63 #define DELAY_1 1000
Davidroid 0:5148e9486cf2 64 #define DELAY_2 2000
Davidroid 0:5148e9486cf2 65 #define DELAY_3 5000
Davidroid 0:5148e9486cf2 66
Davidroid 0:5148e9486cf2 67
Davidroid 0:5148e9486cf2 68 /* Variables -----------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 69
Davidroid 0:5148e9486cf2 70 /* Motor Control Expansion Board. */
Davidroid 26:caec5f51abe8 71 XNucleoIHM02A1 *x_nucleo_ihm02a1;
Davidroid 0:5148e9486cf2 72
Davidroid 9:f35fbeedb8f4 73 /* Initialization parameters of the motors connected to the expansion board. */
davide.aliprandi@st.com 24:d1f487cb02ba 74 L6470_init_t init[L6470DAISYCHAINSIZE] = {
Davidroid 9:f35fbeedb8f4 75 /* First Motor. */
Davidroid 9:f35fbeedb8f4 76 {
Davidroid 18:591a007effc2 77 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 78 400, /* Min number of steps per revolution for the motor. */
Davidroid 18:591a007effc2 79 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 80 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 81 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 82 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 83 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 84 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 85 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 86 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 87 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 88 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 89 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 90 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 91 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 92 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 93 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 94 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 95 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 96 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 97 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 98 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 99 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 100 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 101 },
Davidroid 9:f35fbeedb8f4 102
Davidroid 9:f35fbeedb8f4 103 /* Second Motor. */
Davidroid 9:f35fbeedb8f4 104 {
Davidroid 18:591a007effc2 105 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 106 400, /* Min number of steps per revolution for the motor. */
Davidroid 18:591a007effc2 107 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 108 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 109 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 110 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 111 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 112 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 113 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 114 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 115 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 116 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 117 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 118 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 119 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 120 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 121 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 122 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 123 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 124 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 125 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 126 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 127 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 128 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 129 }
Davidroid 9:f35fbeedb8f4 130 };
Davidroid 9:f35fbeedb8f4 131
Davidroid 0:5148e9486cf2 132
Davidroid 0:5148e9486cf2 133 /* Main ----------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 134
Davidroid 0:5148e9486cf2 135 int main()
Davidroid 0:5148e9486cf2 136 {
Davidroid 1:9f1974b0960d 137 /*----- Initialization. -----*/
Davidroid 1:9f1974b0960d 138
Davidroid 2:41eeee48951b 139 /* Initializing SPI bus. */
Davidroid 23:073b26366d03 140 #ifdef TARGET_STM32F429
Davidroid 23:073b26366d03 141 DevSPI dev_spi(D11, D12, D13);
Davidroid 23:073b26366d03 142 #else
Davidroid 3:fd280b953f77 143 DevSPI dev_spi(D11, D12, D3);
Davidroid 23:073b26366d03 144 #endif
Davidroid 0:5148e9486cf2 145
Davidroid 5:3b8e19bbf386 146 /* Initializing Motor Control Expansion Board. */
Davidroid 26:caec5f51abe8 147 x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);
Davidroid 0:5148e9486cf2 148
Davidroid 1:9f1974b0960d 149 /* Building a list of motor control components. */
davide.aliprandi@st.com 24:d1f487cb02ba 150 L6470 **motors = x_nucleo_ihm02a1->get_components();
Davidroid 0:5148e9486cf2 151
Davidroid 0:5148e9486cf2 152 /* Printing to the console. */
Davidroid 0:5148e9486cf2 153 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:5148e9486cf2 154
Davidroid 1:9f1974b0960d 155
Davidroid 1:9f1974b0960d 156 /*----- Setting home and marke positions, getting positions, and going to positions. -----*/
Davidroid 1:9f1974b0960d 157
Davidroid 1:9f1974b0960d 158 /* Printing to the console. */
Davidroid 1:9f1974b0960d 159 printf("--> Setting home position.\r\n");
Davidroid 1:9f1974b0960d 160
Davidroid 1:9f1974b0960d 161 /* Setting the home position. */
davide.aliprandi@st.com 24:d1f487cb02ba 162 motors[0]->set_home();
Davidroid 1:9f1974b0960d 163
Davidroid 1:9f1974b0960d 164 /* Waiting. */
Davidroid 1:9f1974b0960d 165 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 166
Davidroid 1:9f1974b0960d 167 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 168 int position = motors[0]->get_position();
Davidroid 18:591a007effc2 169
Davidroid 1:9f1974b0960d 170 /* Printing to the console. */
Davidroid 1:9f1974b0960d 171 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 172
Davidroid 1:9f1974b0960d 173 /* Waiting. */
Davidroid 1:9f1974b0960d 174 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 175
Davidroid 1:9f1974b0960d 176 /* Printing to the console. */
Davidroid 1:9f1974b0960d 177 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 1:9f1974b0960d 178
Davidroid 1:9f1974b0960d 179 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 180 motors[0]->move(StepperMotor::FWD, STEPS_1);
Davidroid 0:5148e9486cf2 181
Davidroid 1:9f1974b0960d 182 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 183 motors[0]->wait_while_active();
Davidroid 1:9f1974b0960d 184
Davidroid 1:9f1974b0960d 185 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 186 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 187
Davidroid 1:9f1974b0960d 188 /* Printing to the console. */
Davidroid 1:9f1974b0960d 189 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 190
Davidroid 1:9f1974b0960d 191 /* Printing to the console. */
Davidroid 1:9f1974b0960d 192 printf("--> Marking the current position.\r\n");
Davidroid 0:5148e9486cf2 193
Davidroid 1:9f1974b0960d 194 /* Marking the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 195 motors[0]->set_mark();
Davidroid 1:9f1974b0960d 196
Davidroid 1:9f1974b0960d 197 /* Waiting. */
Davidroid 1:9f1974b0960d 198 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 199
Davidroid 1:9f1974b0960d 200 /* Printing to the console. */
Davidroid 1:9f1974b0960d 201 printf("--> Moving backward %d steps.\r\n", STEPS_2);
Davidroid 1:9f1974b0960d 202
Davidroid 1:9f1974b0960d 203 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 204 motors[0]->move(StepperMotor::BWD, STEPS_2);
Davidroid 1:9f1974b0960d 205
Davidroid 1:9f1974b0960d 206 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 207 motors[0]->wait_while_active();
Davidroid 0:5148e9486cf2 208
Davidroid 1:9f1974b0960d 209 /* Waiting. */
Davidroid 1:9f1974b0960d 210 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 211
Davidroid 1:9f1974b0960d 212 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 213 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 214
Davidroid 1:9f1974b0960d 215 /* Printing to the console. */
Davidroid 1:9f1974b0960d 216 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 217
Davidroid 1:9f1974b0960d 218 /* Waiting. */
Davidroid 1:9f1974b0960d 219 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 220
Davidroid 1:9f1974b0960d 221 /* Printing to the console. */
Davidroid 1:9f1974b0960d 222 printf("--> Going to marked position.\r\n");
Davidroid 0:5148e9486cf2 223
Davidroid 1:9f1974b0960d 224 /* Going to marked position. */
davide.aliprandi@st.com 24:d1f487cb02ba 225 motors[0]->go_mark();
Davidroid 1:9f1974b0960d 226
Davidroid 1:9f1974b0960d 227 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 228 motors[0]->wait_while_active();
Davidroid 0:5148e9486cf2 229
Davidroid 1:9f1974b0960d 230 /* Waiting. */
Davidroid 1:9f1974b0960d 231 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 232
Davidroid 1:9f1974b0960d 233 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 234 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 235
Davidroid 1:9f1974b0960d 236 /* Printing to the console. */
Davidroid 1:9f1974b0960d 237 printf("--> Getting the current position: %d\r\n", position);
Davidroid 0:5148e9486cf2 238
Davidroid 1:9f1974b0960d 239 /* Waiting. */
Davidroid 1:9f1974b0960d 240 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 241
Davidroid 1:9f1974b0960d 242 /* Printing to the console. */
Davidroid 1:9f1974b0960d 243 printf("--> Going to home position.\r\n");
Davidroid 0:5148e9486cf2 244
Davidroid 1:9f1974b0960d 245 /* Going to home position. */
davide.aliprandi@st.com 24:d1f487cb02ba 246 motors[0]->go_home();
Davidroid 1:9f1974b0960d 247
Davidroid 1:9f1974b0960d 248 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 249 motors[0]->wait_while_active();
Davidroid 1:9f1974b0960d 250
Davidroid 1:9f1974b0960d 251 /* Waiting. */
Davidroid 1:9f1974b0960d 252 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 253
Davidroid 1:9f1974b0960d 254 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 255 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 256
Davidroid 1:9f1974b0960d 257 /* Printing to the console. */
Davidroid 1:9f1974b0960d 258 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 259
Davidroid 1:9f1974b0960d 260 /* Waiting. */
Davidroid 18:591a007effc2 261 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 262
Davidroid 18:591a007effc2 263 /* Printing to the console. */
Davidroid 18:591a007effc2 264 printf("--> Halving the microsteps.\r\n");
Davidroid 18:591a007effc2 265
Davidroid 18:591a007effc2 266 /* Halving the microsteps. */
Davidroid 18:591a007effc2 267 init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel - 1 : init[0].step_sel);
davide.aliprandi@st.com 24:d1f487cb02ba 268 if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel)) {
Davidroid 18:591a007effc2 269 printf(" Step Mode not allowed.\r\n");
Davidroid 22:e81ccf73bc5d 270 }
Davidroid 18:591a007effc2 271
Davidroid 18:591a007effc2 272 /* Waiting. */
Davidroid 18:591a007effc2 273 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 274
Davidroid 18:591a007effc2 275 /* Printing to the console. */
Davidroid 18:591a007effc2 276 printf("--> Setting home position.\r\n");
Davidroid 18:591a007effc2 277
Davidroid 18:591a007effc2 278 /* Setting the home position. */
davide.aliprandi@st.com 24:d1f487cb02ba 279 motors[0]->set_home();
Davidroid 18:591a007effc2 280
Davidroid 18:591a007effc2 281 /* Waiting. */
Davidroid 18:591a007effc2 282 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 283
Davidroid 18:591a007effc2 284 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 285 position = motors[0]->get_position();
Davidroid 18:591a007effc2 286
Davidroid 18:591a007effc2 287 /* Printing to the console. */
Davidroid 18:591a007effc2 288 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 289
Davidroid 18:591a007effc2 290 /* Waiting. */
Davidroid 18:591a007effc2 291 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 292
Davidroid 18:591a007effc2 293 /* Printing to the console. */
Davidroid 18:591a007effc2 294 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 18:591a007effc2 295
Davidroid 18:591a007effc2 296 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 297 motors[0]->move(StepperMotor::FWD, STEPS_1);
Davidroid 18:591a007effc2 298
Davidroid 18:591a007effc2 299 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 300 motors[0]->wait_while_active();
Davidroid 18:591a007effc2 301
Davidroid 18:591a007effc2 302 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 303 position = motors[0]->get_position();
Davidroid 18:591a007effc2 304
Davidroid 18:591a007effc2 305 /* Printing to the console. */
Davidroid 18:591a007effc2 306 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 307
Davidroid 18:591a007effc2 308 /* Printing to the console. */
Davidroid 18:591a007effc2 309 printf("--> Marking the current position.\r\n");
Davidroid 18:591a007effc2 310
Davidroid 18:591a007effc2 311 /* Marking the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 312 motors[0]->set_mark();
Davidroid 18:591a007effc2 313
Davidroid 18:591a007effc2 314 /* Waiting. */
Davidroid 1:9f1974b0960d 315 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 316
Davidroid 0:5148e9486cf2 317
Davidroid 1:9f1974b0960d 318 /*----- Running together for a certain amount of time. -----*/
Davidroid 1:9f1974b0960d 319
Davidroid 1:9f1974b0960d 320 /* Printing to the console. */
Davidroid 1:9f1974b0960d 321 printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 322
Davidroid 1:9f1974b0960d 323 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 324 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 325 motors[m]->prepare_run(StepperMotor::BWD, 400);
Davidroid 22:e81ccf73bc5d 326 }
Davidroid 0:5148e9486cf2 327
Davidroid 1:9f1974b0960d 328 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 329 x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 330
Davidroid 1:9f1974b0960d 331 /* Waiting. */
Davidroid 1:9f1974b0960d 332 wait_ms(DELAY_3);
Davidroid 1:9f1974b0960d 333
Davidroid 1:9f1974b0960d 334
Davidroid 1:9f1974b0960d 335 /*----- Increasing the speed while running. -----*/
Davidroid 0:5148e9486cf2 336
Davidroid 1:9f1974b0960d 337 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 338 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 339 motors[m]->prepare_get_speed();
Davidroid 22:e81ccf73bc5d 340 }
Davidroid 0:5148e9486cf2 341
Davidroid 1:9f1974b0960d 342 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 343 uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 344
Davidroid 1:9f1974b0960d 345 /* Printing to the console. */
Davidroid 1:9f1974b0960d 346 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 347
Davidroid 1:9f1974b0960d 348 /* Printing to the console. */
Davidroid 18:591a007effc2 349 printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 350
Davidroid 1:9f1974b0960d 351 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 352 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 353 motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1);
Davidroid 22:e81ccf73bc5d 354 }
Davidroid 1:9f1974b0960d 355
Davidroid 1:9f1974b0960d 356 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 357 results = x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 358
Davidroid 1:9f1974b0960d 359 /* Waiting. */
Davidroid 1:9f1974b0960d 360 wait_ms(DELAY_3);
Davidroid 0:5148e9486cf2 361
Davidroid 1:9f1974b0960d 362 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 363 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 364 motors[m]->prepare_get_speed();
Davidroid 22:e81ccf73bc5d 365 }
Davidroid 0:5148e9486cf2 366
Davidroid 1:9f1974b0960d 367 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 368 results = x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 369
Davidroid 1:9f1974b0960d 370 /* Printing to the console. */
Davidroid 1:9f1974b0960d 371 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 372
Davidroid 1:9f1974b0960d 373 /* Waiting. */
Davidroid 1:9f1974b0960d 374 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 375
Davidroid 0:5148e9486cf2 376
Davidroid 1:9f1974b0960d 377 /*----- Hard Stop. -----*/
Davidroid 0:5148e9486cf2 378
Davidroid 1:9f1974b0960d 379 /* Printing to the console. */
Davidroid 1:9f1974b0960d 380 printf("--> Hard Stop.\r\n");
Davidroid 0:5148e9486cf2 381
Davidroid 1:9f1974b0960d 382 /* Preparing each motor to perform a hard stop. */
Davidroid 22:e81ccf73bc5d 383 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 384 motors[m]->prepare_hard_stop();
Davidroid 22:e81ccf73bc5d 385 }
Davidroid 0:5148e9486cf2 386
Davidroid 1:9f1974b0960d 387 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 388 x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 389
Davidroid 1:9f1974b0960d 390 /* Waiting. */
Davidroid 1:9f1974b0960d 391 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 392
Davidroid 0:5148e9486cf2 393
Davidroid 1:9f1974b0960d 394 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 1:9f1974b0960d 395
Davidroid 1:9f1974b0960d 396 /* Printing to the console. */
Davidroid 1:9f1974b0960d 397 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 1:9f1974b0960d 398
Davidroid 1:9f1974b0960d 399 /* Doing a full revolution on each motor, one after the other. */
Davidroid 22:e81ccf73bc5d 400 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 22:e81ccf73bc5d 401 for (int i = 0; i < MPR_1; i++) {
Davidroid 1:9f1974b0960d 402 /* Computing the number of steps. */
Davidroid 9:f35fbeedb8f4 403 int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1);
Davidroid 1:9f1974b0960d 404
Davidroid 1:9f1974b0960d 405 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 406 motors[m]->move(StepperMotor::FWD, steps);
Davidroid 1:9f1974b0960d 407
Davidroid 1:9f1974b0960d 408 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 409 motors[m]->wait_while_active();
Davidroid 1:9f1974b0960d 410
Davidroid 1:9f1974b0960d 411 /* Waiting. */
Davidroid 1:9f1974b0960d 412 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 413 }
Davidroid 22:e81ccf73bc5d 414 }
Davidroid 1:9f1974b0960d 415
Davidroid 1:9f1974b0960d 416 /* Waiting. */
Davidroid 1:9f1974b0960d 417 wait_ms(DELAY_2);
Davidroid 1:9f1974b0960d 418
Davidroid 1:9f1974b0960d 419
Davidroid 1:9f1974b0960d 420 /*----- High Impedance State. -----*/
Davidroid 1:9f1974b0960d 421
Davidroid 1:9f1974b0960d 422 /* Printing to the console. */
Davidroid 1:9f1974b0960d 423 printf("--> High Impedance State.\r\n");
Davidroid 1:9f1974b0960d 424
Davidroid 1:9f1974b0960d 425 /* Preparing each motor to set High Impedance State. */
Davidroid 22:e81ccf73bc5d 426 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 427 motors[m]->prepare_hard_hiz();
Davidroid 22:e81ccf73bc5d 428 }
Davidroid 1:9f1974b0960d 429
Davidroid 1:9f1974b0960d 430 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 431 x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 1:9f1974b0960d 432
Davidroid 1:9f1974b0960d 433 /* Waiting. */
Davidroid 1:9f1974b0960d 434 wait_ms(DELAY_2);
Davidroid 22:e81ccf73bc5d 435 }