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 ST

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.
Revision:
28:9618700cd2f6
Parent:
21:08ddc48353ac
--- a/main.cpp	Fri Oct 28 13:32:48 2016 +0000
+++ b/main.cpp	Tue Mar 14 15:05:42 2017 +0000
@@ -6,7 +6,7 @@
  * @date    October 16th, 2015
  * @brief   mbed vertical application using the STMicroelectronics
  *          X-NUCLEO-IHM01A1 Motor Control Expansion Board and the
- *          X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensors Expansion
+ *          X-NUCLEO-IKS01A2 MEMS Inertial & Environmental Sensors Expansion
  *          Board to get a MEMS-based motor control (direction and speed).
  ******************************************************************************
  * @attention
@@ -48,8 +48,8 @@
 #include "DevSPI.h"
 
 /* Components and expansion boards specific header files. */
-#include "x_nucleo_iks01a1.h"
-#include "l6474_class.h"
+#include "XNucleoIKS01A2.h"
+#include "L6474.h"
 
 
 /* Definitions ---------------------------------------------------------------*/
@@ -64,7 +64,7 @@
 /* Variables -----------------------------------------------------------------*/
 
 /* MEMS Expansion Board. */
-X_NUCLEO_IKS01A1 *x_nucleo_iks01a1;
+XNucleoIKS01A2 *sensors;
 
 /* Motor Control Component. */
 L6474 *motor;
@@ -83,21 +83,22 @@
     DevSPI dev_spi(D11, D12, D13);
 
     /* Initializing MEMS Expansion Board. */
-    x_nucleo_iks01a1 = X_NUCLEO_IKS01A1::Instance(&dev_i2c);
+    sensors = XNucleoIKS01A2::instance(&dev_i2c);
 
     /* Retrieving the accelerometer. */
-    MotionSensor *accelerometer = x_nucleo_iks01a1->GetAccelerometer();
-    int acceleration_axis = x_nucleo_iks01a1->gyro_lsm6ds3 == NULL ? 0 : 1;
+    LSM303AGRAccSensor *accelerometer = sensors->accelerometer;
+    accelerometer->enable();
+    int acceleration_axis = 1;
 
     /* Initializing Motor Control Component. */
     motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
-    if (motor->Init() != COMPONENT_OK)
+    if (motor->init() != COMPONENT_OK)
         exit(EXIT_FAILURE);
 
     /* Set defaults. */
-    motor->SetAcceleration(10000);
-    motor->SetDeceleration(10000);
-    motor->SetMinSpeed(100);
+    motor->set_acceleration(10000);
+    motor->set_deceleration(10000);
+    motor->set_min_speed(100);
     int status = 0;
     int speed = 0;
 
@@ -112,7 +113,7 @@
     {
         /* Reading Accelerometer. */
         int accelerometer_data[3];
-        accelerometer->Get_X_Axes(accelerometer_data);
+        accelerometer->get_x_axes(accelerometer_data);
 
         /* Motor Control. */
         int module = abs(accelerometer_data[acceleration_axis]);
@@ -120,24 +121,24 @@
         {
             int sign = accelerometer_data[acceleration_axis] < 0 ? -1 : 1;
             speed = module * ROTATION_SPEED_GAIN;
-            
+
             /* Requesting to run. */
             if (status != sign)
             {
-                motor->Run(sign == -1 ? StepperMotor::BWD : StepperMotor::FWD);
+                motor->run(sign == -1 ? StepperMotor::BWD : StepperMotor::FWD);
                 status = sign;
             }
 
             /* Setting Speed. */
-            motor->SetMaxSpeed(speed);
+            motor->set_max_speed(speed);
 
             /* Printing to the console. */
-            printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->GetSpeed());
+            printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->get_speed());
         }
         else if (status != 0)
         {
             /* Requesting to stop. */
-            motor->SoftStop();
+            motor->soft_stop();
             status = 0;
             speed = 0;