Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple parameters in G-Code Shell Command Extension #233

Closed
s-show opened this issue Aug 20, 2022 · 2 comments
Closed

Multiple parameters in G-Code Shell Command Extension #233

s-show opened this issue Aug 20, 2022 · 2 comments
Labels
✨ feature request New feature or request

Comments

@s-show
Copy link

s-show commented Aug 20, 2022

Is your feature request related to a problem? Please describe

Not related to a problem.

Describe the solution you'd like

The G-Code Shell Command Extension is a great extension.

I would like to be able to use this extension to change the rotation_distance setting from the console.

To do this, I have created a script that takes a new rotation_distance value as an argument and replaces the rotation_distance value in printer.cfg.
The usage of this script is: sh change_rotation_distance.sh -x 40.2 -y 40.4 -z 8.4

With this script, I want to be able to change the setting by typing RUN_SHELL_COMMAND CMD=change_rotation_distance PARAMS=-x 40.5 -y 40.6 -z 8.7 at the Mainsail console. Therefore, we request the ability to specify multiple parameters.

Translated with www.DeepL.com/Translator (free version)

Describe alternatives you've considered

Access the Raspberry Pi via SSH and run sh change_rotation_distance.sh -x 40.2 -y 40.4 -z 8.4.
However, if the work can be completed in the Mainsail console, the work will be easier than accessing via SSH, so we are making the above suggestion.

Translated with www.DeepL.com/Translator (free version)

Additional information

None.

@s-show s-show added the ✨ feature request New feature or request label Aug 20, 2022
@droans
Copy link

droans commented Sep 28, 2022

Try something like this:

[gcode_shell_command change_rotation_distance]
command: sh change_rotation_distance.sh
timeout: 2
verbose: True

[gcode_macro change_rotation_distance]
gcode:
    # Set the X, Y, Z variables up here. Assuming that the default is None

    {% set param_data = [] %}

    # Repeat below for Y and Z
    # If variable is None, ignore and don't add it to param_data list
    {% if X is not None %}
      {% set param_data = param_data +  ['-x ' + X] %}
    {% endif %}

    # param_data should now be something like ['-x 1.23', '-y 4.56', '-z 7.89']

    # Join param_data with a space
    {% set param_data = ' '.join(param_data) %}
    # Param data should now read '-x 1.23 -y 4.56 -z 7.89'
    
    RUN_SHELL_COMMAND CMD=change_rotation_distance PARAMS="{pass_data}"
    # Note the quotation marks around the pass_data. Without them, Klipper will attempt (and fail) to interpret each word in the params

@s-show
Copy link
Author

s-show commented Sep 30, 2022

Using the code you gave me, I was able to achieve the desired behavior.
I have modified some of the code. The modified parts are as follows.

- {% if X is not None %}
+ {% if X is not none %}

Current G-Code.

[gcode_shell_command change_rotation_distance]
command: sh /home/pi/change_rotation_distance.sh
timeout: 30.
verbose: True

[gcode_macro change_rotation_distance]
gcode:
    # Set the X, Y, Z variables up here. Assuming that the default is None
    {% set param_data = [] %}

    # If variable is None, ignore and don't add it to param_data list
    {% if X is not none %}
      {% set param_data = param_data +  ['-x ' + X] %}
    {% endif %}
    {% if Y is not none %}
      {% set param_data = param_data +  ['-y ' + Y] %}
    {% endif %}
    {% if Z is not none %}
      {% set param_data = param_data +  ['-z ' + Z] %}
    {% endif %}

    # Join param_data with a space
    {% set param_data = ' '.join(param_data) %}

    RUN_SHELL_COMMAND CMD=change_rotation_distance PARAMS="{pass_data}"
    # Note the quotation marks around the pass_data. Without them, Klipper will attempt (and fail) to interpret each word in the params

G-code input into the Mainsail Console.

RUN_SHELL_COMMAND CMD=change_rotation_distance PARAMS='-x 31.644 -y 31.574 -z 8.042'

Shell Script Code Changing rotation_distance Settings.

#!/usr/bin/bash

Message() {
  cat <<- EOF
This script is change rotation_distance value.
Usage: sh change_rotation_distance -x 40 -y 39.5 -z 8.4
       sh change_rotation_distance -y 40 -z 8
       sh change_rotation_distance -h
 NG -> sh change_rotation_distance -x -y -z 8.9
       sh change_rotation_distance -x -40 -y -40 -z 8.9
EOF
}

isNumeric() {
  if expr "$1" : "[0-9]*$" > /dev/null 2>&1 || ( expr "$1" : "[0-9]*\.[0-9]*$" > /dev/null 2>&1 && [ "$1" != "." ] );then
    return_value=0
  else
    return_value=1
  fi
  return "$return_value"
}

if [ $# -eq 0 ]
then
  Message
  exit 0
fi

while getopts x:y:z:h OPT
do 
  isNumeric "$OPTARG"
  return_value=$?
  if [ "$return_value" -eq 1 ]
  then
    if [ "$OPT" = 'h' ]
    then
      Message
      exit 0
    elif [ "$OPT" != '?' ]
    then
      echo "-${OPT} argument is not a positive number."
      Message
      exit 1
    fi
  fi
  case $OPT in
    x) x_axis="$OPTARG" ;;
    y) y_axis="$OPTARG" ;; 
    z) z_axis="$OPTARG" ;; 
    ?) Message
       exit 1 ;;
  esac
done

if [ "${x_axis}" ]
then
  sed -E "s/^(rotation_distance:)\s?[0-9]+\.?[0-9]+(.+#\s?[xX])/\1 ${x_axis}\2/" -i $HOME/klipper_config/printer.cfg
fi

if [ "${y_axis}" ]
then
  sed -E "s/^(rotation_distance:)\s?[0-9]+\.?[0-9]+(.+#\s?[yY])/\1 ${y_axis}\2/" -i $HOME/klipper_config/printer.cfg
fi

if [ "${z_axis}" ]
then
  sed -E "s/^(rotation_distance:)\s?[0-9]+\.?[0-9]+(.+#\s?[zZ])/\1 ${z_axis}\2/" -i $HOME/klipper_config/printer.cfg
fi

Changes to printer_cfg.

[stepper_x]
- rotation_distance: 32.14
+ rotation_distance: 32.14  # x_axis
[stepper_y]
- rotation_distance: 32.07
+ rotation_distance: 32.07  # y_axis
[stepper_z]
- rotation_distance: 8.09
+ rotation_distance: 8.09  # z_axis

Thank you very much for your advice.

@s-show s-show closed this as completed Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants