Frästisch mit elektronischer Höhenverstellung mittels Schrittmotoren.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Fraestisch_SFTools.ino 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. Steuerung für Frästisch_SFTools
  3. Diese Datei enthält den Code für die Implementierung der Steuerung eines Frästisches mit 2 Nema17 Schrittmotoren.
  4. Sie unterstützt:
  5. - Schnelles/langsames Verstellen der Fräserhöhe
  6. - Werkzeugwechsel
  7. - Automatisches Nullen mit WLS
  8. - Eintauchen mit vordefinierter Tiefe
  9. - Für obige Funktionen notwendige Konfiguration
  10. Erstellt: 05.01.2021
  11. Autor: Flo Smilari
  12. */
  13. #include <Arduino.h>
  14. #include <RotaryEncoder.h>
  15. #include "WLS.h"
  16. #include "ExEzButton.h"
  17. #include <Wire.h>
  18. #include <Adafruit_GFX.h>
  19. #include <Adafruit_SSD1306.h>
  20. #include <ESP_FlexyStepper.h>
  21. #include "fonts/titillium_web_6pt7b.h"
  22. #include "fonts/titillium_web_8pt7b.h"
  23. #include "fonts/titillium_web_12pt7b.h"
  24. #include "fonts/titillium_web_24pt7b.h"
  25. #include "fonts/titillium_web_30pt7b.h"
  26. #define SCREEN_WIDTH 128
  27. #define SCREEN_HEIGHT 64
  28. #define SCREEN_ADDRESS 0x3C
  29. #define SDA 22
  30. #define SCX 23
  31. #define STEP 2
  32. #define DIR 4
  33. #define LIMIT_SWITCH 5
  34. static const byte led_gpio = 15;
  35. static const int WLS_Pin = 34;
  36. static const int WLS_DETECT_Pin = 35;
  37. static const int GreenBtn_Pin = 12;
  38. static const int RedBtn_Pin = 13;
  39. static const int BlueBtn_Pin = 14;
  40. static const int RotEnc_Switch_Pin = 25;
  41. static const int RotEnc_Clk_Pin = 32;
  42. static const int RotEnc_Dta_Pin = 33;
  43. // Speed settings
  44. const int DISTANCE_TO_TRAVEL_IN_STEPS = 2000;
  45. const int SPEED_IN_STEPS_PER_SECOND = 300;
  46. const int ACCELERATION_IN_STEPS_PER_SECOND = 800;
  47. const int DECELERATION_IN_STEPS_PER_SECOND = 800;
  48. //variables for software debouncing of the limit switches
  49. unsigned long lastDebounceTime = 0;
  50. unsigned long debounceDelay = 100; //the minimum delay in milliseconds to check for bouncing of the switch. Increase this slighlty if you switches tend to bounce a lot
  51. byte limitSwitchState = 1;
  52. byte oldConfirmedLimitSwitchState = 0;
  53. ExEzButton RedButton(RedBtn_Pin, false, 2000);
  54. ExEzButton GreenButton(GreenBtn_Pin, false, 2000);
  55. ExEzButton BlueButton(BlueBtn_Pin, false, 2000);
  56. ExEzButton RotarySwitch(RotEnc_Switch_Pin, true, 2000);
  57. WLS WlsDetect(WLS_DETECT_Pin, true);
  58. WLS Wls(WLS_Pin);
  59. RotaryEncoder encoder(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotaryEncoder::LatchMode::FOUR3);
  60. Adafruit_SSD1306 Display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // SCK on Pin 23, SDA on PIN 22
  61. ESP_FlexyStepper stepper;
  62. int previousDirection = 1;
  63. void limitSwitchHandler()
  64. {
  65. limitSwitchState = digitalRead(LIMIT_SWITCH);
  66. lastDebounceTime = millis();
  67. }
  68. //**********************
  69. //*** SETUP ************
  70. //**********************
  71. void setup() {
  72. Wire.begin(SDA, SCX);
  73. Serial.begin(115200);
  74. if (!Display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, true, true)) {
  75. Serial.println(F("SSD1306 allocation failed"));
  76. for (;;)
  77. ; // Don't proceed, loop forever
  78. }
  79. pinMode(led_gpio, OUTPUT);
  80. pinMode(GreenBtn_Pin, INPUT);
  81. pinMode(RedBtn_Pin, INPUT);
  82. pinMode(BlueBtn_Pin, INPUT);
  83. pinMode(RotEnc_Switch_Pin, INPUT);
  84. pinMode(RotEnc_Clk_Pin, INPUT);
  85. pinMode(RotEnc_Dta_Pin, INPUT);
  86. pinMode(LIMIT_SWITCH, INPUT);
  87. RedButton.setDebounceTime(50); // set debounce time to 50 millis
  88. GreenButton.setDebounceTime(50);
  89. BlueButton.setDebounceTime(50);
  90. WlsDetect.setDebounceTime(50);
  91. Wls.setDebounceTime(50);
  92. RotarySwitch.setDebounceTime(50);
  93. Display.display();
  94. delay(2000); // Pause for 2 seconds
  95. // testdrawchar();
  96. // delay(5000); // Pause for 5 seconds
  97. testdrawstyles();
  98. //attach an interrupt to the IO pin of the limit switch and specify the handler function
  99. // attachInterrupt(digitalPinToInterrupt(LIMIT_SWITCH), limitSwitchHandler, CHANGE);
  100. //
  101. // stepper.connectToPins(STEP, DIR);
  102. // set the speed and acceleration rates for the stepper motor
  103. // stepper.setSpeedInStepsPerSecond(SPEED_IN_STEPS_PER_SECOND);
  104. // stepper.setAccelerationInStepsPerSecondPerSecond(ACCELERATION_IN_STEPS_PER_SECOND);
  105. // stepper.setDecelerationInStepsPerSecondPerSecond(DECELERATION_IN_STEPS_PER_SECOND);
  106. //
  107. // stepper.startAsService(1);
  108. }
  109. //**********************
  110. //*** MAIN LOOP ********
  111. //**********************
  112. void loop() {
  113. RedButton.loop(); // MUST call the loop() function first
  114. GreenButton.loop();
  115. BlueButton.loop();
  116. WlsDetect.loop();
  117. Wls.loop();
  118. RotarySwitch.loop();
  119. if (WlsDetect.isPlugged()) {
  120. Serial.println("The WLS was connected");
  121. } else if (WlsDetect.isUnplugged()) {
  122. Serial.println("The WLS was disconnected");
  123. }
  124. if (WlsDetect.getStateRaw() == HIGH) {
  125. if (Wls.isPlugged()) {
  126. Serial.println("The Tool is away from WLS");
  127. } else if (Wls.isUnplugged()) {
  128. Serial.println("The Tool touched the WLS");
  129. }
  130. }
  131. if (RedButton.isPressed()) {
  132. Serial.println("Red button was pressed");
  133. digitalWrite(led_gpio, HIGH);
  134. Serial.print("Limit Switch: ");
  135. Serial.println(digitalRead(LIMIT_SWITCH) ? "HIGH" : "LOW");
  136. }
  137. if (RedButton.isLongPressed()) {
  138. Serial.println("Red button was long pressed");
  139. digitalWrite(led_gpio, LOW);
  140. }
  141. bool greenPressed = GreenButton.isPressed();
  142. bool bluePressed = BlueButton.isPressed();
  143. if (GreenButton.isPressing() && BlueButton.isPressing()) {
  144. Serial.println("Blue and green button were simultaneous pressed");
  145. } else if (greenPressed) {
  146. Serial.println("Green button was pressed");
  147. } else if (bluePressed) {
  148. Serial.println("Blue button was pressed");
  149. }
  150. if (RotarySwitch.isPressed()) {
  151. Serial.println("Rotary switch was pressed");
  152. }
  153. if (RotarySwitch.isLongPressed()) {
  154. Serial.println("Rotary switch long was pressed");
  155. }
  156. static int pos = 0;
  157. encoder.tick();
  158. int newPos = encoder.getPosition();
  159. if (pos != newPos) {
  160. Serial.print("pos:");
  161. Serial.print(newPos);
  162. Serial.print(" dir:");
  163. Serial.println((int) (encoder.getDirection()));
  164. pos = newPos;
  165. }
  166. // if (limitSwitchState == LOW) {
  167. // stepper.setLimitSwitchActive(stepper.LIMIT_SWITCH_COMBINED_BEGIN_AND_END); // this will cause to stop any motion that is currently going on and block further movement in the same direction as long as the switch is agtive
  168. // } else {
  169. // stepper.clearLimitSwitchActive(); // clear the limit switch flag to allow movement in both directions again
  170. // }
  171. //
  172. // if (limitSwitchState == HIGH && stepper.getDistanceToTargetSigned() == 0) {
  173. // delay(100);
  174. // previousDirection *= -1;
  175. // long relativeTargetPosition = DISTANCE_TO_TRAVEL_IN_STEPS * previousDirection;
  176. // Serial.printf("Moving stepper by %ld steps\n", relativeTargetPosition);
  177. // stepper.setTargetPositionRelativeInSteps(relativeTargetPosition);
  178. // }
  179. }
  180. void testdrawchar(void) {
  181. Display.clearDisplay();
  182. Display.setTextSize(1); // Normal 1:1 pixel scale
  183. Display.setTextColor(SSD1306_WHITE); // Draw white text
  184. Display.setCursor(0, 0); // Start at top-left corner
  185. Display.cp437(true); // Use full 256 char 'Code Page 437' font
  186. // Not all the characters will fit on the Display. This is normal.
  187. // Library will draw what it can and the rest will be clipped.
  188. for (int16_t i = 0; i < 256; i++) {
  189. if (i == '\n')
  190. Display.write(' ');
  191. else
  192. Display.write(i);
  193. }
  194. Display.display();
  195. delay(2000);
  196. }
  197. void testdrawstyles(void) {
  198. Display.clearDisplay();
  199. // Display.setFont(&Rubik_Regular8pt7b);
  200. // Display.setTextSize(1); // Normal 1:1 pixel scale
  201. // Display.setTextColor(SSD1306_WHITE); // Draw white text
  202. // Display.setCursor(0, 8); // Start at top-left corner
  203. // Display.println(F("Hello, world!"));
  204. //
  205. // Display.setFont(&Rubik_Regular12pt7b);
  206. // Display.setTextSize(1); // Normal 1:1 pixel scale
  207. // Display.setTextColor(SSD1306_WHITE); // Draw white text
  208. // Display.setCursor(0, 24); // Start at top-left corner
  209. // Display.println(F("Hello, world!"));
  210. // Display.drawRect(0, 15, 128, 13, SSD1306_WHITE);
  211. // Display.setFont(&Rubik_Regular24pt7b);
  212. // Display.setTextSize(1); // Normal 1:1 pixel scale
  213. // Display.setTextColor(SSD1306_WHITE); // Draw white text
  214. // Display.setCursor(0, 55); // Start at top-left corner
  215. // Display.println(F("Hello, world!"));
  216. //
  217. // Display.display();
  218. // delay(2000);
  219. //
  220. // Display.clearDisplay();
  221. char decimals[3];
  222. uint16_t w = 0, h = 0;
  223. for (int i = 0; i <= 99; i++) {
  224. Display.clearDisplay();
  225. Display.setFont(&titillium_web_semibold30pt7b);
  226. Display.setTextSize(1); // Normal 1:1 pixel scale
  227. Display.setTextColor(SSD1306_WHITE); // Draw white text
  228. sprintf(decimals, "%02d", i);
  229. if (i < 33) {
  230. calculateWH("99.", w, h);
  231. Display.setCursor((SCREEN_WIDTH / 2 - 1) - w, SCREEN_HEIGHT / 2 + h / 2);
  232. Display.print("99.");
  233. } else if (i >= 33 && i < 66) {
  234. calculateWH("01.", w, h);
  235. Display.setCursor((SCREEN_WIDTH / 2 - 1) - w, SCREEN_HEIGHT / 2 + h / 2);
  236. Display.print("01.");
  237. } else {
  238. calculateWH("77.", w, h);
  239. Display.setCursor((SCREEN_WIDTH / 2 - 1) - w, SCREEN_HEIGHT / 2 + h / 2);
  240. Display.print("77.");
  241. }
  242. Display.println(decimals);
  243. Display.display();
  244. delay(100);
  245. }
  246. // Display.setFont(&Rubik_Regular30pt7b);
  247. // Display.setTextSize(1); // Normal 1:1 pixel scale
  248. // Display.setTextColor(SSD1306_WHITE); // Draw white text
  249. // Display.setCursor(0, 55); // Start at top-left corner
  250. // Display.println(F("99.99"));
  251. //
  252. // Display.display();
  253. // delay(1000);
  254. //
  255. // Display.clearDisplay();
  256. //
  257. // Display.setFont(&Rubik_Regular30pt7b);
  258. // Display.setTextSize(1); // Normal 1:1 pixel scale
  259. // Display.setTextColor(SSD1306_WHITE); // Draw white text
  260. // Display.setCursor(0, 55); // Start at top-left corner
  261. // Display.println(F("01.11"));
  262. // Display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  263. // Display.println("3.141592");
  264. //
  265. // Display.setTextSize(2); // Draw 2X-scale text
  266. // Display.setTextColor(SSD1306_WHITE);
  267. // Display.print(F("0x"));
  268. // Display.println(0xDEADBEEF, HEX);
  269. //
  270. Display.display();
  271. delay(2000);
  272. }
  273. void calculateWH(String units, uint16_t &w, uint16_t &h) {
  274. int x = 0;
  275. int y = 0;
  276. int16_t x1, y1;
  277. uint16_t w1, h1;
  278. Display.getTextBounds(units, x, y, &x1, &y1, &w1, &h1);
  279. w = w1;
  280. h = h1;
  281. }