Team DIANA / Mbed OS Arm_Stepper_nucleo

Dependencies:   X-NUCLEO-IHM05A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* Includes ------------------------------------------------------------------*/
00003 /* mbed specific header files. */
00004 #include "mbed.h"
00005 /* Component specific header files. */
00006 #include "L6208.h"
00007 /* Definitions ---------------------------------------------------------------*/
00008 #define VREFA_PWM_PIN D3
00009 #define VREFB_PWM_PIN D9
00010 /* Variables -----------------------------------------------------------------*/
00011 /* Initialization parameters of the motor connected to the expansion board. */
00012 l6208_init_t init =
00013 {
00014   1500,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
00015   80,              //Acceleration current torque in % (from 0 to 100)
00016   1500,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
00017   80,              //Deceleration current torque in % (from 0 to 100)
00018   1000,            //Running speed in step/s or (1/16)th step/s for microstep modes
00019   80,              //Running current torque in % (from 0 to 100)
00020   40,              //Holding current torque in % (from 0 to 100)
00021   STEP_MODE_1_16,  //Step mode via enum motorStepMode_t
00022   FAST_DECAY,      //Decay mode via enum motorDecayMode_t
00023   0,               //Dwelling time in ms
00024   FALSE,           //Automatic HIZ STOP
00025   100000           //VREFA and VREFB PWM frequency (Hz)
00026 };
00027 
00028 /* Motor Control Component. */
00029 L6208 *motor;
00030 DigitalIn button (USER_BUTTON);
00031 DigitalIn end(PC_10);
00032 DigitalOut led(LED1);
00033 /* Functions -----------------------------------------------------------------*/
00034 
00035 /**
00036  * @brief  This is an example of user handler for the flag interrupt.
00037  * @param  None
00038  * @retval None
00039  * @note   If needed, implement it, and then attach and enable it:
00040  *           + motor->attach_flag_irq(&my_flag_irq_handler);
00041  *           + motor->enable_flag_irq();
00042  *         To disable it:
00043  *           + motor->DisbleFlagIRQ();
00044  */
00045 void my_flag_irq_handler(void)
00046 {
00047   printf("    WARNING: \"FLAG\" interrupt triggered:\r\n");
00048   motor->disable();
00049   printf("    Motor disabled.\r\n\n");
00050 }
00051 
00052 /**
00053  * @brief  This is an example of error handler.
00054  * @param[in] error Number of the error
00055  * @retval None
00056  * @note   If needed, implement it, and then attach it:
00057  *           + motor->attach_error_handler(&my_error_handler);
00058  */
00059 void my_error_handler(uint16_t error)
00060 {
00061   /* Printing to the console. */
00062   printf("Error %d detected\r\n\n", error);
00063   
00064   /* Infinite loop */
00065   while (true) {
00066   }    
00067 }
00068 
00069 /* Main ----------------------------------------------------------------------*/
00070 
00071 int main()
00072 {
00073 
00074 //----- Initialization 
00075   /* Initializing Motor Control Component. */
00076   motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
00077   if (motor->init(&init) != COMPONENT_OK) {
00078     printf("error init\n\r");
00079     exit(EXIT_FAILURE);
00080   }
00081 
00082   /* Attaching and enabling an interrupt handler. */
00083   motor->attach_flag_irq(&my_flag_irq_handler);
00084   motor->enable_flag_irq();
00085     
00086   /* Attaching an error handler */
00087   motor->attach_error_handler(&my_error_handler);
00088  
00089   motor->set_step_mode(StepperMotor::STEP_MODE_1_8);
00090 
00091   /* Set speed, acceleration and deceleration to scale with microstep mode */
00092  //motor->set_max_speed(2000);
00093  //motor->set_acceleration(2400);
00094  //motor->set_deceleration(2400);
00095   motor->set_max_speed(motor->get_max_speed()<<2);
00096   motor->set_acceleration(motor->get_acceleration()<<2);
00097   motor->set_deceleration(motor->get_deceleration()<<2);
00098   printf(" init\n\r");
00099   end.mode(PullUp);
00100   /* Request to go position 9000 (quarter steps) */
00101    motor->run(StepperMotor::BWD);
00102   while(1)
00103     {
00104    //  motor->wait_while_active();
00105      printf("while");
00106       if(end) break;
00107     }
00108   motor->hard_stop(); 
00109   led=1;
00110   
00111   int pos=motor->get_position();
00112   printf("pos:%d\n\r", pos);
00113   motor->go_to(pos+200);
00114   motor->wait_while_active();
00115   led=0;
00116   wait(0.5);
00117 
00118   motor->set_home();
00119   wait(1);
00120 
00121   motor->go_to(1000<<4);
00122   motor->wait_while_active();
00123  
00124   while (true)
00125 {
00126   motor->go_to(500<<4);
00127   motor->wait_while_active();
00128  
00129    motor->go_to(0);
00130    motor->wait_while_active();
00131    while (button)
00132   {
00133   
00134    }
00135 }
00136     /* Waiting while the motor is active. */
00137     motor->wait_while_active();
00138   
00139     wait_ms(5000);  
00140   motor->disable();
00141 }
00142 
00143