a stepper motor view as a serial midi device, use an interface such ttymidi to generate a serial to 115200 baudrate midi instrument
Dependencies: MIDI mbed X-NUCLEO-IHM05A1
main.cpp@0:36aa6787d4f9, 2016-04-27 (annotated)
- Committer:
- nucleosam
- Date:
- Wed Apr 27 16:32:15 2016 +0000
- Revision:
- 0:36aa6787d4f9
- Child:
- 2:c5f455885df7
Initial version tested on NUCLEO-F401RE and NUCLEO-F334R8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucleosam | 0:36aa6787d4f9 | 1 | /** |
nucleosam | 0:36aa6787d4f9 | 2 | ****************************************************************************** |
nucleosam | 0:36aa6787d4f9 | 3 | * @file main.cpp |
nucleosam | 0:36aa6787d4f9 | 4 | * @author IPC Rennes |
nucleosam | 0:36aa6787d4f9 | 5 | * @version V1.0.0 |
nucleosam | 0:36aa6787d4f9 | 6 | * @date April 13th, 2016 |
nucleosam | 0:36aa6787d4f9 | 7 | * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM03A1 |
nucleosam | 0:36aa6787d4f9 | 8 | * Motor Control Expansion Board: control of 1 motor. |
nucleosam | 0:36aa6787d4f9 | 9 | ****************************************************************************** |
nucleosam | 0:36aa6787d4f9 | 10 | * @attention |
nucleosam | 0:36aa6787d4f9 | 11 | * |
nucleosam | 0:36aa6787d4f9 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nucleosam | 0:36aa6787d4f9 | 13 | * |
nucleosam | 0:36aa6787d4f9 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
nucleosam | 0:36aa6787d4f9 | 15 | * are permitted provided that the following conditions are met: |
nucleosam | 0:36aa6787d4f9 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
nucleosam | 0:36aa6787d4f9 | 17 | * this list of conditions and the following disclaimer. |
nucleosam | 0:36aa6787d4f9 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nucleosam | 0:36aa6787d4f9 | 19 | * this list of conditions and the following disclaimer in the documentation |
nucleosam | 0:36aa6787d4f9 | 20 | * and/or other materials provided with the distribution. |
nucleosam | 0:36aa6787d4f9 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nucleosam | 0:36aa6787d4f9 | 22 | * may be used to endorse or promote products derived from this software |
nucleosam | 0:36aa6787d4f9 | 23 | * without specific prior written permission. |
nucleosam | 0:36aa6787d4f9 | 24 | * |
nucleosam | 0:36aa6787d4f9 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nucleosam | 0:36aa6787d4f9 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nucleosam | 0:36aa6787d4f9 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nucleosam | 0:36aa6787d4f9 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nucleosam | 0:36aa6787d4f9 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nucleosam | 0:36aa6787d4f9 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nucleosam | 0:36aa6787d4f9 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nucleosam | 0:36aa6787d4f9 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nucleosam | 0:36aa6787d4f9 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nucleosam | 0:36aa6787d4f9 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nucleosam | 0:36aa6787d4f9 | 35 | * |
nucleosam | 0:36aa6787d4f9 | 36 | ****************************************************************************** |
nucleosam | 0:36aa6787d4f9 | 37 | */ |
nucleosam | 0:36aa6787d4f9 | 38 | |
nucleosam | 0:36aa6787d4f9 | 39 | /* Includes ------------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 40 | |
nucleosam | 0:36aa6787d4f9 | 41 | /* mbed specific header files. */ |
nucleosam | 0:36aa6787d4f9 | 42 | #include "mbed.h" |
nucleosam | 0:36aa6787d4f9 | 43 | |
nucleosam | 0:36aa6787d4f9 | 44 | /* Helper header files. */ |
nucleosam | 0:36aa6787d4f9 | 45 | #include "DevSPI.h" |
nucleosam | 0:36aa6787d4f9 | 46 | |
nucleosam | 0:36aa6787d4f9 | 47 | /* Component specific header files. */ |
nucleosam | 0:36aa6787d4f9 | 48 | #include "l6208_class.h" |
nucleosam | 0:36aa6787d4f9 | 49 | |
nucleosam | 0:36aa6787d4f9 | 50 | |
nucleosam | 0:36aa6787d4f9 | 51 | /* Definitions ---------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 52 | #ifdef TARGET_NUCLEO_F334R8 |
nucleosam | 0:36aa6787d4f9 | 53 | #define VREFA_PWM_PIN D11 |
nucleosam | 0:36aa6787d4f9 | 54 | #else |
nucleosam | 0:36aa6787d4f9 | 55 | #define VREFA_PWM_PIN D3 |
nucleosam | 0:36aa6787d4f9 | 56 | #endif |
nucleosam | 0:36aa6787d4f9 | 57 | |
nucleosam | 0:36aa6787d4f9 | 58 | /* Variables -----------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 59 | |
nucleosam | 0:36aa6787d4f9 | 60 | /* Initialization parameters of the motor connected to the expansion board. */ |
nucleosam | 0:36aa6787d4f9 | 61 | l6208_Init_t initDeviceParameters = |
nucleosam | 0:36aa6787d4f9 | 62 | { |
nucleosam | 0:36aa6787d4f9 | 63 | 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes |
nucleosam | 0:36aa6787d4f9 | 64 | 40, //Acceleration current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 65 | 1500, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes |
nucleosam | 0:36aa6787d4f9 | 66 | 30, //Deceleration current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 67 | 1500, //Running speed in step/s or (1/16)th step/s for microstep modes |
nucleosam | 0:36aa6787d4f9 | 68 | 50, //Running current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 69 | 20, //Holding current torque in % (from 0 to 100) |
nucleosam | 0:36aa6787d4f9 | 70 | STEP_MODE_1_16, //Step mode via enum motorStepMode_t |
nucleosam | 0:36aa6787d4f9 | 71 | FAST_DECAY, //Decay mode via enum motorDecayMode_t |
nucleosam | 0:36aa6787d4f9 | 72 | 0, //Dwelling time in ms |
nucleosam | 0:36aa6787d4f9 | 73 | FALSE, //Automatic HIZ STOP |
nucleosam | 0:36aa6787d4f9 | 74 | 100000 //VREFA and VREFB PWM frequency (Hz) |
nucleosam | 0:36aa6787d4f9 | 75 | }; |
nucleosam | 0:36aa6787d4f9 | 76 | |
nucleosam | 0:36aa6787d4f9 | 77 | /* Motor Control Component. */ |
nucleosam | 0:36aa6787d4f9 | 78 | L6208 *motor; |
nucleosam | 0:36aa6787d4f9 | 79 | |
nucleosam | 0:36aa6787d4f9 | 80 | /* Functions -----------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 81 | |
nucleosam | 0:36aa6787d4f9 | 82 | /** |
nucleosam | 0:36aa6787d4f9 | 83 | * @brief This is an example of user handler for the flag interrupt. |
nucleosam | 0:36aa6787d4f9 | 84 | * @param None |
nucleosam | 0:36aa6787d4f9 | 85 | * @retval None |
nucleosam | 0:36aa6787d4f9 | 86 | * @note If needed, implement it, and then attach and enable it: |
nucleosam | 0:36aa6787d4f9 | 87 | * + motor->AttachFlagIRQ(&myFlagIRQHandler); |
nucleosam | 0:36aa6787d4f9 | 88 | * + motor->EnableFlagIRQ(); |
nucleosam | 0:36aa6787d4f9 | 89 | * To disable it: |
nucleosam | 0:36aa6787d4f9 | 90 | * + motor->DisbleFlagIRQ(); |
nucleosam | 0:36aa6787d4f9 | 91 | */ |
nucleosam | 0:36aa6787d4f9 | 92 | void myFlagIRQHandler(void) |
nucleosam | 0:36aa6787d4f9 | 93 | { |
nucleosam | 0:36aa6787d4f9 | 94 | printf(" WARNING: \"FLAG\" interrupt triggered:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 95 | motor->Disable(); |
nucleosam | 0:36aa6787d4f9 | 96 | printf(" Motor disabled.\r\n\n"); |
nucleosam | 0:36aa6787d4f9 | 97 | } |
nucleosam | 0:36aa6787d4f9 | 98 | |
nucleosam | 0:36aa6787d4f9 | 99 | /** |
nucleosam | 0:36aa6787d4f9 | 100 | * @brief This is an example of error handler. |
nucleosam | 0:36aa6787d4f9 | 101 | * @param[in] error Number of the error |
nucleosam | 0:36aa6787d4f9 | 102 | * @retval None |
nucleosam | 0:36aa6787d4f9 | 103 | * @note If needed, implement it, and then attach it: |
nucleosam | 0:36aa6787d4f9 | 104 | * + motor->AttachErrorHandler(&myErrorHandler); |
nucleosam | 0:36aa6787d4f9 | 105 | */ |
nucleosam | 0:36aa6787d4f9 | 106 | void myErrorHandler(uint16_t error) |
nucleosam | 0:36aa6787d4f9 | 107 | { |
nucleosam | 0:36aa6787d4f9 | 108 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 109 | printf("Error %d detected\r\n\n", error); |
nucleosam | 0:36aa6787d4f9 | 110 | |
nucleosam | 0:36aa6787d4f9 | 111 | /* Infinite loop */ |
nucleosam | 0:36aa6787d4f9 | 112 | while(1) |
nucleosam | 0:36aa6787d4f9 | 113 | { |
nucleosam | 0:36aa6787d4f9 | 114 | } |
nucleosam | 0:36aa6787d4f9 | 115 | } |
nucleosam | 0:36aa6787d4f9 | 116 | |
nucleosam | 0:36aa6787d4f9 | 117 | /* Main ----------------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 118 | |
nucleosam | 0:36aa6787d4f9 | 119 | int main() |
nucleosam | 0:36aa6787d4f9 | 120 | { |
nucleosam | 0:36aa6787d4f9 | 121 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 122 | printf("STARTING MAIN PROGRAM\r\n"); |
nucleosam | 0:36aa6787d4f9 | 123 | printf(" Reminder:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 124 | printf(" The position unit is in agreement to the step mode.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 125 | printf(" The speed, acceleration or deceleration unit depend on the step mode:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 126 | printf(" - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 127 | printf(" - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 128 | |
nucleosam | 0:36aa6787d4f9 | 129 | //----- Initialization |
nucleosam | 0:36aa6787d4f9 | 130 | /* Initializing Motor Control Component. */ |
nucleosam | 0:36aa6787d4f9 | 131 | motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, D9); |
nucleosam | 0:36aa6787d4f9 | 132 | if (motor->Init(&initDeviceParameters) != COMPONENT_OK) exit(EXIT_FAILURE); |
nucleosam | 0:36aa6787d4f9 | 133 | |
nucleosam | 0:36aa6787d4f9 | 134 | /* Attaching and enabling an interrupt handler. */ |
nucleosam | 0:36aa6787d4f9 | 135 | motor->AttachFlagIRQ(&myFlagIRQHandler); |
nucleosam | 0:36aa6787d4f9 | 136 | motor->EnableFlagIRQ(); |
nucleosam | 0:36aa6787d4f9 | 137 | |
nucleosam | 0:36aa6787d4f9 | 138 | /* Attaching an error handler */ |
nucleosam | 0:36aa6787d4f9 | 139 | motor->AttachErrorHandler(&myErrorHandler); |
nucleosam | 0:36aa6787d4f9 | 140 | |
nucleosam | 0:36aa6787d4f9 | 141 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 142 | printf("Motor Control Application Example for 1 Motor\r\n"); |
nucleosam | 0:36aa6787d4f9 | 143 | |
nucleosam | 0:36aa6787d4f9 | 144 | //----- Run the motor BACKWARD |
nucleosam | 0:36aa6787d4f9 | 145 | printf("--> Running the motor backward.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 146 | motor->Run(StepperMotor::BWD); |
nucleosam | 0:36aa6787d4f9 | 147 | |
nucleosam | 0:36aa6787d4f9 | 148 | while (motor->GetStatus()!=STEADY) |
nucleosam | 0:36aa6787d4f9 | 149 | { |
nucleosam | 0:36aa6787d4f9 | 150 | /* Print reached speed to the console in step/s or microsteps/s */ |
nucleosam | 0:36aa6787d4f9 | 151 | //printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); |
nucleosam | 0:36aa6787d4f9 | 152 | //wait_ms(50); |
nucleosam | 0:36aa6787d4f9 | 153 | } |
nucleosam | 0:36aa6787d4f9 | 154 | //printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); |
nucleosam | 0:36aa6787d4f9 | 155 | |
nucleosam | 0:36aa6787d4f9 | 156 | /* Wait for 1 second */ |
nucleosam | 0:36aa6787d4f9 | 157 | wait_ms(1000); |
nucleosam | 0:36aa6787d4f9 | 158 | |
nucleosam | 0:36aa6787d4f9 | 159 | //----- Decrease speed while running to one quarter of the previous speed |
nucleosam | 0:36aa6787d4f9 | 160 | motor->SetMaxSpeed(motor->GetSpeed()>>2); |
nucleosam | 0:36aa6787d4f9 | 161 | |
nucleosam | 0:36aa6787d4f9 | 162 | /* Wait until the motor starts decelerating */ |
nucleosam | 0:36aa6787d4f9 | 163 | while (motor->GetStatus()==STEADY); |
nucleosam | 0:36aa6787d4f9 | 164 | /* Wait and print speed while the motor is not steady running */ |
nucleosam | 0:36aa6787d4f9 | 165 | while (motor->GetStatus()!=STEADY) |
nucleosam | 0:36aa6787d4f9 | 166 | { |
nucleosam | 0:36aa6787d4f9 | 167 | /* Print reached speed to the console in step/s or microsteps/s */ |
nucleosam | 0:36aa6787d4f9 | 168 | //printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); |
nucleosam | 0:36aa6787d4f9 | 169 | //wait_ms(50); |
nucleosam | 0:36aa6787d4f9 | 170 | } |
nucleosam | 0:36aa6787d4f9 | 171 | //printf(" Reached Speed: %d microstep/s.\r\n", motor->GetSpeed()); |
nucleosam | 0:36aa6787d4f9 | 172 | |
nucleosam | 0:36aa6787d4f9 | 173 | /* Wait for 5 seconds */ |
nucleosam | 0:36aa6787d4f9 | 174 | wait_ms(5000); |
nucleosam | 0:36aa6787d4f9 | 175 | |
nucleosam | 0:36aa6787d4f9 | 176 | //----- Soft stop required while running |
nucleosam | 0:36aa6787d4f9 | 177 | printf("--> Soft stop requested.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 178 | motor->SoftStop(); |
nucleosam | 0:36aa6787d4f9 | 179 | |
nucleosam | 0:36aa6787d4f9 | 180 | /* Wait for the motor of device ends moving */ |
nucleosam | 0:36aa6787d4f9 | 181 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 182 | |
nucleosam | 0:36aa6787d4f9 | 183 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 184 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 185 | |
nucleosam | 0:36aa6787d4f9 | 186 | //----- Change step mode to full step mode |
nucleosam | 0:36aa6787d4f9 | 187 | motor->SetStepMode(StepperMotor::STEP_MODE_FULL); |
nucleosam | 0:36aa6787d4f9 | 188 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->GetStepMode()); |
nucleosam | 0:36aa6787d4f9 | 189 | |
nucleosam | 0:36aa6787d4f9 | 190 | /* Get current position of device and print to the console */ |
nucleosam | 0:36aa6787d4f9 | 191 | printf(" Position: %d.\r\n", motor->GetPosition()); |
nucleosam | 0:36aa6787d4f9 | 192 | |
nucleosam | 0:36aa6787d4f9 | 193 | /* Set speed, acceleration and deceleration to scale with normal mode */ |
nucleosam | 0:36aa6787d4f9 | 194 | motor->SetMaxSpeed(initDeviceParameters.maxSpeedSps>>4); |
nucleosam | 0:36aa6787d4f9 | 195 | motor->SetAcceleration(motor->GetAcceleration()>>4); |
nucleosam | 0:36aa6787d4f9 | 196 | motor->SetDeceleration(motor->GetDeceleration()>>4); |
nucleosam | 0:36aa6787d4f9 | 197 | /* Print parameters to the console */ |
nucleosam | 0:36aa6787d4f9 | 198 | printf(" Motor Max Speed: %d step/s.\r\n", motor->GetMaxSpeed()); |
nucleosam | 0:36aa6787d4f9 | 199 | printf(" Motor Min Speed: %d step/s.\r\n", motor->GetMinSpeed()); |
nucleosam | 0:36aa6787d4f9 | 200 | printf(" Motor Acceleration: %d step/s.\r\n", motor->GetAcceleration()); |
nucleosam | 0:36aa6787d4f9 | 201 | printf(" Motor Deceleration: %d step/s.\r\n", motor->GetDeceleration()); |
nucleosam | 0:36aa6787d4f9 | 202 | |
nucleosam | 0:36aa6787d4f9 | 203 | //----- Move of 200 steps in the FW direction |
nucleosam | 0:36aa6787d4f9 | 204 | printf("--> Moving forward 200 steps.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 205 | motor->Move(StepperMotor::FWD, 200); |
nucleosam | 0:36aa6787d4f9 | 206 | |
nucleosam | 0:36aa6787d4f9 | 207 | /* Waiting while the motor is active. */ |
nucleosam | 0:36aa6787d4f9 | 208 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 209 | |
nucleosam | 0:36aa6787d4f9 | 210 | /* Get current position of device and print to the console */ |
nucleosam | 0:36aa6787d4f9 | 211 | printf(" Position: %d.\r\n", motor->GetPosition()); |
nucleosam | 0:36aa6787d4f9 | 212 | |
nucleosam | 0:36aa6787d4f9 | 213 | /* Disable the power bridges */ |
nucleosam | 0:36aa6787d4f9 | 214 | motor->Disable(); |
nucleosam | 0:36aa6787d4f9 | 215 | |
nucleosam | 0:36aa6787d4f9 | 216 | /* Check that the power bridges are actually disabled */ |
nucleosam | 0:36aa6787d4f9 | 217 | if (motor->CheckStatusHw()!=0) printf(" Motor driver disabled.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 218 | else printf(" Failed to disable the motor driver.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 219 | |
nucleosam | 0:36aa6787d4f9 | 220 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 221 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 222 | |
nucleosam | 0:36aa6787d4f9 | 223 | //----- Change step mode to 1/4 microstepping mode |
nucleosam | 0:36aa6787d4f9 | 224 | motor->SetStepMode(StepperMotor::STEP_MODE_1_4); |
nucleosam | 0:36aa6787d4f9 | 225 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->GetStepMode()); |
nucleosam | 0:36aa6787d4f9 | 226 | |
nucleosam | 0:36aa6787d4f9 | 227 | /* Get current position of device and print to the console */ |
nucleosam | 0:36aa6787d4f9 | 228 | printf(" Position: %d.\r\n", motor->GetPosition()); |
nucleosam | 0:36aa6787d4f9 | 229 | |
nucleosam | 0:36aa6787d4f9 | 230 | /* Set speed, acceleration and deceleration to scale with microstep mode */ |
nucleosam | 0:36aa6787d4f9 | 231 | motor->SetMaxSpeed(motor->GetMaxSpeed()<<4); |
nucleosam | 0:36aa6787d4f9 | 232 | motor->SetAcceleration(motor->GetAcceleration()<<4); |
nucleosam | 0:36aa6787d4f9 | 233 | motor->SetDeceleration(motor->GetDeceleration()<<4); |
nucleosam | 0:36aa6787d4f9 | 234 | /* Print parameters to the console */ |
nucleosam | 0:36aa6787d4f9 | 235 | printf(" Motor Max Speed: %d step/s.\r\n", motor->GetMaxSpeed()); |
nucleosam | 0:36aa6787d4f9 | 236 | printf(" Motor Min Speed: %d step/s.\r\n", motor->GetMinSpeed()); |
nucleosam | 0:36aa6787d4f9 | 237 | printf(" Motor Acceleration: %d step/s.\r\n", motor->GetAcceleration()); |
nucleosam | 0:36aa6787d4f9 | 238 | printf(" Motor Deceleration: %d step/s.\r\n", motor->GetDeceleration()); |
nucleosam | 0:36aa6787d4f9 | 239 | |
nucleosam | 0:36aa6787d4f9 | 240 | /* Request to go position 800 (quarter steps) */ |
nucleosam | 0:36aa6787d4f9 | 241 | motor->GoTo(800); |
nucleosam | 0:36aa6787d4f9 | 242 | |
nucleosam | 0:36aa6787d4f9 | 243 | /* Wait for the motor ends moving */ |
nucleosam | 0:36aa6787d4f9 | 244 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 245 | |
nucleosam | 0:36aa6787d4f9 | 246 | /* Get current position of device and print to the console */ |
nucleosam | 0:36aa6787d4f9 | 247 | printf(" Position: %d.\r\n", motor->GetPosition()); |
nucleosam | 0:36aa6787d4f9 | 248 | |
nucleosam | 0:36aa6787d4f9 | 249 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 250 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 251 | |
nucleosam | 0:36aa6787d4f9 | 252 | //----- Restore step mode to its initialization value |
nucleosam | 0:36aa6787d4f9 | 253 | motor->SetStepMode((StepperMotor::step_mode_t)initDeviceParameters.stepMode); |
nucleosam | 0:36aa6787d4f9 | 254 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->GetStepMode()); |
nucleosam | 0:36aa6787d4f9 | 255 | |
nucleosam | 0:36aa6787d4f9 | 256 | /* Get current position of device and print to the console */ |
nucleosam | 0:36aa6787d4f9 | 257 | printf(" Position: %d.\r\n", motor->GetPosition()); |
nucleosam | 0:36aa6787d4f9 | 258 | |
nucleosam | 0:36aa6787d4f9 | 259 | //----- Change decay mode |
nucleosam | 0:36aa6787d4f9 | 260 | motor->SetDecayMode(SLOW_DECAY); |
nucleosam | 0:36aa6787d4f9 | 261 | printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->GetDecayMode()); |
nucleosam | 0:36aa6787d4f9 | 262 | |
nucleosam | 0:36aa6787d4f9 | 263 | //----- Go to position -6400 |
nucleosam | 0:36aa6787d4f9 | 264 | printf("--> Go to position -6400 steps.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 265 | motor->GoTo(-6400); |
nucleosam | 0:36aa6787d4f9 | 266 | |
nucleosam | 0:36aa6787d4f9 | 267 | /* Wait for the motor ends moving */ |
nucleosam | 0:36aa6787d4f9 | 268 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 269 | |
nucleosam | 0:36aa6787d4f9 | 270 | /* Get current position of device and print to the console */ |
nucleosam | 0:36aa6787d4f9 | 271 | printf(" Position: %d.\r\n", motor->GetPosition()); |
nucleosam | 0:36aa6787d4f9 | 272 | |
nucleosam | 0:36aa6787d4f9 | 273 | /* Wait for 2 seconds */ |
nucleosam | 0:36aa6787d4f9 | 274 | wait_ms(2000); |
nucleosam | 0:36aa6787d4f9 | 275 | |
nucleosam | 0:36aa6787d4f9 | 276 | //----- Restore decay mode to its initialization value |
nucleosam | 0:36aa6787d4f9 | 277 | motor->SetDecayMode(initDeviceParameters.decayMode); |
nucleosam | 0:36aa6787d4f9 | 278 | printf(" Motor decay mode: %d (0:slow decay, 1:fast decay).\r\n", motor->GetDecayMode()); |
nucleosam | 0:36aa6787d4f9 | 279 | |
nucleosam | 0:36aa6787d4f9 | 280 | //----- Go Home |
nucleosam | 0:36aa6787d4f9 | 281 | printf("--> Go to home position.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 282 | motor->GoHome(); |
nucleosam | 0:36aa6787d4f9 | 283 | |
nucleosam | 0:36aa6787d4f9 | 284 | /* Wait for the motor ends moving */ |
nucleosam | 0:36aa6787d4f9 | 285 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 286 | |
nucleosam | 0:36aa6787d4f9 | 287 | /* Wait for 1 second */ |
nucleosam | 0:36aa6787d4f9 | 288 | wait_ms(1000); |
nucleosam | 0:36aa6787d4f9 | 289 | |
nucleosam | 0:36aa6787d4f9 | 290 | /* Infinite Loop. */ |
nucleosam | 0:36aa6787d4f9 | 291 | printf("--> Infinite Loop...\r\n"); |
nucleosam | 0:36aa6787d4f9 | 292 | while (1) |
nucleosam | 0:36aa6787d4f9 | 293 | { |
nucleosam | 0:36aa6787d4f9 | 294 | /* Request device to go position -3200 */ |
nucleosam | 0:36aa6787d4f9 | 295 | motor->GoTo(-3200); |
nucleosam | 0:36aa6787d4f9 | 296 | |
nucleosam | 0:36aa6787d4f9 | 297 | /* Waiting while the motor is active. */ |
nucleosam | 0:36aa6787d4f9 | 298 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 299 | |
nucleosam | 0:36aa6787d4f9 | 300 | /* Request device to go position 3200 */ |
nucleosam | 0:36aa6787d4f9 | 301 | motor->GoTo(3200); |
nucleosam | 0:36aa6787d4f9 | 302 | |
nucleosam | 0:36aa6787d4f9 | 303 | /* Waiting while the motor is active. */ |
nucleosam | 0:36aa6787d4f9 | 304 | motor->WaitWhileActive(); |
nucleosam | 0:36aa6787d4f9 | 305 | } |
nucleosam | 0:36aa6787d4f9 | 306 | } |
nucleosam | 0:36aa6787d4f9 | 307 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |