Skip to content

Commit

Permalink
Merge pull request #47 from bdring/Devt
Browse files Browse the repository at this point in the history
Devt
  • Loading branch information
bdring authored Oct 8, 2021
2 parents 936305d + a39e92c commit e15a34e
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 193 deletions.
4 changes: 2 additions & 2 deletions FluidNC/src/Machine/Axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace Machine {
Motor* _motors[MAX_MOTORS_PER_AXIS];
Homing* _homing = nullptr;

float _stepsPerMm = 320.0f;
float _stepsPerMm = 80.0f;
float _maxRate = 1000.0f;
float _acceleration = 25.0f;
float _maxTravel = 200.0f;
float _maxTravel = 1000.0f;
bool _softLimits = false;

// Configuration system helpers:
Expand Down
14 changes: 7 additions & 7 deletions FluidNC/src/Machine/Homing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Machine {
// Input: seek - the phase - true for the initial high-speed seek, false for the slow second phase
// Output: axislock - the axes that actually participate, accounting
// Output: target - the endpoint vector of the motion
// Output: rate - the feed rate
// Return: debounce - the maximum delay time of all the axes
// Output: rate - the feed rate
// Return: settle - the maximum delay time of all the axes

// For multi-axis homing, we use the per-axis rates and travel limits to compute
// a target vector and a feedrate as follows:
Expand All @@ -39,7 +39,7 @@ namespace Machine {
uint32_t Homing::plan_move(MotorMask motors, bool approach, bool seek, float customPulloff) {
float maxSeekTime = 0.0;
float limitingRate = 0.0;
uint32_t debounce = 0;
uint32_t settle = 0;
float rate = 0.0;

auto axes = config->_axes;
Expand All @@ -62,7 +62,7 @@ namespace Machine {
auto axisConfig = axes->_axis[axis];
auto homing = axisConfig->_homing;

debounce = std::max(debounce, homing->_debounce_ms);
settle = std::max(settle, homing->_settle_ms);

float axis_rate = seek ? homing->_seekRate : homing->_feedRate;

Expand Down Expand Up @@ -127,7 +127,7 @@ namespace Machine {
Stepper::prep_buffer(); // Prep and fill segment buffer from newly planned block.
Stepper::wake_up(); // Initiate motion

return debounce;
return settle;
}

void Homing::run(MotorMask remainingMotors, bool approach, bool seek, float customPulloff = 0) {
Expand Down Expand Up @@ -300,8 +300,8 @@ namespace Machine {
for (int axis = 0; axis < n_axis; axis++) {
if (bitnum_is_true(motors, axis)) {
auto axisConfig = config->_axes->_axis[axis];
if(axisConfig->hasDualMotor()){
pulloffOffset = axisConfig->pulloffOffset();
if (axisConfig->hasDualMotor()) {
pulloffOffset = axisConfig->pulloffOffset();
if (pulloffOffset != 0) {
//log_info("Pulloff offset needed on axis " << axis << " of " << pulloffOffset);
// TODO Do it
Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Machine/Homing.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Machine {
float _mpos = 0.0f; // After homing this will be the mpos of the switch location
float _feedRate = 50.0f; // pulloff and second touch speed
float _seekRate = 200.0f; // this first approach speed
uint32_t _debounce_ms = 250; // ms settling time for homing switches after motion
uint32_t _settle_ms = 250; // ms settling time for homing switches after motion
float _seek_scaler = 1.1f; // multiplied by max travel for max homing distance on first touch
float _feed_scaler = 1.1f; // multiplier to pulloff for moving to switch after pulloff

Expand All @@ -45,7 +45,7 @@ namespace Machine {
handler.item("mpos_mm", _mpos);
handler.item("feed_mm_per_min", _feedRate);
handler.item("seek_mm_per_min", _seekRate);
handler.item("debounce_ms", _debounce_ms);
handler.item("settle_ms", _settle_ms);
handler.item("seek_scaler", _seek_scaler);
handler.item("feed_scaler", _feed_scaler);
}
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Machine/Motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Machine {
Pin _negPin;
Pin _posPin;
Pin _allPin;
bool _hardLimits = true;
bool _hardLimits = false;

// Configuration system helpers:
void group(Configuration::HandlerBase& handler) override;
Expand Down
3 changes: 1 addition & 2 deletions FluidNC/src/Motors/TrinamicBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <atomic>

namespace MotorDrivers {
EnumItem trinamicModes[] = { { TrinamicMode::Unknown, "Unknown" },
{ TrinamicMode::StealthChop, "StealthChop" },
EnumItem trinamicModes[] = { { TrinamicMode::StealthChop, "StealthChop" },
{ TrinamicMode::CoolStep, "CoolStep" },
{ TrinamicMode::StallGuard, "StallGuard" },
EnumItem(TrinamicMode::StealthChop) };
Expand Down
3 changes: 1 addition & 2 deletions FluidNC/src/Motors/TrinamicBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace MotorDrivers {

enum TrinamicMode {
Unknown = 0,
StealthChop = 1, // very quiet
CoolStep = 2, // cooler so higher current possible
StallGuard = 3, // coolstep plus stall indication
Expand All @@ -26,7 +25,7 @@ namespace MotorDrivers {
bool _has_errors;
uint16_t _driver_part_number; // example: use 2130 for TMC2130
bool _disabled = false;
TrinamicMode _mode = TrinamicMode::Unknown; // no mode set yet
TrinamicMode _mode = TrinamicMode::StealthChop;

// Configurable
int _homing_mode = StealthChop;
Expand Down
170 changes: 101 additions & 69 deletions FluidNC/src/Motors/TrinamicSpiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ namespace MotorDrivers {

if (_driver_part_number == 2130) {
//log_info("ID: " << cs_id << " index:" << _spi_index);
tmcstepper = new TMC2130Stepper(cs_id, _r_sense, _spi_index); // TODO hardwired to non daisy chain index
tmc2130 = new TMC2130Stepper(cs_id, _r_sense, _spi_index); // TODO hardwired to non daisy chain index
} else if (_driver_part_number == 5160) {
tmcstepper = new TMC5160Stepper(cs_id, _r_sense, _spi_index);
tmc5160 = new TMC5160Stepper(cs_id, _r_sense, _spi_index);
} else {
log_info(" Unsupported Trinamic part number TMC" << _driver_part_number);
_has_errors = true; // This motor cannot be used
Expand All @@ -48,7 +48,11 @@ namespace MotorDrivers {

// use slower speed if I2S
if (_cs_pin.capabilities().has(Pin::Capabilities::I2S)) {
tmcstepper->setSPISpeed(_spi_freq);
if (tmc2130) {
tmc2130->setSPISpeed(_spi_freq);
} else {
tmc5160->setSPISpeed(_spi_freq);
}
}

link = List;
Expand Down Expand Up @@ -77,8 +81,11 @@ namespace MotorDrivers {
}

void TrinamicSpiDriver::config_motor() {
tmcstepper->begin();

if (tmc2130) {
tmc2130->begin();
} else {
tmc5160->begin();
}
_has_errors = !test(); // Try communicating with motor. Prints an error if there is a problem.

init_step_dir_pins();
Expand All @@ -99,7 +106,8 @@ namespace MotorDrivers {
return false;
}

switch (tmcstepper->test_connection()) {
uint8_t result = tmc2130 ? tmc2130->test_connection() : tmc5160->test_connection();
switch (result) {
case 1:
log_info(axisName() << " driver test failed. Check connection");
return false;
Expand All @@ -110,7 +118,7 @@ namespace MotorDrivers {
// driver responded, so check for other errors from the DRV_STATUS register

// TMC2130_n ::DRV_STATUS_t status { 0 }; // a useful struct to access the bits.
// status.sr = tmcstepper->DRV_STATUS();
// status.sr = tmc2130 ? tmc2130stepper->DRV_STATUS() : tmc5160stepper->DRV_STATUS();;

// bool err = false;

Expand Down Expand Up @@ -139,10 +147,9 @@ namespace MotorDrivers {
}

/*
Read setting and send them to the driver. Called at init() and whenever related settings change
both are stored as float Amps, but TMCStepper library expects...
uint16_t run (mA)
float hold (as a percentage of run)
Run and hold current configuration items are in (float) Amps,
but the TMCStepper library expresses run current as (uint16_t) mA
and hold current as (float) fraction of run current.
*/
void TrinamicSpiDriver::read_settings() {
if (_has_errors) {
Expand All @@ -161,100 +168,121 @@ namespace MotorDrivers {
}
}

tmcstepper->microsteps(_microsteps);
tmcstepper->rms_current(run_i_ma, hold_i_percent);
if (tmc2130) {
tmc2130->microsteps(_microsteps);
tmc2130->rms_current(run_i_ma, hold_i_percent);
} else {
tmc5160->microsteps(_microsteps);
tmc5160->rms_current(run_i_ma, hold_i_percent);
}
}

bool TrinamicSpiDriver::set_homing_mode(bool isHoming) {
set_mode(isHoming);
return true;
}

/*
There are ton of settings. I'll start by grouping then into modes for now.
Many people will want quiet and stallguard homing. Stallguard only run in
Coolstep mode, so it will need to switch to Coolstep when homing
*/
void TrinamicSpiDriver::set_mode(bool isHoming) {
if (_has_errors) {
return;
}

TrinamicMode newMode = static_cast<TrinamicMode>(trinamicModes[isHoming ? _homing_mode : _run_mode].value);
_mode = static_cast<TrinamicMode>(trinamicModes[isHoming ? _homing_mode : _run_mode].value);

if (newMode == _mode) {
return;
}
_mode = newMode;

switch (_mode) {
case TrinamicMode ::StealthChop:
//log_info("StealthChop");
tmcstepper->en_pwm_mode(true);
tmcstepper->pwm_autoscale(true);
tmcstepper->diag1_stall(false);
break;
case TrinamicMode ::CoolStep:
//log_info("Coolstep");
tmcstepper->en_pwm_mode(false);
tmcstepper->pwm_autoscale(false);
tmcstepper->TCOOLTHRS(NORMAL_TCOOLTHRS); // when to turn on coolstep
tmcstepper->THIGH(NORMAL_THIGH);
break;
case TrinamicMode ::StallGuard:
//log_info("Stallguard");
{
auto feedrate = config->_axes->_axis[axis_index()]->_homing->_feedRate;

tmcstepper->en_pwm_mode(false);
tmcstepper->pwm_autoscale(false);
tmcstepper->TCOOLTHRS(calc_tstep(feedrate, 150.0));
tmcstepper->THIGH(calc_tstep(feedrate, 60.0));
tmcstepper->sfilt(1);
tmcstepper->diag1_stall(true); // stallguard i/o is on diag1
tmcstepper->sgt(constrain(_stallguard, -64, 63));
if (tmc2130) {
switch (_mode) {
case TrinamicMode ::StealthChop:
//log_info("StealthChop");
tmc2130->en_pwm_mode(true);
tmc2130->pwm_autoscale(true);
tmc2130->diag1_stall(false);
break;
}
case TrinamicMode ::Unknown:
log_info("TrinamicMode ::Unknown");
break;
case TrinamicMode ::CoolStep:
//log_info("Coolstep");
tmc2130->en_pwm_mode(false);
tmc2130->pwm_autoscale(false);
tmc2130->TCOOLTHRS(NORMAL_TCOOLTHRS); // when to turn on coolstep
tmc2130->THIGH(NORMAL_THIGH);
break;
case TrinamicMode ::StallGuard:
//log_info("Stallguard");
{
auto feedrate = config->_axes->_axis[axis_index()]->_homing->_feedRate;

tmc2130->en_pwm_mode(false);
tmc2130->pwm_autoscale(false);
tmc2130->TCOOLTHRS(calc_tstep(feedrate, 150.0));
tmc2130->THIGH(calc_tstep(feedrate, 60.0));
tmc2130->sfilt(1);
tmc2130->diag1_stall(true); // stallguard i/o is on diag1
tmc2130->sgt(constrain(_stallguard, -64, 63));
break;
}
}
} else {
switch (_mode) {
case TrinamicMode ::StealthChop:
//log_info("StealthChop");
tmc5160->en_pwm_mode(true);
tmc5160->pwm_autoscale(true);
tmc5160->diag1_stall(false);
break;
case TrinamicMode ::CoolStep:
//log_info("Coolstep");
tmc5160->en_pwm_mode(false);
tmc5160->pwm_autoscale(false);
tmc5160->TCOOLTHRS(NORMAL_TCOOLTHRS); // when to turn on coolstep
tmc5160->THIGH(NORMAL_THIGH);
break;
case TrinamicMode ::StallGuard:
//log_info("Stallguard");
{
auto feedrate = config->_axes->_axis[axis_index()]->_homing->_feedRate;

tmc5160->en_pwm_mode(false);
tmc5160->pwm_autoscale(false);
tmc5160->TCOOLTHRS(calc_tstep(feedrate, 150.0));
tmc5160->THIGH(calc_tstep(feedrate, 60.0));
tmc5160->sfilt(1);
tmc5160->diag1_stall(true); // stallguard i/o is on diag1
tmc5160->sgt(constrain(_stallguard, -64, 63));
break;
}
}
}
}

/*
This is the stallguard tuning info. It is call debug, so it could be generic across all classes.
*/
// Report diagnostic and tuning info
void TrinamicSpiDriver::debug_message() {
if (_has_errors) {
return;
}

uint32_t tstep = tmcstepper->TSTEP();
uint32_t tstep = tmc2130 ? tmc2130->TSTEP() : tmc5160->TSTEP();

if (tstep == 0xFFFFF || tstep < 1) { // if axis is not moving return
return;
}
float feedrate = Stepper::get_realtime_rate(); //* settings.microsteps[axis_index] / 60.0 ; // convert mm/min to Hz

log_info(axisName() << " Stallguard " << tmcstepper->stallguard() << " SG_Val:" << tmcstepper->sg_result() << " Rate:" << feedrate
log_info(axisName() << " Stallguard " << (tmc2130 ? tmc2130->stallguard() : tmc5160->stallguard())
<< " SG_Val:" << (tmc2130 ? tmc2130->sg_result() : tmc5160->sg_result()) << " Rate:" << feedrate
<< " mm/min SG_Setting:" << constrain(_stallguard, -64, 63));

// The bit locations differ somewhat between different chips.
// The layout is very different between 2130 and 2208
// The layout is the same for TMC2130 and TMC5160
TMC2130_n ::DRV_STATUS_t status { 0 }; // a useful struct to access the bits.
status.sr = tmcstepper->DRV_STATUS();
status.sr = tmc2130 ? tmc2130->DRV_STATUS() : tmc5160->DRV_STATUS();

// these only report if there is a fault condition
report_open_load(status.ola, status.olb);
report_short_to_ground(status.s2ga, status.s2gb);
report_over_temp(status.ot, status.otpw);
report_short_to_ps(bits_are_true(status.sr, 12), bits_are_true(status.sr, 13));

// log_info(axisName() << " Status Register " << String(status.sr, HEX) << " GSTAT " << String(tmcstepper->GSTAT(), HEX));
// log_info(axisName() << " Status Register " << String(status.sr, HEX) << " GSTAT " << String(tmc2130 ? tmc2130->GSTAT() : tmc5160->GSTAT(), HEX));
}

// this can use the enable feature over SPI. The dedicated pin must be in the enable mode,
// but that can be hardwired that way.
void IRAM_ATTR TrinamicSpiDriver::set_disable(bool disable) {
if (_has_errors) {
return;
Expand All @@ -269,18 +297,22 @@ namespace MotorDrivers {
_disable_pin.synchronousWrite(_disabled);

if (_use_enable) {
uint8_t toff_value;
if (_disabled) {
tmcstepper->toff(_toff_disable);
toff_value = _toff_disable;
} else {
if (_mode == TrinamicMode::StealthChop) {
tmcstepper->toff(_toff_stealthchop);
toff_value = _toff_stealthchop;
} else {
tmcstepper->toff(_toff_coolstep);
toff_value = _toff_coolstep;
}
}
if (tmc2130) {
tmc2130->toff(toff_value);
} else {
tmc5160->toff(toff_value);
}
}
// the pin based enable could be added here.
// This would be for individual motors, not the single pin for all motors.
}

// Configuration registration
Expand Down
Loading

0 comments on commit e15a34e

Please sign in to comment.