Move stepper DAC to a separate file

This commit is contained in:
Scott Lahteine 2016-03-19 19:03:17 -07:00
parent 5d0570cea0
commit 59483221d5
4 changed files with 125 additions and 64 deletions

View File

@ -68,6 +68,10 @@
#include <SPI.h> #include <SPI.h>
#endif #endif
#if ENABLED(DAC_STEPPER_CURRENT)
#include "stepper_dac.h"
#endif
/** /**
* Look here for descriptions of G-codes: * Look here for descriptions of G-codes:
* - http://linuxcnc.org/handbook/gcode/g-code.html * - http://linuxcnc.org/handbook/gcode/g-code.html

View File

@ -30,7 +30,7 @@
#include "language.h" #include "language.h"
#include "cardreader.h" #include "cardreader.h"
#include "speed_lookuptable.h" #include "speed_lookuptable.h"
#include "dac_mcp4728.h"
#if HAS_DIGIPOTSS #if HAS_DIGIPOTSS
#include <SPI.h> #include <SPI.h>
#endif #endif
@ -1227,69 +1227,6 @@ void digipot_current(uint8_t driver, int current) {
#endif #endif
} }
#if ENABLED(DAC_STEPPER_CURRENT)
bool dac_present = false;
const uint8_t dac_order[NUM_AXIS] = DAC_STEPPER_ORDER;
int dac_init() {
mcp4728_init();
if (mcp4728_simpleCommand(RESET)) return -1;
dac_present = true;
mcp4728_setVref_all(DAC_STEPPER_VREF);
mcp4728_setGain_all(DAC_STEPPER_GAIN);
return 0;
}
void dac_current_percent(uint8_t channel, float val) {
if (!dac_present) return;
NOMORE(val, 100);
mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100);
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.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; }
static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); }
void dac_print_values() {
if (!dac_present) return;
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM("Stepper current values in % (Amps):");
SERIAL_ECHO_START;
SERIAL_ECHOPAIR(" X:", dac_perc(0));
SERIAL_ECHOPAIR(" (", dac_amps(0));
SERIAL_ECHOPAIR(") Y:", dac_perc(1));
SERIAL_ECHOPAIR(" (", dac_amps(1));
SERIAL_ECHOPAIR(") Z:", dac_perc(2));
SERIAL_ECHOPAIR(" (", dac_amps(2));
SERIAL_ECHOPAIR(") E:", dac_perc(3));
SERIAL_ECHOPAIR(" (", dac_amps(3));
SERIAL_ECHOLN(")");
}
void dac_commit_eeprom() {
if (!dac_present) return;
mcp4728_eepromWrite();
}
#endif // DAC_STEPPER_CURRENT
void microstep_init() { void microstep_init() {
#if HAS_MICROSTEPS_E1 #if HAS_MICROSTEPS_E1
pinMode(E1_MS1_PIN, OUTPUT); pinMode(E1_MS1_PIN, OUTPUT);

87
Marlin/stepper_dac.cpp Normal file
View File

@ -0,0 +1,87 @@
/*
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;
int dac_init() {
mcp4728_init();
if (mcp4728_simpleCommand(RESET)) return -1;
dac_present = true;
mcp4728_setVref_all(DAC_STEPPER_VREF);
mcp4728_setGain_all(DAC_STEPPER_GAIN);
return 0;
}
void dac_current_percent(uint8_t channel, float val) {
if (!dac_present) return;
NOMORE(val, 100);
mcp4728_analogWrite(dac_order[channel], val * DAC_STEPPER_MAX / 100);
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.0 * mcp4728_getValue(dac_order[n]) / DAC_STEPPER_MAX; }
static float dac_amps(int8_t n) { return ((2.048 * mcp4728_getValue(dac_order[n])) / 4096.0) / (8.0 * DAC_STEPPER_SENSE); }
void dac_print_values() {
if (!dac_present) return;
SERIAL_ECHO_START;
SERIAL_ECHOLNPGM("Stepper current values in % (Amps):");
SERIAL_ECHO_START;
SERIAL_ECHOPAIR(" X:", dac_perc(0));
SERIAL_ECHOPAIR(" (", dac_amps(0));
SERIAL_ECHOPAIR(") Y:", dac_perc(1));
SERIAL_ECHOPAIR(" (", dac_amps(1));
SERIAL_ECHOPAIR(") Z:", dac_perc(2));
SERIAL_ECHOPAIR(" (", dac_amps(2));
SERIAL_ECHOPAIR(") E:", dac_perc(3));
SERIAL_ECHOPAIR(" (", dac_amps(3));
SERIAL_ECHOLN(")");
}
void dac_commit_eeprom() {
if (!dac_present) return;
mcp4728_eepromWrite();
}
#endif // DAC_STEPPER_CURRENT

33
Marlin/stepper_dac.h Normal file
View File

@ -0,0 +1,33 @@
/*
stepper_dac.h - 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/>.
*/
#ifndef STEPPER_DAC_H
#define STEPPER_DAC_H
#include "dac_mcp4728.h"
int dac_init();
void dac_current_percent(uint8_t channel, float val);
void dac_current_raw(uint8_t channel, uint16_t val);
void dac_print_values();
void dac_commit_eeprom();
#endif // STEPPER_DAC_H