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.

Files at this revision

API Documentation at this revision

Comitter:
Davidroid
Date:
Tue Mar 14 15:05:42 2017 +0000
Parent:
27:93a3bd108045
Commit message:
Mems Motor Control application example with X_NUCLEO_IHM01A1 and X_NUCLEO_IKS01A2.

Changed in this revision

X_NUCLEO_IHM01A1.lib Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_IKS01A1.lib Show diff for this revision Revisions of this file
X_NUCLEO_IKS01A2.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/X_NUCLEO_IHM01A1.lib	Fri Oct 28 13:32:48 2016 +0000
+++ b/X_NUCLEO_IHM01A1.lib	Tue Mar 14 15:05:42 2017 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ST/code/X_NUCLEO_IHM01A1/#c4d0fee5ce75
+http://developer.mbed.org/teams/ST/code/X_NUCLEO_IHM01A1/#974ca699c792
--- a/X_NUCLEO_IKS01A1.lib	Fri Oct 28 13:32:48 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/ST/code/X_NUCLEO_IKS01A1/#bd74c33ecbbd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_IKS01A2.lib	Tue Mar 14 15:05:42 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ST/code/X_NUCLEO_IKS01A2/#7ced1e5f49dc
--- 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;
 
--- a/mbed.bld	Fri Oct 28 13:32:48 2016 +0000
+++ b/mbed.bld	Tue Mar 14 15:05:42 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90
\ No newline at end of file