Skip to content

Commit

Permalink
tmc5160: expose drive_cs and adjust gscaler calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerlz committed Sep 23, 2024
1 parent 936e3d0 commit 10aaabb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ See the [Danger Features document](https://dangerklipper.io/Danger_Features.html

- [stepper: current_change_dwell_time](https://github.com/DangerKlippers/danger-klipper/pull/90)

- [tmc5160: expose driver_cs](https://github.com/DangerKlippers/danger-klipper/pull/337)

- [homing: post-home retract](https://github.com/DangerKlippers/danger-klipper/pull/65)

- [homing: sensorless minimum home distance](https://github.com/DangerKlippers/danger-klipper/pull/65)
Expand Down
14 changes: 9 additions & 5 deletions docs/Config_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ serial:
# reset itself. The default is 'arduino' if the micro-controller
# communicates over a serial port, 'command' otherwise.
#is_non_critical: False
# Setting this to True will allow the mcu to be disconnected and
# Setting this to True will allow the mcu to be disconnected and
# reconnected at will without errors. Helpful for USB-accelerometer boards
# and USB-probes
```
Expand Down Expand Up @@ -1383,12 +1383,12 @@ extended [G-Code command](G-Codes.md#z_tilt) becomes available.
# The height (in mm) that the head should be commanded to move to
# just prior to starting a probe operation. The default is 5.
#min_horizontal_move_z: 1.0
# minimum value for horizontal move z
# minimum value for horizontal move z
# (only used when adaptive_horizontal_move_z is True)
#adaptive_horizontal_move_z: False
# if we should adjust horizontal move z after the first adjustment round,
# based on error.
# when set to True, initial horizontal_move_z is the config value,
# when set to True, initial horizontal_move_z is the config value,
# subsequent iterations will set horizontal_move_z to
# the ceil of error, or min_horizontal_move_z - whichever is greater.
#retries: 0
Expand Down Expand Up @@ -1506,12 +1506,12 @@ Where x is the 0, 0 point on the bed
# The height (in mm) that the head should be commanded to move to
# just prior to starting a probe operation. The default is 5.
#min_horizontal_move_z: 1.0
# minimum value for horizontal move z
# minimum value for horizontal move z
# (only used when adaptive_horizontal_move_z is True)
#adaptive_horizontal_move_z: False
# if we should adjust horizontal move z after the first adjustment round,
# based on error.
# when set to True, initial horizontal_move_z is the config value,
# when set to True, initial horizontal_move_z is the config value,
# subsequent iterations will set horizontal_move_z to
# the ceil of error, or min_horizontal_move_z - whichever is greater.
#max_adjust: 4
Expand Down Expand Up @@ -4450,6 +4450,10 @@ run_current:
# chip. This may be used to set custom motor parameters. The
# defaults for each parameter are next to the parameter name in the
# above list.
#driver_cs: 31
# Set the CS (Current Scale) value during the configuration of
# the TMC5160 chip. The tuning range is 0-31 and the default is 31.
# See the [tmc5160 "Calculation Sheet"](https://www.analog.com/media/en/engineering-tools/design-tools/tmc5160_calculations.xlsx) for more information.
#diag0_pin:
#diag1_pin:
# The micro-controller pin attached to one of the DIAG lines of the
Expand Down
8 changes: 8 additions & 0 deletions docs/Danger_Features.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [`[exclude_object]`](./Config_Reference.md#exclude_object) is enabled by default. Use `[exclude_object] enable_exclude_object: False` to disable it

## Additional configuration options

- [`[mcu] is_non_critical`](./Config_Reference.md#mcu) enables marking of an mcu as optional - it can be freely disconnected and connected at will. (useful for MCU-based accelerometer boards, mcu-based probes that shut down in hot chambers, etc...)
- [`[danger_options]`](./Config_Reference.md#danger-options) - New configuration options to adjust klipper values that were previously hidden
- Additional kinematics versions enabled per-axis acceleration, see [limited_cartesian](./Config_Reference.md#⚠️-cartesian-kinematics-with-limits-for-x-and-y-axes) and [limited_corexy](./Config_Reference.md#⚠️-corexy-kinematics-with-limits-for-x-and-y-axes)
Expand All @@ -33,7 +34,9 @@
- [`[z_tilt_ng]`](./Config_Reference.md#z_tilt_ng) adds enforced 3-point z tilt calibration
- [`[z_tilt/quad_gantry_level] increasing_threshold`](./Config_Reference.md#z_tilt) allows you to customize the allowed variation when probing multiple times
- [`[z_tilt/quad_gantry_level] adaptive_horizontal_move_z`](./Config_Reference.md#z_tilt) adaptively decrease horizontal_move_z based on resulting error - z_tilt and QGL faster and safer!

## Heaters, Fans, and PID changes

- [Model Predictive Control](./MPC.md) is an advanced temperature control method that offers an alternative to traditional PID control.
- [Velocity PID](./PID.md) can be more accurate than positional PID, but is more susceptible to noisy sensors and may require larger smoothing times
- [`PID_PROFILE [LOAD/SAVE]`](./G-Codes.md#pid_profile) allows you to calibrate and save PID profiles at multiple temperatures and fan speeds, and later restore them. With some clever macros, automatic per-material pid tuning is within reach!
Expand All @@ -45,13 +48,18 @@
- [`[temperature_fan] reverse: True`](./Config_Reference.md#temperature_fan) will let you control a fan in reverse to temperature control. The lower the temperature, the higher the fan runs.
- Fans now normalize PWM power within `off_below` and `max_power`, so setting a fan to 10% will get you 10% fan speed within your configured off/max range.

## Steppers / TMC Drivers

- [`[tmc5160] driver_cs`](./Config_Reference.html#tmc5160) allows you to tune the CS (Current Scale) value.

## Macros

- The jinja `do` extension has been enabled. You can now call functions in your macros without resorting to dirty hacks: `{% do array.append(5) %}`
- The python [`math`](https://docs.python.org/3/library/math.html) library is available to macros. `{math.sin(math.pi * variable)}` and more!
- New [`RELOAD_GCODE_MACROS`](./G-Codes.md#reload_gcode_macros) G-Code command to reload `[gcode_macro]` templates without requiring a restart.

## [Plugins](./Plugins.md)

Extend your Danger Klipper installation with custom plugins.

Your python plugins can now extend [`klippy/extras`](https://github.com/DangerKlippers/danger-klipper/tree/master/klippy/extras) adding new modules to klipper without causing updates to fail due to a "dirty" git tree.
Expand Down
40 changes: 24 additions & 16 deletions klippy/extras/tmc5160.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,39 +272,46 @@ def __init__(self, config, mcu_tmc):
self.sense_resistor = config.getfloat(
"sense_resistor", 0.075, above=0.0
)
gscaler, irun, ihold = self._calc_current(
self.req_run_current, self.req_hold_current

self.cs = config.getint("driver_cs", 31, maxval=31, minval=0)

gscaler = self._calc_globalscaler(self.req_run_current)
if gscaler < 32:
raise config.error(
f"""[{self.name}] GLOBALSCALER ({gscaler}) calculation out of bonds.
The target current can't be achieved with the given R_SENSE ({self.sense_resistor})
and CS ({self.cs}). Please adjust your configuration"""
)

ihold = self._calc_current_bits(
min(self.req_run_current, self.req_hold_current), gscaler
)

self.fields.set_field("globalscaler", gscaler)
self.fields.set_field("ihold", ihold)
self.fields.set_field("irun", irun)
self.fields.set_field("irun", self.cs)

def _calc_globalscaler(self, current):
globalscaler = int(
(current * 256.0 * math.sqrt(2.0) * self.sense_resistor / VREF)
(current * 256.0 * self.sense_resistor * math.sqrt(2.0))
* ((self.cs + 1) / 32)
/ VREF
+ 0.5
)
globalscaler = max(32, globalscaler)
if globalscaler >= 256:
globalscaler = 0
return globalscaler

def _calc_current_bits(self, current, globalscaler):
if not globalscaler:
globalscaler = 256
cs = int(
bits = int(
(current * 256.0 * 32.0 * math.sqrt(2.0) * self.sense_resistor)
/ (globalscaler * VREF)
- 1.0
+ 0.5
)
return max(0, min(31, cs))

def _calc_current(self, run_current, hold_current):
gscaler = self._calc_globalscaler(run_current)
irun = self._calc_current_bits(run_current, gscaler)
ihold = self._calc_current_bits(min(hold_current, run_current), gscaler)
return gscaler, irun, ihold
return max(0, min(31, bits))

def _calc_current_from_field(self, field_name):
globalscaler = self.fields.get_field("globalscaler")
Expand All @@ -330,13 +337,14 @@ def get_current(self):
)

def apply_current(self, print_time):
gscaler, irun, ihold = self._calc_current(
self.actual_current, self.req_hold_current
gscaler = self._calc_globalscaler(self.actual_current)
ihold = self._calc_current_bits(
min(self.actual_current, self.req_hold_current), gscaler
)
val = self.fields.set_field("globalscaler", gscaler)
self.mcu_tmc.set_register("GLOBALSCALER", val, print_time)
self.fields.set_field("ihold", ihold)
val = self.fields.set_field("irun", irun)
val = self.fields.set_field("irun", self.cs)
self.mcu_tmc.set_register("IHOLD_IRUN", val, print_time)


Expand Down
2 changes: 1 addition & 1 deletion test/klippy/action_log.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ max_z_accel: 100

[gcode_macro ACTION_LOG]
gcode:
{action_log("TEST")}
{action_log("TEST")}
1 change: 1 addition & 0 deletions test/klippy/tmc.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ home_current: .5
current_change_dwell_time: 1
sense_resistor: 0.220
diag1_pin: !PK7
driver_cs: 29

[stepper_y1]
step_pin: PA4
Expand Down

0 comments on commit 10aaabb

Please sign in to comment.