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@0:bd157c8f770c, 2015-10-16 (annotated)
- Committer:
- Davidroid
- Date:
- Fri Oct 16 14:08:33 2015 +0000
- Revision:
- 0:bd157c8f770c
- Child:
- 2:1784677a5955
mbed vertical application using the STMicrolectronics X-NUCLEO-IHM01A1 Motor Control Expansion Board and the X-NUCLEO-IKS01A1 MEMS Inertial and Environmental Sensors Expansion Board to get a MEMS-based motor control (direction and speed).
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 | 0:bd157c8f770c | 57 | /* Absolute value of the rotation threshold. */ |
Davidroid | 0:bd157c8f770c | 58 | #define ROTATION_TH 50 |
Davidroid | 0:bd157c8f770c | 59 | |
Davidroid | 0:bd157c8f770c | 60 | |
Davidroid | 0:bd157c8f770c | 61 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 0:bd157c8f770c | 62 | |
Davidroid | 0:bd157c8f770c | 63 | /* MEMS Expansion Board. */ |
Davidroid | 0:bd157c8f770c | 64 | X_NUCLEO_IKS01A1 *x_nucleo_iks01a1; |
Davidroid | 0:bd157c8f770c | 65 | |
Davidroid | 0:bd157c8f770c | 66 | /* Motor Control Component. */ |
Davidroid | 0:bd157c8f770c | 67 | L6474 *motor; |
Davidroid | 0:bd157c8f770c | 68 | |
Davidroid | 0:bd157c8f770c | 69 | |
Davidroid | 0:bd157c8f770c | 70 | /* Main ----------------------------------------------------------------------*/ |
Davidroid | 0:bd157c8f770c | 71 | |
Davidroid | 0:bd157c8f770c | 72 | int main() |
Davidroid | 0:bd157c8f770c | 73 | { |
Davidroid | 0:bd157c8f770c | 74 | /* Initializing I2C bus. */ |
Davidroid | 0:bd157c8f770c | 75 | DevI2C dev_i2c(D14, D15); |
Davidroid | 0:bd157c8f770c | 76 | |
Davidroid | 0:bd157c8f770c | 77 | /* Initializing SPI bus. */ |
Davidroid | 0:bd157c8f770c | 78 | DevSPI dev_spi(D11, D12, D13); |
Davidroid | 0:bd157c8f770c | 79 | |
Davidroid | 0:bd157c8f770c | 80 | /* Initializing MEMS Expansion Board. */ |
Davidroid | 0:bd157c8f770c | 81 | x_nucleo_iks01a1 = X_NUCLEO_IKS01A1::Instance(&dev_i2c); |
Davidroid | 0:bd157c8f770c | 82 | |
Davidroid | 0:bd157c8f770c | 83 | /* Retrieving the accelerometer. */ |
Davidroid | 0:bd157c8f770c | 84 | MotionSensor *accelerometer = x_nucleo_iks01a1->GetAccelerometer(); |
Davidroid | 0:bd157c8f770c | 85 | |
Davidroid | 0:bd157c8f770c | 86 | /* Initializing Motor Control Component. */ |
Davidroid | 0:bd157c8f770c | 87 | motor = new L6474(D8, D7, D9, D10, dev_spi); |
Davidroid | 0:bd157c8f770c | 88 | if (motor->Init(NULL) != COMPONENT_OK) |
Davidroid | 0:bd157c8f770c | 89 | return false; |
Davidroid | 0:bd157c8f770c | 90 | |
Davidroid | 0:bd157c8f770c | 91 | /* Set defaults. */ |
Davidroid | 0:bd157c8f770c | 92 | motor->SetAcceleration(10000); |
Davidroid | 0:bd157c8f770c | 93 | motor->SetDeceleration(10000); |
Davidroid | 0:bd157c8f770c | 94 | motor->SetMinSpeed(10); |
Davidroid | 0:bd157c8f770c | 95 | int status = 0; |
Davidroid | 0:bd157c8f770c | 96 | |
Davidroid | 0:bd157c8f770c | 97 | /* Printing to the console. */ |
Davidroid | 0:bd157c8f770c | 98 | printf("Motor Control with MEMS\r\n\n"); |
Davidroid | 0:bd157c8f770c | 99 | |
Davidroid | 0:bd157c8f770c | 100 | /* Main Loop. */ |
Davidroid | 0:bd157c8f770c | 101 | while(true) |
Davidroid | 0:bd157c8f770c | 102 | { |
Davidroid | 0:bd157c8f770c | 103 | /* Reading Accelerometer. */ |
Davidroid | 0:bd157c8f770c | 104 | int accelerometer_data[3]; |
Davidroid | 0:bd157c8f770c | 105 | accelerometer->Get_X_Axes(accelerometer_data); |
Davidroid | 0:bd157c8f770c | 106 | |
Davidroid | 0:bd157c8f770c | 107 | /* Motor Control. */ |
Davidroid | 0:bd157c8f770c | 108 | int module = abs(accelerometer_data[1]); |
Davidroid | 0:bd157c8f770c | 109 | int sign = accelerometer_data[1] < 0 ? -1 : 1; |
Davidroid | 0:bd157c8f770c | 110 | if (module > ROTATION_TH) |
Davidroid | 0:bd157c8f770c | 111 | { |
Davidroid | 0:bd157c8f770c | 112 | /* Requesting to run. */ |
Davidroid | 0:bd157c8f770c | 113 | if (status != sign) |
Davidroid | 0:bd157c8f770c | 114 | { |
Davidroid | 0:bd157c8f770c | 115 | motor->Run(sign == -1 ? BACKWARD : FORWARD); |
Davidroid | 0:bd157c8f770c | 116 | status = sign; |
Davidroid | 0:bd157c8f770c | 117 | } |
Davidroid | 0:bd157c8f770c | 118 | |
Davidroid | 0:bd157c8f770c | 119 | /* Setting Speed. */ |
Davidroid | 0:bd157c8f770c | 120 | motor->SetMaxSpeed(module * 10); |
Davidroid | 0:bd157c8f770c | 121 | |
Davidroid | 0:bd157c8f770c | 122 | /* Printing to the console. */ |
Davidroid | 0:bd157c8f770c | 123 | printf("Speed: %c%d\r\n", sign == -1 ? '-' : '+', motor->GetCurrentSpeed()); |
Davidroid | 0:bd157c8f770c | 124 | } |
Davidroid | 0:bd157c8f770c | 125 | else if (status != 0) |
Davidroid | 0:bd157c8f770c | 126 | { |
Davidroid | 0:bd157c8f770c | 127 | /* Requesting to stop. */ |
Davidroid | 0:bd157c8f770c | 128 | motor->SoftStop(); |
Davidroid | 0:bd157c8f770c | 129 | status = 0; |
Davidroid | 0:bd157c8f770c | 130 | |
Davidroid | 0:bd157c8f770c | 131 | /* Printing to the console. */ |
Davidroid | 0:bd157c8f770c | 132 | printf("Stop.\r\n"); |
Davidroid | 0:bd157c8f770c | 133 | } |
Davidroid | 0:bd157c8f770c | 134 | |
Davidroid | 0:bd157c8f770c | 135 | /* Waiting. */ |
Davidroid | 0:bd157c8f770c | 136 | wait_ms(500); |
Davidroid | 0:bd157c8f770c | 137 | } |
Davidroid | 0:bd157c8f770c | 138 | } |