Source_File
Dependencies: mbed X_NUCLEO_IHM02A1
Diff: main.cpp
- Revision:
- 27:b6e6afc3b1f7
- Parent:
- 26:caec5f51abe8
--- a/main.cpp Mon Mar 13 17:53:12 2017 +0000 +++ b/main.cpp Wed Apr 03 07:36:07 2019 +0000 @@ -41,6 +41,8 @@ /* mbed specific header files. */ #include "mbed.h" +#include "main.h" +#include "PWM.h" /* Helper header files. */ #include "DevSPI.h" @@ -63,12 +65,43 @@ #define DELAY_2 2000 #define DELAY_3 5000 +DigitalOut led1(LED1); + + + +InterruptIn button1(USER_BUTTON); +volatile int idx = 0; +volatile bool button1_pressed = false; // Used in the main loop +volatile bool button1_enabled = true; // Used for debouncing +Timeout button1_timeout; // Used for debouncing + +// Enables button when bouncing is over +void button1_enabled_cb(void) +{ + button1_enabled = true; +} + +// ISR handling button pressed event +void button1_onpressed_cb(void) +{ + if (button1_enabled) { // Disabled while the button is bouncing + button1_enabled = false; + button1_pressed = true; // To be read by the main loop + + if(idx<4){ + idx++; + } + else{idx=1;} + + button1_timeout.attach(callback(button1_enabled_cb), 0.03); // Debounce time 300 ms + } +} /* Variables -----------------------------------------------------------------*/ /* Motor Control Expansion Board. */ -XNucleoIHM02A1 *x_nucleo_ihm02a1; - +XNucleoIHM02A1 *x_nucleo_ihm02a1_1; +//XNucleoIHM02A1 *x_nucleo_ihm02a1_2; /* Initialization parameters of the motors connected to the expansion board. */ L6470_init_t init[L6470DAISYCHAINSIZE] = { /* First Motor. */ @@ -143,292 +176,116 @@ #endif /* Initializing Motor Control Expansion Board. */ - x_nucleo_ihm02a1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi); + x_nucleo_ihm02a1_1 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi); + // x_nucleo_ihm02a1_2 = new XNucleoIHM02A1(&init[0], &init[1], A4, A5, D4, A2, &dev_spi); /* Building a list of motor control components. */ - L6470 **motors = x_nucleo_ihm02a1->get_components(); - - /* Printing to the console. */ - printf("Motor Control Application Example for 2 Motors\r\n\n"); - - + L6470 **motors = x_nucleo_ihm02a1_1->get_components(); + //L6470 **motors2 = x_nucleo_ihm02a1_2->get_components(); /*----- Setting home and marke positions, getting positions, and going to positions. -----*/ - /* Printing to the console. */ - printf("--> Setting home position.\r\n"); - - /* Setting the home position. */ - motors[0]->set_home(); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Getting the current position. */ - int position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Moving forward %d steps.\r\n", STEPS_1); - - /* Moving. */ - motors[0]->move(StepperMotor::FWD, STEPS_1); - - /* Waiting while active. */ - motors[0]->wait_while_active(); - - /* Getting the current position. */ - position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Printing to the console. */ - printf("--> Marking the current position.\r\n"); - - /* Marking the current position. */ - motors[0]->set_mark(); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Moving backward %d steps.\r\n", STEPS_2); - - /* Moving. */ - motors[0]->move(StepperMotor::BWD, STEPS_2); - - /* Waiting while active. */ - motors[0]->wait_while_active(); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Getting the current position. */ - position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Going to marked position.\r\n"); - - /* Going to marked position. */ - motors[0]->go_mark(); - - /* Waiting while active. */ - motors[0]->wait_while_active(); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Getting the current position. */ - position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Going to home position.\r\n"); - - /* Going to home position. */ - motors[0]->go_home(); - - /* Waiting while active. */ - motors[0]->wait_while_active(); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Getting the current position. */ - position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Halving the microsteps.\r\n"); - - /* Halving the microsteps. */ - init[0].step_sel = (init[0].step_sel > 0 ? init[0].step_sel - 1 : init[0].step_sel); - if (!motors[0]->set_step_mode((StepperMotor::step_mode_t) init[0].step_sel)) { - printf(" Step Mode not allowed.\r\n"); - } - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Setting home position.\r\n"); - /* Setting the home position. */ - motors[0]->set_home(); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Getting the current position. */ - position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Waiting. */ - wait_ms(DELAY_1); - - /* Printing to the console. */ - printf("--> Moving forward %d steps.\r\n", STEPS_1); + //motors[0]->set_home(); - /* Moving. */ - motors[0]->move(StepperMotor::FWD, STEPS_1); - - /* Waiting while active. */ - motors[0]->wait_while_active(); - - /* Getting the current position. */ - position = motors[0]->get_position(); - - /* Printing to the console. */ - printf("--> Getting the current position: %d\r\n", position); - - /* Printing to the console. */ - printf("--> Marking the current position.\r\n"); - - /* Marking the current position. */ - motors[0]->set_mark(); - - /* Waiting. */ - wait_ms(DELAY_2); - + /*----- Running together for a certain amount of time. -----*/ - - /* Printing to the console. */ - printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000); - - /* Preparing each motor to perform a run at a specified speed. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_run(StepperMotor::BWD, 400); - } - - /* Performing the action on each motor at the same time. */ - x_nucleo_ihm02a1->perform_prepared_actions(); - - /* Waiting. */ - wait_ms(DELAY_3); - - - /*----- Increasing the speed while running. -----*/ + + button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event - /* Preparing each motor to perform a run at a specified speed. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_get_speed(); - } - - /* Performing the action on each motor at the same time. */ - uint32_t* results = x_nucleo_ihm02a1->perform_prepared_actions(); - - /* Printing to the console. */ - printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]); + while(1) { + if (button1_pressed) { // Set when button is pressed + button1_pressed = false; + switch (idx){ + case 1: + pwm_io(50, 0.0); // 20ms - 0% + /* Printing to the console. */ + printf("--> Running together for %d seconds.\r\n", DELAY_3 / 1000); + + motors[1]->prepare_set_acceleration(7000); + motors[0]->prepare_set_acceleration(7000); + + - /* Printing to the console. */ - printf("--> Doublig the speed while running again for %d seconds.\r\n", DELAY_3 / 1000); + /* Preparing each motor to perform a run at a specified speed. */ + + motors[1]->prepare_run(StepperMotor::BWD, 800); + motors[0]->prepare_run(StepperMotor::BWD, 808); - /* Preparing each motor to perform a run at a specified speed. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_run(StepperMotor::BWD, results[m] << 1); - } - - /* Performing the action on each motor at the same time. */ - results = x_nucleo_ihm02a1->perform_prepared_actions(); - - /* Waiting. */ - wait_ms(DELAY_3); + /* Performing the action on each motor at the same time. */ + x_nucleo_ihm02a1_1->perform_prepared_actions(); + + break; + case 2: + /* Printing to the console. */ + printf("--> Hard Stop.\r\n"); - /* Preparing each motor to perform a run at a specified speed. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_get_speed(); - } - - /* Performing the action on each motor at the same time. */ - results = x_nucleo_ihm02a1->perform_prepared_actions(); - - /* Printing to the console. */ - printf(" Speed: M1 %d, M2 %d.\r\n", results[0], results[1]); - - /* Waiting. */ - wait_ms(DELAY_1); - - - /*----- Hard Stop. -----*/ - - /* Printing to the console. */ - printf("--> Hard Stop.\r\n"); - - /* Preparing each motor to perform a hard stop. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_hard_stop(); - } - - /* Performing the action on each motor at the same time. */ - x_nucleo_ihm02a1->perform_prepared_actions(); - - /* Waiting. */ - wait_ms(DELAY_2); - - - /*----- Doing a full revolution on each motor, one after the other. -----*/ - - /* Printing to the console. */ - printf("--> Doing a full revolution on each motor, one after the other.\r\n"); - - /* Doing a full revolution on each motor, one after the other. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - for (int i = 0; i < MPR_1; i++) { - /* Computing the number of steps. */ - int steps = (int) (((int) init[m].fullstepsperrevolution * pow(2.0f, init[m].step_sel)) / MPR_1); - - /* Moving. */ - motors[m]->move(StepperMotor::FWD, steps); - - /* Waiting while active. */ - motors[m]->wait_while_active(); - - /* Waiting. */ - wait_ms(DELAY_1); + /* Preparing each motor to perform a hard stop. */ + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + motors[m]->prepare_hard_stop(); + } + + /* Performing the action on each motor at the same time. */ + x_nucleo_ihm02a1_1->perform_prepared_actions(); + break; + case 3: + // while (!button1_pressed) { // Set when button is pressed + // button1_pressed = false; + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + motors[m]->prepare_set_acceleration(300); + //motors[m]->prepare_set_speed(1); + } + x_nucleo_ihm02a1_1->perform_prepared_actions(); + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + // motors[m]->prepare_set_acceleration(7000); + motors[m]->prepare_set_speed(100); + } + x_nucleo_ihm02a1_1->perform_prepared_actions(); + /* motors[1]->move(StepperMotor::FWD, 400*128); + motors[0]->move(StepperMotor::FWD, 400*128); + motors[1]->wait_while_active(); + motors[0]->wait_while_active(); + motors[1]->move(StepperMotor::BWD, 400*128); + motors[0]->move(StepperMotor::BWD, 400*128); + motors[1]->wait_while_active(); + motors[0]->wait_while_active(); + */ + //while (idx==3){ + pwm_io(40, 0.85); // 20ms - 25% + wait(0.5); + motors[1]->run(StepperMotor::BWD,100); + motors[0]->run(StepperMotor::BWD,101); + // } + + /* Waiting. */ + // wait_ms(DELAY_2); + + break; + case 4: + + /* Preparing each motor to set High Impedance State. */ + for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { + motors[m]->prepare_hard_hiz(); + } + + /* Performing the action on each motor at the same time. */ + x_nucleo_ihm02a1_1->perform_prepared_actions(); + float zusatz= 0.1; + // for(int i = 0;i>10;i++){ + //zusatz=zusatz-0.005; + + + pwm_io2(20000, 0.075); // 20ms - 25% + wait(1); + pwm_io2(20000, zusatz); // 20ms - 25% + + // } + break; + + } + // printf("Button pressed %d\n", idx++); + // led1 = !led1; } } - - /* Waiting. */ - wait_ms(DELAY_2); - - - /*----- High Impedance State. -----*/ - - /* Printing to the console. */ - printf("--> High Impedance State.\r\n"); - - /* Preparing each motor to set High Impedance State. */ - for (int m = 0; m < L6470DAISYCHAINSIZE; m++) { - motors[m]->prepare_hard_hiz(); - } - - /* Performing the action on each motor at the same time. */ - x_nucleo_ihm02a1->perform_prepared_actions(); - - /* Waiting. */ - wait_ms(DELAY_2); + }