Ethercat protokolü ile fb1111-142 kartı kullanılarak mikroişlemcinin belirlenen komutları uygulaması ve  fb1111-142 kartı ile haberleşme sağlaması
Application of the microprocessor defined commands by using ethercat protocol and fb1111-142 card and communication providing with fb1111-142 card
Yazar: ÖMER FARUK ŞAHAN
Danışman: DR. ÖĞR. ÜYESİ ALİ FUAT ERGENÇ
İstanbul Teknik Üniversitesi / Fen Bilimleri Enstitüsü / Mekatronik Mühendisliği Ana Bilim Dalı / Mekatronik Mühendisliği Bilim Dalı
Derece: Yüksek Lisans, 2019
Konu: Mekatronik Mühendisliği = Mechatronics Engineering

Özet:
EtherCAT Beckhoff  Otomasyon tarafından geliştirilen gerçek zamanlı endüstriyel ethernet teknolojisidir. EtherCAT protokolünde master telegram gönderir. Gönderilen telegram bütün düğümlerden geçer. Her düğüm, telegramı okurken kendi adresine gerekli dataları 'on the fly' olarak yazar. Böylece haberleşme sürecindeki gecikme sadece donanım üzerindeki propagasyon kadar olur. Son düğümdeki donanım, çıkış portunun takılı olmadığını anlayarak, telegramı tekrar mastera ethernet teknolojisinin full-dublex özelliğini kullanarak gönderir. Bu sayede teorik olarak 100Mbit/s'den hızlı olarak data aktarımı gerçekleştirilebilir.
Bu tez kapsamında bir kara aracının motoru üzerindeki belirli bir ihtiyacı karşılayacak şekilde tasarım yapılmıştır.  Oluşturulan sistemde master olarak bir ana bilgisayar, ana bilgisayar ile EtherCAT protokolüyle haberleşen slave FB1111-142 kartı, FB1111-142 kartı ile paralel olarak haberleşen ve bu paralel haberleşmede slave olarak görev yapan Arduino Due kartı bulunmaktadır. Ana bilgisayar tarafından kontrol edilen bir alt sistemin, ana bilgisayar ile eş zamanlı olarak haberleşmesi, verilen komutları işlemesi, yapılan ölçüm sonuçlarını aktarması beklenmektedir.
TwinCAT programı ile 4 çekirdekli bir bilgisayarın 2 çekirdeğinin atamasının yapılmasıyla birlikte sıradan bir bilgisayar, PLC gibi davranarak gerçek zamanlı olarak çalışmaktadır. Böylece ana bilgisayar olarak kullanılabilmektedir.
Ana bilgisayar EtherCAT protokolü üzerinden gerçek zamanlı olarak FB1111-142 piggyback kartı ile haberleşmektedir. FB1111-142 kartı gelen dataları dijital çıkışlar üzerinden paralel olarak Ardiuno Due kartına aktarmaktadır. Aynı zamanda dijital girişler ile Arduino Due kartından paralel olarak ilgili dataları, verilmiş komutların sonuçlarını almaktadır. FB1111-142 kartı Arduino Due kartı ile ana bilgisayar arasında köprü kurarak işlmelerin yapılmasını sağlamaktadır.
Arduino Due kartı gelen datalara göre ADC ölçümü, PWM ve PTO üretimi yapmaktadır. Ayrıca gelen komutlara göre IO çıkışı veya girişini de işlemektedir. 
Haberleşme gereksinimi olarak FB1111-142 üzerindeki 16 bit çıkış - 16 bit giriş olarak kullanılmaktadır. Bu çıkışlardan; Arduino Due'ye adres, komut ve data verilmektedir.  Aynı şekilde girişlerden de sonuçlar alınmaktadır. Her 16 bitin 5 biti adres biti, 3 biti komut biti, 8 biti veri olarak tanımlanmıştır. 8 bitten daha fazla veri için komut biti yardımıyla 2 High Byte – Low Byte olarak olarak 2 parça halinde gönderime imkan tanınmıştır.
Sonuç olarak oluşturulan veri haritası ile haberleşme gerçekleştirilmiştir. Verilen komutlara uygun olarak PWM, PTO çıktısı sağlanmıştır. ADC ölçümleri gerçekleştirilerek veriler gönderilmiştir. Kablo kopukluğuna önlem olarak geliştirilen başlama algoritmasıtyla tüm kabloların bağlı olduğu kontrol edilerek teyit işlemi yaptırılmıştır.

Abstract:
The system is a collection of items or components that are organized for a common purpose. The sytem has a fuction which includes inputs and outputs. The differance between the system and the real-time system is that the real-time system guarantee the outputs will be ready within the specified time constraint. There are three real time system types, hard real time, firm real time and soft real time. Hard real time systems can not tolarate to miss a deadline. In hard real time systems, missing a deadline results catastrophic consequences. In firm real time systems, missing a deadline may not cause catastrophic consequences but could couse undesired consequences. For example reduction of huge quality of product. In soft real time systems, missing deadlines are tolerable but not desired. The most important components of real-time systems are time constraint, latency, priority and being deterministic.
EtherCAT is a real-time industrial ethernet technology developed by Beckhoff Automation. The master sends telegram to slaves in the EtherCAT protocol. The telegram sent passes through all slave nodes. Each node reads the telegram and writes the necessary data to its address 'on the fly'. Thus, the delay in the communication process is only as much as propagation on the hardware. The hardware on the last node recognizes that the output port is not plugged in, then sends the telegram again to master, using the full-duplex feature of the ethernet technology. In this way, theoretically, data transfer can be performed faster than 100Mbit/s.
Within the scope of this thesis, a design has been designed to meet a specific need on the engine of a land vehicle. In the generated system, there is a master computer, the slave FB1111-142 board communicating with the host computer and the EtherCAT protocol, and the Arduino Due board, which operates in parallel communication with the FB1111-142 board. It is expected that a subsystem controlled by the host will simultaneously communicate with the host, process the given commands, and transmit the measured results. 
In applications with spatially distributed processes requiring simultaneous actions, exact synchronization is particularly important. The ethercat communication is based on distirubuted clocks for synchronization of nodes. At the hardware level, the clocks on the nodes are adjusted according to the reference clock. Calibration is performed according to the reference in each cycle and the result is lower than 1us to jitter level. the signal sent from the reference clock is delayed to reach the slaves. This delay time can be measured during the network start-up period or by continuously measuring the clocks under 1us relative to each other.
The FB1111-142 board is equipped with an ET1100 processor. The processor processes the incoming EtherCAT data and sets the outputs according to the incoming data. At the same time, the input port values are sent to the host by typing in the telegram. Updating the outputs takes place with four ways. First way is at end of the EtherCAT frame which is used in thesis. Second way is 'Distributed Clocks SYNC 0' events. 'Distributed Clocks SYNC 1' and last way is at the end of EtherCAT frame which triggerd by process data watchdog. In addition,  to observe the outputs data from pins, the following condition should be provided; logic 1 must be applied to the output enable pin, SyncManager watchdog must be either active or disabled, output values have to be written register within a valid EtherCAT frame and configured output update event must be occured for the outputs to be active on the FB1111-142 board. An output event is always signaled by a pulse on 'OUT_VALID' even if the digital outputs are not changed.
With the assignment of 2 cores of a 4-core computer with TwinCAT program; an ordinary computer works in real time by acting as a PLC. If the assigned kernels are shared with Windows, the kernels serve TwinCAT within the set CPU limits. During the period outside the CPU limits, the CPUs are running on the Windows operating system. Since the TwinCAT program performs CPU settings over the kernel, there is no link between the operating system and the CPU run times. That is, the operating system cannot assign a task to the kernels. In real time systems, the specified cycle times are completed with the schduler and real time requirements are provided. Real-time systems programmed on TwinCAT usually end in much less time than the specified time intervals. Thanks to the intelligent programming of TwinCAT, the time remaining from the end of the job is transferred to Windows and system performance is improved. In addition, warnings can be given by setting latency times via TwinCAT. With all these features, an ordinary computer can be used as a host that provides real-time features.
The host communicates with the FB1111-142 piggyback board in real time via the EtherCAT protocol. The FB1111-142 board transmits incoming data in parallel to the Ardiuno Due board via digital outputs. At the same time, the corresponding inputs from the digital inputs and Arduino Due board are the results of the given commands. The FB1111-142 board is a bridge between the Arduino Due board and the host computer, allowing transactions to be made. The Arduino Due board produces ADC measurement, PWM and PTO according to the data. It also processes IO output or input according to incoming commands.
On Arduino Due Atmel 32 bit SAM3X8E ARM Cortex-M3 processor. There are 54 digital inputs and outputs and 12 analog inputs. 12 of the digital input-outputs can be used as PWM output. It has a clock speed of 84 MHz and features up to 512 Kbytes of flash and up to 100Kbytes SRAM. The peripheral harwares consist of  a High Speed USB Host and Device port with embedded transceiver, an Ethernet MAC, 2 CANs, a High Speed MCI for SDIO/SD/MMC, an External Bus Interface with NAND Flash Controller (NFC), 5 UARTs, 2 TWIs, 4 SPIs, a PWM timer, three 3-channel general-purpose 32-bit timers, a low-power RTC, a lowpower RTT, 256-bit General Purpose Backup Registers, a 12-bit ADC and a 12-bit DAC. In addition, Up to 103 input - output lines with external interrupt capability (edge or level sensitivity), debouncing, glitch filtering and ondie Series Resistor Termination. The FB1111-142 board gives the pulse to the 'OUT_VALID' pin when it sets its outputs according to the incoming data. The rising edge of the 'OUT_VALID' pulse enters the Arduino Due board port interrupt. It checks that the results of the previous command are given to the outputs in the port interrupt. Then the input ports are read and the data is received.
Atmel SAM3X has two types of input output lines. These are general purpose inputs and outputs and system inputs and outputs. General purpose input and outputs can have alternate functions with the multiplexing capabilities of the Paralel Input Output controller. The same PIO line can also be used in input output mode or by a multiplexed peripheral. System inputs outputs includes pins, such as test pins, oscillators, erasing or analog inputs. General purpose input output lines are controlled by parallel input output controller. All inputs outputs have several input or output modes such as pull-up, input Schmitt triggers, multi-drive (open-drain), glitch filters, debouncing or input change interrupt. Programming of these modes is performed independently for each inputs outputs line through the PIO controller user interface. Each input output line is dedicated to a bit number from 32 bit registers of 32 bit user interface. 
Parallel input output controller attributes are; An input change interrupt enabling level change detection on any input output line. Additional Interrupt features enabling rising edge, falling edge, low level or high level detection on input output line. A glitch filter providing rejection of glitches lower than one-half of system clock cycle. A debouncing filter providing rejection of unwanted pulses from key or push button operations. Multi-drive capability similar to an open drain input output line. Control of the pull-up of the input output line. Input visibility and output control. The parallel input output controller also enables a synchronous output providing up to 32 bits of data output in a single write operation. Especially synchronous output feature is a key function for parallel comminucation by using general purpose input outputs. With this function desired cycle times can be reached.
To using input and outputs with parallel input output controller, PIO_PER (PIO Enable Register) and PIO_PDR (PIO Disable Register) registers should be written. Result could be controlled by reading PIO_PSR (PIO Status Register) register. A value of set indicates the pin is controlled by PIO Controller. 
To reading inputs level from PIO controller requires to read PIO_PDSR (Pin Data Status Register). The register provides the values regardless of whether the pins configured as inputs or outputs. The result of configuration could be checked from PIO_OSR (Output Status Register).
The levels of outputs can be set by writing PIO_SODR (Set Output Data Register) and PIO_CODR (Clear Output Data Register).
For synchronous data output, firstly PIO controller configuration should be made. By setting PIO_OER (Output Enable Register) and PIO_ODR (Output Disable Register) registers the pins are configured as driven by PIO controller. Setting outpus by PIO_SODR and PIO_CODR requires two succesive write operations into two different register. To overcome this, the PIO Controller provides a direct control of PIO outputs by single write access to PIO_ODSR (Output Data Status Register).Only bits unmasked by PIO_OWSR (Output Write Status Register) are written. The mask bits in PIO_OWSR are set by writing to PIO_OWER (Output Write Enable Register) and cleared by writing to PIO_OWDR (Output Write Disable Register).
The 16-bit output on FB1111-142 is used as a 16-bit input as a communication requirement. From these outputs; Arduino Due is provided with address, command and data. Similarly, the results are taken from the inputs. 5 bits of each 16 bits are defined as address bits, 3 bits are command bits and 8 bits are defined as data. For more than 8 bits, 2 High Bytes - Low Bytes can be sent with 2 bits.
According to the established communication protocol, the parameters are read in 5 bit address, the commands are executed with 3 bit command and in the 8 bit data part according to the command or address. 8 different commands can be sent with a 3-bit command. The commands are as follows; no operation, read analog digital converter, set pulse time output, logic input value read, write logic output value.
As a result, communication with the generated data map has been carried out. PWM, PTO output is provided according to given commands. Data were sent by performing ADC measurements. With the start-up algorithm developed as a precaution to the cable disconnection, all the cables are checked and confirmed.