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:
Tue Mar 14 15:05:42 2017 +0000
Revision:
28:9618700cd2f6
Parent:
21:08ddc48353ac
Mems Motor Control application example with X_NUCLEO_IHM01A1 and X_NUCLEO_IKS01A2.

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 28:9618700cd2f6 9 * X-NUCLEO-IKS01A2 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 28:9618700cd2f6 51 #include "XNucleoIKS01A2.h"
Davidroid 28:9618700cd2f6 52 #include "L6474.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 28:9618700cd2f6 67 XNucleoIKS01A2 *sensors;
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 28:9618700cd2f6 86 sensors = XNucleoIKS01A2::instance(&dev_i2c);
Davidroid 0:bd157c8f770c 87
Davidroid 0:bd157c8f770c 88 /* Retrieving the accelerometer. */
Davidroid 28:9618700cd2f6 89 LSM303AGRAccSensor *accelerometer = sensors->accelerometer;
Davidroid 28:9618700cd2f6 90 accelerometer->enable();
Davidroid 28:9618700cd2f6 91 int acceleration_axis = 1;
Davidroid 0:bd157c8f770c 92
Davidroid 0:bd157c8f770c 93 /* Initializing Motor Control Component. */
Davidroid 6:b09e55633002 94 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
Davidroid 28:9618700cd2f6 95 if (motor->init() != COMPONENT_OK)
Davidroid 14:3f4dc127ac4b 96 exit(EXIT_FAILURE);
Davidroid 0:bd157c8f770c 97
Davidroid 0:bd157c8f770c 98 /* Set defaults. */
Davidroid 28:9618700cd2f6 99 motor->set_acceleration(10000);
Davidroid 28:9618700cd2f6 100 motor->set_deceleration(10000);
Davidroid 28:9618700cd2f6 101 motor->set_min_speed(100);
Davidroid 0:bd157c8f770c 102 int status = 0;
Davidroid 2:1784677a5955 103 int speed = 0;
Davidroid 0:bd157c8f770c 104
Davidroid 9:218d1ac31590 105
Davidroid 9:218d1ac31590 106 /*----- Infinite Loop. -----*/
Davidroid 9:218d1ac31590 107
Davidroid 0:bd157c8f770c 108 /* Printing to the console. */
Davidroid 0:bd157c8f770c 109 printf("Motor Control with MEMS\r\n\n");
Davidroid 0:bd157c8f770c 110
Davidroid 0:bd157c8f770c 111 /* Main Loop. */
Davidroid 0:bd157c8f770c 112 while(true)
Davidroid 0:bd157c8f770c 113 {
Davidroid 0:bd157c8f770c 114 /* Reading Accelerometer. */
Davidroid 0:bd157c8f770c 115 int accelerometer_data[3];
Davidroid 28:9618700cd2f6 116 accelerometer->get_x_axes(accelerometer_data);
Davidroid 0:bd157c8f770c 117
Davidroid 0:bd157c8f770c 118 /* Motor Control. */
Davidroid 3:112d46906bee 119 int module = abs(accelerometer_data[acceleration_axis]);
Davidroid 3:112d46906bee 120 if (module > ACCELERATION_TH)
Davidroid 0:bd157c8f770c 121 {
Davidroid 3:112d46906bee 122 int sign = accelerometer_data[acceleration_axis] < 0 ? -1 : 1;
Davidroid 2:1784677a5955 123 speed = module * ROTATION_SPEED_GAIN;
Davidroid 28:9618700cd2f6 124
Davidroid 0:bd157c8f770c 125 /* Requesting to run. */
Davidroid 0:bd157c8f770c 126 if (status != sign)
Davidroid 0:bd157c8f770c 127 {
Davidroid 28:9618700cd2f6 128 motor->run(sign == -1 ? StepperMotor::BWD : StepperMotor::FWD);
Davidroid 0:bd157c8f770c 129 status = sign;
Davidroid 0:bd157c8f770c 130 }
Davidroid 0:bd157c8f770c 131
Davidroid 0:bd157c8f770c 132 /* Setting Speed. */
Davidroid 28:9618700cd2f6 133 motor->set_max_speed(speed);
Davidroid 0:bd157c8f770c 134
Davidroid 0:bd157c8f770c 135 /* Printing to the console. */
Davidroid 28:9618700cd2f6 136 printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->get_speed());
Davidroid 0:bd157c8f770c 137 }
Davidroid 0:bd157c8f770c 138 else if (status != 0)
Davidroid 0:bd157c8f770c 139 {
Davidroid 0:bd157c8f770c 140 /* Requesting to stop. */
Davidroid 28:9618700cd2f6 141 motor->soft_stop();
Davidroid 0:bd157c8f770c 142 status = 0;
Davidroid 2:1784677a5955 143 speed = 0;
Davidroid 0:bd157c8f770c 144
Davidroid 0:bd157c8f770c 145 /* Printing to the console. */
Davidroid 0:bd157c8f770c 146 printf("Stop.\r\n");
Davidroid 0:bd157c8f770c 147 }
Davidroid 0:bd157c8f770c 148
Davidroid 0:bd157c8f770c 149 /* Waiting. */
Davidroid 2:1784677a5955 150 wait_ms(50);
Davidroid 0:bd157c8f770c 151 }
Davidroid 0:bd157c8f770c 152 }