P5_Automation_can-dev.ino 13 KB

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