<p>The <code>M204</code> command is how gcode sets the acceleration for moves that follow (up until the next <code>M204</code> command changes it). This command appears in gcode generated by a slicer with &quot;acceleration control&quot; enabled. You can also write it manually in your start gcode (not very useful unless acceleration control is off, since the first slicer-emitted <code>M204</code> will override it, but may be useful for priming line, etc.) or send it manually over a terminal session/via Octoprint terminal/etc.</p>
<p>The macro you're looking at overrides the <code>M204</code> command and replaces it with one that does several things:</p>
<ul>
<li><p>Does an over-complicated staircase of if/else clauses to accept the Marlin <code>P</code> and <code>T</code> parameters to the <code>M204</code> command for separate print and travel accelerations, but then ignores them and just uses the min of the two.</p>
</li>
<li><p>Accepts a new <code>F</code> parameter (likely meaning factor) and sets Klipper's &quot;accel to decel&quot; setting to this factor times the requested acceleration. This allows &quot;accel to decel&quot; to be kept consistently to whatever the desired percentage of the acceleration limit is, even under changes in acceleration.</p>
</li>
</ul>
<p>Aside from the if/else staircase being a mess, this macro fails to save the factor passed by <code>F</code>, so unless the slicer knows to emit <code>F</code> each time it emits an <code>M204</code> command, it's always going to be resetting the factor to <code>0.5</code> (the default above) each time acceleration changes. Klipper allows definition of persistent variables for gcode macros; this is probably what the author intended to do but didn't know how to do.</p>
<p>Overall, the above looks not-very-useful. &quot;Accel to decel&quot; is a questionable feature to begin with, and with input shaper, it arguably just makes your prints a lot slower with no benefit. The modern recommendation of most folks is that you just set <code>max_accel_to_decel</code> to 999999999 in your configuration to turn it off, rather than going through hoops to change it dynamically like this, as long as you have turned on input shaping.</p>
<p>In any case, the presence of this macro in no ways interferes with acceleration control by the slicer, and you don't have to &quot;use&quot; this macro at all. It's purpose is not for you to &quot;use&quot; it but for it to hook the <code>M204</code> commands the slicer emitted and make them do something extra.</p>

CC BY-SA 4.0
2023-04-27T14:02:53.860Z
2023-04-27T14:02:53.860Z
R.. GitHub STOP HELPING ICE
<p>It is my vague understanding that the following macro somehow controls acceleration and accel to decel.</p>
<ol>
<li><p>How do I set the speed I want for say, acceleration in this macro?</p>
</li>
<li><p>If acceleration is set in this macro and also set in a slicer like Cura, which value will end up being implemented?</p>
</li>
</ol>
<p>I would greatly appreciate it if someone were to walk me through whats happening in the macro conditional. I have experience with php bot nil with Python.</p>
<pre><code>[gcode_macro M204]
rename_existing: M204.1
gcode:
  {% set f = params.F|default(0.5)|float %}

  {% if 'S' in params %}
    {% set s = params.S|float %}
    SET_VELOCITY_LIMIT ACCEL={s} ACCEL_TO_DECEL={ s * f }
  {% else %}
    {% if 'P' in params %}
  {% set p = params.P|float %}
  {% if 'T' in params %}
    {% set t = params.T|float %}
    {% if p &lt; t %}
      SET_VELOCITY_LIMIT ACCEL={p} ACCEL_TO_DECEL={ p * f }
    {% else %}
      SET_VELOCITY_LIMIT ACCEL={t} ACCEL_TO_DECEL={ t * f }
    {% endif %}
  {% else %}
    SET_VELOCITY_LIMIT ACCEL={p} ACCEL_TO_DECEL={ p * f }
  {% endif %}
{% elif 'T' in params %}
  {% set t = params.T|float %}
  SET_VELOCITY_LIMIT ACCEL={t} ACCEL_TO_DECEL={ t * f }
{% endif %}
{% endif %}
</code></pre>

<klipper>
Klipper M204 macro query
General raid
# Klipper M204 macro query

(Asked by: username_0 on 2023-04-25)

It is my vague understanding that the following macro somehow controls acceleration and accel to decel.

1. How do I set the speed I want for say, acceleration in this macro?
2. If acceleration is set in this macro and also set in a slicer like Cura, which value will end up being implemented?

I would greatly appreciate it if someone were to walk me through whats happening in the macro conditional. I have experience with php bot nil with Python.

```
[gcode_macro M204]
rename_existing: M204.1
gcode:
  {% set f = params.F|default(0.5)|float %}

  {% if 'S' in params %}
    {% set s = params.S|float %}
    SET_VELOCITY_LIMIT ACCEL={s} ACCEL_TO_DECEL={ s * f }
  {% else %}
    {% if 'P' in params %}
  {% set p = params.P|float %}
  {% if 'T' in params %}
    {% set t = params.T|float %}
    {% if p < t %}
      SET_VELOCITY_LIMIT ACCEL={p} ACCEL_TO_DECEL={ p * f }
    {% else %}
      SET_VELOCITY_LIMIT ACCEL={t} ACCEL_TO_DECEL={ t * f }
    {% endif %}
  {% else %}
    SET_VELOCITY_LIMIT ACCEL={p} ACCEL_TO_DECEL={ p * f }
  {% endif %}
{% elif 'T' in params %}
  {% set t = params.T|float %}
  SET_VELOCITY_LIMIT ACCEL={t} ACCEL_TO_DECEL={ t * f }
{% endif %}
{% endif %}

```

**Tags:** klipper


---

## 1 answers

### Answer by username_1 on 2023-04-27. Score: 0

The `M204` command is how gcode sets the acceleration for moves that follow (up until the next `M204` command changes it). This command appears in gcode generated by a slicer with "acceleration control" enabled. You can also write it manually in your start gcode (not very useful unless acceleration control is off, since the first slicer-emitted `M204` will override it, but may be useful for priming line, etc.) or send it manually over a terminal session/via Octoprint terminal/etc.

The macro you're looking at overrides the `M204` command and replaces it with one that does several things:

* Does an over-complicated staircase of if/else clauses to accept the Marlin `P` and `T` parameters to the `M204` command for separate print and travel accelerations, but then ignores them and just uses the min of the two.
* Accepts a new `F` parameter (likely meaning factor) and sets Klipper's "accel to decel" setting to this factor times the requested acceleration. This allows "accel to decel" to be kept consistently to whatever the desired percentage of the acceleration limit is, even under changes in acceleration.

Aside from the if/else staircase being a mess, this macro fails to save the factor passed by `F`, so unless the slicer knows to emit `F` each time it emits an `M204` command, it's always going to be resetting the factor to `0.5` (the default above) each time acceleration changes. Klipper allows definition of persistent variables for gcode macros; this is probably what the author intended to do but didn't know how to do.

Overall, the above looks not-very-useful. "Accel to decel" is a questionable feature to begin with, and with input shaper, it arguably just makes your prints a lot slower with no benefit. The modern recommendation of most folks is that you just set `max_accel_to_decel` to 999999999 in your configuration to turn it off, rather than going through hoops to change it dynamically like this, as long as you have turned on input shaping.

In any case, the presence of this macro in no ways interferes with acceleration control by the slicer, and you don't have to "use" this macro at all. It's purpose is not for you to "use" it but for it to hook the `M204` commands the slicer emitted and make them do something extra.


---

