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:
Thu Apr 07 16:51:24 2016 +0000
Revision:
21:08ddc48353ac
Parent:
20:4f0729b99bf5
Child:
28:9618700cd2f6
+ Updated with the new version of the library.

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 16:df53cf8dd1b4 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:bd157c8f770c 5 * @version V1.0.0
Davidroid 0:bd157c8f770c 6 * @date October 16th, 2015
Davidroid 16:df53cf8dd1b4 7 * @brief mbed vertical application using the STMicroelectronics
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 9:218d1ac31590 77 /*----- Initialization. -----*/
Davidroid 9:218d1ac31590 78
Davidroid 0:bd157c8f770c 79 /* Initializing I2C bus. */
Davidroid 0:bd157c8f770c 80 DevI2C dev_i2c(D14, D15);
Davidroid 0:bd157c8f770c 81
Davidroid 0:bd157c8f770c 82 /* Initializing SPI bus. */
Davidroid 0:bd157c8f770c 83 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:bd157c8f770c 84
Davidroid 0:bd157c8f770c 85 /* Initializing MEMS Expansion Board. */
Davidroid 0:bd157c8f770c 86 x_nucleo_iks01a1 = X_NUCLEO_IKS01A1::Instance(&dev_i2c);
Davidroid 0:bd157c8f770c 87
Davidroid 0:bd157c8f770c 88 /* Retrieving the accelerometer. */
Davidroid 0:bd157c8f770c 89 MotionSensor *accelerometer = x_nucleo_iks01a1->GetAccelerometer();
Davidroid 3:112d46906bee 90 int acceleration_axis = x_nucleo_iks01a1->gyro_lsm6ds3 == NULL ? 0 : 1;
Davidroid 0:bd157c8f770c 91
Davidroid 0:bd157c8f770c 92 /* Initializing Motor Control Component. */
Davidroid 6:b09e55633002 93 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 14:3f4dc127ac4b 94 if (motor->Init() != COMPONENT_OK)
Davidroid 14:3f4dc127ac4b 95 exit(EXIT_FAILURE);
Davidroid 0:bd157c8f770c 96
Davidroid 0:bd157c8f770c 97 /* Set defaults. */
Davidroid 0:bd157c8f770c 98 motor->SetAcceleration(10000);
Davidroid 0:bd157c8f770c 99 motor->SetDeceleration(10000);
Davidroid 2:1784677a5955 100 motor->SetMinSpeed(100);
Davidroid 0:bd157c8f770c 101 int status = 0;
Davidroid 2:1784677a5955 102 int speed = 0;
Davidroid 0:bd157c8f770c 103
Davidroid 9:218d1ac31590 104
Davidroid 9:218d1ac31590 105 /*----- Infinite Loop. -----*/
Davidroid 9:218d1ac31590 106
Davidroid 0:bd157c8f770c 107 /* Printing to the console. */
Davidroid 0:bd157c8f770c 108 printf("Motor Control with MEMS\r\n\n");
Davidroid 0:bd157c8f770c 109
Davidroid 0:bd157c8f770c 110 /* Main Loop. */
Davidroid 0:bd157c8f770c 111 while(true)
Davidroid 0:bd157c8f770c 112 {
Davidroid 0:bd157c8f770c 113 /* Reading Accelerometer. */
Davidroid 0:bd157c8f770c 114 int accelerometer_data[3];
Davidroid 0:bd157c8f770c 115 accelerometer->Get_X_Axes(accelerometer_data);
Davidroid 0:bd157c8f770c 116
Davidroid 0:bd157c8f770c 117 /* Motor Control. */
Davidroid 3:112d46906bee 118 int module = abs(accelerometer_data[acceleration_axis]);
Davidroid 3:112d46906bee 119 if (module > ACCELERATION_TH)
Davidroid 0:bd157c8f770c 120 {
Davidroid 3:112d46906bee 121 int sign = accelerometer_data[acceleration_axis] < 0 ? -1 : 1;
Davidroid 2:1784677a5955 122 speed = module * ROTATION_SPEED_GAIN;
Davidroid 2:1784677a5955 123
Davidroid 0:bd157c8f770c 124 /* Requesting to run. */
Davidroid 0:bd157c8f770c 125 if (status != sign)
Davidroid 0:bd157c8f770c 126 {
Davidroid 5:d88c9c8df082 127 motor->Run(sign == -1 ? StepperMotor::BWD : StepperMotor::FWD);
Davidroid 0:bd157c8f770c 128 status = sign;
Davidroid 0:bd157c8f770c 129 }
Davidroid 0:bd157c8f770c 130
Davidroid 0:bd157c8f770c 131 /* Setting Speed. */
Davidroid 2:1784677a5955 132 motor->SetMaxSpeed(speed);
Davidroid 0:bd157c8f770c 133
Davidroid 0:bd157c8f770c 134 /* Printing to the console. */
Davidroid 21:08ddc48353ac 135 printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->GetSpeed());
Davidroid 0:bd157c8f770c 136 }
Davidroid 0:bd157c8f770c 137 else if (status != 0)
Davidroid 0:bd157c8f770c 138 {
Davidroid 0:bd157c8f770c 139 /* Requesting to stop. */
Davidroid 0:bd157c8f770c 140 motor->SoftStop();
Davidroid 0:bd157c8f770c 141 status = 0;
Davidroid 2:1784677a5955 142 speed = 0;
Davidroid 0:bd157c8f770c 143
Davidroid 0:bd157c8f770c 144 /* Printing to the console. */
Davidroid 0:bd157c8f770c 145 printf("Stop.\r\n");
Davidroid 0:bd157c8f770c 146 }
Davidroid 0:bd157c8f770c 147
Davidroid 0:bd157c8f770c 148 /* Waiting. */
Davidroid 2:1784677a5955 149 wait_ms(50);
Davidroid 0:bd157c8f770c 150 }
Davidroid 0:bd157c8f770c 151 }