my 2axes stepper

Dependencies:   mbed X_NUCLEO_IHM02A1

Committer:
Kiskovce
Date:
Mon Jan 27 13:32:48 2020 +0000
Revision:
29:e1062f9e6394
Parent:
28:f6b184e44cc6
Child:
30:da5a3676bfe9
new;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:5148e9486cf2 1 #include "mbed.h"
Davidroid 0:5148e9486cf2 2 #include "DevSPI.h"
Davidroid 26:caec5f51abe8 3 #include "XNucleoIHM02A1.h"
Davidroid 0:5148e9486cf2 4
Davidroid 0:5148e9486cf2 5
Kiskovce 29:e1062f9e6394 6 // MBED PUBLISH -A //////////////////////////
Davidroid 0:5148e9486cf2 7
Davidroid 0:5148e9486cf2 8
Kiskovce 29:e1062f9e6394 9 #define MPR_1 4 /* Number of movements per revolution. */
Kiskovce 29:e1062f9e6394 10 #define STEPS_1 (400 * 128) /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
Kiskovce 29:e1062f9e6394 11 #define STEPS_2 (STEPS_1 * 2) /* 1 revolution given a 400 steps motor configured at 1/128 microstep mode. */
Kiskovce 29:e1062f9e6394 12 #define DELAY_1 1000 /* Delay in milliseconds. */
Kiskovce 29:e1062f9e6394 13 #define DELAY_2 2000 /* Delay in milliseconds. */
Kiskovce 29:e1062f9e6394 14 #define DELAY_3 5000 /* Delay in milliseconds. */
Davidroid 0:5148e9486cf2 15
Kiskovce 29:e1062f9e6394 16
Kiskovce 29:e1062f9e6394 17
Kiskovce 29:e1062f9e6394 18 XNucleoIHM02A1 *x_nucleo_ihm02a1; /* Motor Control Expansion Board. */
Davidroid 0:5148e9486cf2 19
Davidroid 9:f35fbeedb8f4 20 /* Initialization parameters of the motors connected to the expansion board. */
davide.aliprandi@st.com 24:d1f487cb02ba 21 L6470_init_t init[L6470DAISYCHAINSIZE] = {
Davidroid 9:f35fbeedb8f4 22 /* First Motor. */
Davidroid 9:f35fbeedb8f4 23 {
Kiskovce 29:e1062f9e6394 24 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 25 400, /* Min number of steps per revolution for the motor. */
Kiskovce 29:e1062f9e6394 26 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 27 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 28 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 29 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 30 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 31 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 32 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 33 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 34 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 35 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 36 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 37 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 38 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 39 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 40 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 41 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 42 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 43 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 44 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 45 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 46 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 47 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 48 },
Davidroid 9:f35fbeedb8f4 49
Davidroid 9:f35fbeedb8f4 50 /* Second Motor. */
Davidroid 9:f35fbeedb8f4 51 {
Kiskovce 29:e1062f9e6394 52 9.0, /* Motor supply voltage in V. */
Davidroid 18:591a007effc2 53 400, /* Min number of steps per revolution for the motor. */
Kiskovce 29:e1062f9e6394 54 1.7, /* Max motor phase voltage in A. */
Davidroid 18:591a007effc2 55 3.06, /* Max motor phase voltage in V. */
Davidroid 18:591a007effc2 56 300.0, /* Motor initial speed [step/s]. */
Davidroid 18:591a007effc2 57 500.0, /* Motor acceleration [step/s^2] (comment for infinite acceleration mode). */
Davidroid 18:591a007effc2 58 500.0, /* Motor deceleration [step/s^2] (comment for infinite deceleration mode). */
Davidroid 18:591a007effc2 59 992.0, /* Motor maximum speed [step/s]. */
Davidroid 18:591a007effc2 60 0.0, /* Motor minimum speed [step/s]. */
Davidroid 18:591a007effc2 61 602.7, /* Motor full-step speed threshold [step/s]. */
Davidroid 18:591a007effc2 62 3.06, /* Holding kval [V]. */
Davidroid 18:591a007effc2 63 3.06, /* Constant speed kval [V]. */
Davidroid 18:591a007effc2 64 3.06, /* Acceleration starting kval [V]. */
Davidroid 18:591a007effc2 65 3.06, /* Deceleration starting kval [V]. */
Davidroid 18:591a007effc2 66 61.52, /* Intersect speed for bemf compensation curve slope changing [step/s]. */
Davidroid 18:591a007effc2 67 392.1569e-6, /* Start slope [s/step]. */
Davidroid 18:591a007effc2 68 643.1372e-6, /* Acceleration final slope [s/step]. */
Davidroid 18:591a007effc2 69 643.1372e-6, /* Deceleration final slope [s/step]. */
Davidroid 18:591a007effc2 70 0, /* Thermal compensation factor (range [0, 15]). */
Davidroid 18:591a007effc2 71 3.06 * 1000 * 1.10, /* Ocd threshold [ma] (range [375 ma, 6000 ma]). */
Davidroid 18:591a007effc2 72 3.06 * 1000 * 1.00, /* Stall threshold [ma] (range [31.25 ma, 4000 ma]). */
Davidroid 18:591a007effc2 73 StepperMotor::STEP_MODE_1_128, /* Step mode selection. */
Davidroid 18:591a007effc2 74 0xFF, /* Alarm conditions enable. */
Davidroid 18:591a007effc2 75 0x2E88 /* Ic configuration. */
Davidroid 9:f35fbeedb8f4 76 }
Davidroid 9:f35fbeedb8f4 77 };
Davidroid 9:f35fbeedb8f4 78
Davidroid 0:5148e9486cf2 79
Davidroid 0:5148e9486cf2 80 /* Main ----------------------------------------------------------------------*/
Davidroid 0:5148e9486cf2 81
Davidroid 0:5148e9486cf2 82 int main()
Davidroid 0:5148e9486cf2 83 {
Davidroid 1:9f1974b0960d 84 /*----- Initialization. -----*/
Davidroid 1:9f1974b0960d 85
Davidroid 2:41eeee48951b 86 /* Initializing SPI bus. */
Davidroid 23:073b26366d03 87 #ifdef TARGET_STM32F429
Davidroid 23:073b26366d03 88 DevSPI dev_spi(D11, D12, D13);
Davidroid 23:073b26366d03 89 #else
Davidroid 3:fd280b953f77 90 DevSPI dev_spi(D11, D12, D3);
Davidroid 23:073b26366d03 91 #endif
Davidroid 0:5148e9486cf2 92
Davidroid 0:5148e9486cf2 93
Kiskovce 29:e1062f9e6394 94 x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi); /* Initializing Motor Control Expansion Board. */
Kiskovce 29:e1062f9e6394 95 L6470 **motors = x_nucleo_ihm02a1->get_components(); /* Building a list of motor control components. */
Davidroid 1:9f1974b0960d 96
Davidroid 1:9f1974b0960d 97
Kiskovce 29:e1062f9e6394 98 printf("Motor Control Application Example for 2 Motors\r\n\n");
Kiskovce 29:e1062f9e6394 99 printf("--> Setting home position.\r\n"); /* Printing to the console. */
Kiskovce 29:e1062f9e6394 100 motors[0]->set_home(); /* Setting the home position. */
Davidroid 1:9f1974b0960d 101 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 102
Kiskovce 29:e1062f9e6394 103 int position = motors[0]->get_position(); /* Getting the current position. */
Kiskovce 29:e1062f9e6394 104 printf("--> Getting the current position: %d\r\n", position);
Davidroid 1:9f1974b0960d 105 wait_ms(DELAY_1);
Davidroid 1:9f1974b0960d 106
Kiskovce 29:e1062f9e6394 107 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Kiskovce 29:e1062f9e6394 108 motors[0]->move(StepperMotor::FWD, STEPS_1); /* Moving. */
Kiskovce 29:e1062f9e6394 109 motors[0]->wait_while_active(); /* Waiting while active. */
Kiskovce 29:e1062f9e6394 110 position = motors[0]->get_position(); /* Getting the current position. */
Davidroid 1:9f1974b0960d 111 printf("--> Getting the current position: %d\r\n", position);
Davidroid 0:5148e9486cf2 112
Davidroid 1:9f1974b0960d 113
Kiskovce 29:e1062f9e6394 114 printf("--> Marking the current position.\r\n");
Kiskovce 29:e1062f9e6394 115 motors[0]->set_mark(); /* Marking the current position. */
Davidroid 1:9f1974b0960d 116 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 117
Kiskovce 29:e1062f9e6394 118
Kiskovce 29:e1062f9e6394 119 printf("--> Moving backward %d steps.\r\n", STEPS_2);
Kiskovce 29:e1062f9e6394 120 motors[0]->move(StepperMotor::BWD, STEPS_2); /* Moving. */
Kiskovce 29:e1062f9e6394 121 motors[0]->wait_while_active(); /* Waiting while active. */
Kiskovce 29:e1062f9e6394 122 wait_ms(DELAY_1); /* Waiting. */
Kiskovce 29:e1062f9e6394 123 position = motors[0]->get_position(); /* Getting the current position. */
Davidroid 1:9f1974b0960d 124 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 125 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 126
Davidroid 18:591a007effc2 127
Kiskovce 29:e1062f9e6394 128 printf("--> Going to marked position.\r\n"); /* Printing to the console. */
Kiskovce 29:e1062f9e6394 129 motors[0]->go_mark(); /* Going to marked position. */
Kiskovce 29:e1062f9e6394 130 motors[0]->wait_while_active(); /* Waiting while active. */
Kiskovce 29:e1062f9e6394 131 wait_ms(DELAY_1); /* Waiting. */
Kiskovce 29:e1062f9e6394 132 position = motors[0]->get_position(); /* Getting the current position. */
Kiskovce 29:e1062f9e6394 133 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 134 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 135
Davidroid 18:591a007effc2 136
Kiskovce 29:e1062f9e6394 137 printf("--> Going to home position.\r\n");
Kiskovce 29:e1062f9e6394 138 motors[0]->go_home(); /* Going to home position. */
Kiskovce 29:e1062f9e6394 139 motors[0]->wait_while_active(); /* Waiting while active. */
Davidroid 18:591a007effc2 140 wait_ms(DELAY_1);
Kiskovce 29:e1062f9e6394 141 position = motors[0]->get_position(); /* Getting the current position. */
Kiskovce 29:e1062f9e6394 142 printf("--> Getting the current position: %d\r\n", position); /* Printing to the console. */
Kiskovce 29:e1062f9e6394 143 wait_ms(DELAY_1); /* Waiting. */
Davidroid 18:591a007effc2 144
Kiskovce 29:e1062f9e6394 145 printf("--> Halving the microsteps.\r\n");
Kiskovce 29:e1062f9e6394 146
Kiskovce 29:e1062f9e6394 147 init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel - 1 : init[0].step_sel); /* Halving the microsteps. */
Kiskovce 29:e1062f9e6394 148 if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel))
Kiskovce 29:e1062f9e6394 149 {
Kiskovce 29:e1062f9e6394 150 printf(" Step Mode not allowed.\r\n");
Kiskovce 29:e1062f9e6394 151 }
Kiskovce 29:e1062f9e6394 152 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 153
Kiskovce 29:e1062f9e6394 154
Kiskovce 29:e1062f9e6394 155 printf("--> Setting home position.\r\n");
Kiskovce 29:e1062f9e6394 156 motors[0]->set_home(); /* Setting the home position. */
Kiskovce 29:e1062f9e6394 157 wait_ms(DELAY_1);
Kiskovce 29:e1062f9e6394 158 position = motors[0]->get_position(); /* Getting the current position. */
Davidroid 18:591a007effc2 159 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 160 wait_ms(DELAY_1);
Davidroid 18:591a007effc2 161
Kiskovce 29:e1062f9e6394 162
Davidroid 18:591a007effc2 163 printf("--> Moving forward %d steps.\r\n", STEPS_1);
Kiskovce 29:e1062f9e6394 164 motors[0]->move(StepperMotor::FWD, STEPS_1); /* Moving. */
Kiskovce 29:e1062f9e6394 165 motors[0]->wait_while_active(); /* Waiting while active. */
Kiskovce 29:e1062f9e6394 166 position = motors[0]->get_position(); /* Getting the current position. */
Davidroid 18:591a007effc2 167 printf("--> Getting the current position: %d\r\n", position);
Davidroid 18:591a007effc2 168
Kiskovce 29:e1062f9e6394 169
Davidroid 18:591a007effc2 170 printf("--> Marking the current position.\r\n");
Kiskovce 29:e1062f9e6394 171 motors[0]->set_mark(); /* Marking the current position. */
Davidroid 1:9f1974b0960d 172 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 173
Davidroid 0:5148e9486cf2 174
Davidroid 1:9f1974b0960d 175 /*----- Running together for a certain amount of time. -----*/
Davidroid 1:9f1974b0960d 176 printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000);
Kiskovce 29:e1062f9e6394 177 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Preparing each motor to perform a run at a specified speed. */
Kiskovce 29:e1062f9e6394 178 {
Kiskovce 29:e1062f9e6394 179 motors[m]->prepare_run(StepperMotor::BWD, 400);
Kiskovce 29:e1062f9e6394 180 }
Kiskovce 29:e1062f9e6394 181 x_nucleo_ihm02a1->perform_prepared_actions(); /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 182 wait_ms(DELAY_3);
Davidroid 1:9f1974b0960d 183
Davidroid 1:9f1974b0960d 184
Davidroid 1:9f1974b0960d 185 /*----- Increasing the speed while running. -----*/
Kiskovce 29:e1062f9e6394 186 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Preparing each motor to perform a run at a specified speed. */
Kiskovce 29:e1062f9e6394 187 {
Kiskovce 29:e1062f9e6394 188 motors[m]->prepare_get_speed();
Kiskovce 29:e1062f9e6394 189 }
Kiskovce 29:e1062f9e6394 190 uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions(); /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 191 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Kiskovce 29:e1062f9e6394 192 printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000);
Kiskovce 29:e1062f9e6394 193 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Preparing each motor to perform a run at a specified speed. */
Kiskovce 29:e1062f9e6394 194 {
Kiskovce 29:e1062f9e6394 195 motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1);
Kiskovce 29:e1062f9e6394 196 }
Davidroid 1:9f1974b0960d 197
Davidroid 0:5148e9486cf2 198
Kiskovce 29:e1062f9e6394 199 results = x_nucleo_ihm02a1->perform_prepared_actions(); /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 200 wait_ms(DELAY_3);
Davidroid 0:5148e9486cf2 201
Kiskovce 29:e1062f9e6394 202
Davidroid 0:5148e9486cf2 203
Kiskovce 29:e1062f9e6394 204 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Preparing each motor to perform a run at a specified speed. */
Kiskovce 29:e1062f9e6394 205 {
Kiskovce 29:e1062f9e6394 206 motors[m]->prepare_get_speed();
Kiskovce 29:e1062f9e6394 207 }
Kiskovce 29:e1062f9e6394 208 results = x_nucleo_ihm02a1->perform_prepared_actions(); /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 209 printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]);
Davidroid 1:9f1974b0960d 210 wait_ms(DELAY_1);
Davidroid 0:5148e9486cf2 211
Davidroid 0:5148e9486cf2 212
Davidroid 1:9f1974b0960d 213 /*----- Hard Stop. -----*/
Davidroid 1:9f1974b0960d 214 printf("--> Hard Stop.\r\n");
Davidroid 0:5148e9486cf2 215
Davidroid 0:5148e9486cf2 216
Kiskovce 29:e1062f9e6394 217 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Preparing each motor to perform a hard stop. */
Kiskovce 29:e1062f9e6394 218 {
Kiskovce 29:e1062f9e6394 219 motors[m]->prepare_hard_stop();
Kiskovce 29:e1062f9e6394 220 }
Kiskovce 29:e1062f9e6394 221 x_nucleo_ihm02a1->perform_prepared_actions(); /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 222 wait_ms(DELAY_2);
Davidroid 0:5148e9486cf2 223
Davidroid 0:5148e9486cf2 224
Davidroid 1:9f1974b0960d 225 /*----- Doing a full revolution on each motor, one after the other. -----*/
Davidroid 1:9f1974b0960d 226 printf("--> Doing a full revolution on each motor, one after the other.\r\n");
Davidroid 1:9f1974b0960d 227
Davidroid 1:9f1974b0960d 228
Kiskovce 29:e1062f9e6394 229 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Doing a full revolution on each motor, one after the other. */
Kiskovce 29:e1062f9e6394 230 {
Kiskovce 29:e1062f9e6394 231 for (int i = 0; i < MPR_1; i++)
Kiskovce 29:e1062f9e6394 232 {
Kiskovce 29:e1062f9e6394 233 int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1); /* Computing the number of steps. */
Kiskovce 29:e1062f9e6394 234 motors[m]->move(StepperMotor::FWD, steps);/* Moving. */
Kiskovce 29:e1062f9e6394 235 motors[m]->wait_while_active(); /* Waiting while active. */
Kiskovce 29:e1062f9e6394 236 wait_ms(DELAY_1);
Kiskovce 29:e1062f9e6394 237 }
Davidroid 1:9f1974b0960d 238 }
Davidroid 1:9f1974b0960d 239 wait_ms(DELAY_2);
Davidroid 1:9f1974b0960d 240
Davidroid 1:9f1974b0960d 241
Davidroid 1:9f1974b0960d 242 /*----- High Impedance State. -----*/
Davidroid 1:9f1974b0960d 243
Davidroid 1:9f1974b0960d 244 printf("--> High Impedance State.\r\n");
Davidroid 1:9f1974b0960d 245
Davidroid 1:9f1974b0960d 246
Kiskovce 29:e1062f9e6394 247 for (int m = 0; m < L6470DAISYCHAINSIZE; m++) /* Preparing each motor to set High Impedance State. */
Kiskovce 29:e1062f9e6394 248 {
Kiskovce 29:e1062f9e6394 249 motors[m]->prepare_hard_hiz();
Kiskovce 29:e1062f9e6394 250 }
Kiskovce 29:e1062f9e6394 251 x_nucleo_ihm02a1->perform_prepared_actions(); /* Performing the action on each motor at the same time. */
Davidroid 1:9f1974b0960d 252 wait_ms(DELAY_2);
Davidroid 22:e81ccf73bc5d 253 }