Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EsmacatShield X_NUCLEO_IHM01A1
main.cpp@40:74e394ae8b5f, 2020-02-12 (annotated)
- Committer:
- pratima_hb
- Date:
- Wed Feb 12 22:08:37 2020 +0000
- Revision:
- 40:74e394ae8b5f
- Parent:
- 39:1270497828ca
cosmetic changes
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pratima_hb | 39:1270497828ca | 1 | /** |
| pratima_hb | 39:1270497828ca | 2 | ****************************************************************************** |
| pratima_hb | 39:1270497828ca | 3 | * @file main.cpp |
| pratima_hb | 39:1270497828ca | 4 | * @date February 06, 2020 |
| pratima_hb | 39:1270497828ca | 5 | * @brief mbed test application Esmacat Shield working together with |
| pratima_hb | 39:1270497828ca | 6 | * STMicroelectronics X-NUCLEO-IHM01A1 |
| pratima_hb | 39:1270497828ca | 7 | * Motor Control Expansion Board: control of 1 motor. |
| pratima_hb | 39:1270497828ca | 8 | * The original code is STMicroelectronics which is modified to integrate EASE. |
| pratima_hb | 39:1270497828ca | 9 | *The Copyright of STMicroelectronics is retained below. |
| pratima_hb | 39:1270497828ca | 10 | ****************************************************************************** |
| pratima_hb | 39:1270497828ca | 11 | |
| pratima_hb | 39:1270497828ca | 12 | Copyright (c) 2020 https://www.esmacat.com/ |
| pratima_hb | 39:1270497828ca | 13 | |
| pratima_hb | 39:1270497828ca | 14 | Licensed under the Apache License, Version 2.0 (the "License"); |
| pratima_hb | 39:1270497828ca | 15 | you may not use this file except in compliance with the License. |
| pratima_hb | 39:1270497828ca | 16 | You may obtain a copy of the License at |
| pratima_hb | 39:1270497828ca | 17 | |
| pratima_hb | 39:1270497828ca | 18 | http://www.apache.org/licenses/LICENSE-2.0 |
| pratima_hb | 39:1270497828ca | 19 | |
| pratima_hb | 39:1270497828ca | 20 | Unless required by applicable law or agreed to in writing, software |
| pratima_hb | 39:1270497828ca | 21 | distributed under the License is distributed on an "AS IS" BASIS, |
| pratima_hb | 39:1270497828ca | 22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| pratima_hb | 39:1270497828ca | 23 | See the License for the specific language governing permissions and |
| pratima_hb | 39:1270497828ca | 24 | limitations under the License. |
| pratima_hb | 39:1270497828ca | 25 | |
| pratima_hb | 39:1270497828ca | 26 | EsmacatShield.h - Library for using EtherCAT Arduino Shield by Esmacat(EASE). |
| pratima_hb | 39:1270497828ca | 27 | Created by Esmacat, 01/22/2020 |
| pratima_hb | 39:1270497828ca | 28 | |
| pratima_hb | 39:1270497828ca | 29 | ******************************************************************************* |
| pratima_hb | 39:1270497828ca | 30 | * @file EsmacatShield.h |
| pratima_hb | 39:1270497828ca | 31 | ******************************************************************************* |
| pratima_hb | 39:1270497828ca | 32 | */ |
| pratima_hb | 39:1270497828ca | 33 | |
| pratima_hb | 39:1270497828ca | 34 | |
| Davidroid | 0:e6a49a092e2a | 35 | /** |
| Davidroid | 0:e6a49a092e2a | 36 | ****************************************************************************** |
| Davidroid | 0:e6a49a092e2a | 37 | * @file main.cpp |
| Davidroid | 21:0189493b15ec | 38 | * @author Davide Aliprandi, STMicroelectronics |
| Davidroid | 0:e6a49a092e2a | 39 | * @version V1.0.0 |
| Davidroid | 0:e6a49a092e2a | 40 | * @date October 14th, 2015 |
| Davidroid | 21:0189493b15ec | 41 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IHM01A1 |
| Davidroid | 0:e6a49a092e2a | 42 | * Motor Control Expansion Board: control of 1 motor. |
| Davidroid | 0:e6a49a092e2a | 43 | ****************************************************************************** |
| Davidroid | 0:e6a49a092e2a | 44 | * @attention |
| Davidroid | 0:e6a49a092e2a | 45 | * |
| Davidroid | 0:e6a49a092e2a | 46 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
| Davidroid | 0:e6a49a092e2a | 47 | * |
| Davidroid | 0:e6a49a092e2a | 48 | * Redistribution and use in source and binary forms, with or without modification, |
| Davidroid | 0:e6a49a092e2a | 49 | * are permitted provided that the following conditions are met: |
| Davidroid | 0:e6a49a092e2a | 50 | * 1. Redistributions of source code must retain the above copyright notice, |
| Davidroid | 0:e6a49a092e2a | 51 | * this list of conditions and the following disclaimer. |
| Davidroid | 0:e6a49a092e2a | 52 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| Davidroid | 0:e6a49a092e2a | 53 | * this list of conditions and the following disclaimer in the documentation |
| Davidroid | 0:e6a49a092e2a | 54 | * and/or other materials provided with the distribution. |
| Davidroid | 0:e6a49a092e2a | 55 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| Davidroid | 0:e6a49a092e2a | 56 | * may be used to endorse or promote products derived from this software |
| Davidroid | 0:e6a49a092e2a | 57 | * without specific prior written permission. |
| Davidroid | 0:e6a49a092e2a | 58 | * |
| Davidroid | 0:e6a49a092e2a | 59 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| Davidroid | 0:e6a49a092e2a | 60 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| Davidroid | 0:e6a49a092e2a | 61 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| Davidroid | 0:e6a49a092e2a | 62 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| Davidroid | 0:e6a49a092e2a | 63 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| Davidroid | 0:e6a49a092e2a | 64 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| Davidroid | 0:e6a49a092e2a | 65 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| Davidroid | 0:e6a49a092e2a | 66 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| Davidroid | 0:e6a49a092e2a | 67 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| Davidroid | 0:e6a49a092e2a | 68 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Davidroid | 0:e6a49a092e2a | 69 | * |
| Davidroid | 0:e6a49a092e2a | 70 | ****************************************************************************** |
| Davidroid | 0:e6a49a092e2a | 71 | */ |
| Davidroid | 0:e6a49a092e2a | 72 | |
| Davidroid | 0:e6a49a092e2a | 73 | |
| Davidroid | 0:e6a49a092e2a | 74 | /* Includes ------------------------------------------------------------------*/ |
| Davidroid | 0:e6a49a092e2a | 75 | |
| Davidroid | 0:e6a49a092e2a | 76 | /* mbed specific header files. */ |
| Davidroid | 0:e6a49a092e2a | 77 | #include "mbed.h" |
| Davidroid | 38:ca71d404823c | 78 | #include "rtos.h" |
| Davidroid | 0:e6a49a092e2a | 79 | |
| Davidroid | 0:e6a49a092e2a | 80 | /* Helper header files. */ |
| Davidroid | 0:e6a49a092e2a | 81 | #include "DevSPI.h" |
| Davidroid | 0:e6a49a092e2a | 82 | |
| Davidroid | 0:e6a49a092e2a | 83 | /* Component specific header files. */ |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 84 | #include "L6474.h" |
| Davidroid | 0:e6a49a092e2a | 85 | |
| pratima_hb | 39:1270497828ca | 86 | /* Esmacat code*/ |
| pratima_hb | 39:1270497828ca | 87 | #include <EsmacatShield.h> //Include Esmacat Library |
| pratima_hb | 39:1270497828ca | 88 | |
| pratima_hb | 39:1270497828ca | 89 | int16_t ease_read[8]; //EASE 8 registers |
| pratima_hb | 39:1270497828ca | 90 | |
| pratima_hb | 39:1270497828ca | 91 | /*D9 Chip select for EASE. Remember to change the jumper on the board*/ |
| pratima_hb | 39:1270497828ca | 92 | DigitalOut selectPin(D9); |
| pratima_hb | 39:1270497828ca | 93 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
| pratima_hb | 39:1270497828ca | 94 | /* Esmacat code*/ |
| Davidroid | 0:e6a49a092e2a | 95 | |
| Davidroid | 0:e6a49a092e2a | 96 | /* Definitions ---------------------------------------------------------------*/ |
| Davidroid | 26:b0203c2265e5 | 97 | /* Delay in milliseconds. */ |
| Davidroid | 26:b0203c2265e5 | 98 | #define DELAY_1 1000 |
| Davidroid | 0:e6a49a092e2a | 99 | |
| Davidroid | 0:e6a49a092e2a | 100 | /* Variables -----------------------------------------------------------------*/ |
| Davidroid | 0:e6a49a092e2a | 101 | |
| Davidroid | 19:1cd7f65c155c | 102 | /* Initialization parameters. */ |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 103 | L6474_init_t init = { |
| Davidroid | 29:a80a213c3c94 | 104 | 160, /* Acceleration rate in pps^2. Range: (0..+inf). */ |
| Davidroid | 29:a80a213c3c94 | 105 | 160, /* Deceleration rate in pps^2. Range: (0..+inf). */ |
| Davidroid | 29:a80a213c3c94 | 106 | 1600, /* Maximum speed in pps. Range: (30..10000]. */ |
| Davidroid | 29:a80a213c3c94 | 107 | 800, /* Minimum speed in pps. Range: [30..10000). */ |
| Davidroid | 19:1cd7f65c155c | 108 | 250, /* Torque regulation current in mA. Range: 31.25mA to 4000mA. */ |
| Davidroid | 19:1cd7f65c155c | 109 | L6474_OCD_TH_750mA, /* Overcurrent threshold (OCD_TH register). */ |
| Davidroid | 19:1cd7f65c155c | 110 | L6474_CONFIG_OC_SD_ENABLE, /* Overcurrent shutwdown (OC_SD field of CONFIG register). */ |
| Davidroid | 19:1cd7f65c155c | 111 | L6474_CONFIG_EN_TQREG_TVAL_USED, /* Torque regulation method (EN_TQREG field of CONFIG register). */ |
| Davidroid | 26:b0203c2265e5 | 112 | L6474_STEP_SEL_1_8, /* Step selection (STEP_SEL field of STEP_MODE register). */ |
| Davidroid | 19:1cd7f65c155c | 113 | L6474_SYNC_SEL_1_2, /* Sync selection (SYNC_SEL field of STEP_MODE register). */ |
| Davidroid | 19:1cd7f65c155c | 114 | L6474_FAST_STEP_12us, /* Fall time value (T_FAST field of T_FAST register). Range: 2us to 32us. */ |
| Davidroid | 19:1cd7f65c155c | 115 | L6474_TOFF_FAST_8us, /* Maximum fast decay time (T_OFF field of T_FAST register). Range: 2us to 32us. */ |
| Davidroid | 19:1cd7f65c155c | 116 | 3, /* Minimum ON time in us (TON_MIN register). Range: 0.5us to 64us. */ |
| Davidroid | 19:1cd7f65c155c | 117 | 21, /* Minimum OFF time in us (TOFF_MIN register). Range: 0.5us to 64us. */ |
| Davidroid | 19:1cd7f65c155c | 118 | L6474_CONFIG_TOFF_044us, /* Target Swicthing Period (field TOFF of CONFIG register). */ |
| Davidroid | 19:1cd7f65c155c | 119 | L6474_CONFIG_SR_320V_us, /* Slew rate (POW_SR field of CONFIG register). */ |
| Davidroid | 19:1cd7f65c155c | 120 | L6474_CONFIG_INT_16MHZ, /* Clock setting (OSC_CLK_SEL field of CONFIG register). */ |
| Davidroid | 19:1cd7f65c155c | 121 | L6474_ALARM_EN_OVERCURRENT | |
| Davidroid | 19:1cd7f65c155c | 122 | L6474_ALARM_EN_THERMAL_SHUTDOWN | |
| Davidroid | 19:1cd7f65c155c | 123 | L6474_ALARM_EN_THERMAL_WARNING | |
| Davidroid | 19:1cd7f65c155c | 124 | L6474_ALARM_EN_UNDERVOLTAGE | |
| Davidroid | 19:1cd7f65c155c | 125 | L6474_ALARM_EN_SW_TURN_ON | |
| Davidroid | 19:1cd7f65c155c | 126 | L6474_ALARM_EN_WRONG_NPERF_CMD /* Alarm (ALARM_EN register). */ |
| Davidroid | 19:1cd7f65c155c | 127 | }; |
| Davidroid | 19:1cd7f65c155c | 128 | |
| Davidroid | 0:e6a49a092e2a | 129 | /* Motor Control Component. */ |
| Davidroid | 1:fbf28f3367aa | 130 | L6474 *motor; |
| Davidroid | 0:e6a49a092e2a | 131 | |
| Davidroid | 0:e6a49a092e2a | 132 | |
| Davidroid | 19:1cd7f65c155c | 133 | /* Functions -----------------------------------------------------------------*/ |
| Davidroid | 19:1cd7f65c155c | 134 | |
| Davidroid | 19:1cd7f65c155c | 135 | /** |
| Davidroid | 25:3c863b420ac5 | 136 | * @brief This is an example of user handler for the flag interrupt. |
| Davidroid | 25:3c863b420ac5 | 137 | * @param None |
| Davidroid | 25:3c863b420ac5 | 138 | * @retval None |
| Davidroid | 25:3c863b420ac5 | 139 | * @note If needed, implement it, and then attach and enable it: |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 140 | * + motor->attach_flag_irq(&flag_irq_handler); |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 141 | * + motor->enable_flag_irq(); |
| Davidroid | 25:3c863b420ac5 | 142 | * To disable it: |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 143 | * + motor->disble_flag_irq(); |
| Davidroid | 25:3c863b420ac5 | 144 | */ |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 145 | void flag_irq_handler(void) |
| Davidroid | 19:1cd7f65c155c | 146 | { |
| Davidroid | 19:1cd7f65c155c | 147 | /* Set ISR flag. */ |
| Davidroid | 19:1cd7f65c155c | 148 | motor->isr_flag = TRUE; |
| Davidroid | 19:1cd7f65c155c | 149 | |
| Davidroid | 19:1cd7f65c155c | 150 | /* Get the value of the status register. */ |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 151 | unsigned int status = motor->get_status(); |
| Davidroid | 34:543d0d1147d9 | 152 | |
| Davidroid | 19:1cd7f65c155c | 153 | /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ |
| Davidroid | 19:1cd7f65c155c | 154 | /* This often occures when a command is sent to the L6474 while it is not in HiZ state. */ |
| Davidroid | 34:543d0d1147d9 | 155 | if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) { |
| Davidroid | 19:1cd7f65c155c | 156 | printf(" WARNING: \"FLAG\" interrupt triggered. Non-performable command detected when updating L6474's registers while not in HiZ state.\r\n"); |
| Davidroid | 34:543d0d1147d9 | 157 | } |
| Davidroid | 19:1cd7f65c155c | 158 | |
| Davidroid | 19:1cd7f65c155c | 159 | /* Reset ISR flag. */ |
| Davidroid | 19:1cd7f65c155c | 160 | motor->isr_flag = FALSE; |
| Davidroid | 19:1cd7f65c155c | 161 | } |
| Davidroid | 19:1cd7f65c155c | 162 | |
| Davidroid | 19:1cd7f65c155c | 163 | |
| Davidroid | 0:e6a49a092e2a | 164 | /* Main ----------------------------------------------------------------------*/ |
| Davidroid | 0:e6a49a092e2a | 165 | |
| Davidroid | 0:e6a49a092e2a | 166 | int main() |
| Davidroid | 0:e6a49a092e2a | 167 | { |
| pratima_hb | 39:1270497828ca | 168 | |
| pratima_hb | 40:74e394ae8b5f | 169 | /* Initializing SPI bus for Motor */ |
| pratima_hb | 39:1270497828ca | 170 | DevSPI dev_spi(D11, D12, D13);// SPI device for the motor |
| pratima_hb | 39:1270497828ca | 171 | dev_spi.setup(8, 3, 3E6); // Set-up SPI device for the motor |
| pratima_hb | 40:74e394ae8b5f | 172 | /* EASE slave object creation and Initializing SPI bus for EASE */ |
| pratima_hb | 39:1270497828ca | 173 | EsmacatShield slave(spi, selectPin); //Define Chip Selector Pin |
| pratima_hb | 39:1270497828ca | 174 | slave.setup_spi(); //Setup SPI for EASE |
| pratima_hb | 39:1270497828ca | 175 | |
| Davidroid | 11:3e303a25770d | 176 | /* Initializing Motor Control Component. */ |
| pratima_hb | 39:1270497828ca | 177 | motor = new L6474(D2, D8, D7, D3, D10, dev_spi);//PWM pin is set to D3 as D9 is used by EASE |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 178 | if (motor->init(&init) != COMPONENT_OK) { |
| Davidroid | 17:4830b25fec7f | 179 | exit(EXIT_FAILURE); |
| Davidroid | 34:543d0d1147d9 | 180 | } |
| pratima_hb | 40:74e394ae8b5f | 181 | |
| Davidroid | 19:1cd7f65c155c | 182 | /* Attaching and enabling interrupt handlers. */ |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 183 | motor->attach_flag_irq(&flag_irq_handler); |
| davide.aliprandi@st.com | 35:2b44ed4ec7a0 | 184 | motor->enable_flag_irq(); |
| pratima_hb | 40:74e394ae8b5f | 185 | |
| pratima_hb | 39:1270497828ca | 186 | while(1) |
| pratima_hb | 39:1270497828ca | 187 | { |
| pratima_hb | 39:1270497828ca | 188 | // This part of the code sets up the Mode 1 for SPI to be used for |
| pratima_hb | 39:1270497828ca | 189 | // communication with EASE. This is needed because SPI for EASE operates |
| pratima_hb | 39:1270497828ca | 190 | // in Mode 1 which is different than what the motor shield SPI operates in. |
| pratima_hb | 39:1270497828ca | 191 | |
| pratima_hb | 39:1270497828ca | 192 | slave.setup_spi(); //Mode 1 set SPI for EASE |
| pratima_hb | 39:1270497828ca | 193 | wait_us(200); |
| pratima_hb | 39:1270497828ca | 194 | |
| pratima_hb | 39:1270497828ca | 195 | // get the data from Esmacat Master |
| pratima_hb | 39:1270497828ca | 196 | slave.get_ecat_registers(ease_read); //read all registers |
| pratima_hb | 39:1270497828ca | 197 | |
| pratima_hb | 39:1270497828ca | 198 | wait_us(1000); |
| pratima_hb | 39:1270497828ca | 199 | /* Printing to the console. */ |
| pratima_hb | 39:1270497828ca | 200 | printf("--> Moving to position : %d\r\n", ease_read[0]); |
| pratima_hb | 40:74e394ae8b5f | 201 | |
| pratima_hb | 39:1270497828ca | 202 | // This part of the code sets up the Mode 1 for SPI to be used for |
| pratima_hb | 39:1270497828ca | 203 | // communication with EASE. This is needed because SPI for EASE operates |
| pratima_hb | 39:1270497828ca | 204 | // in Mode 1 which is different than what the motor shield SPI operates in. |
| pratima_hb | 39:1270497828ca | 205 | dev_spi.setup(8, 3, 3E6); // Mode 3 set for SPI device for the motor |
| pratima_hb | 39:1270497828ca | 206 | wait_us(200); |
| pratima_hb | 39:1270497828ca | 207 | |
| pratima_hb | 39:1270497828ca | 208 | // command the motor to go to a set postion |
| pratima_hb | 40:74e394ae8b5f | 209 | motor->go_to(ease_read[0]); |
| Davidroid | 6:32166bfc04b0 | 210 | |
| pratima_hb | 39:1270497828ca | 211 | /* Getting current position. */ |
| pratima_hb | 39:1270497828ca | 212 | int position = motor->get_position(); |
| pratima_hb | 39:1270497828ca | 213 | |
| pratima_hb | 39:1270497828ca | 214 | /* Printing to the console. */ |
| pratima_hb | 39:1270497828ca | 215 | printf(" Position: %d.\r\n", position); |
| pratima_hb | 39:1270497828ca | 216 | |
| pratima_hb | 39:1270497828ca | 217 | /* Waiting. */ |
| pratima_hb | 39:1270497828ca | 218 | wait_ms(DELAY_1); |
| pratima_hb | 39:1270497828ca | 219 | |
| pratima_hb | 39:1270497828ca | 220 | } |
| Davidroid | 6:32166bfc04b0 | 221 | |
| Davidroid | 0:e6a49a092e2a | 222 | } |