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.
Committer:
Davidroid
Date:
Wed Nov 18 19:18:00 2015 +0000
Revision:
5:d88c9c8df082
Parent:
4:778a7effb013
Child:
6:b09e55633002
+ Program updated to reflect the modifications to the "StepperMotor" abstract class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:bd157c8f770c 1 /**
Davidroid 0:bd157c8f770c 2 ******************************************************************************
Davidroid 0:bd157c8f770c 3 * @file main.cpp
Davidroid 0:bd157c8f770c 4 * @author Davide Aliprandi / AST
Davidroid 0:bd157c8f770c 5 * @version V1.0.0
Davidroid 0:bd157c8f770c 6 * @date October 16th, 2015
Davidroid 0:bd157c8f770c 7 * @brief mbed vertical application using the STMicrolectronics
Davidroid 0:bd157c8f770c 8 * X-NUCLEO-IHM01A1 Motor Control Expansion Board and the
Davidroid 0:bd157c8f770c 9 * X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensors Expansion
Davidroid 0:bd157c8f770c 10 * Board to get a MEMS-based motor control (direction and speed).
Davidroid 0:bd157c8f770c 11 ******************************************************************************
Davidroid 0:bd157c8f770c 12 * @attention
Davidroid 0:bd157c8f770c 13 *
Davidroid 0:bd157c8f770c 14 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:bd157c8f770c 15 *
Davidroid 0:bd157c8f770c 16 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:bd157c8f770c 17 * are permitted provided that the following conditions are met:
Davidroid 0:bd157c8f770c 18 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:bd157c8f770c 19 * this list of conditions and the following disclaimer.
Davidroid 0:bd157c8f770c 20 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:bd157c8f770c 21 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:bd157c8f770c 22 * and/or other materials provided with the distribution.
Davidroid 0:bd157c8f770c 23 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:bd157c8f770c 24 * may be used to endorse or promote products derived from this software
Davidroid 0:bd157c8f770c 25 * without specific prior written permission.
Davidroid 0:bd157c8f770c 26 *
Davidroid 0:bd157c8f770c 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:bd157c8f770c 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:bd157c8f770c 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:bd157c8f770c 30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:bd157c8f770c 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:bd157c8f770c 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:bd157c8f770c 33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:bd157c8f770c 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:bd157c8f770c 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:bd157c8f770c 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:bd157c8f770c 37 *
Davidroid 0:bd157c8f770c 38 ******************************************************************************
Davidroid 0:bd157c8f770c 39 */
Davidroid 0:bd157c8f770c 40
Davidroid 0:bd157c8f770c 41
Davidroid 0:bd157c8f770c 42 /* Includes ------------------------------------------------------------------*/
Davidroid 0:bd157c8f770c 43
Davidroid 0:bd157c8f770c 44 /* mbed specific header files. */
Davidroid 0:bd157c8f770c 45 #include "mbed.h"
Davidroid 0:bd157c8f770c 46
Davidroid 0:bd157c8f770c 47 /* Helper header files. */
Davidroid 0:bd157c8f770c 48 #include "DevSPI.h"
Davidroid 0:bd157c8f770c 49
Davidroid 0:bd157c8f770c 50 /* Components and expansion boards specific header files. */
Davidroid 0:bd157c8f770c 51 #include "x_nucleo_iks01a1.h"
Davidroid 0:bd157c8f770c 52 #include "l6474_class.h"
Davidroid 0:bd157c8f770c 53
Davidroid 0:bd157c8f770c 54
Davidroid 0:bd157c8f770c 55 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:bd157c8f770c 56
Davidroid 2:1784677a5955 57 /* Absolute value of the threshold on the Y axis acceleration. */
Davidroid 4:778a7effb013 58 #define ACCELERATION_TH 50
Davidroid 2:1784677a5955 59
Davidroid 2:1784677a5955 60 /* Rotation gain. */
Davidroid 5:d88c9c8df082 61 #define ROTATION_SPEED_GAIN 20
Davidroid 0:bd157c8f770c 62
Davidroid 0:bd157c8f770c 63
Davidroid 0:bd157c8f770c 64 /* Variables -----------------------------------------------------------------*/
Davidroid 0:bd157c8f770c 65
Davidroid 0:bd157c8f770c 66 /* MEMS Expansion Board. */
Davidroid 0:bd157c8f770c 67 X_NUCLEO_IKS01A1 *x_nucleo_iks01a1;
Davidroid 0:bd157c8f770c 68
Davidroid 0:bd157c8f770c 69 /* Motor Control Component. */
Davidroid 0:bd157c8f770c 70 L6474 *motor;
Davidroid 0:bd157c8f770c 71
Davidroid 0:bd157c8f770c 72
Davidroid 0:bd157c8f770c 73 /* Main ----------------------------------------------------------------------*/
Davidroid 0:bd157c8f770c 74
Davidroid 0:bd157c8f770c 75 int main()
Davidroid 0:bd157c8f770c 76 {
Davidroid 0:bd157c8f770c 77 /* Initializing I2C bus. */
Davidroid 0:bd157c8f770c 78 DevI2C dev_i2c(D14, D15);
Davidroid 0:bd157c8f770c 79
Davidroid 0:bd157c8f770c 80 /* Initializing SPI bus. */
Davidroid 0:bd157c8f770c 81 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:bd157c8f770c 82
Davidroid 0:bd157c8f770c 83 /* Initializing MEMS Expansion Board. */
Davidroid 0:bd157c8f770c 84 x_nucleo_iks01a1 = X_NUCLEO_IKS01A1::Instance(&dev_i2c);
Davidroid 0:bd157c8f770c 85
Davidroid 0:bd157c8f770c 86 /* Retrieving the accelerometer. */
Davidroid 0:bd157c8f770c 87 MotionSensor *accelerometer = x_nucleo_iks01a1->GetAccelerometer();
Davidroid 3:112d46906bee 88 int acceleration_axis = x_nucleo_iks01a1->gyro_lsm6ds3 == NULL ? 0 : 1;
Davidroid 0:bd157c8f770c 89
Davidroid 0:bd157c8f770c 90 /* Initializing Motor Control Component. */
Davidroid 0:bd157c8f770c 91 motor = new L6474(D8, D7, D9, D10, dev_spi);
Davidroid 0:bd157c8f770c 92 if (motor->Init(NULL) != COMPONENT_OK)
Davidroid 0:bd157c8f770c 93 return false;
Davidroid 0:bd157c8f770c 94
Davidroid 0:bd157c8f770c 95 /* Set defaults. */
Davidroid 0:bd157c8f770c 96 motor->SetAcceleration(10000);
Davidroid 0:bd157c8f770c 97 motor->SetDeceleration(10000);
Davidroid 2:1784677a5955 98 motor->SetMinSpeed(100);
Davidroid 0:bd157c8f770c 99 int status = 0;
Davidroid 2:1784677a5955 100 int speed = 0;
Davidroid 0:bd157c8f770c 101
Davidroid 0:bd157c8f770c 102 /* Printing to the console. */
Davidroid 0:bd157c8f770c 103 printf("Motor Control with MEMS\r\n\n");
Davidroid 0:bd157c8f770c 104
Davidroid 0:bd157c8f770c 105 /* Main Loop. */
Davidroid 0:bd157c8f770c 106 while(true)
Davidroid 0:bd157c8f770c 107 {
Davidroid 0:bd157c8f770c 108 /* Reading Accelerometer. */
Davidroid 0:bd157c8f770c 109 int accelerometer_data[3];
Davidroid 0:bd157c8f770c 110 accelerometer->Get_X_Axes(accelerometer_data);
Davidroid 0:bd157c8f770c 111
Davidroid 0:bd157c8f770c 112 /* Motor Control. */
Davidroid 3:112d46906bee 113 int module = abs(accelerometer_data[acceleration_axis]);
Davidroid 3:112d46906bee 114 if (module > ACCELERATION_TH)
Davidroid 0:bd157c8f770c 115 {
Davidroid 3:112d46906bee 116 int sign = accelerometer_data[acceleration_axis] < 0 ? -1 : 1;
Davidroid 2:1784677a5955 117 speed = module * ROTATION_SPEED_GAIN;
Davidroid 2:1784677a5955 118
Davidroid 0:bd157c8f770c 119 /* Requesting to run. */
Davidroid 0:bd157c8f770c 120 if (status != sign)
Davidroid 0:bd157c8f770c 121 {
Davidroid 5:d88c9c8df082 122 motor->Run(sign == -1 ? StepperMotor::BWD : StepperMotor::FWD);
Davidroid 0:bd157c8f770c 123 status = sign;
Davidroid 0:bd157c8f770c 124 }
Davidroid 0:bd157c8f770c 125
Davidroid 0:bd157c8f770c 126 /* Setting Speed. */
Davidroid 2:1784677a5955 127 motor->SetMaxSpeed(speed);
Davidroid 0:bd157c8f770c 128
Davidroid 0:bd157c8f770c 129 /* Printing to the console. */
Davidroid 4:778a7effb013 130 printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->GetSpeed());
Davidroid 0:bd157c8f770c 131 }
Davidroid 0:bd157c8f770c 132 else if (status != 0)
Davidroid 0:bd157c8f770c 133 {
Davidroid 0:bd157c8f770c 134 /* Requesting to stop. */
Davidroid 0:bd157c8f770c 135 motor->SoftStop();
Davidroid 0:bd157c8f770c 136 status = 0;
Davidroid 2:1784677a5955 137 speed = 0;
Davidroid 0:bd157c8f770c 138
Davidroid 0:bd157c8f770c 139 /* Printing to the console. */
Davidroid 0:bd157c8f770c 140 printf("Stop.\r\n");
Davidroid 0:bd157c8f770c 141 }
Davidroid 0:bd157c8f770c 142
Davidroid 0:bd157c8f770c 143 /* Waiting. */
Davidroid 2:1784677a5955 144 wait_ms(50);
Davidroid 0:bd157c8f770c 145 }
Davidroid 0:bd157c8f770c 146 }