my 2axes stepper

Dependencies:   mbed X_NUCLEO_IHM02A1

Committer:
Kiskovce
Date:
Mon Jan 27 13:41:52 2020 +0000
Revision:
31:ea45071b46e0
Parent:
30:da5a3676bfe9
Child:
32:c8741915d088
add code empty;

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