Simple code to run steps

Dependencies:   X-NUCLEO-IHM05A1 mbed

Fork of HelloWorld_IHM05A1 by ST

Revision:
9:4b2736fbecdf
Parent:
8:baedd289a8cc
Child:
10:93b6d1b02071
--- a/main.cpp	Fri Jul 28 13:07:13 2017 +0000
+++ b/main.cpp	Mon Jan 01 11:02:36 2018 +0000
@@ -44,7 +44,11 @@
 /* Component specific header files. */
 #include "L6208.h"
 
-
+#define DELAY_STEPS 1000
+#define STEPS_PER 1 
+#define NUM_STEPS 400
+#define NUM_ROTATION 1
+int32_t steps=0;
 /* Definitions ---------------------------------------------------------------*/
 #ifdef TARGET_NUCLEO_F334R8
 #define VREFA_PWM_PIN D11
@@ -62,14 +66,14 @@
 /* Initialization parameters of the motor connected to the expansion board. */
 l6208_init_t init =
 {
-  1500,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
-  40,              //Acceleration current torque in % (from 0 to 100)
-  1500,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
+  400,            //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
+  30,              //Acceleration current torque in % (from 0 to 100)
+  400,            //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
   30,              //Deceleration current torque in % (from 0 to 100)
-  1500,            //Running speed in step/s or (1/16)th step/s for microstep modes
-  50,              //Running current torque in % (from 0 to 100)
+  20,            //Running speed in step/s or (1/16)th step/s for microstep modes
+  30,              //Running current torque in % (from 0 to 100)
   20,              //Holding current torque in % (from 0 to 100)
-  STEP_MODE_1_16,  //Step mode via enum motorStepMode_t
+  STEP_MODE_HALF,  //Step mode via enum motorStepMode_t
   FAST_DECAY,      //Decay mode via enum motorDecayMode_t
   0,               //Dwelling time in ms
   FALSE,           //Automatic HIZ STOP
@@ -142,171 +146,52 @@
   motor->attach_error_handler(&my_error_handler);
 
   /* Printing to the console. */
-  printf("Motor Control Application Example for 1 Motor\r\n");
+  printf("Run motor at 1/16 micro stepping");
 
-//----- run the motor BACKWARD
-  printf("--> Running the motor backward.\r\n");
-  motor->run(StepperMotor::BWD);
-  
-  while (motor->get_status()!=STEADY) {
-    /* Print reached speed to the console in step/s or microsteps/s */
-    printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
-    wait_ms(50);    
-  }
-  printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
-     
-  /* Wait for 1 second */  
-  wait_ms(1000);
-  
-//----- Decrease speed while running to one quarter of the previous speed
-  motor->set_max_speed(motor->get_speed()>>2);
-  
-  /* Wait until the motor starts decelerating */
-  while (motor->get_status()==STEADY);
-  /* Wait and print speed while the motor is not steady running */
-  while (motor->get_status()!=STEADY) {
-    /* Print reached speed to the console in step/s or microsteps/s */
-    printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
-    wait_ms(50);    
-  }
-  printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());  
-
-  /* Wait for 5 seconds */  
-  wait_ms(5000);
-  
-//----- Soft stop required while running
-  printf("--> Soft stop requested.\r\n");
-  motor->soft_stop();
-  
-  /* Wait for the motor of device ends moving */  
-  motor->wait_while_active();
-
-  /* Wait for 2 seconds */
-  wait_ms(2000);
-
-//----- Change step mode to full step mode
-  motor->set_step_mode(StepperMotor::STEP_MODE_FULL);
-  printf("    Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode());
-  
-  /* Get current position of device and print to the console */
-  printf("    Position: %d.\r\n", motor->get_position());
-  
-  /* Set speed, acceleration and deceleration to scale with normal mode */
-  motor->set_max_speed(init.maxSpeedSps>>4);
-  motor->set_acceleration(motor->get_acceleration()>>4);
-  motor->set_deceleration(motor->get_deceleration()>>4);
   /* Print parameters to the console */  
   printf("    Motor Max Speed: %d step/s.\r\n", motor->get_max_speed());
   printf("    Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());
   printf("    Motor Acceleration: %d step/s.\r\n", motor->get_acceleration());
   printf("    Motor Deceleration: %d step/s.\r\n", motor->get_deceleration());
   
-//----- move of 200 steps in the FW direction
-  printf("--> Moving forward 200 steps.\r\n");
-  motor->move(StepperMotor::FWD, 200);
-  
-  /* Waiting while the motor is active. */
-  motor->wait_while_active();
-  
-  /* Get current position of device and print to the console */
-  printf("    Position: %d.\r\n", motor->get_position());
-  
-  /* Disable the power bridges */
-  motor->disable();
-  
-  /* Check that the power bridges are actually disabled */
-  if (motor->check_status_hw()!=0) {
-    printf("    Motor driver disabled.\r\n");
-  } else {
-    printf("    Failed to disable the motor driver.\r\n");
-  }
-    
-  /* Wait for 2 seconds */  
-  wait_ms(2000);
-  
- //----- Change step mode to 1/4 microstepping mode  
-  motor->set_step_mode(StepperMotor::STEP_MODE_1_4);
+
+//----- Change step mode to full step mode
+  motor->set_step_mode(StepperMotor::STEP_MODE_HALF);
   printf("    Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode());
-  
-  /* Get current position of device and print to the console */
-  printf("    Position: %d.\r\n", motor->get_position());
-
-  /* Set speed, acceleration and deceleration to scale with microstep mode */
-  motor->set_max_speed(motor->get_max_speed()<<4);
-  motor->set_acceleration(motor->get_acceleration()<<4);
-  motor->set_deceleration(motor->get_deceleration()<<4);
-  /* Print parameters to the console */
-  printf("    Motor Max Speed: %d step/s.\r\n", motor->get_max_speed());
-  printf("    Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());
-  printf("    Motor Acceleration: %d step/s.\r\n", motor->get_acceleration());
-  printf("    Motor Deceleration: %d step/s.\r\n", motor->get_deceleration());
-  
-  /* Request to go position 800 (quarter steps) */
-  motor->go_to(800);
-
-  /* Wait for the motor ends moving */
-  motor->wait_while_active();
 
-  /* Get current position of device and print to the console */
-  printf("    Position: %d.\r\n", motor->get_position());
-  
-  /* Wait for 2 seconds */  
-  wait_ms(2000);
-  
-//----- Restore step mode to its initialization value
-  motor->set_step_mode((StepperMotor::step_mode_t)init.stepMode);
-  printf("    Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode());  
-
-  /* Get current position of device and print to the console */
-  printf("    Position: %d.\r\n", motor->get_position());
-  
-//----- Change decay mode
-  motor->set_decay_mode(SLOW_DECAY);
-  printf("    Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode());   
-  
-//----- Go to position -6400
-  printf("--> Go to position -6400 steps.\r\n");
-  motor->go_to(-6400);
-  
-  /* Wait for the motor ends moving */
-  motor->wait_while_active();
-
-  /* Get current position of device and print to the console */
-  printf("    Position: %d.\r\n", motor->get_position());
-  
-  /* Wait for 2 seconds */  
-  wait_ms(2000);
+////----- run the motor BACKWARD
+//  printf("--> Running the motor backward.\r\n");
+//  motor->run(StepperMotor::BWD);
+//  
+//  
+//  // wait untill motor is at const speed
+//  while (motor->get_status()!=STEADY) {
+//    /* Print reached speed to the console in step/s or microsteps/s */
+//    printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
+//    wait_ms(50);    
+//  }
+//  printf("    Reached Speed: %d microstep/s.\r\n", motor->get_speed());
+//  
+//  // run continious   
+//  /* Wait for 1 second */  
+//  wait(60);
+//  
+////----- Soft stop required while running
+//  printf("--> Soft stop requested.\r\n");
+//  motor->soft_stop();
+//  
+//  /* Wait for the motor of device ends moving */  
+//  motor->wait_while_active();
 
-//----- Restore decay mode to its initialization value
-  motor->set_decay_mode(init.decayMode);
-  printf("    Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->get_decay_mode());
-  
-//----- Go Home
-  printf("--> Go to home position.\r\n");
-  motor->go_home();
- 
-  /* Wait for the motor ends moving */
-  motor->wait_while_active();
-  
-  /* Wait for 1 second */
-  wait_ms(1000);
-  
-  /* Infinite Loop. */
-  printf("--> Infinite Loop...\r\n");
-  //while (true) {
-  int i;
-  for (i = 0; i < 2; i++) {
-    /* Request device to go position -3200 */
-    motor->go_to(-3200);
 
-    /* Waiting while the motor is active. */
-    motor->wait_while_active();
-
-    /* Request device to go position 3200 */
-    motor->go_to(3200);
- 
-    /* Waiting while the motor is active. */
-    motor->wait_while_active();
+while (steps<NUM_STEPS*NUM_ROTATION){
+    //----- move of STEPS_PER steps in the FW direction
+    
+    motor->move(StepperMotor::FWD, STEPS_PER);
+    /* Wait for DELAY_STEPS*/
+    wait_ms(DELAY_STEPS);
+    steps=motor->get_position();
+    printf("--> %d steps.\r\n",steps);
   }
 }