my 2axes stepper

Dependencies:   mbed X_NUCLEO_IHM02A1

Committer:
Kiskovce
Date:
Mon Jan 27 13:56:24 2020 +0000
Revision:
32:c8741915d088
Parent:
31:ea45071b46e0
a

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