my 2axes stepper

Dependencies:   mbed X_NUCLEO_IHM02A1

Committer:
Kiskovce
Date:
Mon Jan 27 13:35:43 2020 +0000
Revision:
30:da5a3676bfe9
Parent:
29:e1062f9e6394
Child:
31:ea45071b46e0
add comment;

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