No Description
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.

states.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include <Arduino.h>
  2. #include "config.h"
  3. #include "config_pins.h"
  4. #include "data.h"
  5. #include "common.h"
  6. #include "lcd.h"
  7. #include "steppers.h"
  8. #include "statemachine.h"
  9. #include "states.h"
  10. // --------------------------------------
  11. StateText sm_ask_homing = StateText();
  12. StateText sm_do_homing = StateText(&sm_ask_homing);
  13. StateMenu sm_menu = StateMenu(&sm_do_homing, false);
  14. StateMenu sm_auto = StateMenu(&sm_menu);
  15. StateDynamicMenu sm_presets = StateDynamicMenu(&sm_auto);
  16. // TODO dispense
  17. StateText sm_new_preset = StateText(&sm_auto);
  18. StateText sm_wiz_move = StateText(&sm_new_preset);
  19. StateValues<uint8_t, 2> sm_wiz_count = StateValues<uint8_t, 2>(&sm_wiz_move);
  20. StateValues<float, 2> sm_wiz_first_pos = StateValues<float, 2>(&sm_wiz_count);
  21. StateValues<float, 2> sm_wiz_last_pos = StateValues<float, 2>(&sm_wiz_first_pos);
  22. // TODO move z above container
  23. // TODO move z inside container
  24. // TODO move to extruder depth
  25. StateText sm_new_preset_done = StateText(&sm_wiz_last_pos);
  26. StateDynamicMenu sm_mod_preset = StateDynamicMenu(&sm_auto);
  27. // TODO modify
  28. StateDynamicMenu sm_del_preset = StateDynamicMenu(&sm_auto);
  29. // TODO delete
  30. float move_pos_x = 0.0, move_pos_y = 0.0, move_pos_z = 0.0, move_pos_e = 0.0;
  31. StateMenu sm_move = StateMenu(&sm_menu);
  32. StateValue<float> sm_move_x = StateValue<float>(&sm_move, move_pos_x, X_AXIS_MIN, X_AXIS_MAX);
  33. StateValue<float> sm_move_y = StateValue<float>(&sm_move, move_pos_y, Y_AXIS_MIN, Y_AXIS_MAX);
  34. StateValue<float> sm_move_z = StateValue<float>(&sm_move, move_pos_z, Z_AXIS_MIN, Z_AXIS_MAX);
  35. StateValue<float> sm_move_e = StateValue<float>(&sm_move, move_pos_e, E_AXIS_MIN, E_AXIS_MAX);
  36. StateMenu sm_config = StateMenu(&sm_menu);
  37. StateText sm_conf_load = StateText(&sm_config);
  38. StateText sm_conf_save = StateText(&sm_config);
  39. StateValue<float> sm_conf_speed_xy = StateValue<float>(&sm_config, data_options()->speed_x, 1.0, XY_MAX_SPEED);
  40. StateValue<float> sm_conf_speed_z = StateValue<float>(&sm_config, data_options()->speed_z, 1.0, Z_MAX_SPEED);
  41. StateValue<float> sm_conf_speed_e = StateValue<float>(&sm_config, data_options()->speed_e, 1.0, E_MAX_SPEED);
  42. StateValue<float> sm_conf_accel_xy = StateValue<float>(&sm_config, data_options()->accel_x, 1.0, XY_MAX_ACCEL);
  43. StateValue<float> sm_conf_accel_z = StateValue<float>(&sm_config, data_options()->accel_z, 1.0, Z_MAX_ACCEL);
  44. StateValue<float> sm_conf_accel_e = StateValue<float>(&sm_config, data_options()->accel_e, 1.0, E_MAX_ACCEL);
  45. // --------------------------------------
  46. State *current_state = NULL;
  47. String strbuf;
  48. struct data_config_preset wizard_preset = { .count_x = 1, .count_y = 1 };
  49. float wizard_first_x = 0.0, wizard_first_y = 0.0;
  50. float wizard_last_x = 0.0, wizard_last_y = 0.0;
  51. void states_init(void) {
  52. // ----------------------------------
  53. sm_ask_homing.setTitle("Ask for homing");
  54. sm_ask_homing.setHeading("Homing Required!");
  55. sm_ask_homing.setText("Click to home all four axes.");
  56. // ----------------------------------
  57. sm_do_homing.setTitle("Home all axes");
  58. sm_do_homing.setHeading("Homing...");
  59. sm_do_homing.onEnter([]() {
  60. //steppers_start_homing();
  61. });
  62. sm_do_homing.whenIn([](StateMachineInput smi) {
  63. //if (smi.motors_done) {
  64. // if (steppers_homed()) {
  65. async_beep(HOMING_BEEP_TIME, HOMING_BEEP_FREQ);
  66. states_go_to(&sm_menu);
  67. // }
  68. //}
  69. // TODO update text with current axis
  70. });
  71. // ----------------------------------
  72. sm_menu.setTitle("Main Menu");
  73. sm_menu.addChild(&sm_do_homing, 1);
  74. // ----------------------------------
  75. sm_move.setTitle("Move Axis");
  76. sm_move_x.setTitle("X Axis");
  77. sm_move_x.onEnter([]() {
  78. move_pos_x = steppers_pos_x();
  79. });
  80. sm_move_x.onLiveUpdate([](float v) {
  81. steppers_move_x(v);
  82. });
  83. sm_move_y.setTitle("Y Axis");
  84. sm_move_y.onEnter([]() {
  85. move_pos_y = steppers_pos_y();
  86. });
  87. sm_move_y.onLiveUpdate([](float v) {
  88. steppers_move_y(v);
  89. });
  90. sm_move_z.setTitle("Z Axis");
  91. sm_move_z.onEnter([]() {
  92. move_pos_z = steppers_pos_z();
  93. });
  94. sm_move_z.onLiveUpdate([](float v) {
  95. steppers_move_z(v);
  96. });
  97. sm_move_e.setTitle("E Axis");
  98. sm_move_e.onEnter([]() {
  99. move_pos_e = steppers_pos_e();
  100. });
  101. sm_move_e.onLiveUpdate([](float v) {
  102. steppers_move_e(v);
  103. });
  104. // ----------------------------------
  105. sm_auto.setTitle("Filling Menu");
  106. sm_presets.setTitle("Use Preset");
  107. sm_presets.setPrefix("Preset ");
  108. sm_presets.dataCount([]() {
  109. return (int)data_preset_count();
  110. });
  111. sm_new_preset.setTitle("Add new Preset");
  112. sm_new_preset.setHeading("Preset Wizard");
  113. sm_new_preset.setText("Moving axes into initial positions. Click to continue.");
  114. sm_wiz_move.setTitle("Initial Movement");
  115. sm_wiz_move.onEnter([]() {
  116. //steppers_start_homing();
  117. });
  118. sm_wiz_move.whenIn([](StateMachineInput smi) {
  119. //if (smi.motors_done) {
  120. // if (steppers_homed()) {
  121. states_go_to(&sm_wiz_count);
  122. // }
  123. //}
  124. // TODO update text with current axis
  125. });
  126. sm_wiz_count.setTitle("Container Count");
  127. sm_wiz_count.setData(0, "X: ", &wizard_preset.count_x, 1, 50);
  128. sm_wiz_count.setData(1, "Y: ", &wizard_preset.count_y, 1, 100);
  129. sm_wiz_first_pos.setTitle("First Container Position");
  130. sm_wiz_first_pos.setData(0, "X: ", &wizard_first_x, X_AXIS_MIN, X_AXIS_MAX);
  131. sm_wiz_first_pos.setData(1, "Y: ", &wizard_first_y, Y_AXIS_MIN, Y_AXIS_MAX);
  132. sm_wiz_first_pos.onLiveUpdate([](size_t i, float v) {
  133. if (i == 0) {
  134. steppers_move_x(v);
  135. } else {
  136. steppers_move_y(v);
  137. }
  138. });
  139. sm_wiz_first_pos.onUpdate([](size_t i, float v) {
  140. if (i == 0) {
  141. wizard_preset.offset_x = v;
  142. } else {
  143. wizard_preset.offset_y = v;
  144. }
  145. });
  146. sm_wiz_last_pos.setTitle("Last Container Position");
  147. sm_wiz_last_pos.setData(0, "X: ", &wizard_last_x, X_AXIS_MIN, X_AXIS_MAX);
  148. sm_wiz_last_pos.setData(1, "Y: ", &wizard_last_y, Y_AXIS_MIN, Y_AXIS_MAX);
  149. sm_wiz_last_pos.onLiveUpdate([](size_t i, float v) {
  150. if (i == 0) {
  151. steppers_move_x(v);
  152. } else {
  153. steppers_move_y(v);
  154. }
  155. });
  156. sm_wiz_last_pos.onUpdate([](size_t i, float v) {
  157. if (i == 0) {
  158. wizard_preset.distance_x = (v - wizard_preset.offset_x) / wizard_preset.count_x;
  159. } else {
  160. wizard_preset.distance_y = (v - wizard_preset.offset_y) / wizard_preset.count_y;
  161. }
  162. });
  163. sm_new_preset_done.setTitle("Preset Wizard Done");
  164. sm_new_preset_done.setHeading("Preset Wizard Done");
  165. sm_new_preset_done.onEnter([]() {
  166. if (!data_preset_add(wizard_preset)) {
  167. strbuf = String(data_eeprom_error()) + F(" Error adding preset!");
  168. sm_new_preset_done.setText(strbuf.c_str());
  169. } else {
  170. strbuf = F("Preset ");
  171. strbuf += String(data_preset_count());
  172. strbuf += F(" has been added! Don't forget to store config to EEPROM to keep it.");
  173. sm_new_preset_done.setText(strbuf.c_str());
  174. }
  175. });
  176. sm_new_preset_done.whenIn([](StateMachineInput smi) {
  177. if (smi.click) {
  178. states_go_to(&sm_auto);
  179. }
  180. });
  181. sm_mod_preset.setTitle("Modify Preset");
  182. sm_mod_preset.setPrefix("Preset ");
  183. sm_mod_preset.dataCount([]() {
  184. return (int)data_preset_count();
  185. });
  186. sm_del_preset.setTitle("Delete Preset");
  187. sm_del_preset.setPrefix("Preset ");
  188. sm_del_preset.dataCount([]() {
  189. return (int)data_preset_count();
  190. });
  191. // ----------------------------------
  192. sm_config.setTitle("Configuration");
  193. sm_conf_load.setTitle("Load from EEPROM");
  194. sm_conf_load.setHeading("Load from EEPROM");
  195. sm_conf_load.onEnter([]() {
  196. if (!data_eeprom_read()) {
  197. strbuf = String(data_eeprom_error()) + " Error reading configuration!";
  198. sm_conf_load.setText(strbuf.c_str());
  199. } else {
  200. sm_conf_load.setText("Configuration read successful!");
  201. steppers_set_speed_x(data_options()->speed_x);
  202. steppers_set_accel_x(data_options()->accel_x);
  203. steppers_set_speed_y(data_options()->speed_y);
  204. steppers_set_accel_y(data_options()->accel_y);
  205. steppers_set_speed_z(data_options()->speed_z);
  206. steppers_set_accel_z(data_options()->accel_z);
  207. steppers_set_speed_e(data_options()->speed_e);
  208. steppers_set_accel_e(data_options()->accel_e);
  209. }
  210. });
  211. sm_conf_save.setTitle("Store to EEPROM");
  212. sm_conf_save.setHeading("Store to EEPROM");
  213. sm_conf_save.setText("Configuration written to EEPROM!");
  214. sm_conf_save.onEnter([]() {
  215. data_eeprom_write();
  216. });
  217. sm_conf_speed_xy.setTitle("XY Speed");
  218. sm_conf_speed_xy.onUpdate([](float v) {
  219. steppers_set_speed_x(v);
  220. steppers_set_speed_y(v);
  221. });
  222. sm_conf_speed_z.setTitle("Z Speed");
  223. sm_conf_speed_z.onUpdate([](float v) {
  224. steppers_set_speed_z(v);
  225. });
  226. sm_conf_speed_e.setTitle("E Speed");
  227. sm_conf_speed_e.onUpdate([](float v) {
  228. steppers_set_speed_e(v);
  229. });
  230. sm_conf_accel_xy.setTitle("XY Acceleration");
  231. sm_conf_accel_xy.onUpdate([](float v) {
  232. steppers_set_accel_x(v);
  233. steppers_set_accel_y(v);
  234. });
  235. sm_conf_accel_z.setTitle("Z Acceleration");
  236. sm_conf_accel_z.onUpdate([](float v) {
  237. steppers_set_accel_z(v);
  238. });
  239. sm_conf_accel_e.setTitle("E Acceleration");
  240. sm_conf_accel_e.onUpdate([](float v) {
  241. steppers_set_accel_e(v);
  242. });
  243. // ----------------------------------
  244. states_go_to(&sm_ask_homing);
  245. }
  246. void states_run(StateMachineInput smi) {
  247. if (smi.click) {
  248. async_beep(ENCODER_CLICK_BEEP_TIME, ENCODER_CLICK_BEEP_FREQ);
  249. }
  250. if (smi.kill) {
  251. steppers_kill();
  252. Serial.println(F("Kill button pressed!"));
  253. blocking_beep(KILL_BEEP_TIME, KILL_BEEP_FREQ, KILL_BEEP_REPEAT - 1);
  254. states_go_to(&sm_ask_homing);
  255. return;
  256. }
  257. if (current_state != NULL) {
  258. current_state->inState(smi);
  259. }
  260. }
  261. State *states_get(void) {
  262. return current_state;
  263. }
  264. void states_go_to(State *state) {
  265. current_state = state;
  266. if (current_state != NULL) {
  267. current_state->enterState();
  268. }
  269. }