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.1KB

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