Simple application using the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board and the X-NUCLEO-IKS01A1 MEMS Inertial and Environmental Sensors Expansion Board to get a MEMS-based motor control in terms of direction and speed.
Dependencies: X_NUCLEO_IHM01A1 X_NUCLEO_IKS01A1 mbed
Fork of MemsMotorControl by
MEMS-based Motor Control
This application provides an intuitive and natural way for controlling a stepper motor through an accelerometer. It makes use of the STMicroelectronics X-NUCLEO-IKS01A1 MEMS Inertial and Environmental Sensors Expansion Board to get accelerometer values and the X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board to directly control a stepper motor with:
- speed proportional to the angle measured by the MEMS board;
- direction driven by the direction of rotation captured by the MEMS board.
Diff: main.cpp
- Revision:
- 2:1784677a5955
- Parent:
- 0:bd157c8f770c
- Child:
- 3:112d46906bee
--- a/main.cpp Fri Oct 16 14:32:10 2015 +0000 +++ b/main.cpp Tue Oct 27 15:15:47 2015 +0000 @@ -54,8 +54,11 @@ /* Definitions ---------------------------------------------------------------*/ -/* Absolute value of the rotation threshold. */ -#define ROTATION_TH 50 +/* Absolute value of the threshold on the Y axis acceleration. */ +#define ACCELERATION_Y_TH 30 + +/* Rotation gain. */ +#define ROTATION_SPEED_GAIN 10 /* Variables -----------------------------------------------------------------*/ @@ -91,8 +94,9 @@ /* Set defaults. */ motor->SetAcceleration(10000); motor->SetDeceleration(10000); - motor->SetMinSpeed(10); + motor->SetMinSpeed(100); int status = 0; + int speed = 0; /* Printing to the console. */ printf("Motor Control with MEMS\r\n\n"); @@ -106,9 +110,11 @@ /* Motor Control. */ int module = abs(accelerometer_data[1]); - int sign = accelerometer_data[1] < 0 ? -1 : 1; - if (module > ROTATION_TH) + if (module > ACCELERATION_Y_TH) { + int sign = accelerometer_data[1] < 0 ? -1 : 1; + speed = module * ROTATION_SPEED_GAIN; + /* Requesting to run. */ if (status != sign) { @@ -117,7 +123,7 @@ } /* Setting Speed. */ - motor->SetMaxSpeed(module * 10); + motor->SetMaxSpeed(speed); /* Printing to the console. */ printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->GetCurrentSpeed()); @@ -127,12 +133,13 @@ /* Requesting to stop. */ motor->SoftStop(); status = 0; + speed = 0; /* Printing to the console. */ printf("Stop.\r\n"); } /* Waiting. */ - wait_ms(500); + wait_ms(50); } }