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