motore cofano

Dependencies:   mbed X-NUCLEO-IHM05A1

Committer:
francesco01
Date:
Wed Nov 21 15:52:33 2018 +0000
Revision:
11:a7e099ee909e
Parent:
10:2622d1ab5984
motore

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pinofal 9:900e18a2310b 1 /**
pinofal 9:900e18a2310b 2 ******************************************************************************
pinofal 9:900e18a2310b 3 * @file main.cpp
pinofal 9:900e18a2310b 4 * @author IPC Rennes
pinofal 9:900e18a2310b 5 * @version V1.0.0
pinofal 9:900e18a2310b 6 * @date April 13th, 2016
pinofal 9:900e18a2310b 7 * @brief mbed simple application for the STMicroelectronics X-NUCLEO-IHM05A1
pinofal 9:900e18a2310b 8 * Motor Control Expansion Board: control of 1 motor.
pinofal 9:900e18a2310b 9 ******************************************************************************
pinofal 9:900e18a2310b 10 * @attention
pinofal 9:900e18a2310b 11 *
pinofal 9:900e18a2310b 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
pinofal 9:900e18a2310b 13 *
pinofal 9:900e18a2310b 14 * Redistribution and use in source and binary forms, with or without modification,
pinofal 9:900e18a2310b 15 * are permitted provided that the following conditions are met:
pinofal 9:900e18a2310b 16 * 1. Redistributions of source code must retain the above copyright notice,
pinofal 9:900e18a2310b 17 * this list of conditions and the following disclaimer.
pinofal 9:900e18a2310b 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
pinofal 9:900e18a2310b 19 * this list of conditions and the following disclaimer in the documentation
pinofal 9:900e18a2310b 20 * and/or other materials provided with the distribution.
pinofal 9:900e18a2310b 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
pinofal 9:900e18a2310b 22 * may be used to endorse or promote products derived from this software
pinofal 9:900e18a2310b 23 * without specific prior written permission.
pinofal 9:900e18a2310b 24 *
pinofal 9:900e18a2310b 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
pinofal 9:900e18a2310b 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
pinofal 9:900e18a2310b 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
pinofal 9:900e18a2310b 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
pinofal 9:900e18a2310b 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
pinofal 9:900e18a2310b 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
pinofal 9:900e18a2310b 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
pinofal 9:900e18a2310b 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
pinofal 9:900e18a2310b 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
pinofal 9:900e18a2310b 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pinofal 9:900e18a2310b 35 *
pinofal 9:900e18a2310b 36 ******************************************************************************
pinofal 9:900e18a2310b 37 */
pinofal 9:900e18a2310b 38
pinofal 9:900e18a2310b 39 /* Includes ------------------------------------------------------------------*/
pinofal 9:900e18a2310b 40
pinofal 9:900e18a2310b 41 /* mbed specific header files. */
pinofal 9:900e18a2310b 42 #include "mbed.h"
pinofal 9:900e18a2310b 43
pinofal 9:900e18a2310b 44 /* Component specific header files. */
pinofal 9:900e18a2310b 45 #include "L6208.h"
pinofal 9:900e18a2310b 46
francesco01 10:2622d1ab5984 47 DigitalIn myButton(USER_BUTTON);
francesco01 10:2622d1ab5984 48 DigitalIn Input (PC_0);
pinofal 9:900e18a2310b 49
pinofal 9:900e18a2310b 50 /* Definitions ---------------------------------------------------------------*/
pinofal 9:900e18a2310b 51 #ifdef TARGET_NUCLEO_F334R8
pinofal 9:900e18a2310b 52 #define VREFA_PWM_PIN D11
pinofal 9:900e18a2310b 53 #define VREFB_PWM_PIN D9
pinofal 9:900e18a2310b 54 #elif TARGET_NUCLEO_F302R8
pinofal 9:900e18a2310b 55 #define VREFA_PWM_PIN D11
pinofal 9:900e18a2310b 56 #define VREFB_PWM_PIN D15 /* HW mandatory patch: bridge manually D9 with D15 */
pinofal 9:900e18a2310b 57 #else
pinofal 9:900e18a2310b 58 #define VREFA_PWM_PIN D3
pinofal 9:900e18a2310b 59 #define VREFB_PWM_PIN D9
pinofal 9:900e18a2310b 60 #endif
pinofal 9:900e18a2310b 61
pinofal 9:900e18a2310b 62 Serial pc(SERIAL_TX, SERIAL_RX);
pinofal 9:900e18a2310b 63
pinofal 9:900e18a2310b 64 /* Variables -----------------------------------------------------------------*/
pinofal 9:900e18a2310b 65
pinofal 9:900e18a2310b 66 /* Initialization parameters of the motor connected to the expansion board. */
pinofal 9:900e18a2310b 67 l6208_init_t init =
pinofal 9:900e18a2310b 68 {
francesco01 10:2622d1ab5984 69
pinofal 9:900e18a2310b 70 1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
francesco01 10:2622d1ab5984 71 20, //Acceleration current torque in % (from 0 to 100)
pinofal 9:900e18a2310b 72 1500, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
pinofal 9:900e18a2310b 73 30, //Deceleration current torque in % (from 0 to 100)
pinofal 9:900e18a2310b 74 1500, //Running speed in step/s or (1/16)th step/s for microstep modes
pinofal 9:900e18a2310b 75 50, //Running current torque in % (from 0 to 100)
pinofal 9:900e18a2310b 76 20, //Holding current torque in % (from 0 to 100)
pinofal 9:900e18a2310b 77 STEP_MODE_1_16, //Step mode via enum motorStepMode_t
pinofal 9:900e18a2310b 78 FAST_DECAY, //Decay mode via enum motorDecayMode_t
pinofal 9:900e18a2310b 79 0, //Dwelling time in ms
pinofal 9:900e18a2310b 80 FALSE, //Automatic HIZ STOP
pinofal 9:900e18a2310b 81 100000 //VREFA and VREFB PWM frequency (Hz)
pinofal 9:900e18a2310b 82 };
pinofal 9:900e18a2310b 83
pinofal 9:900e18a2310b 84 /* Motor Control Component. */
pinofal 9:900e18a2310b 85 L6208 *motor;
pinofal 9:900e18a2310b 86
pinofal 9:900e18a2310b 87 /* Functions -----------------------------------------------------------------*/
pinofal 9:900e18a2310b 88
pinofal 9:900e18a2310b 89 /**
pinofal 9:900e18a2310b 90 * @brief This is an example of user handler for the flag interrupt.
pinofal 9:900e18a2310b 91 * @param None
pinofal 9:900e18a2310b 92 * @retval None
pinofal 9:900e18a2310b 93 * @note If needed, implement it, and then attach and enable it:
pinofal 9:900e18a2310b 94 * + motor->attach_flag_irq(&my_flag_irq_handler);
pinofal 9:900e18a2310b 95 * + motor->enable_flag_irq();
pinofal 9:900e18a2310b 96 * To disable it:
pinofal 9:900e18a2310b 97 * + motor->DisbleFlagIRQ();
pinofal 9:900e18a2310b 98 */
pinofal 9:900e18a2310b 99 void my_flag_irq_handler(void)
pinofal 9:900e18a2310b 100 {
pinofal 9:900e18a2310b 101 pc.printf(" WARNING: \"FLAG\" interrupt triggered:\r\n");
pinofal 9:900e18a2310b 102 motor->disable();
pinofal 9:900e18a2310b 103 pc.printf(" Motor disabled.\r\n\n");
pinofal 9:900e18a2310b 104 }
pinofal 9:900e18a2310b 105
pinofal 9:900e18a2310b 106 /**
pinofal 9:900e18a2310b 107 * @brief This is an example of error handler.
pinofal 9:900e18a2310b 108 * @param[in] error Number of the error
pinofal 9:900e18a2310b 109 * @retval None
pinofal 9:900e18a2310b 110 * @note If needed, implement it, and then attach it:
pinofal 9:900e18a2310b 111 * + motor->attach_error_handler(&my_error_handler);
pinofal 9:900e18a2310b 112 */
pinofal 9:900e18a2310b 113 void my_error_handler(uint16_t error)
pinofal 9:900e18a2310b 114 {
pinofal 9:900e18a2310b 115 /* Printing to the console. */
pinofal 9:900e18a2310b 116 pc.printf("Error %d detected\r\n\n", error);
pinofal 9:900e18a2310b 117
pinofal 9:900e18a2310b 118 /* Infinite loop */
pinofal 9:900e18a2310b 119 while (true) {
pinofal 9:900e18a2310b 120 }
pinofal 9:900e18a2310b 121 }
pinofal 9:900e18a2310b 122
pinofal 9:900e18a2310b 123 /* Main ----------------------------------------------------------------------*/
pinofal 9:900e18a2310b 124
pinofal 9:900e18a2310b 125 int main()
pinofal 9:900e18a2310b 126 {
pinofal 9:900e18a2310b 127 // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto
pinofal 9:900e18a2310b 128 pc.baud(921600); //921600 bps
pinofal 9:900e18a2310b 129 //pc.baud(9600); //256000 bps
pinofal 9:900e18a2310b 130 pc.printf("*** Test Motor ***\n\r");
pinofal 9:900e18a2310b 131
pinofal 9:900e18a2310b 132 /* Printing to the console. */
pinofal 9:900e18a2310b 133 pc.printf("STARTING MAIN PROGRAM\r\n");
pinofal 9:900e18a2310b 134 pc.printf(" Reminder:\r\n");
pinofal 9:900e18a2310b 135 pc.printf(" The position unit is in agreement to the step mode.\r\n");
pinofal 9:900e18a2310b 136 pc.printf(" The speed, acceleration or deceleration unit depend on the step mode:\r\n");
pinofal 9:900e18a2310b 137 pc.printf(" - For normal mode and half step mode, the unit is steps/s or /s^2.\r\n");
pinofal 9:900e18a2310b 138 pc.printf(" - For microstep modes, the unit is (1/16)steps/s or /s^2.\r\n");
pinofal 9:900e18a2310b 139
pinofal 9:900e18a2310b 140 //----- Initialization
pinofal 9:900e18a2310b 141 /* Initializing Motor Control Component. */
pinofal 9:900e18a2310b 142 motor = new L6208(D2, D8, D7, D4, D5, D6, VREFA_PWM_PIN, VREFB_PWM_PIN);
pinofal 9:900e18a2310b 143 if (motor->init(&init) != COMPONENT_OK) {
pinofal 9:900e18a2310b 144 exit(EXIT_FAILURE);
pinofal 9:900e18a2310b 145 }
pinofal 9:900e18a2310b 146
pinofal 9:900e18a2310b 147 /* Attaching and enabling an interrupt handler. */
pinofal 9:900e18a2310b 148 motor->attach_flag_irq(&my_flag_irq_handler);
pinofal 9:900e18a2310b 149 motor->enable_flag_irq();
pinofal 9:900e18a2310b 150
pinofal 9:900e18a2310b 151 /* Attaching an error handler */
pinofal 9:900e18a2310b 152 motor->attach_error_handler(&my_error_handler);
pinofal 9:900e18a2310b 153
pinofal 9:900e18a2310b 154 /* Printing to the console. */
pinofal 9:900e18a2310b 155 pc.printf("Motor Control Application Example for 1 Motor\r\n");
pinofal 9:900e18a2310b 156
pinofal 9:900e18a2310b 157 //----- run the motor BACKWARD
pinofal 9:900e18a2310b 158 pc.printf("--> Running the motor backward.\r\n");
pinofal 9:900e18a2310b 159 motor->run(StepperMotor::BWD);
pinofal 9:900e18a2310b 160
francesco01 11:a7e099ee909e 161
pinofal 9:900e18a2310b 162
pinofal 9:900e18a2310b 163
pinofal 9:900e18a2310b 164 //----- Soft stop required while running
pinofal 9:900e18a2310b 165 pc.printf("--> Soft stop requested.\r\n");
francesco01 10:2622d1ab5984 166 motor->soft_stop();
pinofal 9:900e18a2310b 167
francesco01 10:2622d1ab5984 168
pinofal 9:900e18a2310b 169 //----- Change step mode to full step mode
pinofal 9:900e18a2310b 170 motor->set_step_mode(StepperMotor::STEP_MODE_FULL);
pinofal 9:900e18a2310b 171 pc.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());
pinofal 9:900e18a2310b 172
pinofal 9:900e18a2310b 173 /* Get current position of device and print to the console */
pinofal 9:900e18a2310b 174 pc.printf(" Position: %d.\r\n", motor->get_position());
pinofal 9:900e18a2310b 175
pinofal 9:900e18a2310b 176 /* Set speed, acceleration and deceleration to scale with normal mode */
pinofal 9:900e18a2310b 177 motor->set_max_speed(init.maxSpeedSps>>4);
pinofal 9:900e18a2310b 178 motor->set_acceleration(motor->get_acceleration()>>4);
pinofal 9:900e18a2310b 179 motor->set_deceleration(motor->get_deceleration()>>4);
pinofal 9:900e18a2310b 180 /* Print parameters to the console */
pinofal 9:900e18a2310b 181 pc.printf(" Motor Max Speed: %d step/s.\r\n", motor->get_max_speed());
pinofal 9:900e18a2310b 182 pc.printf(" Motor Min Speed: %d step/s.\r\n", motor->get_min_speed());
pinofal 9:900e18a2310b 183 pc.printf(" Motor Acceleration: %d step/s.\r\n", motor->get_acceleration());
pinofal 9:900e18a2310b 184 pc.printf(" Motor Deceleration: %d step/s.\r\n", motor->get_deceleration());
pinofal 9:900e18a2310b 185
pinofal 9:900e18a2310b 186 //----- move of 200 steps in the FW direction
pinofal 9:900e18a2310b 187 pc.printf("--> Moving forward 200 steps.\r\n");
pinofal 9:900e18a2310b 188 motor->move(StepperMotor::FWD, 200);
francesco01 10:2622d1ab5984 189 int i,a=0;
pinofal 9:900e18a2310b 190
francesco01 10:2622d1ab5984 191 while (true) {
pinofal 9:900e18a2310b 192
francesco01 11:a7e099ee909e 193 /* if ( Input == 0 )
francesco01 10:2622d1ab5984 194 {
francesco01 10:2622d1ab5984 195 a=a+1;
francesco01 10:2622d1ab5984 196 }
francesco01 10:2622d1ab5984 197 if(a>1)
francesco01 10:2622d1ab5984 198 {
francesco01 10:2622d1ab5984 199 a=0;
francesco01 10:2622d1ab5984 200
francesco01 11:a7e099ee909e 201 }*/
francesco01 10:2622d1ab5984 202
francesco01 11:a7e099ee909e 203 if(myButton==0)
francesco01 10:2622d1ab5984 204 {
francesco01 10:2622d1ab5984 205 /* Request device to go position -3200 */
francesco01 10:2622d1ab5984 206 motor->go_to(150);
francesco01 10:2622d1ab5984 207 /* Waiting while the motor is active. */
francesco01 10:2622d1ab5984 208 motor->wait_while_active();
francesco01 10:2622d1ab5984 209 }
francesco01 10:2622d1ab5984 210 else
francesco01 10:2622d1ab5984 211 {
francesco01 10:2622d1ab5984 212 /* Request device to go position -3200 */
francesco01 10:2622d1ab5984 213 motor->go_to(-150);
francesco01 10:2622d1ab5984 214 /* Waiting while the motor is active. */
francesco01 10:2622d1ab5984 215 motor->wait_while_active();
pinofal 9:900e18a2310b 216
francesco01 10:2622d1ab5984 217 }
francesco01 10:2622d1ab5984 218
pinofal 9:900e18a2310b 219
francesco01 10:2622d1ab5984 220 }
pinofal 9:900e18a2310b 221 }
pinofal 9:900e18a2310b 222 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/