Mems Motor Control application example with X_NUCLEO_IHM01A1 and X_NUCLEO_IKS01A2 expansion boards.
Dependencies: X_NUCLEO_IHM01A1 X_NUCLEO_IKS01A2 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-IKS01A2 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:
- 3:112d46906bee
- Parent:
- 2:1784677a5955
- Child:
- 4:778a7effb013
--- a/main.cpp Tue Oct 27 15:15:47 2015 +0000 +++ b/main.cpp Wed Oct 28 12:46:15 2015 +0000 @@ -55,7 +55,7 @@ /* Definitions ---------------------------------------------------------------*/ /* Absolute value of the threshold on the Y axis acceleration. */ -#define ACCELERATION_Y_TH 30 +#define ACCELERATION_TH 30 /* Rotation gain. */ #define ROTATION_SPEED_GAIN 10 @@ -85,6 +85,7 @@ /* Retrieving the accelerometer. */ MotionSensor *accelerometer = x_nucleo_iks01a1->GetAccelerometer(); + int acceleration_axis = x_nucleo_iks01a1->gyro_lsm6ds3 == NULL ? 0 : 1; /* Initializing Motor Control Component. */ motor = new L6474(D8, D7, D9, D10, dev_spi); @@ -109,10 +110,10 @@ accelerometer->Get_X_Axes(accelerometer_data); /* Motor Control. */ - int module = abs(accelerometer_data[1]); - if (module > ACCELERATION_Y_TH) + int module = abs(accelerometer_data[acceleration_axis]); + if (module > ACCELERATION_TH) { - int sign = accelerometer_data[1] < 0 ? -1 : 1; + int sign = accelerometer_data[acceleration_axis] < 0 ? -1 : 1; speed = module * ROTATION_SPEED_GAIN; /* Requesting to run. */