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
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.
main.cpp
- Committer:
- Davidroid
- Date:
- 2015-10-16
- Revision:
- 0:bd157c8f770c
- Child:
- 2:1784677a5955
File content as of revision 0:bd157c8f770c:
/** ****************************************************************************** * @file main.cpp * @author Davide Aliprandi / AST * @version V1.0.0 * @date October 16th, 2015 * @brief mbed vertical application using the STMicrolectronics * X-NUCLEO-IHM01A1 Motor Control Expansion Board and the * X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensors Expansion * Board to get a MEMS-based motor control (direction and speed). ****************************************************************************** * @attention * * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ /* mbed specific header files. */ #include "mbed.h" /* Helper header files. */ #include "DevSPI.h" /* Components and expansion boards specific header files. */ #include "x_nucleo_iks01a1.h" #include "l6474_class.h" /* Definitions ---------------------------------------------------------------*/ /* Absolute value of the rotation threshold. */ #define ROTATION_TH 50 /* Variables -----------------------------------------------------------------*/ /* MEMS Expansion Board. */ X_NUCLEO_IKS01A1 *x_nucleo_iks01a1; /* Motor Control Component. */ L6474 *motor; /* Main ----------------------------------------------------------------------*/ int main() { /* Initializing I2C bus. */ DevI2C dev_i2c(D14, D15); /* Initializing SPI bus. */ DevSPI dev_spi(D11, D12, D13); /* Initializing MEMS Expansion Board. */ x_nucleo_iks01a1 = X_NUCLEO_IKS01A1::Instance(&dev_i2c); /* Retrieving the accelerometer. */ MotionSensor *accelerometer = x_nucleo_iks01a1->GetAccelerometer(); /* Initializing Motor Control Component. */ motor = new L6474(D8, D7, D9, D10, dev_spi); if (motor->Init(NULL) != COMPONENT_OK) return false; /* Set defaults. */ motor->SetAcceleration(10000); motor->SetDeceleration(10000); motor->SetMinSpeed(10); int status = 0; /* Printing to the console. */ printf("Motor Control with MEMS\r\n\n"); /* Main Loop. */ while(true) { /* Reading Accelerometer. */ int accelerometer_data[3]; accelerometer->Get_X_Axes(accelerometer_data); /* Motor Control. */ int module = abs(accelerometer_data[1]); int sign = accelerometer_data[1] < 0 ? -1 : 1; if (module > ROTATION_TH) { /* Requesting to run. */ if (status != sign) { motor->Run(sign == -1 ? BACKWARD : FORWARD); status = sign; } /* Setting Speed. */ motor->SetMaxSpeed(module * 10); /* Printing to the console. */ printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->GetCurrentSpeed()); } else if (status != 0) { /* Requesting to stop. */ motor->SoftStop(); status = 0; /* Printing to the console. */ printf("Stop.\r\n"); } /* Waiting. */ wait_ms(500); } }