APS2 MEA Rodrigo Vianna

Dependencies:   mbed X_NUCLEO_IHM01A1

Committer:
rodrigo_vianna
Date:
Mon Oct 25 23:29:55 2021 +0000
Revision:
38:08e421486f20
Parent:
35:2b44ed4ec7a0
APS 2 MEA Rodrigo Vianna

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:e6a49a092e2a 1 /**
Davidroid 0:e6a49a092e2a 2 ******************************************************************************
Davidroid 0:e6a49a092e2a 3 * @file main.cpp
Davidroid 21:0189493b15ec 4 * @author Davide Aliprandi, STMicroelectronics
Davidroid 0:e6a49a092e2a 5 * @version V1.0.0
Davidroid 0:e6a49a092e2a 6 * @date October 14th, 2015
Davidroid 21:0189493b15ec 7 * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1
Davidroid 0:e6a49a092e2a 8 * Motor Control Expansion Board: control of 1 motor.
Davidroid 0:e6a49a092e2a 9 ******************************************************************************
Davidroid 0:e6a49a092e2a 10 * @attention
Davidroid 0:e6a49a092e2a 11 *
Davidroid 0:e6a49a092e2a 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:e6a49a092e2a 13 *
Davidroid 0:e6a49a092e2a 14 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:e6a49a092e2a 15 * are permitted provided that the following conditions are met:
Davidroid 0:e6a49a092e2a 16 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:e6a49a092e2a 17 * this list of conditions and the following disclaimer.
Davidroid 0:e6a49a092e2a 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:e6a49a092e2a 19 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:e6a49a092e2a 20 * and/or other materials provided with the distribution.
Davidroid 0:e6a49a092e2a 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:e6a49a092e2a 22 * may be used to endorse or promote products derived from this software
Davidroid 0:e6a49a092e2a 23 * without specific prior written permission.
Davidroid 0:e6a49a092e2a 24 *
Davidroid 0:e6a49a092e2a 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:e6a49a092e2a 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:e6a49a092e2a 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:e6a49a092e2a 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:e6a49a092e2a 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:e6a49a092e2a 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:e6a49a092e2a 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:e6a49a092e2a 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:e6a49a092e2a 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:e6a49a092e2a 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:e6a49a092e2a 35 *
Davidroid 0:e6a49a092e2a 36 ******************************************************************************
Davidroid 0:e6a49a092e2a 37 */
Davidroid 0:e6a49a092e2a 38
Davidroid 0:e6a49a092e2a 39
Davidroid 0:e6a49a092e2a 40 /* Includes ------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 41
Davidroid 0:e6a49a092e2a 42 /* mbed specific header files. */
Davidroid 0:e6a49a092e2a 43 #include "mbed.h"
Davidroid 0:e6a49a092e2a 44
Davidroid 0:e6a49a092e2a 45 /* Helper header files. */
Davidroid 0:e6a49a092e2a 46 #include "DevSPI.h"
Davidroid 0:e6a49a092e2a 47
Davidroid 0:e6a49a092e2a 48 /* Component specific header files. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 49 #include "L6474.h"
Davidroid 0:e6a49a092e2a 50
rodrigo_vianna 38:08e421486f20 51 //
rodrigo_vianna 38:08e421486f20 52 DigitalIn botao(PC_13);
rodrigo_vianna 38:08e421486f20 53
Davidroid 0:e6a49a092e2a 54
Davidroid 0:e6a49a092e2a 55 /* Definitions ---------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 56
Davidroid 26:b0203c2265e5 57 /* Number of steps. */
rodrigo_vianna 38:08e421486f20 58 #define STEPS_1 (200*2) /* 1 revolution given a 200 steps motor configured at 1/2 microstep mode. */
Davidroid 26:b0203c2265e5 59
Davidroid 26:b0203c2265e5 60 /* Delay in milliseconds. */
Davidroid 26:b0203c2265e5 61 #define DELAY_1 1000
Davidroid 26:b0203c2265e5 62 #define DELAY_2 2000
Davidroid 26:b0203c2265e5 63 #define DELAY_3 6000
Davidroid 26:b0203c2265e5 64 #define DELAY_4 8000
Davidroid 26:b0203c2265e5 65
Davidroid 29:a80a213c3c94 66 /* Speed in pps (Pulses Per Second).
Davidroid 29:a80a213c3c94 67 In Full Step mode: 1 pps = 1 step/s).
Davidroid 29:a80a213c3c94 68 In 1/N Step Mode: N pps = 1 step/s). */
Davidroid 26:b0203c2265e5 69 #define SPEED_1 2400
Davidroid 26:b0203c2265e5 70 #define SPEED_2 1200
Davidroid 0:e6a49a092e2a 71
Davidroid 0:e6a49a092e2a 72
Davidroid 0:e6a49a092e2a 73 /* Variables -----------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 74
Davidroid 19:1cd7f65c155c 75 /* Initialization parameters. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 76 L6474_init_t init = {
rodrigo_vianna 38:08e421486f20 77 100, /* Acceleration rate in pps^2. Range: (0..+inf). */
rodrigo_vianna 38:08e421486f20 78 100, /* Deceleration rate in pps^2. Range: (0..+inf). */
rodrigo_vianna 38:08e421486f20 79 700, /* Maximum speed in pps. Range: (30..10000]. */
rodrigo_vianna 38:08e421486f20 80 30, /* Minimum speed in pps. Range: [30..10000). */
Davidroid 19:1cd7f65c155c 81 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */
Davidroid 19:1cd7f65c155c 82 L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */
Davidroid 19:1cd7f65c155c 83 L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */
Davidroid 19:1cd7f65c155c 84 L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */
rodrigo_vianna 38:08e421486f20 85 L6474_STEP_SEL_1_2, /* Step selection (STEP_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 86 L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */
Davidroid 19:1cd7f65c155c 87 L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 88 L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */
Davidroid 19:1cd7f65c155c 89 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 90 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */
Davidroid 19:1cd7f65c155c 91 L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */
Davidroid 19:1cd7f65c155c 92 L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */
Davidroid 19:1cd7f65c155c 93 L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */
Davidroid 19:1cd7f65c155c 94 L6474_ALARM_EN_OVERCURRENT |
Davidroid 19:1cd7f65c155c 95 L6474_ALARM_EN_THERMAL_SHUTDOWN |
Davidroid 19:1cd7f65c155c 96 L6474_ALARM_EN_THERMAL_WARNING |
Davidroid 19:1cd7f65c155c 97 L6474_ALARM_EN_UNDERVOLTAGE |
Davidroid 19:1cd7f65c155c 98 L6474_ALARM_EN_SW_TURN_ON |
Davidroid 19:1cd7f65c155c 99 L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */
Davidroid 19:1cd7f65c155c 100 };
Davidroid 19:1cd7f65c155c 101
Davidroid 0:e6a49a092e2a 102 /* Motor Control Component. */
Davidroid 1:fbf28f3367aa 103 L6474 *motor;
Davidroid 0:e6a49a092e2a 104
Davidroid 0:e6a49a092e2a 105
Davidroid 19:1cd7f65c155c 106 /* Functions -----------------------------------------------------------------*/
Davidroid 19:1cd7f65c155c 107
Davidroid 19:1cd7f65c155c 108 /**
Davidroid 25:3c863b420ac5 109 * @brief This is an example of user handler for the flag interrupt.
Davidroid 25:3c863b420ac5 110 * @param None
Davidroid 25:3c863b420ac5 111 * @retval None
Davidroid 25:3c863b420ac5 112 * @note If needed, implement it, and then attach and enable it:
davide.aliprandi@st.com 35:2b44ed4ec7a0 113 * + motor->attach_flag_irq(&flag_irq_handler);
davide.aliprandi@st.com 35:2b44ed4ec7a0 114 * + motor->enable_flag_irq();
Davidroid 25:3c863b420ac5 115 * To disable it:
davide.aliprandi@st.com 35:2b44ed4ec7a0 116 * + motor->disble_flag_irq();
Davidroid 25:3c863b420ac5 117 */
davide.aliprandi@st.com 35:2b44ed4ec7a0 118 void flag_irq_handler(void)
Davidroid 19:1cd7f65c155c 119 {
Davidroid 19:1cd7f65c155c 120 /* Set ISR flag. */
Davidroid 19:1cd7f65c155c 121 motor->isr_flag = TRUE;
Davidroid 19:1cd7f65c155c 122
Davidroid 19:1cd7f65c155c 123 /* Get the value of the status register. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 124 unsigned int status = motor->get_status();
Davidroid 34:543d0d1147d9 125
Davidroid 19:1cd7f65c155c 126 /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */
Davidroid 19:1cd7f65c155c 127 /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */
Davidroid 34:543d0d1147d9 128 if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) {
Davidroid 19:1cd7f65c155c 129 printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n");
Davidroid 34:543d0d1147d9 130 }
rodrigo_vianna 38:08e421486f20 131
Davidroid 19:1cd7f65c155c 132 /* Reset ISR flag. */
Davidroid 19:1cd7f65c155c 133 motor->isr_flag = FALSE;
Davidroid 19:1cd7f65c155c 134 }
Davidroid 19:1cd7f65c155c 135
Davidroid 0:e6a49a092e2a 136 /* Main ----------------------------------------------------------------------*/
Davidroid 0:e6a49a092e2a 137
Davidroid 0:e6a49a092e2a 138 int main()
Davidroid 0:e6a49a092e2a 139 {
Davidroid 6:32166bfc04b0 140 /*----- Initialization. -----*/
rodrigo_vianna 38:08e421486f20 141
Davidroid 0:e6a49a092e2a 142 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:e6a49a092e2a 143
Davidroid 11:3e303a25770d 144 /* Initializing Motor Control Component. */
Davidroid 5:8065b587ade0 145 motor = new L6474(D2, D8, D7, D9, D10, dev_spi);
davide.aliprandi@st.com 35:2b44ed4ec7a0 146 if (motor->init(&init) != COMPONENT_OK) {
Davidroid 17:4830b25fec7f 147 exit(EXIT_FAILURE);
Davidroid 34:543d0d1147d9 148 }
Davidroid 0:e6a49a092e2a 149
Davidroid 19:1cd7f65c155c 150 /* Attaching and enabling interrupt handlers. */
davide.aliprandi@st.com 35:2b44ed4ec7a0 151 motor->attach_flag_irq(&flag_irq_handler);
davide.aliprandi@st.com 35:2b44ed4ec7a0 152 motor->enable_flag_irq();
Davidroid 19:1cd7f65c155c 153
Davidroid 6:32166bfc04b0 154
Davidroid 6:32166bfc04b0 155 /* Infinite Loop. */
Davidroid 34:543d0d1147d9 156 while (true) {
rodrigo_vianna 38:08e421486f20 157
rodrigo_vianna 38:08e421486f20 158 if(!botao){
rodrigo_vianna 38:08e421486f20 159 //15 voltas para frente
rodrigo_vianna 38:08e421486f20 160 motor->move(StepperMotor::FWD, STEPS_1*15);
rodrigo_vianna 38:08e421486f20 161 //Espera
rodrigo_vianna 38:08e421486f20 162 motor->wait_while_active();
rodrigo_vianna 38:08e421486f20 163
rodrigo_vianna 38:08e421486f20 164 wait(1);
rodrigo_vianna 38:08e421486f20 165 //15 voltas para trás
rodrigo_vianna 38:08e421486f20 166 motor->move(StepperMotor::BWD, STEPS_1*15);
rodrigo_vianna 38:08e421486f20 167
Davidroid 0:e6a49a092e2a 168
rodrigo_vianna 38:08e421486f20 169 }
Davidroid 0:e6a49a092e2a 170 }
Davidroid 0:e6a49a092e2a 171 }