DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
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.

Statemachine.cpp 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. #include "Plants.h"
  2. #include "DebugLog.h"
  3. #include "Statemachine.h"
  4. #include "config.h"
  5. Statemachine::DigitBuffer::DigitBuffer(int _size) {
  6. size = _size;
  7. pos = 0;
  8. digits = new int[size];
  9. }
  10. Statemachine::DigitBuffer::~DigitBuffer() {
  11. delete digits;
  12. }
  13. bool Statemachine::DigitBuffer::spaceLeft(void) {
  14. return (pos < size);
  15. }
  16. bool Statemachine::DigitBuffer::hasDigits(void) {
  17. return (pos > 0);
  18. }
  19. int Statemachine::DigitBuffer::countDigits(void) {
  20. return pos;
  21. }
  22. void Statemachine::DigitBuffer::addDigit(int d) {
  23. if (spaceLeft()) {
  24. digits[pos] = d;
  25. pos++;
  26. }
  27. }
  28. void Statemachine::DigitBuffer::removeDigit(void) {
  29. if (hasDigits()) {
  30. pos--;
  31. }
  32. }
  33. void Statemachine::DigitBuffer::clear(void) {
  34. pos = 0;
  35. }
  36. uint32_t Statemachine::DigitBuffer::getNumber(void) {
  37. uint32_t fact = 1;
  38. uint32_t sum = 0;
  39. for (int i = (pos - 1); i >= 0; i--) {
  40. sum += digits[i] * fact;
  41. fact *= 10;
  42. }
  43. return sum;
  44. }
  45. static const char *state_names[] = {
  46. stringify(init),
  47. stringify(menu),
  48. stringify(auto_mode),
  49. stringify(auto_fert),
  50. stringify(auto_fert_run),
  51. stringify(auto_tank_run),
  52. stringify(auto_plant),
  53. stringify(auto_plant_run),
  54. stringify(auto_done),
  55. stringify(menu_pumps),
  56. stringify(menu_pumps_time),
  57. stringify(menu_pumps_go),
  58. stringify(menu_pumps_run),
  59. stringify(menu_pumps_done),
  60. stringify(menu_valves),
  61. stringify(menu_valves_time),
  62. stringify(menu_valves_go),
  63. stringify(menu_valves_run),
  64. stringify(menu_valves_done),
  65. stringify(error)
  66. };
  67. const char *Statemachine::getStateName(void) {
  68. return state_names[state];
  69. }
  70. Statemachine::Statemachine(print_fn _print, backspace_fn _backspace)
  71. : db(7) {
  72. state = init;
  73. old_state = init;
  74. print = _print;
  75. backspace = _backspace;
  76. selected_id = 0;
  77. selected_time = 0;
  78. start_time = 0;
  79. stop_time = 0;
  80. last_animation_time = 0;
  81. error_condition = "";
  82. }
  83. void Statemachine::begin(void) {
  84. switch_to(init);
  85. }
  86. void Statemachine::input(int n) {
  87. if (state == init) {
  88. switch_to(menu);
  89. } else if (state == menu) {
  90. if (n == 1) {
  91. switch_to(auto_mode);
  92. } else if (n == 2) {
  93. switch_to(menu_pumps);
  94. } else if (n == 3) {
  95. switch_to(menu_valves);
  96. } else if ((n == -1) || (n == -2)) {
  97. switch_to(init);
  98. }
  99. } else if (state == auto_mode) {
  100. if (n == 1) {
  101. switch_to(auto_fert);
  102. } else if (n == 2) {
  103. auto wl = plants.getWaterlevel();
  104. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  105. plants.openWaterInlet();
  106. selected_id = plants.countPlants() + 1;
  107. selected_time = MAX_TANK_FILL_TIME;
  108. start_time = millis();
  109. switch_to(auto_tank_run);
  110. } else if (wl == Plants::full) {
  111. stop_time = millis();
  112. switch_to(auto_mode);
  113. } else if (wl == Plants::invalid) {
  114. error_condition = "Invalid sensor state";
  115. state = auto_mode;
  116. switch_to(error);
  117. }
  118. } else if (n == 3) {
  119. switch_to(auto_plant);
  120. } else if ((n == -1) || (n == -2)) {
  121. switch_to(menu);
  122. }
  123. } else if (state == auto_fert) {
  124. if ((n >= 1) && (n <= 3)) {
  125. auto wl = plants.getWaterlevel();
  126. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  127. plants.startFertilizer(n - 1);
  128. selected_id = n;
  129. selected_time = AUTO_PUMP_RUNTIME;
  130. start_time = millis();
  131. switch_to(auto_fert_run);
  132. } else if (wl == Plants::full) {
  133. stop_time = millis();
  134. switch_to(auto_mode);
  135. } else if (wl == Plants::invalid) {
  136. error_condition = "Invalid sensor state";
  137. state = auto_mode;
  138. switch_to(error);
  139. }
  140. } else if ((n == -1) || (n == -2)) {
  141. switch_to(auto_mode);
  142. }
  143. } else if (state == auto_fert_run) {
  144. plants.abort();
  145. stop_time = millis();
  146. switch_to(auto_done);
  147. } else if (state == auto_tank_run) {
  148. plants.abort();
  149. stop_time = millis();
  150. switch_to(auto_done);
  151. } else if (state == auto_plant) {
  152. if (n == -1) {
  153. if (db.hasDigits()) {
  154. backspace();
  155. db.removeDigit();
  156. } else {
  157. switch_to(auto_mode);
  158. }
  159. } else if (n == -2) {
  160. if (!db.hasDigits()) {
  161. return;
  162. }
  163. selected_id = number_input();
  164. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  165. error_condition = "Invalid plant ID!";
  166. switch_to(error);
  167. } else {
  168. auto wl = plants.getWaterlevel();
  169. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  170. plants.startPlant(selected_id - 1);
  171. selected_time = MAX_AUTO_PLANT_RUNTIME;
  172. start_time = millis();
  173. switch_to(auto_plant_run);
  174. } else if (wl == Plants::empty) {
  175. stop_time = millis();
  176. switch_to(auto_mode);
  177. } else if (wl == Plants::invalid) {
  178. error_condition = "Invalid sensor state";
  179. state = auto_mode;
  180. switch_to(error);
  181. }
  182. }
  183. } else {
  184. if (db.spaceLeft()) {
  185. db.addDigit(n);
  186. } else {
  187. backspace();
  188. }
  189. }
  190. } else if (state == auto_plant_run) {
  191. plants.abort();
  192. stop_time = millis();
  193. switch_to(auto_done);
  194. } else if (state == auto_done) {
  195. switch_to(auto_mode);
  196. } else if (state == menu_pumps) {
  197. if (n == -1) {
  198. if (db.hasDigits()) {
  199. backspace();
  200. db.removeDigit();
  201. } else {
  202. switch_to(menu);
  203. }
  204. } else if (n == -2) {
  205. if (!db.hasDigits()) {
  206. return;
  207. }
  208. selected_id = number_input();
  209. if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
  210. error_condition = "Invalid pump ID!";
  211. switch_to(error);
  212. } else {
  213. switch_to(menu_pumps_time);
  214. }
  215. } else {
  216. if (db.spaceLeft()) {
  217. db.addDigit(n);
  218. } else {
  219. backspace();
  220. }
  221. }
  222. } else if (state == menu_pumps_time) {
  223. if (n == -1) {
  224. if (db.hasDigits()) {
  225. backspace();
  226. db.removeDigit();
  227. } else {
  228. switch_to(menu_pumps);
  229. }
  230. } else if (n == -2) {
  231. if (!db.hasDigits()) {
  232. return;
  233. }
  234. selected_time = number_input();
  235. if ((selected_time <= 0) || (selected_time > 120)) {
  236. error_condition = "Invalid time range!";
  237. switch_to(error);
  238. } else {
  239. switch_to(menu_pumps_go);
  240. }
  241. } else {
  242. if (db.spaceLeft()) {
  243. db.addDigit(n);
  244. } else {
  245. backspace();
  246. }
  247. }
  248. } else if (state == menu_pumps_go) {
  249. if (n == -2) {
  250. start_time = millis();
  251. last_animation_time = start_time;
  252. auto wl = plants.getWaterlevel();
  253. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  254. plants.startFertilizer(selected_id - 1);
  255. switch_to(menu_pumps_run);
  256. } else if (wl == Plants::full) {
  257. stop_time = millis();
  258. switch_to(menu_pumps_done);
  259. } else if (wl == Plants::invalid) {
  260. error_condition = "Invalid sensor state";
  261. state = menu_pumps;
  262. switch_to(error);
  263. }
  264. } else {
  265. switch_to(menu_pumps_time);
  266. }
  267. } else if (state == menu_pumps_run) {
  268. plants.abort();
  269. stop_time = millis();
  270. switch_to(menu_pumps_done);
  271. } else if (state == menu_pumps_done) {
  272. switch_to(menu);
  273. } else if (state == menu_valves) {
  274. if (n == -1) {
  275. if (db.hasDigits()) {
  276. backspace();
  277. db.removeDigit();
  278. } else {
  279. switch_to(menu);
  280. }
  281. } else if (n == -2) {
  282. if (!db.hasDigits()) {
  283. return;
  284. }
  285. selected_id = number_input();
  286. if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
  287. error_condition = "Invalid valve ID!";
  288. switch_to(error);
  289. } else {
  290. switch_to(menu_valves_time);
  291. }
  292. } else {
  293. if (db.spaceLeft()) {
  294. db.addDigit(n);
  295. } else {
  296. backspace();
  297. }
  298. }
  299. } else if (state == menu_valves_time) {
  300. if (n == -1) {
  301. if (db.hasDigits()) {
  302. backspace();
  303. db.removeDigit();
  304. } else {
  305. switch_to(menu_valves);
  306. }
  307. } else if (n == -2) {
  308. if (!db.hasDigits()) {
  309. return;
  310. }
  311. selected_time = number_input();
  312. if ((selected_time <= 0) || (selected_time > 120)) {
  313. error_condition = "Invalid time range!";
  314. switch_to(error);
  315. } else {
  316. switch_to(menu_valves_go);
  317. }
  318. } else {
  319. if (db.spaceLeft()) {
  320. db.addDigit(n);
  321. } else {
  322. backspace();
  323. }
  324. }
  325. } else if (state == menu_valves_go) {
  326. if (n == -2) {
  327. start_time = millis();
  328. last_animation_time = start_time;
  329. auto wl = plants.getWaterlevel();
  330. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  331. if (selected_id >= (plants.countPlants() + 1)) {
  332. plants.openWaterInlet();
  333. } else {
  334. plants.startPlant(selected_id - 1);
  335. }
  336. switch_to(menu_valves_run);
  337. } else if (wl == Plants::full) {
  338. stop_time = millis();
  339. switch_to(menu_valves_done);
  340. } else if (wl == Plants::invalid) {
  341. error_condition = "Invalid sensor state";
  342. state = menu_valves;
  343. switch_to(error);
  344. }
  345. } else {
  346. switch_to(menu_valves_time);
  347. }
  348. } else if (state == menu_valves_run) {
  349. plants.abort();
  350. stop_time = millis();
  351. switch_to(menu_valves_done);
  352. } else if (state == menu_valves_done) {
  353. switch_to(menu);
  354. } else if (state == error) {
  355. if (old_state != error) {
  356. switch_to(old_state);
  357. } else {
  358. switch_to(menu);
  359. }
  360. }
  361. }
  362. uint32_t Statemachine::number_input(void) {
  363. for (int i = 0; i < db.countDigits(); i++) {
  364. backspace();
  365. }
  366. uint32_t n = db.getNumber();
  367. db.clear();
  368. debug.print("Whole number input: ");
  369. debug.println(n);
  370. return n;
  371. }
  372. void Statemachine::act(void) {
  373. if ((state == menu_pumps_run) || (state == menu_valves_run)) {
  374. unsigned long runtime = millis() - start_time;
  375. if ((runtime / 1000UL) >= selected_time) {
  376. // stop if timeout has been reached
  377. plants.abort();
  378. stop_time = millis();
  379. switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
  380. } else if ((millis() - last_animation_time) >= 500) {
  381. // update animation if needed
  382. last_animation_time = millis();
  383. switch_to(state);
  384. }
  385. }
  386. if ((state == menu_pumps_run) || ((state == menu_valves_run) && (selected_id == (plants.countPlants() + 1)))) {
  387. // check water level state
  388. auto wl = plants.getWaterlevel();
  389. if (wl == Plants::full) {
  390. plants.abort();
  391. stop_time = millis();
  392. switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
  393. } else if (wl == Plants::invalid) {
  394. plants.abort();
  395. error_condition = "Invalid sensor state";
  396. state = (state == menu_pumps_run) ? menu_pumps : menu_valves;
  397. switch_to(error);
  398. }
  399. }
  400. if ((state == menu_valves_run) && (selected_id <= plants.countPlants())) {
  401. // check water level state
  402. auto wl = plants.getWaterlevel();
  403. if (wl == Plants::empty) {
  404. plants.abort();
  405. stop_time = millis();
  406. switch_to(menu_valves_done);
  407. } else if (wl == Plants::invalid) {
  408. plants.abort();
  409. error_condition = "Invalid sensor state";
  410. state = menu_valves;
  411. switch_to(error);
  412. }
  413. }
  414. if ((state == auto_fert_run) || (state == auto_tank_run)) {
  415. unsigned long runtime = millis() - start_time;
  416. if ((runtime / 1000UL) >= selected_time) {
  417. // stop if timeout has been reached
  418. plants.abort();
  419. stop_time = millis();
  420. switch_to(auto_done);
  421. } else if ((millis() - last_animation_time) >= 500) {
  422. // update animation if needed
  423. last_animation_time = millis();
  424. switch_to(state);
  425. }
  426. // check water level state
  427. auto wl = plants.getWaterlevel();
  428. if (wl == Plants::full) {
  429. plants.abort();
  430. stop_time = millis();
  431. switch_to(auto_done);
  432. } else if (wl == Plants::invalid) {
  433. plants.abort();
  434. error_condition = "Invalid sensor state";
  435. state = auto_mode;
  436. switch_to(error);
  437. }
  438. }
  439. if (state == auto_plant_run) {
  440. unsigned long runtime = millis() - start_time;
  441. if ((runtime / 1000UL) >= selected_time) {
  442. // stop if timeout has been reached
  443. plants.abort();
  444. stop_time = millis();
  445. switch_to(auto_done);
  446. } else if ((millis() - last_animation_time) >= 500) {
  447. // update animation if needed
  448. last_animation_time = millis();
  449. switch_to(state);
  450. }
  451. // check water level state
  452. auto wl = plants.getWaterlevel();
  453. if (wl == Plants::empty) {
  454. plants.abort();
  455. stop_time = millis();
  456. switch_to(auto_done);
  457. } else if (wl == Plants::invalid) {
  458. plants.abort();
  459. error_condition = "Invalid sensor state";
  460. state = auto_mode;
  461. switch_to(error);
  462. }
  463. }
  464. }
  465. void Statemachine::switch_to(States s) {
  466. old_state = state;
  467. state = s;
  468. if (s == init) {
  469. String a = String("- Giess-o-mat V") + FIRMWARE_VERSION + String(" -");
  470. print(a.c_str(),
  471. "Usage: Enter number",
  472. "* Delete prev. digit",
  473. "# Execute input num.",
  474. -1);
  475. } else if (s == menu) {
  476. print("------- Menu -------",
  477. "1: Automatic program",
  478. "2: Fertilizer pumps",
  479. "3: Outlet valves",
  480. -1);
  481. } else if (s == auto_mode) {
  482. print("------- Auto -------",
  483. "1: Add Fertilizer",
  484. "2: Fill Reservoir",
  485. "3: Water a plant",
  486. -1);
  487. } else if (s == auto_fert) {
  488. print("---- Fertilizer ----",
  489. "1: Vegetation Phase",
  490. "2: Bloom Phase",
  491. "3: Special",
  492. -1);
  493. } else if (s == auto_fert_run) {
  494. unsigned long runtime = millis() - start_time;
  495. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  496. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  497. String b;
  498. for (unsigned long i = 0; i < anim; i++) {
  499. b += '#';
  500. }
  501. print("---- Dispensing ----",
  502. a.c_str(),
  503. b.c_str(),
  504. "Hit any key to stop!",
  505. -1);
  506. } else if (s == auto_tank_run) {
  507. unsigned long runtime = millis() - start_time;
  508. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  509. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  510. String b;
  511. for (unsigned long i = 0; i < anim; i++) {
  512. b += '#';
  513. }
  514. print("--- Filling Tank ---",
  515. a.c_str(),
  516. b.c_str(),
  517. "Hit any key to stop!",
  518. -1);
  519. } else if (s == auto_plant) {
  520. String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
  521. print("--- Select Plant ---",
  522. "Which plant to water",
  523. a.c_str(),
  524. "Plant: ",
  525. 3);
  526. } else if (s == auto_plant_run) {
  527. unsigned long runtime = millis() - start_time;
  528. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  529. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  530. String b;
  531. for (unsigned long i = 0; i < anim; i++) {
  532. b += '#';
  533. }
  534. print("----- Watering -----",
  535. a.c_str(),
  536. b.c_str(),
  537. "Hit any key to stop!",
  538. -1);
  539. } else if (s == auto_done) {
  540. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  541. print("------- Done -------",
  542. "Dispensing finished",
  543. a.c_str(),
  544. "Hit any key for menu",
  545. -1);
  546. } else if (s == menu_pumps) {
  547. String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
  548. print("------- Pump -------",
  549. "Please select pump",
  550. a.c_str(),
  551. "Pump: ",
  552. 3);
  553. } else if (s == menu_pumps_time) {
  554. String header = String("------ Pump ") + String(selected_id) + String(" ------");
  555. print(header.c_str(),
  556. "Please set runtime",
  557. "(Input in seconds)",
  558. "Runtime: ",
  559. 3);
  560. } else if (s == menu_pumps_go) {
  561. String a = String("Pump No. ") + String(selected_id);
  562. String b = String("Runtime ") + String(selected_time) + String('s');
  563. print("----- Confirm? -----",
  564. a.c_str(),
  565. b.c_str(),
  566. " # Confirm",
  567. -1);
  568. } else if (s == menu_pumps_run) {
  569. unsigned long runtime = millis() - start_time;
  570. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  571. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  572. String b;
  573. for (unsigned long i = 0; i < anim; i++) {
  574. b += '#';
  575. }
  576. print("---- Dispensing ----",
  577. a.c_str(),
  578. b.c_str(),
  579. "Hit any key to stop!",
  580. -1);
  581. } else if (s == menu_pumps_done) {
  582. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  583. print("------- Done -------",
  584. "Dispensing finished",
  585. a.c_str(),
  586. "Hit any key for menu",
  587. -1);
  588. } else if (s == menu_valves) {
  589. String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
  590. print("------ Valves ------",
  591. "Please select valve",
  592. a.c_str(),
  593. "Valve: ",
  594. 3);
  595. } else if (s == menu_valves_time) {
  596. String header = String("----- Valve ") + String(selected_id) + String(" -----");
  597. print(header.c_str(),
  598. "Please set runtime",
  599. "(Input in seconds)",
  600. "Runtime: ",
  601. 3);
  602. } else if (s == menu_valves_go) {
  603. String a = String("Valve No. ") + String(selected_id);
  604. String b = String("Runtime ") + String(selected_time) + String('s');
  605. print("----- Confirm? -----",
  606. a.c_str(),
  607. b.c_str(),
  608. " # Confirm",
  609. -1);
  610. } else if (s == menu_valves_run) {
  611. unsigned long runtime = millis() - start_time;
  612. String a = String("Runtime: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  613. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  614. String b;
  615. for (unsigned long i = 0; i <= anim; i++) {
  616. b += '#';
  617. }
  618. print("---- Dispensing ----",
  619. a.c_str(),
  620. b.c_str(),
  621. "Hit any key to stop!",
  622. -1);
  623. } else if (s == menu_valves_done) {
  624. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  625. print("------- Done -------",
  626. "Dispensing finished",
  627. a.c_str(),
  628. "Hit any key for menu",
  629. -1);
  630. } else if (s == error) {
  631. print("------ Error! ------",
  632. "There is a problem:",
  633. error_condition.c_str(),
  634. " Press any key...",
  635. -1);
  636. }
  637. }