EtherCAT slave that reads 3 Xsens IMU's connected to a Xbus Master

Dependencies:   MODSERIAL mbed KL25Z_ClockControl

Fork of EtherCAT by First Last

Committer:
vsluiter
Date:
Thu Dec 11 23:12:11 2014 +0000
Revision:
5:6d75f432a41f
Parent:
4:bb72df6dce33
Child:
6:3ccb89a58ff8
Child:
8:09dcef3ed467
Not working; changed both inputs, processing and CoE ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 1:5e22bf1a3817 1 /*
vsluiter 1:5e22bf1a3817 2 * SOES Simple Open EtherCAT Slave
vsluiter 1:5e22bf1a3817 3 *
vsluiter 1:5e22bf1a3817 4 * File : soes.c
vsluiter 1:5e22bf1a3817 5 * Version : 0.9.2
vsluiter 1:5e22bf1a3817 6 * Date : 22-02-2010
vsluiter 1:5e22bf1a3817 7 * Copyright (C) 2007-2010 Arthur Ketels
vsluiter 1:5e22bf1a3817 8 *
vsluiter 1:5e22bf1a3817 9 * SOES is free software; you can redistribute it and/or modify it under
vsluiter 1:5e22bf1a3817 10 * the terms of the GNU General Public License version 2 as published by the Free
vsluiter 1:5e22bf1a3817 11 * Software Foundation.
vsluiter 1:5e22bf1a3817 12 *
vsluiter 1:5e22bf1a3817 13 * SOES is distributed in the hope that it will be useful, but WITHOUT ANY
vsluiter 1:5e22bf1a3817 14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
vsluiter 1:5e22bf1a3817 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
vsluiter 1:5e22bf1a3817 16 * for more details.
vsluiter 1:5e22bf1a3817 17 *
vsluiter 1:5e22bf1a3817 18 * As a special exception, if other files instantiate templates or use macros
vsluiter 1:5e22bf1a3817 19 * or inline functions from this file, or you compile this file and link it
vsluiter 1:5e22bf1a3817 20 * with other works to produce a work based on this file, this file does not
vsluiter 1:5e22bf1a3817 21 * by itself cause the resulting work to be covered by the GNU General Public
vsluiter 1:5e22bf1a3817 22 * License. However the source code for this file must still be made available
vsluiter 1:5e22bf1a3817 23 * in accordance with section (3) of the GNU General Public License.
vsluiter 1:5e22bf1a3817 24 *
vsluiter 1:5e22bf1a3817 25 * This exception does not invalidate any other reasons why a work based on
vsluiter 1:5e22bf1a3817 26 * this file might be covered by the GNU General Public License.
vsluiter 1:5e22bf1a3817 27 *
vsluiter 1:5e22bf1a3817 28 * The EtherCAT Technology, the trade name and logo "EtherCAT" are the intellectual
vsluiter 1:5e22bf1a3817 29 * property of, and protected by Beckhoff Automation GmbH.
vsluiter 1:5e22bf1a3817 30 */
vsluiter 1:5e22bf1a3817 31
vsluiter 1:5e22bf1a3817 32 /****************************************************
vsluiter 1:5e22bf1a3817 33 Chip type : STM32F051R8
vsluiter 1:5e22bf1a3817 34 Clock frequency : 48 MHz
vsluiter 1:5e22bf1a3817 35 *****************************************************/
vsluiter 1:5e22bf1a3817 36
vsluiter 1:5e22bf1a3817 37 /* Includes ------------------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 38 #include "mbed.h"
vsluiter 1:5e22bf1a3817 39 #include "cpuinit.h"
vsluiter 1:5e22bf1a3817 40 #include "utypes.h"
vsluiter 1:5e22bf1a3817 41 #include "esc.h"
vsluiter 1:5e22bf1a3817 42
vsluiter 1:5e22bf1a3817 43 /* Private typedef -----------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 44 /* Private define ------------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 45 #define wd_reset 1000
vsluiter 1:5e22bf1a3817 46
vsluiter 5:6d75f432a41f 47 #define BALANCE_WIDTH 0.435
vsluiter 5:6d75f432a41f 48 #define BALANCE_HEIGHT 0.24
vsluiter 1:5e22bf1a3817 49 /* Private macro -------------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 50 /* Private variables ---------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 51 _ESCvar ESCvar;
vsluiter 1:5e22bf1a3817 52 uint8 APPstate;
vsluiter 1:5e22bf1a3817 53 _MBX MBX[MBXBUFFERS];
vsluiter 1:5e22bf1a3817 54 _MBXcontrol MBXcontrol[MBXBUFFERS];
vsluiter 1:5e22bf1a3817 55 uint8 MBXrun=0;
vsluiter 1:5e22bf1a3817 56 uint16 SM2_sml,SM3_sml;
vsluiter 1:5e22bf1a3817 57 _Rbuffer Rb;
vsluiter 1:5e22bf1a3817 58 _Wbuffer Wb;
vsluiter 5:6d75f432a41f 59 //_Ebuffer Eb; //EEprom
vsluiter 1:5e22bf1a3817 60 uint8 TXPDOsize,RXPDOsize;
vsluiter 1:5e22bf1a3817 61 uint16 wd_ok = 1, wd_cnt = wd_reset;
vsluiter 5:6d75f432a41f 62 volatile uint8 correct_offset;
vsluiter 5:6d75f432a41f 63 float FrontRight_offset;
vsluiter 5:6d75f432a41f 64 float FrontLeft_offset;
vsluiter 5:6d75f432a41f 65 float BackLeft_offset;
vsluiter 5:6d75f432a41f 66 float BackRight_offset;
vsluiter 5:6d75f432a41f 67 float rawFrontLeft;
vsluiter 5:6d75f432a41f 68 float rawFrontRight;
vsluiter 5:6d75f432a41f 69 float rawBackLeft;
vsluiter 5:6d75f432a41f 70 float rawBackRight;
vsluiter 5:6d75f432a41f 71 //volatile uint8 diginput;
vsluiter 1:5e22bf1a3817 72
vsluiter 2:3dd1240eb938 73 //Serial shoe_serial(SHOE_SERIAL_TX,SHOE_SERIAL_RX);
vsluiter 1:5e22bf1a3817 74 DigitalOut led(LED_PIN);
vsluiter 1:5e22bf1a3817 75 DigitalOut et1100_ss(ET1100_SS);
vsluiter 1:5e22bf1a3817 76 DigitalIn et1100_miso(ET1100_MISO);
vsluiter 1:5e22bf1a3817 77 SPI et1100_spi(ET1100_MOSI,ET1100_MISO,ET1100_SCK);
vsluiter 5:6d75f432a41f 78 AnalogIn adcFrontLeft(ADC_FL);
vsluiter 5:6d75f432a41f 79 AnalogIn adcFrontRight(ADC_FR);
vsluiter 5:6d75f432a41f 80 AnalogIn adcBackLeft(ADC_BL);
vsluiter 5:6d75f432a41f 81 AnalogIn adcBackRight(ADC_BR);
vsluiter 1:5e22bf1a3817 82
vsluiter 1:5e22bf1a3817 83 /* Private function prototypes -----------------------------------------------*/
vsluiter 1:5e22bf1a3817 84 /* Private functions ---------------------------------------------------------*/
vsluiter 1:5e22bf1a3817 85
vsluiter 1:5e22bf1a3817 86 /** void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 87 \brief Object handler, declared from esc.h, as extern function
vsluiter 1:5e22bf1a3817 88 \param index
vsluiter 1:5e22bf1a3817 89 \param subindex
vsluiter 1:5e22bf1a3817 90 */
vsluiter 1:5e22bf1a3817 91 void ESC_objecthandler(uint16 index, uint8 subindex);
vsluiter 1:5e22bf1a3817 92 void TXPDO_update(void);
vsluiter 1:5e22bf1a3817 93 void RXPDO_update(void);
vsluiter 1:5e22bf1a3817 94 void DIG_process(void);
vsluiter 5:6d75f432a41f 95 void sample(void);
vsluiter 1:5e22bf1a3817 96
vsluiter 1:5e22bf1a3817 97 void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 98 {
vsluiter 1:5e22bf1a3817 99 uint8 dummy8;
vsluiter 1:5e22bf1a3817 100 uint16 dummy16;
vsluiter 1:5e22bf1a3817 101 switch (index)
vsluiter 1:5e22bf1a3817 102 {
vsluiter 1:5e22bf1a3817 103 case 0x8000:
vsluiter 1:5e22bf1a3817 104 switch (subindex)
vsluiter 1:5e22bf1a3817 105 {
vsluiter 1:5e22bf1a3817 106 case 0x01:
vsluiter 5:6d75f432a41f 107 dummy8 = 0;//Eb.setting8;//Write value to EEPROM; eeprom_write_byte(&eedat.setting8, Wb.setting8);
vsluiter 1:5e22bf1a3817 108 break;
vsluiter 1:5e22bf1a3817 109 case 0x02:
vsluiter 5:6d75f432a41f 110 dummy16 = 0;//Eb.setting16;//Write value to EEPROM; eeprom_write_word(&eedat.setting16, Wb.setting16);
vsluiter 1:5e22bf1a3817 111 break;
vsluiter 1:5e22bf1a3817 112 }
vsluiter 1:5e22bf1a3817 113 break;
vsluiter 1:5e22bf1a3817 114 }
vsluiter 1:5e22bf1a3817 115 }
vsluiter 1:5e22bf1a3817 116
vsluiter 1:5e22bf1a3817 117 void TXPDO_update(void)
vsluiter 1:5e22bf1a3817 118 {
vsluiter 1:5e22bf1a3817 119 ESC_write(SM3_sma, &Rb, TXPDOsize, &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 120 }
vsluiter 1:5e22bf1a3817 121
vsluiter 1:5e22bf1a3817 122 void RXPDO_update(void)
vsluiter 1:5e22bf1a3817 123 {
vsluiter 1:5e22bf1a3817 124 ESC_read(SM2_sma, &Wb, RXPDOsize, &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 125 }
vsluiter 1:5e22bf1a3817 126
vsluiter 1:5e22bf1a3817 127 void APP_safeoutput(void)
vsluiter 1:5e22bf1a3817 128 {
vsluiter 1:5e22bf1a3817 129 asm("nop");
vsluiter 1:5e22bf1a3817 130 //Wb.dout = 0;
vsluiter 1:5e22bf1a3817 131 //DOUTPORT = (Wb.dout >> 4) & 0xf0;
vsluiter 1:5e22bf1a3817 132 }
vsluiter 1:5e22bf1a3817 133
vsluiter 1:5e22bf1a3817 134 void DIG_process(void)
vsluiter 1:5e22bf1a3817 135 {
vsluiter 1:5e22bf1a3817 136 if (APPstate & APPSTATE_OUTPUT) //output enabled
vsluiter 1:5e22bf1a3817 137 {
vsluiter 1:5e22bf1a3817 138 if (ESCvar.ALevent & ESCREG_ALEVENT_SM2) // SM2 trigger ?
vsluiter 1:5e22bf1a3817 139 {
vsluiter 1:5e22bf1a3817 140 ESCvar.ALevent &= ~ESCREG_ALEVENT_SM2;
vsluiter 1:5e22bf1a3817 141 RXPDO_update();
vsluiter 1:5e22bf1a3817 142 // dummy output point
vsluiter 5:6d75f432a41f 143 correct_offset = Wb.correct_offset;
vsluiter 5:6d75f432a41f 144 if(correct_offset & 0x01) {
vsluiter 1:5e22bf1a3817 145 led.write(1);
vsluiter 5:6d75f432a41f 146 FrontLeft_offset = adcFrontLeft;
vsluiter 5:6d75f432a41f 147 FrontRight_offset = adcFrontRight;
vsluiter 5:6d75f432a41f 148 BackLeft_offset = adcBackLeft;
vsluiter 5:6d75f432a41f 149 BackRight_offset = adcBackRight;
vsluiter 5:6d75f432a41f 150 }
vsluiter 1:5e22bf1a3817 151 wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 152 }
vsluiter 1:5e22bf1a3817 153
vsluiter 5:6d75f432a41f 154 if (!wd_cnt) {
vsluiter 1:5e22bf1a3817 155 ESC_stopoutput();
vsluiter 1:5e22bf1a3817 156 // watchdog, invalid outputs
vsluiter 1:5e22bf1a3817 157 ESC_ALerror(ALERR_WATCHDOG);
vsluiter 1:5e22bf1a3817 158 // goto safe-op with error bit set
vsluiter 1:5e22bf1a3817 159 ESC_ALstatus(ESCsafeop | ESCerror);
vsluiter 1:5e22bf1a3817 160 }
vsluiter 1:5e22bf1a3817 161 }
vsluiter 1:5e22bf1a3817 162 else
vsluiter 1:5e22bf1a3817 163 {
vsluiter 1:5e22bf1a3817 164 //wd_ok = 1;
vsluiter 1:5e22bf1a3817 165 wd_cnt = wd_reset;
vsluiter 1:5e22bf1a3817 166 }
vsluiter 1:5e22bf1a3817 167 if (APPstate) //input or output enabled
vsluiter 5:6d75f432a41f 168 {
vsluiter 5:6d75f432a41f 169 float fl,fr,br,bl,copx,copy;
vsluiter 1:5e22bf1a3817 170 Rb.timestamp = ESCvar.Time;
vsluiter 1:5e22bf1a3817 171 //just some dummy data to test
vsluiter 4:bb72df6dce33 172 //Rb.counter++;
vsluiter 4:bb72df6dce33 173 //Rb.diginput = diginput;
vsluiter 4:bb72df6dce33 174 //Rb.analog[0] = 1;
vsluiter 4:bb72df6dce33 175 //Rb.analog[1] = 2;
vsluiter 5:6d75f432a41f 176 fr = rawFrontRight - FrontRight_offset;
vsluiter 5:6d75f432a41f 177 fl = rawFrontLeft - FrontLeft_offset;
vsluiter 5:6d75f432a41f 178 br = rawBackRight - BackRight_offset;
vsluiter 5:6d75f432a41f 179 bl = rawBackLeft - BackLeft_offset;
vsluiter 5:6d75f432a41f 180 copx = ((fl+bl)-(fr+br))*BALANCE_WIDTH;
vsluiter 5:6d75f432a41f 181 copy = ((fr+fl)-(br+bl))*BALANCE_HEIGHT;
vsluiter 5:6d75f432a41f 182 Rb.CoPx = copx;
vsluiter 5:6d75f432a41f 183 Rb.CoPy = copy;
vsluiter 5:6d75f432a41f 184 Rb.FrontRight = rawFrontRight;
vsluiter 5:6d75f432a41f 185 Rb.FrontLeft = rawFrontLeft;
vsluiter 5:6d75f432a41f 186 Rb.BackRight = rawBackRight;
vsluiter 5:6d75f432a41f 187 Rb.BackLeft = rawBackLeft;
vsluiter 1:5e22bf1a3817 188
vsluiter 1:5e22bf1a3817 189 TXPDO_update();
vsluiter 5:6d75f432a41f 190 }
vsluiter 1:5e22bf1a3817 191 }
vsluiter 1:5e22bf1a3817 192
vsluiter 5:6d75f432a41f 193 void sample(void)
vsluiter 5:6d75f432a41f 194 {
vsluiter 5:6d75f432a41f 195 rawFrontLeft = adcFrontLeft;
vsluiter 5:6d75f432a41f 196 rawFrontRight = adcFrontRight;
vsluiter 5:6d75f432a41f 197 rawBackLeft = adcBackLeft;
vsluiter 5:6d75f432a41f 198 rawBackRight = adcBackRight;
vsluiter 5:6d75f432a41f 199
vsluiter 5:6d75f432a41f 200 }
vsluiter 1:5e22bf1a3817 201 int main(void)
vsluiter 1:5e22bf1a3817 202 {
vsluiter 1:5e22bf1a3817 203 /*!< At this stage the microcontroller clock setting is already configured,
vsluiter 1:5e22bf1a3817 204 this is done through SystemInit() function which is called from startup
vsluiter 1:5e22bf1a3817 205 file (startup_stm32f0xx.s) before to branch to application main.
vsluiter 1:5e22bf1a3817 206 To reconfigure the default setting of SystemInit() function, refer to
vsluiter 1:5e22bf1a3817 207 system_stm32f0xx.c file
vsluiter 1:5e22bf1a3817 208 */
vsluiter 5:6d75f432a41f 209 Ticker adc_sampler;
vsluiter 1:5e22bf1a3817 210 cpuinit();
vsluiter 5:6d75f432a41f 211 adc_sampler.attach(sample,0.001);
vsluiter 1:5e22bf1a3817 212 TXPDOsize = sizeTXPDO();
vsluiter 1:5e22bf1a3817 213 RXPDOsize = sizeRXPDO();
vsluiter 1:5e22bf1a3817 214 wait_ms(200);
vsluiter 1:5e22bf1a3817 215 /*initialize configuration*/
vsluiter 5:6d75f432a41f 216 //Eb.setting16 = 0xABCD;
vsluiter 5:6d75f432a41f 217 //Eb.setting8 = 111;
vsluiter 1:5e22bf1a3817 218 // wait until ESC is started up
vsluiter 1:5e22bf1a3817 219 while ((ESCvar.DLstatus & 0x0001) == 0)
vsluiter 1:5e22bf1a3817 220 ESC_read(ESCREG_DLSTATUS, &ESCvar.DLstatus, sizeof(ESCvar.DLstatus), &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 221
vsluiter 1:5e22bf1a3817 222 // reset ESC to init state
vsluiter 1:5e22bf1a3817 223 ESC_ALstatus(ESCinit);
vsluiter 1:5e22bf1a3817 224 ESC_ALerror(ALERR_NONE);
vsluiter 1:5e22bf1a3817 225 ESC_stopmbx();
vsluiter 1:5e22bf1a3817 226 ESC_stopinput();
vsluiter 1:5e22bf1a3817 227 ESC_stopoutput();
vsluiter 1:5e22bf1a3817 228
vsluiter 1:5e22bf1a3817 229 // application run loop
vsluiter 1:5e22bf1a3817 230 while (1)
vsluiter 1:5e22bf1a3817 231 {
vsluiter 1:5e22bf1a3817 232 ESC_read(ESCREG_LOCALTIME, &ESCvar.Time, sizeof(ESCvar.Time), &ESCvar.ALevent);
vsluiter 1:5e22bf1a3817 233 ESC_ALevent();
vsluiter 1:5e22bf1a3817 234 ESC_state();
vsluiter 1:5e22bf1a3817 235 if (ESC_mbxprocess())
vsluiter 1:5e22bf1a3817 236 {
vsluiter 1:5e22bf1a3817 237 ESC_coeprocess();
vsluiter 1:5e22bf1a3817 238 ESC_xoeprocess();
vsluiter 1:5e22bf1a3817 239 }
vsluiter 1:5e22bf1a3817 240 DIG_process();
vsluiter 1:5e22bf1a3817 241 }
vsluiter 1:5e22bf1a3817 242 }
vsluiter 1:5e22bf1a3817 243
vsluiter 1:5e22bf1a3817 244 /** void ESC_objecthandler(uint16 index, uint8 subindex)
vsluiter 1:5e22bf1a3817 245 \brief Object handler, declared from esc.h, as extern function
vsluiter 1:5e22bf1a3817 246 \param index
vsluiter 1:5e22bf1a3817 247 \param subindex
vsluiter 1:5e22bf1a3817 248 */
vsluiter 1:5e22bf1a3817 249
vsluiter 1:5e22bf1a3817 250
vsluiter 1:5e22bf1a3817 251 #ifdef USE_FULL_ASSERT
vsluiter 1:5e22bf1a3817 252
vsluiter 1:5e22bf1a3817 253 /**
vsluiter 1:5e22bf1a3817 254 * @brief Reports the name of the source file and the source line number
vsluiter 1:5e22bf1a3817 255 * where the assert_param error has occurred.
vsluiter 1:5e22bf1a3817 256 * @param file: pointer to the source file name
vsluiter 1:5e22bf1a3817 257 * @param line: assert_param error line source number
vsluiter 1:5e22bf1a3817 258 * @retval None
vsluiter 1:5e22bf1a3817 259 */
vsluiter 1:5e22bf1a3817 260 void assert_failed(uint8_t* file, uint32_t line)
vsluiter 1:5e22bf1a3817 261 {
vsluiter 1:5e22bf1a3817 262 /* User can add his own implementation to report the file name and line number,
vsluiter 1:5e22bf1a3817 263 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
vsluiter 1:5e22bf1a3817 264
vsluiter 1:5e22bf1a3817 265 /* Infinite loop */
vsluiter 1:5e22bf1a3817 266 while (1)
vsluiter 1:5e22bf1a3817 267 {
vsluiter 1:5e22bf1a3817 268 }
vsluiter 1:5e22bf1a3817 269 }
vsluiter 1:5e22bf1a3817 270 #endif
vsluiter 1:5e22bf1a3817 271
vsluiter 1:5e22bf1a3817 272 /**
vsluiter 1:5e22bf1a3817 273 * @}
vsluiter 1:5e22bf1a3817 274 */
vsluiter 1:5e22bf1a3817 275
vsluiter 1:5e22bf1a3817 276 /**
vsluiter 1:5e22bf1a3817 277 * @}
vsluiter 1:5e22bf1a3817 278 */
vsluiter 1:5e22bf1a3817 279
vsluiter 1:5e22bf1a3817 280 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
vsluiter 1:5e22bf1a3817 281