P5_Automation_can-dev.ino 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* P5_Automation_can-dev
  2. * 2020-06 testing communications CAN for integrate further with Procesy5
  3. * A.Binder
  4. * Uses:
  5. * LCD1602 - big 2x16 character display
  6. * TFT_ILI9163C 128x128 lcd display
  7. * MCP_2515 - can port controller
  8. *
  9. * controls
  10. * Slave ports via:
  11. * I2C
  12. * ]PCA9685 - 16port 12bit PWM driver - can controll 16 eg. LEDs which can be 0....12V dimmed=]
  13. * or 16 servos
  14. * - can have addressed many of PCA9685 on I2C bus - so can controll over 64 ports
  15. * to be added another devices tested
  16. * TODO structure of port is in _Slave_Ports.h
  17. * _Slave_Port _Slave_Ports[] = {
  18. {"CAN", 0x102, 0x80, 0x00, 0, 0x00, 0x00 },
  19. {"CAN", 0x102, 0x80, 0x00, 1, 0xFF, 0x20 } //testowy ttl
  20. };
  21. - to be optimalized for inegration
  22. - to allow of subscribe ports
  23. - to allow of concurent routing
  24. - to allow new port detectioj
  25. - to allow creating ports dependencies e.g. IF
  26. - port has value
  27. - port AND port
  28. THEN
  29. - set port
  30. - probably should be avilable creations of virtual ports which would describe
  31. - state of port AND port etc...
  32. * TO BE INTEGRATED WITH p5
  33. * - interface and WFST
  34. * TO BE INGEGRATED WITH MARLIN 3D PRINTERS - machine and automation purpose
  35. * - controll remote ports via g-code
  36. * - allows interrupts
  37. */
  38. #define _ControllerID 0x101
  39. #define _Slave1_ID 0x103
  40. #define Active_ControllerID _ControllerID
  41. //#define Active_ControllerID _Slave1_ID
  42. //REMOTE PORTS DEFINITIONS
  43. #include "_Slave_Ports.h"
  44. // #define _CAN_P_0x102_PWM0x00_0 { _Slave1_ID , "PWM", 0x00, 0 }
  45. //#define _CAN_P_0x102_PWMServoDriver0x40
  46. // #define Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver();
  47. // Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);
  48. #if Active_ControllerID == _ControllerID
  49. String lcdPattern = "s" ;
  50. #define HAS_LCD1602
  51. // #define HAS_TFT_ILI9163C
  52. #endif
  53. #if Active_ControllerID == _Slave1_ID
  54. String lcdPattern = "." ;
  55. #define HAS_TFT_ILI9163C
  56. // #define HAS_LCD1602
  57. #endif
  58. // #define __CSpin 53 // 10 // chip select
  59. int LCDtim = 250; //the value of delay time
  60. //BEGIN 1602
  61. #if defined(HAS_LCD1602)
  62. #include <LiquidCrystal.h>
  63. // initialize the library with the numbers of the interface pins
  64. //LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  65. //LiquidCrystal lcd(4, 6, 53, 51, 50, 52 );
  66. const int LCD1602rs=12, LCD1602en=11, LCD1602db4=5, LCD1602db5=4, LCD1602db6=3, LCD1602db7=2; // lcd keypad shield pins
  67. LiquidCrystal lcd(LCD1602rs, LCD1602en, LCD1602db4, LCD1602db5, LCD1602db6, LCD1602db7);
  68. int lcdPos = 0 ;
  69. #endif
  70. // include the library code
  71. /**********************************************************/
  72. //EOF BEGIN 1602
  73. #if defined(HAS_TFT_ILI9163C)
  74. #include <SPI.h>
  75. #include <Adafruit_GFX.h>
  76. #include <TFT_ILI9163C.h>
  77. //#define TFT_ILI9163C_CSpin __CSpin // 53 // 10 // chip select
  78. #define TFT_ILI9163C_CSpin 7 // __CSpin // 53 // 10 // chip select
  79. #define TFT_ILI9163C_DCpin 8 // A0 data/command pin. sometimes called DC, RS, ...
  80. #define TFT_ILI9163C__RSTpin 9 //reset pin. you must connect. (or pullup)
  81. //#define __SDA 51 //11 //+ bidirectional data pin. Connect to MOSI
  82. //#define __SCK 52 //13 //+ clock. sometimes called SCLK, SCL, ...
  83. // Color definitions
  84. #define BLACK 0x0000
  85. #define BLUE 0x001F
  86. #define RED 0xF800
  87. #define GREEN 0x07E0
  88. #define CYAN 0x07FF
  89. #define MAGENTA 0xF81F
  90. #define YELLOW 0xFFE0
  91. #define WHITE 0xFFFF
  92. TFT_ILI9163C display = TFT_ILI9163C(TFT_ILI9163C_CSpin, TFT_ILI9163C_DCpin, TFT_ILI9163C__RSTpin);
  93. float p = 3.1415926;
  94. #endif
  95. //BEGIN CAN
  96. #include <mcp_can.h>
  97. #include <SPI.h>
  98. long unsigned int CANrxId;
  99. unsigned char CANlen = 0;
  100. unsigned char CANrxBuf[8];
  101. const int CAN_CSpin = 53 , CAN_INTpin = 10 ;
  102. MCP_CAN CAN0(CAN_CSpin); // Set CS to pin 10
  103. //EOF CAN
  104. void setup() {
  105. String LCDarray1="CAN_1602_LCD "; //the string to print onthe LCD
  106. String LCDarray2="Testing id "; //the string to print onthe LCD
  107. LCDarray2.concat(String(Active_ControllerID, HEX) ) ;
  108. // put your setup code here, to run once:
  109. #if defined(HAS_LCD1602)
  110. lcd.begin(16, 2); // set up the LCD's number of columns and rows:
  111. lcd.print("TESTING id:");
  112. lcd.print(Active_ControllerID);
  113. lcd.setCursor(0,1);
  114. lcd.print(LCDarray2);
  115. #endif
  116. #if defined(HAS_TFT_ILI9163C)
  117. display.begin();
  118. display.clearScreen();
  119. display.setCursor(0,0);
  120. display.print(LCDarray1);
  121. display.setCursor(0,10);
  122. display.print(LCDarray2);
  123. delay(LCDtim);
  124. #endif
  125. //BEGIN CAN
  126. Serial.begin(115200);
  127. //if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  128. if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) {
  129. Serial.print("MCP2515 Init Okay!!\r\n");
  130. #if defined(HAS_LCD1602)
  131. delay(LCDtim);
  132. lcd.setCursor(0,1);
  133. lcd.print("MCP2515 Init Okay!!\r\n");
  134. delay(LCDtim);
  135. // lcd.clear();
  136. #endif
  137. #if defined(HAS_TFT_ILI9163C)
  138. delay(LCDtim);
  139. display.setCursor(0,20);
  140. display.print("MCP2515 Init Okay!!\r\n");
  141. delay(LCDtim);
  142. #endif
  143. } else {
  144. Serial.print("MCP2515 Init Failed!!\r\n");
  145. #if defined(HAS_LCD1602)
  146. delay(LCDtim);
  147. lcd.setCursor(0,1);
  148. lcd.print("MCP2515 Init Failed!!\r\n");
  149. delay(LCDtim);
  150. lcd.setCursor(0,0);
  151. lcd.print("CAN_CSpin: ");
  152. lcd.print(CAN_CSpin);
  153. lcd.print("CAN_INTpin: ");
  154. lcd.print(CAN_INTpin);
  155. delay(LCDtim);
  156. #endif
  157. #if defined(HAS_TFT_ILI9163C)
  158. delay(LCDtim);
  159. display.setCursor(0,20);
  160. display.print("MCP2515 Init Failed!! Check: \r\n");
  161. delay(LCDtim);
  162. display.setCursor(0,50);
  163. display.print("CAN_CSpin: ");
  164. display.print(CAN_CSpin);
  165. display.print("CAN_INTpin: ");
  166. display.print(CAN_INTpin);
  167. delay(LCDtim);
  168. #endif
  169. }
  170. #if Active_ControllerID == _Slave1_ID
  171. LCDarray1 = "Slave Module ";
  172. LCDarray2 = "enabling filters";
  173. //CAN0.init_Mask(0,0,0x010F0000); // Init first mask...
  174. //CAN0.init_Filt(0,0,0x01000000); // Init first filter...
  175. //CAN0.init_Filt(1,0,0x01010000); // Init second filter...
  176. //CAN0.init_Mask(1,0,0x010F0000); // Init second mask...
  177. //CAN0.init_Filt(2,0,0x01030000); // Init third filter...
  178. //CAN0.init_Filt(3,0,0x01040000); // Init fouth filter...
  179. //CAN0.init_Filt(4,0,0x01060000); // Init fifth filter...
  180. //CAN0.init_Filt(5,0,0x01070000); // Init sixth filter...
  181. //CAN0.init_Mask(0, 0, 0x01FF);
  182. //CAN0.init_Filt(0, 0, 0x103);
  183. //CAN.init_Mask(1, 0, 0x04FF);
  184. //CAN.init_Filt(1, 0, 0x4C0);
  185. #elif Active_ControllerID == _ControllerID
  186. LCDarray1 = "Master Module ";
  187. LCDarray2 = "enabling remotes";
  188. #endif
  189. Serial.println("");
  190. #if defined(HAS_LCD1602)
  191. delay(LCDtim);
  192. lcd.setCursor(0,0);
  193. lcd.print(LCDarray1);
  194. lcd.setCursor(0,1);
  195. lcd.print(LCDarray2);
  196. delay(LCDtim * 10);
  197. #endif
  198. #if defined(HAS_TFT_ILI9163C)
  199. delay(LCDtim);
  200. display.setCursor(0,30);
  201. display.print(LCDarray1); display.print(" ");
  202. display.print(LCDarray2);
  203. delay(LCDtim);
  204. #endif
  205. CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
  206. pinMode(CAN_INTpin, INPUT); // Setting pin 2 for /INT input
  207. //EOF CAN
  208. #if defined(HAS_LCD1602)
  209. lcd.setCursor(0,1);
  210. lcd.print("loop...");
  211. lcdPos = 7;
  212. #endif
  213. }
  214. #if Active_ControllerID == _ControllerID
  215. byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  216. #endif
  217. String LCDarray1 ;
  218. String LCDarray2 ;
  219. void loop() {
  220. // put your main code here, to run repeatedly:
  221. //BEGIN CAN
  222. #if Active_ControllerID == _Slave1_ID
  223. if(!digitalRead(CAN_INTpin)) // If pin 2 is low, read receive buffer
  224. {
  225. CAN0.readMsgBuf(&CANrxId, &CANlen, CANrxBuf); // Read data: len = data length, buf = data byte(s)
  226. Serial.print("ID: ");
  227. Serial.print(CANrxId, HEX);
  228. Serial.print(" Data: ");
  229. #if defined(HAS_LCD1602)
  230. lcd.setCursor(0,0);
  231. lcd.print("ID: ");
  232. lcd.print(CANrxId, HEX);
  233. lcd.setCursor(0,1);
  234. lcd.print(" Data: ");
  235. #endif
  236. #if defined(HAS_TFT_ILI9163C)
  237. display.setCursor(0,40);
  238. display.print("ID: ");
  239. display.print(CANrxId, HEX);
  240. display.setCursor(0,50);
  241. display.print(" Data: ");
  242. #endif
  243. for(int i = 0; i<CANlen; i++) // Print each byte of the data
  244. {
  245. if(CANrxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
  246. {
  247. Serial.print("0");
  248. #if defined(HAS_LCD1602)
  249. lcd.print("0");
  250. #endif
  251. #if defined(HAS_TFT_ILI9163C)
  252. display.print("0");
  253. #endif
  254. }
  255. Serial.print(CANrxBuf[i], HEX);
  256. Serial.print(" ");
  257. #if defined(HAS_LCD1602)
  258. lcd.print(CANrxBuf[i], HEX);
  259. lcd.print(" ");
  260. #endif
  261. #if defined(HAS_TFT_ILI9163C)
  262. display.print(CANrxBuf[i], HEX);
  263. display.print(" ");
  264. #endif
  265. }
  266. Serial.println();
  267. }
  268. //EOF CAN
  269. #if defined(HAS_LCD1602)
  270. delay(LCDtim);
  271. //lcd.setCursor(0,1);
  272. lcd.print(lcdPattern);
  273. //lcd.print(lcdPos % 10);
  274. if(lcdPos == 16) {
  275. //lcd.print("N");
  276. lcd.setCursor(0,1);
  277. lcdPos = 0;
  278. if(lcdPattern==".") lcdPattern = "-" ;
  279. else lcdPattern = "." ;
  280. }
  281. lcdPos ++;
  282. #endif
  283. #if defined(HAS_TFT_ILI9163C)
  284. delay(LCDtim);
  285. display.setCursor(0,60);
  286. display.print("loop....");
  287. #endif
  288. #elif Active_ControllerID == _ControllerID
  289. #include "_ControllerID_loop1.h"
  290. #endif
  291. }