Rodrigo Vianna / Mbed 2 deprecated APS2-MEA-2021_1-RodrigoVianna

Dependencies:   mbed X_NUCLEO_IHM01A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  Davide Aliprandi, STMicroelectronics
00005  * @version V1.0.0
00006  * @date    October 14th, 2015
00007  * @brief   mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
00008  *          Motor Control Expansion Board: control of 1 motor.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 
00042 /* mbed specific header files. */
00043 #include "mbed.h"
00044 
00045 /* Helper header files. */
00046 #include "DevSPI.h"
00047 
00048 /* Component specific header files. */
00049 #include "L6474.h"
00050 
00051 //
00052 DigitalIn botao(PC_13);
00053 
00054 
00055 /* Definitions ---------------------------------------------------------------*/
00056 
00057 /* Number of steps. */
00058 #define STEPS_1 (200*2)   /* 1 revolution given a 200 steps motor configured at 1/2 microstep mode. */
00059 
00060 /* Delay in milliseconds. */
00061 #define DELAY_1 1000
00062 #define DELAY_2 2000
00063 #define DELAY_3 6000
00064 #define DELAY_4 8000
00065 
00066 /* Speed in pps (Pulses Per Second).
00067    In Full Step mode: 1 pps = 1 step/s).
00068    In 1/N Step Mode:  N pps = 1 step/s). */
00069 #define SPEED_1 2400
00070 #define SPEED_2 1200
00071 
00072 
00073 /* Variables -----------------------------------------------------------------*/
00074 
00075 /* Initialization parameters. */
00076 L6474_init_t init = {
00077     100,                              /* Acceleration rate in pps^2. Range: (0..+inf). */
00078     100,                              /* Deceleration rate in pps^2. Range: (0..+inf). */
00079     700,                             /* Maximum speed in pps. Range: (30..10000]. */
00080     30,                              /* Minimum speed in pps. Range: [30..10000). */
00081     250,                              /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
00082     L6474_OCD_TH_750mA,               /* Overcurrent threshold (OCD_TH register). */
00083     L6474_CONFIG_OC_SD_ENABLE,        /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
00084     L6474_CONFIG_EN_TQREG_TVAL_USED,  /* Torque regulation method (EN_TQREG field of CONFIG register). */
00085     L6474_STEP_SEL_1_2,               /* Step selection (STEP_SEL field of STEP_MODE register). */
00086     L6474_SYNC_SEL_1_2,               /* Sync selection (SYNC_SEL field of STEP_MODE register). */
00087     L6474_FAST_STEP_12us,             /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
00088     L6474_TOFF_FAST_8us,              /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
00089     3,                                /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
00090     21,                               /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
00091     L6474_CONFIG_TOFF_044us,          /* Target Swicthing Period (field TOFF of CONFIG register). */
00092     L6474_CONFIG_SR_320V_us,          /* Slew rate (POW_SR field of CONFIG register). */
00093     L6474_CONFIG_INT_16MHZ,           /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
00094     L6474_ALARM_EN_OVERCURRENT |
00095     L6474_ALARM_EN_THERMAL_SHUTDOWN |
00096     L6474_ALARM_EN_THERMAL_WARNING |
00097     L6474_ALARM_EN_UNDERVOLTAGE |
00098     L6474_ALARM_EN_SW_TURN_ON |
00099     L6474_ALARM_EN_WRONG_NPERF_CMD    /* Alarm (ALARM_EN register). */
00100 };
00101 
00102 /* Motor Control Component. */
00103 L6474 *motor;
00104 
00105 
00106 /* Functions -----------------------------------------------------------------*/
00107 
00108 /**
00109  * @brief  This is an example of user handler for the flag interrupt.
00110  * @param  None
00111  * @retval None
00112  * @note   If needed, implement it, and then attach and enable it:
00113  *           + motor->attach_flag_irq(&flag_irq_handler);
00114  *           + motor->enable_flag_irq();
00115  *         To disable it:
00116  *           + motor->disble_flag_irq();
00117  */
00118 void flag_irq_handler(void)
00119 {
00120     /* Set ISR flag. */
00121     motor->isr_flag = TRUE;
00122 
00123     /* Get the value of the status register. */
00124     unsigned int status = motor->get_status();
00125 
00126     /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
00127     /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
00128     if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
00129         printf("    WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
00130     }
00131 
00132     /* Reset ISR flag. */
00133     motor->isr_flag = FALSE;
00134 }
00135 
00136 /* Main ----------------------------------------------------------------------*/
00137 
00138 int main()
00139 {
00140     /*----- Initialization. -----*/
00141     
00142     DevSPI dev_spi(D11, D12, D13);
00143 
00144     /* Initializing Motor Control Component. */
00145     motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
00146     if (motor->init(&init) != COMPONENT_OK) {
00147         exit(EXIT_FAILURE);
00148     }
00149 
00150     /* Attaching and enabling interrupt handlers. */
00151     motor->attach_flag_irq(&flag_irq_handler);
00152     motor->enable_flag_irq();
00153 
00154 
00155     /* Infinite Loop. */
00156     while (true) {
00157         
00158         if(!botao){
00159             //15 voltas para frente
00160             motor->move(StepperMotor::FWD, STEPS_1*15);
00161             //Espera 
00162             motor->wait_while_active();
00163             
00164             wait(1);
00165             //15 voltas para trás
00166             motor->move(StepperMotor::BWD, STEPS_1*15);
00167         
00168 
00169             }
00170     }
00171 }