
Simple code to run steps
Dependencies: X-NUCLEO-IHM05A1 mbed
Fork of HelloWorld_IHM05A1 by
main.cpp@10:93b6d1b02071, 2018-01-14 (annotated)
- Committer:
- Arkadi
- Date:
- Sun Jan 14 10:05:05 2018 +0000
- Revision:
- 10:93b6d1b02071
- Parent:
- 9:4b2736fbecdf
Increase Torque
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 | 2:c5f455885df7 | 7 | * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM05A1 |
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 | /* Component specific header files. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 45 | #include "L6208.h" |
nucleosam | 0:36aa6787d4f9 | 46 | |
Arkadi | 9:4b2736fbecdf | 47 | #define DELAY_STEPS 1000 |
Arkadi | 9:4b2736fbecdf | 48 | #define STEPS_PER 1 |
Arkadi | 9:4b2736fbecdf | 49 | #define NUM_STEPS 400 |
Arkadi | 9:4b2736fbecdf | 50 | #define NUM_ROTATION 1 |
Arkadi | 9:4b2736fbecdf | 51 | int32_t steps=0; |
nucleosam | 0:36aa6787d4f9 | 52 | /* Definitions ---------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 53 | #ifdef TARGET_NUCLEO_F334R8 |
nucleosam | 0:36aa6787d4f9 | 54 | #define VREFA_PWM_PIN D11 |
nucleosam | 3:a6e155687c6a | 55 | #define VREFB_PWM_PIN D9 |
nucleosam | 3:a6e155687c6a | 56 | #elif TARGET_NUCLEO_F302R8 |
nucleosam | 3:a6e155687c6a | 57 | #define VREFA_PWM_PIN D11 |
nucleosam | 3:a6e155687c6a | 58 | #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */ |
nucleosam | 0:36aa6787d4f9 | 59 | #else |
nucleosam | 0:36aa6787d4f9 | 60 | #define VREFA_PWM_PIN D3 |
nucleosam | 3:a6e155687c6a | 61 | #define VREFB_PWM_PIN D9 |
nucleosam | 0:36aa6787d4f9 | 62 | #endif |
nucleosam | 0:36aa6787d4f9 | 63 | |
nucleosam | 0:36aa6787d4f9 | 64 | /* Variables -----------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 65 | |
nucleosam | 0:36aa6787d4f9 | 66 | /* Initialization parameters of the motor connected to the expansion board. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 67 | l6208_init_t init = |
nucleosam | 0:36aa6787d4f9 | 68 | { |
Arkadi | 10:93b6d1b02071 | 69 | 100, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes |
Arkadi | 10:93b6d1b02071 | 70 | 50, //Acceleration current torque in % (from 0 to 100) |
Arkadi | 10:93b6d1b02071 | 71 | 100, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes |
Arkadi | 10:93b6d1b02071 | 72 | 50, //Deceleration current torque in % (from 0 to 100) |
Arkadi | 10:93b6d1b02071 | 73 | 100, //Running speed in step/s or (1/16)th step/s for microstep modes |
Arkadi | 10:93b6d1b02071 | 74 | 50, //Running current torque in % (from 0 to 100) |
Arkadi | 10:93b6d1b02071 | 75 | 50, //Holding current torque in % (from 0 to 100) |
Arkadi | 9:4b2736fbecdf | 76 | STEP_MODE_HALF, //Step mode via enum motorStepMode_t |
Arkadi | 10:93b6d1b02071 | 77 | SLOW_DECAY, //Decay mode via enum motorDecayMode_t |
Arkadi | 10:93b6d1b02071 | 78 | 10, //Dwelling time in ms |
nucleosam | 0:36aa6787d4f9 | 79 | FALSE, //Automatic HIZ STOP |
nucleosam | 0:36aa6787d4f9 | 80 | 100000 //VREFA and VREFB PWM frequency (Hz) |
nucleosam | 0:36aa6787d4f9 | 81 | }; |
nucleosam | 0:36aa6787d4f9 | 82 | |
nucleosam | 0:36aa6787d4f9 | 83 | /* Motor Control Component. */ |
nucleosam | 0:36aa6787d4f9 | 84 | L6208 *motor; |
nucleosam | 0:36aa6787d4f9 | 85 | |
nucleosam | 0:36aa6787d4f9 | 86 | /* Functions -----------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 87 | |
nucleosam | 0:36aa6787d4f9 | 88 | /** |
nucleosam | 0:36aa6787d4f9 | 89 | * @brief This is an example of user handler for the flag interrupt. |
nucleosam | 0:36aa6787d4f9 | 90 | * @param None |
nucleosam | 0:36aa6787d4f9 | 91 | * @retval None |
nucleosam | 0:36aa6787d4f9 | 92 | * @note If needed, implement it, and then attach and enable it: |
davide.aliprandi@st.com | 5:bc710d77d801 | 93 | * + motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 5:bc710d77d801 | 94 | * + motor->enable_flag_irq(); |
nucleosam | 0:36aa6787d4f9 | 95 | * To disable it: |
nucleosam | 0:36aa6787d4f9 | 96 | * + motor->DisbleFlagIRQ(); |
nucleosam | 0:36aa6787d4f9 | 97 | */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 98 | void my_flag_irq_handler(void) |
nucleosam | 0:36aa6787d4f9 | 99 | { |
nucleosam | 0:36aa6787d4f9 | 100 | printf(" WARNING: \"FLAG\" interrupt triggered:\r\n"); |
Davidroid | 6:6fb625ef867a | 101 | motor->disable(); |
nucleosam | 0:36aa6787d4f9 | 102 | printf(" Motor disabled.\r\n\n"); |
nucleosam | 0:36aa6787d4f9 | 103 | } |
nucleosam | 0:36aa6787d4f9 | 104 | |
nucleosam | 0:36aa6787d4f9 | 105 | /** |
nucleosam | 0:36aa6787d4f9 | 106 | * @brief This is an example of error handler. |
nucleosam | 0:36aa6787d4f9 | 107 | * @param[in] error Number of the error |
nucleosam | 0:36aa6787d4f9 | 108 | * @retval None |
nucleosam | 0:36aa6787d4f9 | 109 | * @note If needed, implement it, and then attach it: |
davide.aliprandi@st.com | 5:bc710d77d801 | 110 | * + motor->attach_error_handler(&my_error_handler); |
nucleosam | 0:36aa6787d4f9 | 111 | */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 112 | void my_error_handler(uint16_t error) |
nucleosam | 0:36aa6787d4f9 | 113 | { |
nucleosam | 0:36aa6787d4f9 | 114 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 115 | printf("Error %d detected\r\n\n", error); |
nucleosam | 0:36aa6787d4f9 | 116 | |
nucleosam | 0:36aa6787d4f9 | 117 | /* Infinite loop */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 118 | while (true) { |
nucleosam | 0:36aa6787d4f9 | 119 | } |
nucleosam | 0:36aa6787d4f9 | 120 | } |
nucleosam | 0:36aa6787d4f9 | 121 | |
nucleosam | 0:36aa6787d4f9 | 122 | /* Main ----------------------------------------------------------------------*/ |
nucleosam | 0:36aa6787d4f9 | 123 | |
nucleosam | 0:36aa6787d4f9 | 124 | int main() |
nucleosam | 0:36aa6787d4f9 | 125 | { |
nucleosam | 0:36aa6787d4f9 | 126 | /* Printing to the console. */ |
nucleosam | 0:36aa6787d4f9 | 127 | printf("STARTING MAIN PROGRAM\r\n"); |
nucleosam | 0:36aa6787d4f9 | 128 | printf(" Reminder:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 129 | printf(" The position unit is in agreement to the step mode.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 130 | printf(" The speed, acceleration or deceleration unit depend on the step mode:\r\n"); |
nucleosam | 0:36aa6787d4f9 | 131 | printf(" - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 132 | printf(" - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n"); |
nucleosam | 0:36aa6787d4f9 | 133 | |
nucleosam | 0:36aa6787d4f9 | 134 | //----- Initialization |
nucleosam | 0:36aa6787d4f9 | 135 | /* Initializing Motor Control Component. */ |
nucleosam | 3:a6e155687c6a | 136 | motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN); |
davide.aliprandi@st.com | 5:bc710d77d801 | 137 | if (motor->init(&init) != COMPONENT_OK) { |
davide.aliprandi@st.com | 5:bc710d77d801 | 138 | exit(EXIT_FAILURE); |
davide.aliprandi@st.com | 5:bc710d77d801 | 139 | } |
nucleosam | 0:36aa6787d4f9 | 140 | |
nucleosam | 0:36aa6787d4f9 | 141 | /* Attaching and enabling an interrupt handler. */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 142 | motor->attach_flag_irq(&my_flag_irq_handler); |
davide.aliprandi@st.com | 5:bc710d77d801 | 143 | motor->enable_flag_irq(); |
nucleosam | 0:36aa6787d4f9 | 144 | |
nucleosam | 0:36aa6787d4f9 | 145 | /* Attaching an error handler */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 146 | motor->attach_error_handler(&my_error_handler); |
nucleosam | 0:36aa6787d4f9 | 147 | |
nucleosam | 0:36aa6787d4f9 | 148 | /* Printing to the console. */ |
Arkadi | 9:4b2736fbecdf | 149 | printf("Run motor at 1/16 micro stepping"); |
nucleosam | 0:36aa6787d4f9 | 150 | |
nucleosam | 0:36aa6787d4f9 | 151 | /* Print parameters to the console */ |
davide.aliprandi@st.com | 5:bc710d77d801 | 152 | printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 153 | printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 154 | printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration()); |
davide.aliprandi@st.com | 5:bc710d77d801 | 155 | printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration()); |
nucleosam | 0:36aa6787d4f9 | 156 | |
Arkadi | 9:4b2736fbecdf | 157 | |
Arkadi | 9:4b2736fbecdf | 158 | //----- Change step mode to full step mode |
Arkadi | 9:4b2736fbecdf | 159 | motor->set_step_mode(StepperMotor::STEP_MODE_HALF); |
davide.aliprandi@st.com | 5:bc710d77d801 | 160 | printf(" Motor step mode: %d (0:FS, 1:1/2, 2:1/4, 3:1/8, 4:1/16).\r\n", motor->get_step_mode()); |
nucleosam | 0:36aa6787d4f9 | 161 | |
Arkadi | 9:4b2736fbecdf | 162 | ////----- run the motor BACKWARD |
Arkadi | 9:4b2736fbecdf | 163 | // printf("--> Running the motor backward.\r\n"); |
Arkadi | 9:4b2736fbecdf | 164 | // motor->run(StepperMotor::BWD); |
Arkadi | 9:4b2736fbecdf | 165 | // |
Arkadi | 9:4b2736fbecdf | 166 | // |
Arkadi | 9:4b2736fbecdf | 167 | // // wait untill motor is at const speed |
Arkadi | 9:4b2736fbecdf | 168 | // while (motor->get_status()!=STEADY) { |
Arkadi | 9:4b2736fbecdf | 169 | // /* Print reached speed to the console in step/s or microsteps/s */ |
Arkadi | 9:4b2736fbecdf | 170 | // printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); |
Arkadi | 9:4b2736fbecdf | 171 | // wait_ms(50); |
Arkadi | 9:4b2736fbecdf | 172 | // } |
Arkadi | 9:4b2736fbecdf | 173 | // printf(" Reached Speed: %d microstep/s.\r\n", motor->get_speed()); |
Arkadi | 9:4b2736fbecdf | 174 | // |
Arkadi | 9:4b2736fbecdf | 175 | // // run continious |
Arkadi | 9:4b2736fbecdf | 176 | // /* Wait for 1 second */ |
Arkadi | 9:4b2736fbecdf | 177 | // wait(60); |
Arkadi | 9:4b2736fbecdf | 178 | // |
Arkadi | 9:4b2736fbecdf | 179 | ////----- Soft stop required while running |
Arkadi | 9:4b2736fbecdf | 180 | // printf("--> Soft stop requested.\r\n"); |
Arkadi | 9:4b2736fbecdf | 181 | // motor->soft_stop(); |
Arkadi | 9:4b2736fbecdf | 182 | // |
Arkadi | 9:4b2736fbecdf | 183 | // /* Wait for the motor of device ends moving */ |
Arkadi | 9:4b2736fbecdf | 184 | // motor->wait_while_active(); |
nucleosam | 0:36aa6787d4f9 | 185 | |
nucleosam | 0:36aa6787d4f9 | 186 | |
Arkadi | 9:4b2736fbecdf | 187 | while (steps<NUM_STEPS*NUM_ROTATION){ |
Arkadi | 9:4b2736fbecdf | 188 | //----- move of STEPS_PER steps in the FW direction |
Arkadi | 9:4b2736fbecdf | 189 | |
Arkadi | 9:4b2736fbecdf | 190 | motor->move(StepperMotor::FWD, STEPS_PER); |
Arkadi | 9:4b2736fbecdf | 191 | /* Wait for DELAY_STEPS*/ |
Arkadi | 9:4b2736fbecdf | 192 | wait_ms(DELAY_STEPS); |
Arkadi | 9:4b2736fbecdf | 193 | steps=motor->get_position(); |
Arkadi | 9:4b2736fbecdf | 194 | printf("--> %d steps.\r\n",steps); |
nucleosam | 0:36aa6787d4f9 | 195 | } |
nucleosam | 0:36aa6787d4f9 | 196 | } |
davide.aliprandi@st.com | 5:bc710d77d801 | 197 | |
nucleosam | 0:36aa6787d4f9 | 198 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |