Frästisch mit elektronischer Höhenverstellung mittels Schrittmotoren.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RouterSetup.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * RouterSetup.cpp
  3. *
  4. * Created on: 25.01.2022
  5. * Author: FSmilari
  6. */
  7. #include "RouterSetup.h"
  8. #include <HardwareSerial.h>
  9. #include <WString.h>
  10. /******************
  11. ** Constructors
  12. *****************/
  13. RouterSetup::RouterSetup(Display &_display) : display(_display) {
  14. doInitialization = true;
  15. configStepIndex = 0;
  16. }
  17. /******************
  18. ** Public methods
  19. *****************/
  20. float RouterSetup::getSpeed() const {
  21. return speed;
  22. }
  23. void RouterSetup::setSpeed(float speed) {
  24. this->speed = speed;
  25. }
  26. float RouterSetup::getAcceleration() const {
  27. return accelleration;
  28. }
  29. void RouterSetup::setAcceleration(float accelleration) {
  30. this->accelleration = accelleration;
  31. }
  32. uint8_t RouterSetup::getEncoderSpeedFast() const {
  33. return encoderSpeedFast;
  34. }
  35. void RouterSetup::setEncoderSpeedFast(uint8_t encoderSpeedFast) {
  36. this->encoderSpeedFast = encoderSpeedFast;
  37. }
  38. uint8_t RouterSetup::getEncoderSpeedSlow() const {
  39. return encoderSpeedSlow;
  40. }
  41. void RouterSetup::setEncoderSpeedSlow(uint8_t encoderSpeedSlow) {
  42. this->encoderSpeedSlow = encoderSpeedSlow;
  43. }
  44. float RouterSetup::getLevelHeightDiving() const {
  45. return levelHeightDiving;
  46. }
  47. void RouterSetup::setLevelHeightDiving(float levelHeightDiving) {
  48. this->levelHeightDiving = levelHeightDiving;
  49. }
  50. float RouterSetup::getPitch() const {
  51. return pitch;
  52. }
  53. void RouterSetup::setPitch(float pitch) {
  54. this->pitch = pitch;
  55. }
  56. uint16_t RouterSetup::getStepsPerRev() const {
  57. return stepsPerRev;
  58. }
  59. void RouterSetup::setStepsPerRev(uint16_t stepsPerRev) {
  60. this->stepsPerRev = stepsPerRev;
  61. }
  62. float RouterSetup::getToolLenghtSensorHeight() const {
  63. return toolLenghtSensorHeight;
  64. }
  65. void RouterSetup::setToolLenghtSensorHeight(float toolLenghtSensorHeight) {
  66. this->toolLenghtSensorHeight = toolLenghtSensorHeight;
  67. }
  68. bool RouterSetup::isToolChangOnPowerOn() const {
  69. return toolChangeOnPowerOn;
  70. }
  71. void RouterSetup::setToolChangOnPowerOn(bool toolChangOnPowerOn) {
  72. this->toolChangeOnPowerOn = toolChangOnPowerOn;
  73. }
  74. void RouterSetup::clear() {
  75. speed = 0;
  76. accelleration = 0;
  77. stepsPerRev = 0;
  78. pitch = 0;
  79. toolLenghtSensorHeight = 0;
  80. encoderSpeedSlow = 0;
  81. encoderSpeedFast = 0;
  82. levelHeightDiving = 0;
  83. toolChangeOnPowerOn = true;
  84. }
  85. /**
  86. * Reads the RouterSetup from EEPROM
  87. */
  88. void RouterSetup::readFromEEPROM() {
  89. clear();
  90. eeprom.begin(ROUTER_NAMESPACE, false);
  91. setSpeed(eeprom.getFloat(SPEED, 1.0));
  92. setAcceleration(eeprom.getFloat(ACCELERATION, 1.0));
  93. setStepsPerRev(eeprom.getUInt(STEPSPERREV, 1600));
  94. setPitch(eeprom.getFloat(PITCH, 3.0));
  95. setToolLenghtSensorHeight(eeprom.getFloat(TOOLLENGHTSNHT, 10.0));
  96. setEncoderSpeedSlow(eeprom.getInt(ENCSPEEDSLOW, 1));
  97. setEncoderSpeedFast(eeprom.getInt(ENCSPEEDFAST, 20));
  98. setLevelHeightDiving(eeprom.getFloat(LVLHEIGHTDIVE, 3.0));
  99. setToolChangOnPowerOn(eeprom.getBool(TOOLCHGONPWRON, true));
  100. eeprom.end();
  101. }
  102. /**
  103. * Writes the RouterSetup to EEPROM
  104. */
  105. void RouterSetup::saveToEEPROM() {
  106. eeprom.begin(ROUTER_NAMESPACE, false);
  107. eeprom.putFloat(SPEED, getSpeed());
  108. eeprom.putFloat(ACCELERATION, getAcceleration());
  109. eeprom.putUInt(STEPSPERREV, getStepsPerRev());
  110. eeprom.putFloat(PITCH, getPitch());
  111. eeprom.putFloat(TOOLLENGHTSNHT, getToolLenghtSensorHeight());
  112. eeprom.putInt(ENCSPEEDSLOW, getEncoderSpeedSlow());
  113. eeprom.putInt(ENCSPEEDFAST, getEncoderSpeedFast());
  114. eeprom.putFloat(LVLHEIGHTDIVE, getLevelHeightDiving());
  115. eeprom.putBool(TOOLCHGONPWRON, isToolChangOnPowerOn());
  116. eeprom.end();
  117. }
  118. void RouterSetup::printValues() {
  119. Serial.print(String(SPEED) + ": " + String(getSpeed(), 1) + ", ");
  120. Serial.print(String(ACCELERATION) + ": " + String(getAcceleration(), 1) + ", ");
  121. Serial.print(String(STEPSPERREV) + ": " + String(getStepsPerRev()) + ", ");
  122. Serial.print(String(PITCH) + ": " + String(getPitch(), 1) + ", ");
  123. Serial.print(String(TOOLLENGHTSNHT) + ": " + String(getToolLenghtSensorHeight(), 2) + ", ");
  124. Serial.print(String(ENCSPEEDSLOW) + ": " + String(getEncoderSpeedSlow()) + ", ");
  125. Serial.print(String(ENCSPEEDFAST) + ": " + String(getEncoderSpeedFast()) + ", ");
  126. Serial.print(String(LVLHEIGHTDIVE) + ": " + String(getLevelHeightDiving(), 2) + ", ");
  127. Serial.println(String(TOOLCHGONPWRON) + ": " + String(isToolChangOnPowerOn()));
  128. }
  129. void RouterSetup::initialize() {
  130. if (doInitialization) {
  131. configStepIndex = 0;
  132. String stepTxt = ConfigStep[configStepIndex];
  133. String option = getCfgOptForStepIndex(configStepIndex);
  134. option.trim();
  135. display.setConfigText(stepTxt);
  136. display.setConfigOption(option);
  137. doInitialization = false;
  138. }
  139. }
  140. void RouterSetup::onRotaryControlerSwitch() {
  141. configStepIndex++;
  142. configStepIndex = configStepIndex % 9;
  143. String stepTxt = ConfigStep[configStepIndex];
  144. String option = getCfgOptForStepIndex(configStepIndex);
  145. option.trim();
  146. display.setConfigText(stepTxt);
  147. display.setConfigOption(option);
  148. }
  149. void RouterSetup::onRotaryControlerLongSwitch() {
  150. save();
  151. doInitialization = true;
  152. }
  153. void RouterSetup::save() {
  154. saveToEEPROM();
  155. }
  156. void RouterSetup::cancel() {
  157. doInitialization = true;
  158. }
  159. void RouterSetup::onRotaryControlerTurn(RotaryEncoder::Direction turn) {
  160. if (turn != RotaryEncoder::Direction::NOROTATION) {
  161. String value = "";
  162. int index = 0;
  163. int sign = (turn == RotaryEncoder::Direction::CLOCKWISE) ? 1 : -1;
  164. String unit = getCfgOptUnitForStepIndex(configStepIndex);
  165. switch (configStepIndex) {
  166. case 0:
  167. setSpeed(max(0.0, getSpeed() + 0.1 * sign));
  168. value = String(getSpeed(), 1);
  169. break;
  170. case 1:
  171. setAcceleration(max(0.0, getAcceleration() + 0.1 * sign));
  172. value = String(getAcceleration(), 1);
  173. break;
  174. case 2:
  175. index = getIndexOfStepsPerRevValue(getStepsPerRev());
  176. if (index > -1) {
  177. index = min(max((index + 1 * sign), 0), StepsPerRevOptsSize - 1) % 5;
  178. setStepsPerRev(StepsPerRevolutionOptions[index]);
  179. value = String(getStepsPerRev());
  180. }
  181. break;
  182. case 3:
  183. setPitch(max(0.0, getPitch() + 0.1 * sign));
  184. value = String(getPitch(), 1);
  185. break;
  186. case 4:
  187. setToolLenghtSensorHeight(max(0.0, getToolLenghtSensorHeight() + 0.01 * sign));
  188. value = String(getToolLenghtSensorHeight(), 2);
  189. break;
  190. case 5:
  191. setEncoderSpeedSlow(min(max(1, getEncoderSpeedSlow() + 1 * sign), 9));
  192. value = String(getEncoderSpeedSlow());
  193. break;
  194. case 6:
  195. setEncoderSpeedFast(min(max(10, getEncoderSpeedFast() + 1 * sign), 100));
  196. value = String(getEncoderSpeedFast());
  197. break;
  198. case 7:
  199. setLevelHeightDiving(min(max(0.1, getLevelHeightDiving() + 0.1 * sign), 10.0));
  200. value = String(getLevelHeightDiving(), 1);
  201. break;
  202. case 8:
  203. setToolChangOnPowerOn(sign > 0 ? true : false);
  204. value = isToolChangOnPowerOn() ? "JA" : "NEIN";
  205. break;
  206. default:
  207. break;
  208. }
  209. display.setConfigOption(value + " " + unit);
  210. }
  211. }
  212. //***************************************************************
  213. //*********** Private methods ***********************************
  214. //***************************************************************
  215. String RouterSetup::getCfgOptForStepIndex(byte configStepIndex) {
  216. String value = "";
  217. String unit = getCfgOptUnitForStepIndex(configStepIndex);
  218. switch (configStepIndex) {
  219. case 0:
  220. value = String(getSpeed(), 1);
  221. break;
  222. case 1:
  223. value = String(getAcceleration(), 1);
  224. break;
  225. case 2:
  226. value = String(getStepsPerRev());
  227. break;
  228. case 3:
  229. value = String(getPitch(), 1);
  230. break;
  231. case 4:
  232. value = String(getToolLenghtSensorHeight(), 2);
  233. break;
  234. case 5:
  235. value = String(getEncoderSpeedSlow());
  236. break;
  237. case 6:
  238. value = String(getEncoderSpeedFast());
  239. break;
  240. case 7:
  241. value = String(getLevelHeightDiving(), 1);
  242. break;
  243. case 8:
  244. value = isToolChangOnPowerOn() ? "JA" : "NEIN";
  245. break;
  246. default:
  247. break;
  248. }
  249. return value + " " + unit;
  250. }
  251. String RouterSetup::getCfgOptUnitForStepIndex(byte configStepIndex) {
  252. String value = "";
  253. switch (configStepIndex) {
  254. case 0:
  255. value = "U/sec";
  256. break;
  257. case 1:
  258. value = "U/sec2";
  259. break;
  260. case 3:
  261. value = "mm";
  262. break;
  263. case 4:
  264. value = "mm";
  265. break;
  266. case 5:
  267. value = "mm/100";
  268. break;
  269. case 6:
  270. value = "mm/100";
  271. break;
  272. case 7:
  273. value = "mm";
  274. break;
  275. default:
  276. break;
  277. }
  278. return value;
  279. }
  280. int RouterSetup::getIndexOfStepsPerRevValue(uint16_t value) {
  281. for (int i = 0; i < sizeof(StepsPerRevolutionOptions); i++) {
  282. if (StepsPerRevolutionOptions[i] == value) {
  283. return i;
  284. }
  285. }
  286. return -1;
  287. }