j0h

arduino_PLC_encoders

j0h
May 14th, 2026
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "Adafruit_seesaw.h"
  3. #include <Keyboard.h>
  4. #include <SPI.h>
  5. #include <Ethernet.h>
  6.  
  7. #define SS_SWITCH 24
  8. #define SEESAW_BASE_ADDR 0x36
  9.  
  10. const uint8_t Nencoders = 5;
  11.  
  12. // Network config
  13. byte mac[] = { 0x00, 0x03, 0x7B, 0x20, 0xFE, 0xED };
  14. byte ip[] = { 192, 168, 1, 50 };
  15.  
  16. EthernetServer server(2101);
  17.  
  18. Adafruit_seesaw encoders[Nencoders];
  19.  
  20. bool found_encoders[Nencoders] = { false };
  21. int32_t encoder_positions[Nencoders] = { 0 };
  22.  
  23. int16_t w = 0;
  24. int16_t x = 0;
  25. int16_t y = 0;
  26. int16_t z = 0;
  27.  
  28. void setup() {
  29. Serial.begin(9600);
  30.  
  31. Ethernet.begin(mac, ip);
  32. delay(1000);
  33. server.begin();
  34.  
  35. randomSeed(analogRead(0));
  36.  
  37. Wire.begin();
  38.  
  39. while (!Serial) {
  40. delay(5);
  41. }
  42.  
  43. for (uint8_t enc = 0; enc < Nencoders; enc++) {
  44. uint8_t addr = SEESAW_BASE_ADDR + enc;
  45.  
  46. if (!encoders[enc].begin(addr)) {
  47. found_encoders[enc] = false;
  48. } else {
  49. encoders[enc].pinMode(SS_SWITCH, INPUT_PULLUP);
  50. encoders[enc].setEncoderPosition(0);
  51. encoder_positions[enc] = 0;
  52.  
  53. encoders[enc].setGPIOInterrupts((uint32_t)1 << SS_SWITCH, 1);
  54. encoders[enc].enableEncoderInterrupt();
  55.  
  56. found_encoders[enc] = true;
  57. }
  58. }
  59.  
  60. Serial.print("encoders found at addresses: ");
  61.  
  62. for (uint8_t enc = 0; enc < Nencoders; enc++) {
  63. if (found_encoders[enc]) {
  64. Serial.print("0x");
  65. Serial.print(SEESAW_BASE_ADDR + enc, HEX);
  66. Serial.print(" ");
  67. }
  68. }
  69.  
  70. Serial.println();
  71. }
  72.  
  73. void cls() {
  74. for (uint8_t enc = 0; enc < Nencoders; enc++) {
  75. if (found_encoders[enc]) {
  76. encoders[enc].setEncoderPosition(0);
  77. encoder_positions[enc] = 0;
  78. }
  79. }
  80.  
  81. w = 0;
  82. x = 0;
  83. y = 0;
  84. z = 0;
  85. }
  86.  
  87. void oncod() {
  88. for (uint8_t enc = 0; enc < Nencoders; enc++) {
  89. if (!found_encoders[enc]) {
  90. continue;
  91. }
  92.  
  93. int32_t new_position = encoders[enc].getEncoderPosition();
  94.  
  95. if (encoder_positions[enc] != new_position) {
  96. encoder_positions[enc] = new_position;
  97. }
  98. }
  99.  
  100. w = (int16_t)encoder_positions[0];
  101. x = (int16_t)encoder_positions[1];
  102. y = (int16_t)encoder_positions[2];
  103. z = (int16_t)encoder_positions[3];
  104. }
  105.  
  106. uint16_t readDWord(int addr) {
  107. union {
  108. float f;
  109. uint32_t i;
  110. } u;
  111.  
  112. switch (addr) {
  113. // A0 -> D156/D157
  114. case 157:
  115. u.f = analogRead(A0);
  116. return (u.i >> 16) & 0xFFFF;
  117.  
  118. case 156:
  119. u.f = analogRead(A0);
  120. return u.i & 0xFFFF;
  121.  
  122. // A1 -> D160/D161
  123. case 161:
  124. u.f = analogRead(A1);
  125. return (u.i >> 16) & 0xFFFF;
  126.  
  127. case 160:
  128. u.f = analogRead(A1);
  129. return u.i & 0xFFFF;
  130.  
  131. // A2 -> D164/D165
  132. case 165:
  133. u.f = analogRead(A2);
  134. return (u.i >> 16) & 0xFFFF;
  135.  
  136. case 164:
  137. u.f = analogRead(A2);
  138. return u.i & 0xFFFF;
  139.  
  140. case 666:
  141. return digitalRead(21);
  142.  
  143. // Encoder values for HMI
  144. case 669:
  145. return (uint16_t)w;
  146.  
  147. case 670:
  148. return (uint16_t)x;
  149.  
  150. case 671:
  151. return (uint16_t)y;
  152.  
  153. case 672:
  154. return (uint16_t)z;
  155.  
  156. case 570:
  157. return 26;
  158.  
  159. case 3498:
  160. case 3499:
  161. case 3500:
  162. return 0;
  163.  
  164. case 158:
  165. return random(50, 101);
  166.  
  167. default:
  168. return 0;
  169. }
  170. }
  171.  
  172. byte xorBcc(byte *data, int len) {
  173. byte bcc = 0;
  174.  
  175. for (int i = 0; i < len; i++) {
  176. bcc ^= data[i];
  177. }
  178.  
  179. return bcc;
  180. }
  181.  
  182. void replyReadD(EthernetClient &c, int addr, int nbytes) {
  183. byte tx[96];
  184. int p = 0;
  185.  
  186. tx[p++] = 0x06; // ACK
  187. tx[p++] = '0'; // reply device 00
  188. tx[p++] = '0';
  189. tx[p++] = '0'; // OK discontinued
  190.  
  191. int words = nbytes / 2;
  192.  
  193. for (int i = 0; i < words; i++) {
  194. uint16_t v = readDWord(addr + i);
  195.  
  196. const char *h = "0123456789ABCDEF";
  197.  
  198. tx[p++] = h[(v >> 12) & 0xF];
  199. tx[p++] = h[(v >> 8) & 0xF];
  200. tx[p++] = h[(v >> 4) & 0xF];
  201. tx[p++] = h[v & 0xF];
  202. }
  203.  
  204. byte bcc = xorBcc(tx, p);
  205.  
  206. const char *h = "0123456789ABCDEF";
  207.  
  208. tx[p++] = h[(bcc >> 4) & 0xF];
  209. tx[p++] = h[bcc & 0xF];
  210. tx[p++] = '\r';
  211.  
  212. c.write(tx, p);
  213. }
  214.  
  215. void handle(EthernetClient c) {
  216. byte buf[64];
  217. int len = 0;
  218. unsigned long lastRx = millis();
  219.  
  220. while (c.connected() && (millis() - lastRx < 3000)) {
  221. oncod();
  222.  
  223. while (c.available()) {
  224. lastRx = millis();
  225.  
  226. byte b = c.read();
  227.  
  228. if (len < 64) {
  229. buf[len++] = b;
  230. }
  231.  
  232. if (b == 0x0D) {
  233. if (len >= 14 && buf[0] == 0x05 && buf[4] == 'R' && buf[5] == 'D') {
  234. char a[5] = {
  235. (char)buf[6],
  236. (char)buf[7],
  237. (char)buf[8],
  238. (char)buf[9],
  239. 0
  240. };
  241.  
  242. char l[3] = {
  243. (char)buf[10],
  244. (char)buf[11],
  245. 0
  246. };
  247.  
  248. int addr = atoi(a);
  249. int nbytes = strtol(l, NULL, 16);
  250.  
  251. replyReadD(c, addr, nbytes);
  252. }
  253.  
  254. len = 0;
  255. }
  256. }
  257. }
  258.  
  259. c.stop();
  260. }
  261.  
  262. void loop() {
  263. oncod();
  264.  
  265. /*
  266. if (Serial.available()) {
  267. char c = Serial.read();
  268.  
  269. if (c == 'c') {
  270. cls();
  271. }
  272. }
  273. */
  274.  
  275. EthernetClient client = server.available();
  276.  
  277. if (client) {
  278. handle(client);
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment