마스터 로봇 조종기(EsmaCAT EtherCAT Shield 이용)

Dependencies:   EsmacatShield

Revision:
0:76a0d29416ee
Child:
1:9a05b0a731f3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Nov 03 12:51:15 2020 +0000
@@ -0,0 +1,171 @@
+/**
+ ******************************************************************************
+ * @file    main.cpp
+ * @date    February 06, 2020
+ * @brief   mbed test application - Esmacat Shield(EASE) working together with 
+ *          Base Board with Arduino UNO form factor as EtherCAT slave.
+ *          With the successful execution of this code the LED on EASE should
+ *          blink. It should also estabilish data transfer between the EtherCAT
+ *          Master of Esmacat on the PC.
+ *          For further information please refer to the tutorials at
+ *          https://www.esmacat.com/tutorials 
+ ******************************************************************************
+ 
+  Copyright (c) 2020 https://www.esmacat.com/
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+  EsmacatShield.h - Library for using EtherCAT Arduino Shield by Esmacat(EASE).
+  Created by Esmacat, 01/22/2020
+  
+*******************************************************************************
+* @file EsmacatShield.h
+*******************************************************************************
+*/
+#include "mbed.h"
+#include <EsmacatShield.h> //Include EsmacatShield Library
+
+
+int counter;
+int16_t v[8];              //an array of integer is declared for reading the 
+                           //data packet of EtherCAT from EASE 8 registers
+Serial pc(USBTX, USBRX);   // Configuring the serial port to host PC
+Ticker tick1;
+
+
+
+SPI spi(D11, D12, D13);             // Infineon <->ARM mosi, miso, sclk
+SPI spi2(PB_15, PB_14, PB_13);      // Encoder <->ARM mosi, miso, sclk
+SPI spi3(PC_12, PC_11, PC_10);      // Encoder <->ARM mosi, miso, sclk
+
+DigitalOut selectPin(D10);          // D10 is used to drive chip enable low
+
+DigitalOut CS1(PA_10);               // Encoder CS
+DigitalOut CS2(PA_8);
+DigitalOut CS3(PA_9);
+DigitalOut CS4(PC_7);
+DigitalOut CS5(PB_6);
+DigitalOut CS6(PC_1);
+DigitalOut CS7(PC_0); 
+DigitalOut greenLED(PB_0);                               
+
+uint8_t  Hbyte_enc1 = 0;
+uint8_t  Lbyte_enc1 = 0;
+uint8_t  Hbyte_enc2 = 0;
+uint8_t  Lbyte_enc2 = 0;
+uint8_t  Hbyte_enc3 = 0;
+uint8_t  Lbyte_enc3 = 0;
+uint8_t  Hbyte_enc4 = 0;
+uint8_t  Lbyte_enc4 = 0;
+uint8_t  Hbyte_enc5 = 0;
+uint8_t  Lbyte_enc5 = 0;
+uint8_t  Hbyte_enc6 = 0;
+uint8_t  Lbyte_enc6 = 0;
+uint8_t  Hbyte_enc7 = 0;
+uint8_t  Lbyte_enc7 = 0;
+
+uint16_t Angle_encoder1 = 0;
+uint16_t Angle_encoder2 = 0;
+uint16_t Angle_encoder3 = 0;
+uint16_t Angle_encoder4 = 0;
+uint16_t Angle_encoder5 = 0;
+uint16_t Angle_encoder6 = 0;
+uint16_t Angle_encoder7 = 0;
+
+uint32_t presentT = 0;
+uint32_t previousT = 0;
+uint32_t DeltaT = 0;
+
+uint16_t TestData = 0;
+////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+void SetSSI(SPI&spi)
+{
+  spi.format(8, 1);
+  spi.frequency(500000); //500 kHz    
+}
+
+void ReadEncoder(SPI&spi, uint8_t &low, uint8_t &high, DigitalOut& CS, uint16_t &angle)
+{
+  CS = 0;
+  high = spi.write(0x00);
+  low = spi.write(0x00);
+  CS = 1;
+
+  uint16_t Data = ((uint16_t)high << 2) + ((uint16_t)low >>6); 
+  high = Data >> 8;
+  low = Data & 0xff;
+  angle = (float)Data * (360.0 / 1024.0);
+  angle = (uint16_t) angle * 1;
+}
+
+
+//////////////////////////////////////////////////////////////////////////////// 
+////////////////////////////////////////////////////////////////////////////////
+int main() 
+{
+
+    EsmacatShield slave(spi, selectPin);  //Create an Esmacat slave object with defined
+                                          // spi and chip SelectPin
+    slave.setup_spi();                    //Setup SPI for EASE
+    
+    SetSSI(spi2); 
+    SetSSI(spi3); 
+    
+    greenLED = 0;
+    //tick1.attach(MeasureEncoder, 0.01);
+    
+    while(1)
+    {
+      TestData++;
+      if(TestData == 5) {
+        TestData = 0;
+        greenLED = !greenLED;   
+      }
+      
+      ReadEncoder(spi3, Lbyte_enc4, Hbyte_enc4, CS4, Angle_encoder4);
+      ReadEncoder(spi2, Lbyte_enc1, Hbyte_enc1, CS1, Angle_encoder1);
+      ReadEncoder(spi3, Lbyte_enc5, Hbyte_enc5, CS5, Angle_encoder5);
+      ReadEncoder(spi2, Lbyte_enc2, Hbyte_enc2, CS2, Angle_encoder2);
+      ReadEncoder(spi3, Lbyte_enc6, Hbyte_enc6, CS6, Angle_encoder6);
+      ReadEncoder(spi2, Lbyte_enc3, Hbyte_enc3, CS3, Angle_encoder3);
+      ReadEncoder(spi3, Lbyte_enc7, Hbyte_enc7, CS7, Angle_encoder7);
+      
+      slave.write_reg_value(0,Angle_encoder1, true);
+      slave.write_reg_value(1,Angle_encoder2, true);
+      slave.write_reg_value(2,Angle_encoder3, true);
+      slave.write_reg_value(3,Angle_encoder4, true);
+      slave.write_reg_value(4,Angle_encoder5, true);
+      slave.write_reg_value(5,Angle_encoder6, true);
+      slave.write_reg_value(6,Angle_encoder7, true);
+      /*
+      pc.printf("enc1: %d", Angle_encoder1);
+      pc.printf("\n");
+      pc.printf("enc2: %d", Angle_encoder2);
+      pc.printf("\n");
+      pc.printf("enc3: %d", Angle_encoder3);
+      pc.printf("\n");
+      pc.printf("enc4: %d", Angle_encoder4);
+      pc.printf("\n");
+      pc.printf("enc5: %d", Angle_encoder5);
+      pc.printf("\n");
+      pc.printf("enc6: %d", Angle_encoder6);
+      pc.printf("\n");
+      pc.printf("enc7: %d", Angle_encoder7);
+      pc.printf("\n");
+      */
+      wait_us(10000);
+
+    }
+
+}