Trouble with a if statement decision tree using M291 K
-
I think I am describing this properly.
On my IDEX printer I have 4 head options, I am creating a macro for Calibrating X,Y and Z offset. for both heads. The purpose of this macro is to verify a setup is compatiable and assign correct BLtouch offsets based on print head used (BLtouch location is different on a couple of the print heads)
In the start of the macro I am asking to choose which print heads are installed. (As not all are compatiable with each other due to nozzle depth below the gantry.
I have 4 Head options they are: GHR, GS, GHF, and GUF.
First issue is in line # 6, I was going to attempt to use this variable as an input to my K switch on my M291 command. I am not sure if this one is possible but I commented it out for now and added the string directly onto the K switch while I figure out the second problem.
I am having issues starting at line 50, l am trying to get the macro to decide if the print heads are compatiable with each other. I have the values of the heads added as a 2 digit number and stored in var.head_configuration, so I can compare. If the number starts or ends with a 1 the other number must be a 1, If the number in the variable starts or ends with a 3 the other number must also be 3. All other combonations are valid. Is there an easier way to do this or am I going down the wrong path? Suggestions welcome
here is the setup for my macro
; Calibration Setup Macro ;*** VARIABLES *** var primary_head = -1 ; Variable to store primary head selection var secondary_head = -1 ; Variable to store secondary head selection ; var head_names = ["GS", "GHF", "GUF", "GHR"]; ; Names of the heads var head_configuration = -1; ; Variable to store the head configuration var Temp_var = -1; Variable to be used for math echo >"0:/sys/IAC_Setup_log.txt" {state.time}, "Setup Start Time stamp" ;*** WARNING BEFORE HEAD SELECTION *** M291 S3 R"Important" P"** Ensure the correct print heads are installed before proceeding! **" M291 S3 R"WARNING:" P"** Ensure calibration tool is NOT on the bed at this time. **" ;*** NOTICE ABOUT HEAD CHANGES *** M291 S3 R"NOTICE" P"** If you change print heads, you must rerun this setup macro before attempting IDEX calibration again! **" ;*** HOMING COMMANDS *** G28 ; Home all axes G1 Y10 F4000 ; Move Y axis +10mm G1 Y-10 F4000 ; Return Y axis to home G1 Y10 F4000 ; Move Y axis +10mm G1 Y-10 F4000 ; Return Y axis to home ;*** USER PROMPT FOR HEAD SELECTION *** M291 R"Please choose print head installed in front position" P"Choose from list" K{"GS", "GHF", "GUF", "GHR"} S4 set var.primary_head = input("Enter your choice (1-4): ") echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Selected Primary Head:", var.primary_head ;*** HOMING FOR SECONDARY HEAD SELECTION *** G1 U658.6 F4000 ; Move U axis -10mm G1 U668.6 F4000 ; Return U axis to home G1 U658.6 F4000 ; Move U axis -10mm G1 U668.6 F4000 ; Return U axis to home M291 R"Please choose print head installed in rear position" P"Choose from list" K{"GS", "GHF", "GUF", "GHR"} S4 set var.secondary_head = input("Enter your choice (1-4): ") echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Selected Secondary Head:", var.secondary_head ;*** LOG HEAD SELECTION *** echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Selected Primary Head:", var.primary_head echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Selected Secondary Head:", var.secondary_head ;*** VERIFY COMPATIBILITY *** set var.Temp_var = var.primary_head ; placing head identifier in temp variable set var.primary_head = (var.Temp_var*10) ; Changing decimal location of result set var.head_configuration = (var.primary_head + var.secondary_head) echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Variable Head Configuration:", var.head_configuration if (var.primary_head != var.secondary_head) ; Ensure primary and secondary are different ; Check compatibility based on the specified rules if ((var.primary_head == 4 && var.secondary_head == 4) || ; Both GHR (var.primary_head == 2 && var.secondary_head == 2) || ; Both GHF ((var.primary_head == 1 || var.primary_head == 3) && (var.secondary_head == 1 || var.secondary_head == 3))) ; GS and GUF combinations ; Both heads are compatible and different set var.head_configuration = "Primary: " + var.head_names[var.primary_head - 1] + ", Secondary: " + var.head_names[var.secondary_head - 1] echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Head Configuration - "{var.head_configuration}" M291 S3 R"Configuration Confirmed" P"Primary: {head_names[var.primary_head - 1]}, Secondary: {head_names[var.secondary_head - 1]}" else echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Error: Incompatible heads selected." M291 S3 R"Error" P"Sorry, these heads are incompatible together in an IDEX configuration." ; M112 ; Emergency stop to avoid potential issues endif else echo >>"0:/sys/IAC_Setup_log.txt" {state.time}, "Error: Primary and Secondary heads are not compatiable." M291 S3 R"Error" P"Primary and Secondary heads are not compatiable. Please try again." ; M112 ; Emergency stop to avoid potential issues endif
-
@Macgyver I've edited your post to make it easier to read.
1 thing that jumps out at me is RRF doesn't have
endif
https://docs.duet3d.com/en/User_manual/Reference/Gcode_meta_commands#conditional-construct -
@jay_s_uk well darn it, bad when you so used to working in another language
Thank you for that catch, rework time
-
@Macgyver said in Trouble with a if statement decision tree using M291 K:
set var.secondary_head = input("Enter your choice (1-4): ")
Another thing that jumps out here is that
input
is a variable, not a function - withS4
when the message box returns,input
is set to the index of the value you selected, so:M291 R"Choose Print Head" S4 K{"GS","GHF","GUF","GHR"} set var.secondary_head = { input }
would result in the secondary head being set to
1
if you clickedGHF
-
Aside from the previous comments, a few things.
If you want to use variables in the M291 then use something like
var choice= [-1,-1] var options1 = ["A","B","C","D"] M291 K{options1} ..... if input >=0 set var.choice[0]= input
Note that input is the returned index so it is zero based
So it will be 0,1,2,3 or cancelled
Depending on firmware version, the result of a cancellation or timeout is different so you'll need to consider thatI would put your two choices into another array rather than a number as it will be easier to do your compatibility tests.
I would also work out my choice arrays so that after choosing the first, the M291 on the second only presents valid choices
Lastly, you can't use line breaks and mixed comments in your Boolean OR logic like that
You'd need to change it to
if
blah
elif
blah blah -
ok, I am going to rework this using your suggestions,
Side Question but related, It does not appear that the M291 allows for user input except selecting options that are coded into the macro.
Is there a command to allow user free text input using the onscreen keyboard? Think about something like
What is your network name? Then allowing a user to type in the name of the network they want to connect to?
Essentially trying to sudo prompt for an input to the M587 command?If you like I can break this question out to a separate thread for others to search on.
-
@Macgyver said in Trouble with a if statement decision tree using M291 K:
Is there a command to allow user free text input using the onscreen keyboard?
Try the S7 parameter of M297, https://docs.duet3d.com/en/User_manual/Reference/Gcodes#m291-display-message-and-optionally-wait-for-response.