concorso

Dependencies:   HCSR04 NetworkSocketAPI Servo X_NUCLEO_53L0A1 X_NUCLEO_IDW01M1v2 X_NUCLEO_IHM02A1 mbed-rtos mbed

Fork of HelloWorld_IHM02A1 by ST

Committer:
davide.aliprandi@st.com
Date:
Fri Mar 10 11:20:58 2017 +0100
Revision:
24:d1f487cb02ba
Parent:
23:073b26366d03
Child:
26:caec5f51abe8
Aligning to ARM mbed coding style.

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 0:5148e9486cf2 44
Davidroid 0:5148e9486cf2 45 /* Helper header files. */
Davidroid 0:5148e9486cf2 46 #include "DevSPI.h"
Davidroid 0:5148e9486cf2 47
Davidroid 0:5148e9486cf2 48 /* Expansion Board specific header files. */
davide.aliprandi@st.com 24:d1f487cb02ba 49 #include "XNucleoIhm02a1.h"
Davidroid 0:5148e9486cf2 50
Davidroid 0:5148e9486cf2 51
Davidroid 0:5148e9486cf2 52 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 53
Davidroid 0:5148e9486cf2 54 /* Number of movements per revolution. */
Davidroid 0:5148e9486cf2 55 #define MPR_1 4
Davidroid 1:9f1974b0960d 56
Davidroid 1:9f1974b0960d 57 /* Number of steps. */
Davidroid 18:591a007effc2 58 #define STEPS_1 (400 * 128) /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
Davidroid 18:591a007effc2 59 #define STEPS_2 (STEPS_1 * 2)
Davidroid 0:5148e9486cf2 60
Davidroid 0:5148e9486cf2 61 /* Delay in milliseconds. */
Davidroid 1:9f1974b0960d 62 #define DELAY_1 1000
Davidroid 0:5148e9486cf2 63 #define DELAY_2 2000
Davidroid 0:5148e9486cf2 64 #define DELAY_3 5000
Davidroid 0:5148e9486cf2 65
Davidroid 0:5148e9486cf2 66
Davidroid 0:5148e9486cf2 67 /* Variables -----------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 68
Davidroid 0:5148e9486cf2 69 /* Motor Control Expansion Board. */
davide.aliprandi@st.com 24:d1f487cb02ba 70 XNucleoIhm02a1 *x_nucleo_ihm02a1;
Davidroid 0:5148e9486cf2 71
Davidroid 9:f35fbeedb8f4 72 /* Initialization parameters of the motors connected to the expansion board. */
davide.aliprandi@st.com 24:d1f487cb02ba 73 L6470_init_t init[L6470DAISYCHAINSIZE] = {
Davidroid 9:f35fbeedb8f4 74 /* First Motor. */
Davidroid 9:f35fbeedb8f4 75 {
Davidroid 18:591a007effc2 76 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 77 400, /* Min number of steps per revolution for the motor. */
Davidroid 18:591a007effc2 78 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 79 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 80 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 81 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 82 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 83 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 84 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 85 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 86 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 87 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 88 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 89 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 90 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 91 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 92 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 93 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 94 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 95 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 96 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 97 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 98 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 99 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 100 },
Davidroid 9:f35fbeedb8f4 101
Davidroid 9:f35fbeedb8f4 102 /* Second Motor. */
Davidroid 9:f35fbeedb8f4 103 {
Davidroid 18:591a007effc2 104 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 105 400, /* Min number of steps per revolution for the motor. */
Davidroid 18:591a007effc2 106 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 107 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 108 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 109 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 110 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 111 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 112 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 113 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 114 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 115 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 116 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 117 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 118 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 119 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 120 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 121 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 122 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 123 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 124 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 125 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 126 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 127 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 128 }
Davidroid 9:f35fbeedb8f4 129 };
Davidroid 9:f35fbeedb8f4 130
Davidroid 0:5148e9486cf2 131
Davidroid 0:5148e9486cf2 132 /* Main ----------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 133
Davidroid 0:5148e9486cf2 134 int main()
Davidroid 0:5148e9486cf2 135 {
Davidroid 1:9f1974b0960d 136 /*----- Initialization. -----*/
Davidroid 1:9f1974b0960d 137
Davidroid 2:41eeee48951b 138 /* Initializing SPI bus. */
Davidroid 23:073b26366d03 139 #ifdef TARGET_STM32F429
Davidroid 23:073b26366d03 140 DevSPI dev_spi(D11, D12, D13);
Davidroid 23:073b26366d03 141 #else
Davidroid 3:fd280b953f77 142 DevSPI dev_spi(D11, D12, D3);
Davidroid 23:073b26366d03 143 #endif
Davidroid 0:5148e9486cf2 144
Davidroid 5:3b8e19bbf386 145 /* Initializing Motor Control Expansion Board. */
davide.aliprandi@st.com 24:d1f487cb02ba 146 x_nucleo_ihm02a1 = new XNucleoIhm02a1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi);
Davidroid 0:5148e9486cf2 147
Davidroid 1:9f1974b0960d 148 /* Building a list of motor control components. */
davide.aliprandi@st.com 24:d1f487cb02ba 149 L6470 **motors = x_nucleo_ihm02a1->get_components();
Davidroid 0:5148e9486cf2 150
Davidroid 0:5148e9486cf2 151 /* Printing to the console. */
Davidroid 0:5148e9486cf2 152 printf("Motor Control Application Example for 2 Motors\r\n\n");
Davidroid 0:5148e9486cf2 153
Davidroid 1:9f1974b0960d 154
Davidroid 1:9f1974b0960d 155 /*----- Setting home and marke positions, getting positions, and going to positions. -----*/
Davidroid 1:9f1974b0960d 156
Davidroid 1:9f1974b0960d 157 /* Printing to the console. */
Davidroid 1:9f1974b0960d 158 printf("--> Setting home position.\r\n");
Davidroid 1:9f1974b0960d 159
Davidroid 1:9f1974b0960d 160 /* Setting the home position. */
davide.aliprandi@st.com 24:d1f487cb02ba 161 motors[0]->set_home();
Davidroid 1:9f1974b0960d 162
Davidroid 1:9f1974b0960d 163 /* Waiting. */
Davidroid 1:9f1974b0960d 164 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 165
Davidroid 1:9f1974b0960d 166 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 167 int position = motors[0]->get_position();
Davidroid 18:591a007effc2 168
Davidroid 1:9f1974b0960d 169 /* Printing to the console. */
Davidroid 1:9f1974b0960d 170 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 171
Davidroid 1:9f1974b0960d 172 /* Waiting. */
Davidroid 1:9f1974b0960d 173 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 174
Davidroid 1:9f1974b0960d 175 /* Printing to the console. */
Davidroid 1:9f1974b0960d 176 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 1:9f1974b0960d 177
Davidroid 1:9f1974b0960d 178 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 179 motors[0]->move(StepperMotor::FWD, STEPS_1);
Davidroid 0:5148e9486cf2 180
Davidroid 1:9f1974b0960d 181 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 182 motors[0]->wait_while_active();
Davidroid 1:9f1974b0960d 183
Davidroid 1:9f1974b0960d 184 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 185 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 186
Davidroid 1:9f1974b0960d 187 /* Printing to the console. */
Davidroid 1:9f1974b0960d 188 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 189
Davidroid 1:9f1974b0960d 190 /* Printing to the console. */
Davidroid 1:9f1974b0960d 191 printf("--> Marking the current position.\r\n");
Davidroid 0:5148e9486cf2 192
Davidroid 1:9f1974b0960d 193 /* Marking the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 194 motors[0]->set_mark();
Davidroid 1:9f1974b0960d 195
Davidroid 1:9f1974b0960d 196 /* Waiting. */
Davidroid 1:9f1974b0960d 197 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 198
Davidroid 1:9f1974b0960d 199 /* Printing to the console. */
Davidroid 1:9f1974b0960d 200 printf("--> Moving backward %d steps.\r\n", STEPS_2);
Davidroid 1:9f1974b0960d 201
Davidroid 1:9f1974b0960d 202 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 203 motors[0]->move(StepperMotor::BWD, STEPS_2);
Davidroid 1:9f1974b0960d 204
Davidroid 1:9f1974b0960d 205 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 206 motors[0]->wait_while_active();
Davidroid 0:5148e9486cf2 207
Davidroid 1:9f1974b0960d 208 /* Waiting. */
Davidroid 1:9f1974b0960d 209 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 210
Davidroid 1:9f1974b0960d 211 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 212 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 213
Davidroid 1:9f1974b0960d 214 /* Printing to the console. */
Davidroid 1:9f1974b0960d 215 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 216
Davidroid 1:9f1974b0960d 217 /* Waiting. */
Davidroid 1:9f1974b0960d 218 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 219
Davidroid 1:9f1974b0960d 220 /* Printing to the console. */
Davidroid 1:9f1974b0960d 221 printf("--> Going to marked position.\r\n");
Davidroid 0:5148e9486cf2 222
Davidroid 1:9f1974b0960d 223 /* Going to marked position. */
davide.aliprandi@st.com 24:d1f487cb02ba 224 motors[0]->go_mark();
Davidroid 1:9f1974b0960d 225
Davidroid 1:9f1974b0960d 226 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 227 motors[0]->wait_while_active();
Davidroid 0:5148e9486cf2 228
Davidroid 1:9f1974b0960d 229 /* Waiting. */
Davidroid 1:9f1974b0960d 230 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 231
Davidroid 1:9f1974b0960d 232 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 233 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 234
Davidroid 1:9f1974b0960d 235 /* Printing to the console. */
Davidroid 1:9f1974b0960d 236 printf("--> Getting the current position: %d\r\n", position);
Davidroid 0:5148e9486cf2 237
Davidroid 1:9f1974b0960d 238 /* Waiting. */
Davidroid 1:9f1974b0960d 239 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 240
Davidroid 1:9f1974b0960d 241 /* Printing to the console. */
Davidroid 1:9f1974b0960d 242 printf("--> Going to home position.\r\n");
Davidroid 0:5148e9486cf2 243
Davidroid 1:9f1974b0960d 244 /* Going to home position. */
davide.aliprandi@st.com 24:d1f487cb02ba 245 motors[0]->go_home();
Davidroid 1:9f1974b0960d 246
Davidroid 1:9f1974b0960d 247 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 248 motors[0]->wait_while_active();
Davidroid 1:9f1974b0960d 249
Davidroid 1:9f1974b0960d 250 /* Waiting. */
Davidroid 1:9f1974b0960d 251 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 252
Davidroid 1:9f1974b0960d 253 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 254 position = motors[0]->get_position();
Davidroid 1:9f1974b0960d 255
Davidroid 1:9f1974b0960d 256 /* Printing to the console. */
Davidroid 1:9f1974b0960d 257 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 258
Davidroid 1:9f1974b0960d 259 /* Waiting. */
Davidroid 18:591a007effc2 260 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 261
Davidroid 18:591a007effc2 262 /* Printing to the console. */
Davidroid 18:591a007effc2 263 printf("--> Halving the microsteps.\r\n");
Davidroid 18:591a007effc2 264
Davidroid 18:591a007effc2 265 /* Halving the microsteps. */
Davidroid 18:591a007effc2 266 init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel - 1 : init[0].step_sel);
davide.aliprandi@st.com 24:d1f487cb02ba 267 if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel)) {
Davidroid 18:591a007effc2 268 printf(" Step Mode not allowed.\r\n");
Davidroid 22:e81ccf73bc5d 269 }
Davidroid 18:591a007effc2 270
Davidroid 18:591a007effc2 271 /* Waiting. */
Davidroid 18:591a007effc2 272 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 273
Davidroid 18:591a007effc2 274 /* Printing to the console. */
Davidroid 18:591a007effc2 275 printf("--> Setting home position.\r\n");
Davidroid 18:591a007effc2 276
Davidroid 18:591a007effc2 277 /* Setting the home position. */
davide.aliprandi@st.com 24:d1f487cb02ba 278 motors[0]->set_home();
Davidroid 18:591a007effc2 279
Davidroid 18:591a007effc2 280 /* Waiting. */
Davidroid 18:591a007effc2 281 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 282
Davidroid 18:591a007effc2 283 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 284 position = motors[0]->get_position();
Davidroid 18:591a007effc2 285
Davidroid 18:591a007effc2 286 /* Printing to the console. */
Davidroid 18:591a007effc2 287 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 288
Davidroid 18:591a007effc2 289 /* Waiting. */
Davidroid 18:591a007effc2 290 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 291
Davidroid 18:591a007effc2 292 /* Printing to the console. */
Davidroid 18:591a007effc2 293 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Davidroid 18:591a007effc2 294
Davidroid 18:591a007effc2 295 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 296 motors[0]->move(StepperMotor::FWD, STEPS_1);
Davidroid 18:591a007effc2 297
Davidroid 18:591a007effc2 298 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 299 motors[0]->wait_while_active();
Davidroid 18:591a007effc2 300
Davidroid 18:591a007effc2 301 /* Getting the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 302 position = motors[0]->get_position();
Davidroid 18:591a007effc2 303
Davidroid 18:591a007effc2 304 /* Printing to the console. */
Davidroid 18:591a007effc2 305 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 306
Davidroid 18:591a007effc2 307 /* Printing to the console. */
Davidroid 18:591a007effc2 308 printf("--> Marking the current position.\r\n");
Davidroid 18:591a007effc2 309
Davidroid 18:591a007effc2 310 /* Marking the current position. */
davide.aliprandi@st.com 24:d1f487cb02ba 311 motors[0]->set_mark();
Davidroid 18:591a007effc2 312
Davidroid 18:591a007effc2 313 /* Waiting. */
Davidroid 1:9f1974b0960d 314 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 315
Davidroid 0:5148e9486cf2 316
Davidroid 1:9f1974b0960d 317 /*----- Running together for a certain amount of time. -----*/
Davidroid 1:9f1974b0960d 318
Davidroid 1:9f1974b0960d 319 /* Printing to the console. */
Davidroid 1:9f1974b0960d 320 printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 321
Davidroid 1:9f1974b0960d 322 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 323 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 324 motors[m]->prepare_run(StepperMotor::BWD, 400);
Davidroid 22:e81ccf73bc5d 325 }
Davidroid 0:5148e9486cf2 326
Davidroid 1:9f1974b0960d 327 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 328 x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 329
Davidroid 1:9f1974b0960d 330 /* Waiting. */
Davidroid 1:9f1974b0960d 331 wait_ms(DELAY_3);
Davidroid 1:9f1974b0960d 332
Davidroid 1:9f1974b0960d 333
Davidroid 1:9f1974b0960d 334 /*----- Increasing the speed while running. -----*/
Davidroid 0:5148e9486cf2 335
Davidroid 1:9f1974b0960d 336 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 337 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 338 motors[m]->prepare_get_speed();
Davidroid 22:e81ccf73bc5d 339 }
Davidroid 0:5148e9486cf2 340
Davidroid 1:9f1974b0960d 341 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 342 uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 343
Davidroid 1:9f1974b0960d 344 /* Printing to the console. */
Davidroid 1:9f1974b0960d 345 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 346
Davidroid 1:9f1974b0960d 347 /* Printing to the console. */
Davidroid 18:591a007effc2 348 printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
Davidroid 0:5148e9486cf2 349
Davidroid 1:9f1974b0960d 350 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 351 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 352 motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1);
Davidroid 22:e81ccf73bc5d 353 }
Davidroid 1:9f1974b0960d 354
Davidroid 1:9f1974b0960d 355 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 356 results = x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 357
Davidroid 1:9f1974b0960d 358 /* Waiting. */
Davidroid 1:9f1974b0960d 359 wait_ms(DELAY_3);
Davidroid 0:5148e9486cf2 360
Davidroid 1:9f1974b0960d 361 /* Preparing each motor to perform a run at a specified speed. */
Davidroid 22:e81ccf73bc5d 362 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 363 motors[m]->prepare_get_speed();
Davidroid 22:e81ccf73bc5d 364 }
Davidroid 0:5148e9486cf2 365
Davidroid 1:9f1974b0960d 366 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 367 results = x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 368
Davidroid 1:9f1974b0960d 369 /* Printing to the console. */
Davidroid 1:9f1974b0960d 370 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 371
Davidroid 1:9f1974b0960d 372 /* Waiting. */
Davidroid 1:9f1974b0960d 373 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 374
Davidroid 0:5148e9486cf2 375
Davidroid 1:9f1974b0960d 376 /*----- Hard Stop. -----*/
Davidroid 0:5148e9486cf2 377
Davidroid 1:9f1974b0960d 378 /* Printing to the console. */
Davidroid 1:9f1974b0960d 379 printf("--> Hard Stop.\r\n");
Davidroid 0:5148e9486cf2 380
Davidroid 1:9f1974b0960d 381 /* Preparing each motor to perform a hard stop. */
Davidroid 22:e81ccf73bc5d 382 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 383 motors[m]->prepare_hard_stop();
Davidroid 22:e81ccf73bc5d 384 }
Davidroid 0:5148e9486cf2 385
Davidroid 1:9f1974b0960d 386 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 387 x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 0:5148e9486cf2 388
Davidroid 1:9f1974b0960d 389 /* Waiting. */
Davidroid 1:9f1974b0960d 390 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 391
Davidroid 0:5148e9486cf2 392
Davidroid 1:9f1974b0960d 393 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 1:9f1974b0960d 394
Davidroid 1:9f1974b0960d 395 /* Printing to the console. */
Davidroid 1:9f1974b0960d 396 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 1:9f1974b0960d 397
Davidroid 1:9f1974b0960d 398 /* Doing a full revolution on each motor, one after the other. */
Davidroid 22:e81ccf73bc5d 399 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
Davidroid 22:e81ccf73bc5d 400 for (int i = 0; i < MPR_1; i++) {
Davidroid 1:9f1974b0960d 401 /* Computing the number of steps. */
Davidroid 9:f35fbeedb8f4 402 int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1);
Davidroid 1:9f1974b0960d 403
Davidroid 1:9f1974b0960d 404 /* Moving. */
davide.aliprandi@st.com 24:d1f487cb02ba 405 motors[m]->move(StepperMotor::FWD, steps);
Davidroid 1:9f1974b0960d 406
Davidroid 1:9f1974b0960d 407 /* Waiting while active. */
davide.aliprandi@st.com 24:d1f487cb02ba 408 motors[m]->wait_while_active();
Davidroid 1:9f1974b0960d 409
Davidroid 1:9f1974b0960d 410 /* Waiting. */
Davidroid 1:9f1974b0960d 411 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 412 }
Davidroid 22:e81ccf73bc5d 413 }
Davidroid 1:9f1974b0960d 414
Davidroid 1:9f1974b0960d 415 /* Waiting. */
Davidroid 1:9f1974b0960d 416 wait_ms(DELAY_2);
Davidroid 1:9f1974b0960d 417
Davidroid 1:9f1974b0960d 418
Davidroid 1:9f1974b0960d 419 /*----- High Impedance State. -----*/
Davidroid 1:9f1974b0960d 420
Davidroid 1:9f1974b0960d 421 /* Printing to the console. */
Davidroid 1:9f1974b0960d 422 printf("--> High Impedance State.\r\n");
Davidroid 1:9f1974b0960d 423
Davidroid 1:9f1974b0960d 424 /* Preparing each motor to set High Impedance State. */
Davidroid 22:e81ccf73bc5d 425 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) {
davide.aliprandi@st.com 24:d1f487cb02ba 426 motors[m]->prepare_hard_hiz();
Davidroid 22:e81ccf73bc5d 427 }
Davidroid 1:9f1974b0960d 428
Davidroid 1:9f1974b0960d 429 /* Performing the action on each motor at the same time. */
davide.aliprandi@st.com 24:d1f487cb02ba 430 x_nucleo_ihm02a1->perform_prepared_actions();
Davidroid 1:9f1974b0960d 431
Davidroid 1:9f1974b0960d 432 /* Waiting. */
Davidroid 1:9f1974b0960d 433 wait_ms(DELAY_2);
Davidroid 22:e81ccf73bc5d 434 }