1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-29 23:07:42 +00:00
MarlinFirmware/Marlin/stepper_dac.cpp

126 lines
3.9 KiB
C++
Raw Normal View History

/**
2016-03-24 18:01:20 +00:00
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
2016-03-20 02:03:17 +00:00
stepper_dac.cpp - To set stepper current via DAC
Part of Marlin
Copyright (c) 2016 MarlinFirmware
Marlin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Marlin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Marlin. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Marlin.h"
#if ENABLED(DAC_STEPPER_CURRENT)
#include "stepper_dac.h"
bool dac_present = false;
const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER;
2017-06-08 23:23:07 +00:00
uint8_t dac_channel_pct[XYZE] = DAC_MOTOR_CURRENT_DEFAULT;
2016-03-20 02:03:17 +00:00
int dac_init() {
2016-06-27 19:38:00 +00:00
#if PIN_EXISTS(DAC_DISABLE)
2016-09-25 10:52:10 +00:00
OUT_WRITE(DAC_DISABLE_PIN, LOW); // set pin low to enable DAC
2016-06-27 19:38:00 +00:00
#endif
2016-03-20 02:03:17 +00:00
mcp4728_init();
if (mcp4728_simpleCommand(RESET)) return -1;
dac_present = true;
mcp4728_setVref_all(DAC_STEPPER_VREF);
mcp4728_setGain_all(DAC_STEPPER_GAIN);
2017-03-18 08:17:39 +00:00
2017-01-22 03:32:47 +00:00
if (mcp4728_getDrvPct(0) < 1 || mcp4728_getDrvPct(1) < 1 || mcp4728_getDrvPct(2) < 1 || mcp4728_getDrvPct(3) < 1 ) {
mcp4728_setDrvPct(dac_channel_pct);
mcp4728_eepromWrite();
}
2016-03-20 02:03:17 +00:00
return 0;
}
void dac_current_percent(uint8_t channel, float val) {
if (!dac_present) return;
NOMORE(val, 100);
2016-10-07 20:57:24 +00:00
mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX));
2016-03-20 02:03:17 +00:00
mcp4728_simpleCommand(UPDATE);
}
void dac_current_raw(uint8_t channel, uint16_t val) {
if (!dac_present) return;
NOMORE(val, DAC_STEPPER_MAX);
mcp4728_analogWrite(dac_order[channel], val);
mcp4728_simpleCommand(UPDATE);
}
static float dac_perc(int8_t n) { return 100.0f * mcp4728_getValue(dac_order[n]) * (1.0f / (DAC_STEPPER_MAX)); }
static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * (1.0f / (DAC_STEPPER_SENSE)); }
2016-12-15 15:21:32 +00:00
2017-12-09 12:43:38 +00:00
uint8_t dac_current_get_percent(const AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); }
void dac_current_set_percents(const uint8_t pct[XYZE]) {
2016-10-03 22:15:29 +00:00
LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
mcp4728_setDrvPct(dac_channel_pct);
}
2016-03-20 02:03:17 +00:00
void dac_print_values() {
if (!dac_present) return;
2017-06-09 15:51:23 +00:00
SERIAL_ECHO_START();
2016-03-20 02:03:17 +00:00
SERIAL_ECHOLNPGM("Stepper current values in % (Amps):");
2017-06-09 15:51:23 +00:00
SERIAL_ECHO_START();
2016-12-15 15:21:32 +00:00
SERIAL_ECHOPAIR(" X:", dac_perc(X_AXIS));
2016-10-03 22:15:29 +00:00
SERIAL_ECHOPAIR(" (", dac_amps(X_AXIS));
SERIAL_ECHOPAIR(") Y:", dac_perc(Y_AXIS));
SERIAL_ECHOPAIR(" (", dac_amps(Y_AXIS));
SERIAL_ECHOPAIR(") Z:", dac_perc(Z_AXIS));
SERIAL_ECHOPAIR(" (", dac_amps(Z_AXIS));
SERIAL_ECHOPAIR(") E:", dac_perc(E_AXIS));
SERIAL_ECHOPAIR(" (", dac_amps(E_AXIS));
2018-01-24 02:51:37 +00:00
SERIAL_ECHOLNPGM(")");
2016-03-20 02:03:17 +00:00
}
void dac_commit_eeprom() {
if (!dac_present) return;
mcp4728_eepromWrite();
}
#endif // DAC_STEPPER_CURRENT