ソースを参照

testowo do pogladu komuniakcja na arduino po can

arkadiusz binder 5 年 前
コミット
4aba4766b2

+ 1 - 1
SE/projects/bocian

@@ -1 +1 @@
-Subproject commit aa7a386ecd63833f5c10dd288b66f859a6bbe899
+Subproject commit b2da6943377d6c973952e19e448499028dd7945d

+ 346 - 0
SE/stuff/P5_Automation_can-dev/P5_Automation_can-dev.ino

@@ -0,0 +1,346 @@
+/* P5_Automation_can-dev
+ * 2020-06 testing communications CAN for integrate further with Procesy5
+ * A.Binder
+ * Uses: 
+ * LCD1602 - big 2x16 character display
+ * TFT_ILI9163C 128x128 lcd display
+ * MCP_2515 - can port controller
+ * 
+ * controls
+ * Slave ports via:     
+ * I2C      
+ * ]PCA9685 - 16port 12bit PWM driver - can controll 16 eg. LEDs which can be 0....12V dimmed=]                
+ * or 16 servos               
+ * - can have addressed many of PCA9685 on I2C bus - so can controll over 64 ports      
+ * to be added another devices tested
+ *  TODO structure of port is in _Slave_Ports.h  
+ *     _Slave_Port _Slave_Ports[] = {
+        {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 },
+        {"CAN", 0x102, 0x80, 0x00, 1, 0xFF, 0x20 } //testowy ttl
+        };
+        - to be optimalized for inegration
+        - to allow of subscribe ports
+        - to allow of concurent routing
+        - to allow new port detectioj
+        - to allow creating ports dependencies e.g. IF 
+            - port has value
+            - port AND port 
+            THEN
+            - set port
+           - probably should be avilable creations of virtual ports which would describe 
+            - state of port AND port etc... 
+ *  TO BE INTEGRATED WITH p5 
+ *    - interface and WFST
+ *  TO BE INGEGRATED WITH MARLIN 3D PRINTERS - machine and automation purpose
+ *    - controll remote ports via g-code 
+ *    - allows interrupts
+ */
+
+
+#define  _ControllerID   0x101 
+#define  _Slave1_ID   0x103 
+
+#define Active_ControllerID _ControllerID 
+//#define Active_ControllerID _Slave1_ID 
+
+
+ //REMOTE PORTS DEFINITIONS
+  #include "_Slave_Ports.h"
+  // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 } 
+   //#define _CAN_P_0x102_PWMServoDriver0x40
+   
+    // #define   Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
+  //    Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
+
+
+ #if Active_ControllerID == _ControllerID 
+    String lcdPattern = "s" ;
+    #define HAS_LCD1602 
+   //  #define HAS_TFT_ILI9163C 
+ #endif
+ #if Active_ControllerID == _Slave1_ID 
+    String lcdPattern = "." ;
+    #define HAS_TFT_ILI9163C 
+   // #define HAS_LCD1602 
+ #endif
+// #define __CSpin 53 // 10 //  chip select
+ 
+ 
+ 
+ int LCDtim = 250;  //the value of delay time
+  
+//BEGIN 1602
+#if defined(HAS_LCD1602)
+  #include <LiquidCrystal.h>
+  // initialize the library with the numbers of the interface pins
+  //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
+  //LiquidCrystal lcd(4, 6, 53, 51, 50, 52  );
+  const int LCD1602rs=12, LCD1602en=11, LCD1602db4=5, LCD1602db5=4, LCD1602db6=3, LCD1602db7=2; // lcd keypad shield pins
+  LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
+  int lcdPos = 0 ;
+ 
+#endif
+// include the library code
+/**********************************************************/
+//EOF BEGIN 1602
+
+#if defined(HAS_TFT_ILI9163C)
+  #include <SPI.h>
+  #include <Adafruit_GFX.h>
+  #include <TFT_ILI9163C.h>
+
+  //#define TFT_ILI9163C_CSpin __CSpin // 53 // 10 //  chip select
+  #define TFT_ILI9163C_CSpin 7 // __CSpin // 53 // 10 //  chip select
+
+  #define TFT_ILI9163C_DCpin 8 // A0   data/command pin.   sometimes called DC, RS, ...
+
+  #define TFT_ILI9163C__RSTpin 9 //reset pin.   you must connect.   (or pullup)
+  //#define __SDA 51 //11 //+ bidirectional data pin.   Connect to MOSI
+  //#define __SCK 52 //13 //+ clock.   sometimes called SCLK, SCL, ...
+
+  
+  // Color definitions
+  #define BLACK   0x0000
+  #define BLUE    0x001F
+  #define RED     0xF800
+  #define GREEN   0x07E0
+  #define CYAN    0x07FF
+  #define MAGENTA 0xF81F
+  #define YELLOW  0xFFE0  
+  #define WHITE   0xFFFF
+
+  TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin);
+  float p = 3.1415926;
+#endif
+
+
+//BEGIN CAN
+
+
+#include <mcp_can.h>
+#include <SPI.h>
+
+long unsigned int CANrxId;
+unsigned char CANlen = 0;
+unsigned char CANrxBuf[8];
+const int CAN_CSpin = 53 , CAN_INTpin = 10 ;
+MCP_CAN CAN0(CAN_CSpin);                          // Set CS to pin 10
+//EOF CAN
+
+
+
+void setup() {
+
+
+ String LCDarray1="CAN_1602_LCD           ";  //the string to print onthe LCD
+ String LCDarray2="Testing id   ";  //the string to print onthe LCD
+ LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
+ 
+
+  
+  // put your setup code here, to run once:
+  #if defined(HAS_LCD1602)
+    lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
+    lcd.print("TESTING id:");
+    lcd.print(Active_ControllerID);
+    lcd.setCursor(0,1);
+    lcd.print(LCDarray2);
+  #endif
+
+  #if defined(HAS_TFT_ILI9163C)
+     display.begin();
+     display.clearScreen();
+     display.setCursor(0,0);
+     display.print(LCDarray1);
+     display.setCursor(0,10);
+     display.print(LCDarray2);
+     delay(LCDtim); 
+  #endif
+
+  //BEGIN CAN
+   Serial.begin(115200);
+  //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
+  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
+    
+   Serial.print("MCP2515 Init Okay!!\r\n");
+    #if defined(HAS_LCD1602)
+      delay(LCDtim);  
+      lcd.setCursor(0,1); 
+      lcd.print("MCP2515 Init Okay!!\r\n");
+      delay(LCDtim); 
+   //  lcd.clear();  
+    #endif
+    #if defined(HAS_TFT_ILI9163C)
+      delay(LCDtim);  
+      display.setCursor(0,20); 
+      display.print("MCP2515 Init Okay!!\r\n");
+      delay(LCDtim); 
+    #endif
+  } else { 
+    Serial.print("MCP2515 Init Failed!!\r\n");
+      #if defined(HAS_LCD1602)
+        delay(LCDtim);  
+        lcd.setCursor(0,1); 
+        lcd.print("MCP2515 Init Failed!!\r\n");
+        delay(LCDtim); 
+         lcd.setCursor(0,0); 
+         lcd.print("CAN_CSpin: ");
+         lcd.print(CAN_CSpin);
+         lcd.print("CAN_INTpin: ");
+         lcd.print(CAN_INTpin);
+         delay(LCDtim); 
+       
+      #endif
+      #if defined(HAS_TFT_ILI9163C)
+        delay(LCDtim);  
+        display.setCursor(0,20); 
+        display.print("MCP2515 Init Failed!! Check: \r\n");
+        delay(LCDtim); 
+         display.setCursor(0,50); 
+         display.print("CAN_CSpin: ");
+         display.print(CAN_CSpin);
+         display.print("CAN_INTpin: ");
+         display.print(CAN_INTpin);
+         delay(LCDtim); 
+      #endif
+  }
+
+
+       #if Active_ControllerID == _Slave1_ID 
+        LCDarray1 = "Slave Module ";
+        LCDarray2 = "enabling filters";
+        //CAN0.init_Mask(0,0,0x010F0000);                // Init first mask...
+        //CAN0.init_Filt(0,0,0x01000000);                // Init first filter...
+        //CAN0.init_Filt(1,0,0x01010000);                // Init second filter...
+        
+        //CAN0.init_Mask(1,0,0x010F0000);                // Init second mask... 
+        //CAN0.init_Filt(2,0,0x01030000);                // Init third filter...
+        //CAN0.init_Filt(3,0,0x01040000);                // Init fouth filter...
+        //CAN0.init_Filt(4,0,0x01060000);                // Init fifth filter...
+        //CAN0.init_Filt(5,0,0x01070000);                // Init sixth filter...
+        
+        //CAN0.init_Mask(0, 0, 0x01FF);                         
+        //CAN0.init_Filt(0, 0, 0x103); 
+  
+        //CAN.init_Mask(1, 0, 0x04FF);   
+        //CAN.init_Filt(1, 0, 0x4C0); 
+       #elif Active_ControllerID == _ControllerID
+          LCDarray1 = "Master Module ";
+          LCDarray2 = "enabling remotes";
+       #endif
+       
+        
+        Serial.println("");
+         #if defined(HAS_LCD1602)
+          delay(LCDtim);  
+          lcd.setCursor(0,0); 
+          lcd.print(LCDarray1);
+          lcd.setCursor(0,1); 
+          lcd.print(LCDarray2);
+          delay(LCDtim * 10); 
+         #endif
+         #if defined(HAS_TFT_ILI9163C)
+          delay(LCDtim);  
+          display.setCursor(0,30); 
+          display.print(LCDarray1); display.print(" ");
+          display.print(LCDarray2);
+
+          delay(LCDtim); 
+         #endif
+        CAN0.setMode(MCP_NORMAL);                // Change to normal mode to allow messages to be transmitted
+        pinMode(CAN_INTpin, INPUT);                       // Setting pin 2 for /INT input
+
+  //EOF CAN
+   #if defined(HAS_LCD1602) 
+      lcd.setCursor(0,1); 
+      lcd.print("loop...");
+      lcdPos = 7;
+    #endif
+  
+}
+
+    #if Active_ControllerID == _ControllerID
+      byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
+    #endif
+
+  String LCDarray1 ;
+  String LCDarray2 ;
+void loop() {
+  // put your main code here, to run repeatedly:
+  //BEGIN CAN
+   #if Active_ControllerID == _Slave1_ID
+        if(!digitalRead(CAN_INTpin))                    // If pin 2 is low, read receive buffer
+          {
+            CAN0.readMsgBuf(&CANrxId, &CANlen, CANrxBuf); // Read data: len = data length, buf = data byte(s)
+            Serial.print("ID: ");
+            Serial.print(CANrxId, HEX);
+            Serial.print(" Data: ");
+             #if defined(HAS_LCD1602)
+              lcd.setCursor(0,0); 
+              lcd.print("ID: ");
+              lcd.print(CANrxId, HEX);
+              lcd.setCursor(0,1); 
+              lcd.print(" Data: ");
+             #endif
+             #if defined(HAS_TFT_ILI9163C)
+              display.setCursor(0,40); 
+              display.print("ID: ");
+              display.print(CANrxId, HEX);
+              display.setCursor(0,50); 
+              display.print(" Data: ");
+             #endif
+            for(int i = 0; i<CANlen; i++)           // Print each byte of the data
+            {
+              if(CANrxBuf[i] < 0x10)                // If data byte is less than 0x10, add a leading zero
+              {
+                Serial.print("0");
+                 #if defined(HAS_LCD1602)
+                    lcd.print("0");
+                 #endif
+                 #if defined(HAS_TFT_ILI9163C)
+                    display.print("0");
+                 #endif
+              }
+              Serial.print(CANrxBuf[i], HEX);
+              Serial.print(" ");
+               #if defined(HAS_LCD1602)
+                  lcd.print(CANrxBuf[i], HEX);
+                  lcd.print(" ");
+               #endif
+               #if defined(HAS_TFT_ILI9163C)
+                  display.print(CANrxBuf[i], HEX);
+                  display.print(" ");
+               #endif
+            }
+            Serial.println();
+          }
+        //EOF CAN
+          #if defined(HAS_LCD1602)
+            delay(LCDtim);  
+            //lcd.setCursor(0,1); 
+            lcd.print(lcdPattern);
+            //lcd.print(lcdPos % 10);
+            if(lcdPos == 16) { 
+              //lcd.print("N");
+              lcd.setCursor(0,1); 
+              lcdPos = 0;
+              if(lcdPattern==".") lcdPattern = "-" ;
+              else lcdPattern = "." ;
+            }
+            lcdPos ++;
+          #endif
+          #if defined(HAS_TFT_ILI9163C)
+            delay(LCDtim);  
+            display.setCursor(0,60); 
+            display.print("loop....");
+          #endif
+          
+    #elif Active_ControllerID == _ControllerID
+         #include "_ControllerID_loop1.h"
+         
+    #endif
+
+
+
+    
+}

+ 197 - 0
SE/stuff/P5_Automation_can-dev/_ControllerID_loop1.h

@@ -0,0 +1,197 @@
+// send data:  ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
+          //byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
+         
+          
+          
+           Serial.println("For tesing init remote ports");
+         /* for (String v: _Slave_Ports) {
+            Serial.print("Port: ");
+            Serial.println(v);
+             // for (auto i: v) {
+                // do something with i
+              //}
+
+              
+            }
+           const char * const _Protocol;
+  int _Slave_ID;
+  int PortTypes;  //0xFF  =  1111 1111 {  PWM,  .....} = 0x80
+ // byte has_Address;
+  int Address ;
+ // byte has_Local_Port;
+  int LocalPort;
+  int Exp_state;
+  int Exp_state_ttl;  // 0x00 = odswiezyc
+          */
+           for (auto element: _Slave_Ports) {
+             Serial.print(" Prot:");
+             Serial.print(element._Protocol);
+              Serial.print(" , ID:");
+             Serial.print(element._Slave_ID);
+              Serial.print(" , PortTypes:");
+             Serial.print(element.PortTypes);
+               Serial.print(" , Addr:");
+             Serial.print(element.Address);
+              Serial.print(" , Local:");
+             Serial.print(element.LocalPort);
+              Serial.print(" , Exp state:");
+             Serial.print(element.Exp_state);
+                Serial.print(" , Exp_state_ttl");
+             Serial.println(element.Exp_state_ttl);  
+              
+                if(element._Protocol == "CAN") { 
+
+                //  if(element.PortTypes == 0x80) {
+                       Serial.println(" Prepared data");
+
+
+                        #if defined(HAS_LCD1602)
+                               delay(LCDtim);  
+                              
+                               
+                               
+                             String LCDarray1 = "P[";
+                             LCDarray1.concat(element._Protocol);
+                             LCDarray1.concat("] ID[");
+                             LCDarray1.concat(String(element._Slave_ID, HEX));
+                             LCDarray1.concat("] T[");
+                             LCDarray1.concat(String(element.PortTypes, HEX));
+                             LCDarray1.concat("                "); //1234567890ABCDEFGHIJKLMNOPQRTSWabcdefghijklmnopqrstwquwz
+                            // lcd.print(LCDarray1);
+                            
+                             unsigned char LCDarray1buf[15];
+                             Serial.print(" LCDarray1 Strlen:::[");
+                             Serial.print(LCDarray1);
+                             Serial.println(" ]:::");
+                             //char LCDarray11[] = {String(LCDarray1)};
+                             LCDarray1.toCharArray(LCDarray1buf, 15);
+
+                                Serial.print(" LCDarray1BStrlen:::[");
+                                Serial.print(LCDarray1);
+                                Serial.println(" ]:::");
+                                String LCDarray2 = " >";
+                                 LCDarray2.concat(String(element.Address, HEX));
+                                 LCDarray2.concat( "< {");
+                                 LCDarray2.concat( String(element.LocalPort, HEX));
+                                 LCDarray2.concat( "} =[" );
+                                 LCDarray2.concat(String(element.Exp_state, HEX) );
+                                 LCDarray2.concat("] ?[" ) ;
+                                 LCDarray2.concat(String(element.Exp_state_ttl, HEX)) ;
+                                 LCDarray2.concat("]                       ") ;
+                                 unsigned char LCDarray2buf[16];
+                                    Serial.print(" LCDarray2 Strlen:::[");
+                                    Serial.print(LCDarray2);
+                                    Serial.println(" ]:::");
+                                 LCDarray2.toCharArray(LCDarray2buf, 15);
+
+                                    Serial.print(" LCDarray2B`Strlen:::[");
+                                    Serial.print(LCDarray2);
+                                    Serial.println(" ]:::");
+                                         lcd.setCursor(15,0); 
+                                         for ( int positionCounter1 = 15;
+                                        positionCounter1 < 24; positionCounter1++)
+                                            {
+                                              lcd.scrollDisplayLeft();  //Scrolls the contents of the display one space to the left.
+                                              lcd.print(LCDarray1[positionCounter1]);  // Print a message to the LCD.
+                                              delay(LCDtim);  //wait for 250 microseconds
+                                            }
+                                            lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner.
+                                          
+                                          lcd.setCursor(15,1); 
+                                         for ( int positionCounter2 = 15;
+                                        positionCounter2 < 24; positionCounter2++)
+                                            {
+                                              lcd.scrollDisplayLeft();  //Scrolls the contents of the display one space to the left.
+                                              lcd.print(LCDarray2[positionCounter2]);  // Print a message to the LCD.
+                                              delay(LCDtim);  //wait for 250 microseconds
+                                            }
+                                            delay(LCDtim  * 5);  //wait for 250 microseconds
+                                            lcd.clear();  //Clears the LCD screen and positions the cursor in the upper-left corner.
+
+                             /* lcd.print("P[");
+                               lcd.print(element._Protocol);
+                                lcd.print("]ID[");
+                               lcd.print(element._Slave_ID);
+                                lcd.print("]T[");
+                               lcd.print(element.PortTypes);
+                                 */ 
+                                
+                                
+                               // lcd.setCursor(0,1); 
+                               // lcd.print(LCDarray2);
+                              /*   lcd.print("]@[");
+                               lcd.print(element.Address);
+                                lcd.print("]|[");
+                               lcd.print(element.LocalPort);
+                                lcd.print("]=[");
+                               lcd.print(element.Exp_state);
+                                  lcd.print("]?[");
+                               lcd.println(element.Exp_state_ttl);  
+                               */
+                               /*
+                                lcdPos = 14;
+                                 delay(LCDtim * 5);  
+                                  lcd.setCursor(0,0); 
+                                  lcd.print(LCDarray2);
+                                  lcd.setCursor(0,1); 
+                                  lcd.print("                  ");
+                                 //copy cursor to up
+
+                                */
+                                 
+                        #endif
+
+
+                       
+                      byte data[8] = {element.Address, element.LocalPort, element.PortTypes, element.LocalPort, element.Exp_state, element.Exp_state_ttl, 0x06, 0x07};
+                  // }
+                 byte sndStat = CAN0.sendMsgBuf(element._Slave_ID, 0, 8, data);
+                 
+                          if(sndStat == CAN_OK){
+                            Serial.println("Message Sent Successfully!");
+                             String LCDarray1 = "Message Sent";
+                             String LCDarray2 = "Successfully";
+                          } else {
+                            Serial.println("Error Sending Message...");
+                            String LCDarray1 = "Error Sending";
+                            String LCDarray2 = "Message...";
+                          }
+                }
+
+
+
+
+                  
+                          
+                                  #if defined(HAS_LCD1602)
+                                   delay(LCDtim);  
+                                    //lcd.setCursor(0,1); 
+                                    lcd.print(lcdPattern);
+                                    //lcd.print(lcdPos % 10);
+                                  if(lcdPos >= 16) { 
+                                    //lcd.print("N");
+                                    lcd.setCursor(0,1); 
+                                    lcdPos = 0;
+                                    if(lcdPattern=="s") lcdPattern = "S" ;
+                                    else lcdPattern = "s" ;
+                                  }
+                                    lcdPos ++;
+                          #endif
+                                  #if defined(HAS_TFT_ILI9163C)
+                                    delay(LCDtim);  
+                                    display.setCursor(0,60); 
+                                    display.print(LCDarray1); display.print(" "); display.print(LCDarray2);
+                                  #endif
+                          delay(5000);   // send data per 100ms
+
+             
+            }
+          
+          
+           
+          
+         // byte sndStat = CAN0.sendMsgBuf(_Slave1_ID, 0, 8, data);
+
+
+          
+        

+ 105 - 0
SE/stuff/P5_Automation_can-dev/_Slave_Ports.h

@@ -0,0 +1,105 @@
+ 
+
+typedef struct  {
+  const char * const _Protocol;
+  int _Slave_ID;
+  int PortTypes;  //0xFF  =  1111 1111 {  PWM,  .....} = 0x80
+ // byte has_Address;
+  int Address ;
+ // byte has_Local_Port;
+  int LocalPort;
+  int Exp_state;
+  int Exp_state_ttl;  // 0x00 = odswiezyc
+} _Slave_Port;
+
+_Slave_Port _Slave_Ports[] = {
+  {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 },
+  {"CAN", 0x102, 0x80, 0x00, 1, 0xFF, 0x20 } //testowy ttl
+};
+
+
+
+
+/*
+struct _Slave_Port
+{ 
+  char _Protocol ;
+  int _Slave_ID;
+  char Port_Type;
+  int Address;
+  int LocalPort ;
+  void (*fn)();
+};
+
+
+struct _Slave_Port {
+   
+}
+
+#define _Slave_Port =   { "CAN" , 0x102 , "PWM", 0x00, 0  } 
+
+*/
+
+  /* #define __CAN_Ports { _Slave1_ID } ;
+
+
+   #define __CAN_Ports_PWM { _Slave1_ID 
+    {0x00 , 0 } ,
+    {0x00 , 1 }
+   } ;
+
+   */
+/*
+   typedef struct
+ {
+     int Slave1_ID;
+     String PortType;
+     int Address;
+     int LocalPort ;
+ } 
+
+*/
+/*
+
+struct buttons
+{ 
+  int color[3];
+  int positions[4];
+  char init[20];
+  void (*fn)();
+};
+
+//void func1();
+//void func2();
+
+  
+buttons test[] = 
+{
+  { {0,0,0},    {0,0,100,100}, "getSomething",  func1 },
+  { {40,40,40}, {50,50,10,10}, "somethingElse", func2 },
+};
+*/
+
+/*
+typedef struct  {
+  const char * const l_name;
+  byte x_m;
+  byte y_m;
+  boolean period[4];
+} myStruct;
+
+myStruct structs[] = {
+  {"My Name", 0, 0, {true, true, false, false}},
+  {"My Second Name", 0, 0, {true, false, true, false}}
+};
+*/
+
+
+
+ //#define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 } 
+ //#define _CAN_P_0x102_PWM0x00_1 { _Slave1_ID , "PWM", 0x00, 1 } 
+ // char _Slave_Ports ;
+ // _Slave_Ports[0] = "_CAN_P_0x102_PWM0x00_0"  ;
+ // _Slave_Ports[1] = "_CAN_P_0x102_PWM0x00_1"  ;
+
+ // char *_Slave_Ports[] = { "_CAN_P_0x102_PWM0x00_0", "_CAN_P_0x102_PWM0x00_1" } ;