My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

stepper_dac.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * stepper_dac.cpp - To set stepper current via DAC
  24. */
  25. #include "../../inc/MarlinConfig.h"
  26. #if ENABLED(DAC_STEPPER_CURRENT)
  27. #include "stepper_dac.h"
  28. bool dac_present = false;
  29. constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER;
  30. xyze_uint8_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT;
  31. int dac_init() {
  32. #if PIN_EXISTS(DAC_DISABLE)
  33. OUT_WRITE(DAC_DISABLE_PIN, LOW); // set pin low to enable DAC
  34. #endif
  35. mcp4728_init();
  36. if (mcp4728_simpleCommand(RESET)) return -1;
  37. dac_present = true;
  38. mcp4728_setVref_all(DAC_STEPPER_VREF);
  39. mcp4728_setGain_all(DAC_STEPPER_GAIN);
  40. if (mcp4728_getDrvPct(0) < 1 || mcp4728_getDrvPct(1) < 1 || mcp4728_getDrvPct(2) < 1 || mcp4728_getDrvPct(3) < 1 ) {
  41. mcp4728_setDrvPct(dac_channel_pct);
  42. mcp4728_eepromWrite();
  43. }
  44. return 0;
  45. }
  46. void dac_current_percent(uint8_t channel, float val) {
  47. if (!dac_present) return;
  48. NOMORE(val, 100);
  49. mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX));
  50. mcp4728_simpleCommand(UPDATE);
  51. }
  52. void dac_current_raw(uint8_t channel, uint16_t val) {
  53. if (!dac_present) return;
  54. NOMORE(val, uint16_t(DAC_STEPPER_MAX));
  55. mcp4728_analogWrite(dac_order[channel], val);
  56. mcp4728_simpleCommand(UPDATE);
  57. }
  58. static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); }
  59. static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); }
  60. uint8_t dac_current_get_percent(const AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); }
  61. void dac_current_set_percents(xyze_uint8_t &pct) {
  62. LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
  63. mcp4728_setDrvPct(dac_channel_pct);
  64. }
  65. void dac_print_values() {
  66. if (!dac_present) return;
  67. SERIAL_ECHO_MSG("Stepper current values in % (Amps):");
  68. SERIAL_ECHO_START();
  69. SERIAL_ECHOLNPAIR_P(
  70. SP_X_LBL, dac_perc(X_AXIS), PSTR(" ("), dac_amps(X_AXIS), PSTR(")")
  71. SP_Y_LBL, dac_perc(Y_AXIS), PSTR(" ("), dac_amps(Y_AXIS), PSTR(")")
  72. SP_Z_LBL, dac_perc(Z_AXIS), PSTR(" ("), dac_amps(Z_AXIS), PSTR(")")
  73. SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")")
  74. );
  75. }
  76. void dac_commit_eeprom() {
  77. if (!dac_present) return;
  78. mcp4728_eepromWrite();
  79. }
  80. #endif // DAC_STEPPER_CURRENT