My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

stepper_dac.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <https://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(HAS_MOTOR_CURRENT_DAC)
  27. #include "stepper_dac.h"
  28. #include "../../MarlinCore.h" // for SP_X_LBL...
  29. bool dac_present = false;
  30. constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER;
  31. xyze_uint_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT;
  32. StepperDAC stepper_dac;
  33. int StepperDAC::init() {
  34. #if PIN_EXISTS(DAC_DISABLE)
  35. OUT_WRITE(DAC_DISABLE_PIN, LOW); // set pin low to enable DAC
  36. #endif
  37. mcp4728.init();
  38. if (mcp4728.simpleCommand(RESET)) return -1;
  39. dac_present = true;
  40. mcp4728.setVref_all(DAC_STEPPER_VREF);
  41. mcp4728.setGain_all(DAC_STEPPER_GAIN);
  42. if (mcp4728.getDrvPct(0) < 1 || mcp4728.getDrvPct(1) < 1 || mcp4728.getDrvPct(2) < 1 || mcp4728.getDrvPct(3) < 1) {
  43. mcp4728.setDrvPct(dac_channel_pct);
  44. mcp4728.eepromWrite();
  45. }
  46. return 0;
  47. }
  48. void StepperDAC::set_current_value(const uint8_t channel, uint16_t val) {
  49. if (!dac_present) return;
  50. NOMORE(val, uint16_t(DAC_STEPPER_MAX));
  51. mcp4728.analogWrite(dac_order[channel], val);
  52. mcp4728.simpleCommand(UPDATE);
  53. }
  54. void StepperDAC::set_current_percent(const uint8_t channel, float val) {
  55. set_current_value(channel, _MIN(val, 100.0f) * (DAC_STEPPER_MAX) / 100.0f);
  56. }
  57. static float dac_perc(int8_t n) { return mcp4728.getDrvPct(dac_order[n]); }
  58. static float dac_amps(int8_t n) { return mcp4728.getValue(dac_order[n]) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE * 1000); }
  59. uint8_t StepperDAC::get_current_percent(const AxisEnum axis) { return mcp4728.getDrvPct(dac_order[axis]); }
  60. void StepperDAC::set_current_percents(xyze_uint8_t &pct) {
  61. LOOP_LOGICAL_AXES(i) dac_channel_pct[i] = pct[dac_order[i]];
  62. mcp4728.setDrvPct(dac_channel_pct);
  63. }
  64. void StepperDAC::print_values() {
  65. if (!dac_present) return;
  66. SERIAL_ECHO_MSG("Stepper current values in % (Amps):");
  67. SERIAL_ECHO_START();
  68. SERIAL_ECHOPAIR_P(SP_X_LBL, dac_perc(X_AXIS), PSTR(" ("), dac_amps(X_AXIS), PSTR(")"));
  69. #if HAS_Y_AXIS
  70. SERIAL_ECHOPAIR_P(SP_Y_LBL, dac_perc(Y_AXIS), PSTR(" ("), dac_amps(Y_AXIS), PSTR(")"));
  71. #endif
  72. #if HAS_Z_AXIS
  73. SERIAL_ECHOPAIR_P(SP_Z_LBL, dac_perc(Z_AXIS), PSTR(" ("), dac_amps(Z_AXIS), PSTR(")"));
  74. #endif
  75. #if HAS_EXTRUDERS
  76. SERIAL_ECHOLNPAIR_P(SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")"));
  77. #endif
  78. }
  79. void StepperDAC::commit_eeprom() {
  80. if (!dac_present) return;
  81. mcp4728.eepromWrite();
  82. }
  83. #endif // HAS_MOTOR_CURRENT_DAC