Stepper motor control for KYPHOS rig
Dependencies: mbed X_NUCLEO_IHM03A1 HX711
Diff: main.cpp
- Revision:
- 1:5822a95491d3
- Parent:
- 0:1cb05bc2807c
- Child:
- 2:ad3a480d3376
--- a/main.cpp Tue Feb 11 16:02:22 2020 +0000
+++ b/main.cpp Tue Feb 11 16:59:03 2020 +0000
@@ -1,4 +1,6 @@
#include "mbed.h"
+#include "DevSPI.h"
+
#include "PowerStep01.h"
powerstep01_init_u_t init = {
@@ -6,7 +8,7 @@
.cm.cp.cmVmSelection = POWERSTEP01_CM_VM_CURRENT, // enum powerstep01_CmVm_t
582, // Acceleration rate in step/s2, range 14.55 to 59590 steps/s^2
582, // Deceleration rate in step/s2, range 14.55 to 59590 steps/s^2
- 488, // Maximum speed in step/s, range 15.25 to 15610 steps/s
+ 200, // Maximum speed in step/s, range 15.25 to 15610 steps/s
0, // Minimum speed in step/s, range 0 to 976.3 steps/s
POWERSTEP01_LSPD_OPT_OFF, // Low speed optimization bit, enum powerstep01_LspdOpt_t
244.16, // Full step speed in step/s, range 7.63 to 15625 steps/s
@@ -47,7 +49,7 @@
POWERSTEP01_CONFIG_PRED_DISABLE // Predictive current enabling , enum powerstep01_ConfigPredEn_t
};
-/* Motor Control Component. */
+DigitalIn button(USER_BUTTON);
PowerStep01 *motor;
void flag_irq_handler(void)
@@ -97,22 +99,16 @@
*/
void error_handler(uint16_t error)
{
- /* Printing to the console. */
printf("Error %d detected\r\n\n", error);
-
- /* Infinite loop */
- while (true) {
- }
+ while (true);
}
/* Main ----------------------------------------------------------------------*/
int main()
{
- /* Printing to the console. */
printf("STARTING MAIN PROGRAM\r\n");
-//----- Initialization
/* Initializing SPI bus. */
DevSPI dev_spi(D11, D12, D13);
@@ -129,23 +125,22 @@
/* Attaching an error handler */
motor->attach_error_handler(&error_handler);
- /* Printing to the console. */
- printf("Motor Control Application Example for 1 Motor\r\n");
+ printf("Press button to start motor control.\r\n");
+ while(button);
-//----- move of 16000 steps in the FW direction
printf("--> Moving forward 480 steps.\r\n");
- motor->move(StepperMotor::FWD, 480);
+ motor->move(StepperMotor::BWD, 10*16*48);
/* Waiting while the motor is active. */
motor->wait_while_active();
/* Wait for 2 seconds */
- ThisThread::sleep_for(2*1000);
+ wait(2);
//----- Go to position -480
printf("--> Go to position -480 steps.\r\n");
/* Request device to go to position -480 */
- motor->go_to(-480);
+ motor->go_to(10*16*48);
/* Wait for the motor ends moving */
motor->wait_while_active();
@@ -154,9 +149,9 @@
printf(" Position: %d.\r\n", motor->get_position());
/* Wait for 2 seconds */
- ThisThread::sleep_for(2*1000);
+ wait(2);
-//----- Go Home
+ //----- Go Home
/* Printing to the console. */
printf("--> Go to home position.\r\n");
@@ -166,21 +161,21 @@
/* Wait for the motor ends moving */
motor->wait_while_active();
- /* Wait for 2 seconds */
- ThisThread::sleep_for(2*1000);
-
-//----- run the motor BACKWARD at 120 step/s
- printf("--> run the motor backward at 120 step/s.\r\n");
- motor->run(StepperMotor::BWD, 120);
+// /* Wait for 2 seconds */
+// wait(2*1000);
+//
+// //----- run the motor BACKWARD at 120 step/s
+// printf("--> run the motor backward at 120 step/s.\r\n");
+// motor->run(StepperMotor::BWD, 120);
+//
+// //----- Get parameter example
+// /* Wait for device reaches the targeted speed */
+// while ((motor->read_status_register() & POWERSTEP01_STATUS_MOT_STATUS) != POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD);
+//
+// /* Record the reached speed in step/s and print to the console */
+// printf(" Reached Speed: %f step/s.\r\n", motor->get_analog_value(POWERSTEP01_SPEED));
-//----- Get parameter example
- /* Wait for device reaches the targeted speed */
- while ((motor->read_status_register() & POWERSTEP01_STATUS_MOT_STATUS) != POWERSTEP01_STATUS_MOT_STATUS_CONST_SPD);
-
- /* Record the reached speed in step/s and print to the console */
- printf(" Reached Speed: %f step/s.\r\n", motor->get_analog_value(POWERSTEP01_SPEED));
-
-//----- Soft stopped required while running
+ //----- Soft stopped required while running
printf("--> Soft stop requested.\r\n");
/* Request a soft stop of device and keep the power bridges enabled */