Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Writing variables to a file, problems with nested echo commands

    Scheduled Pinned Locked Moved
    Gcode meta commands
    2
    7
    346
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ComedianTF2undefined
      ComedianTF2
      last edited by ComedianTF2

      Firmware: 3.5.0-beta.4
      Duet web control: 3.5.0-beta.4

      I am working on a script to write several global variables to a file, per a series of messages sent to a user, so the user can get the right set of variables for their printer after executing the script.

      The first part of the code (line 1 through 15) work well, and in the resulting config_globals.g, the first parameters (version 1 or version 2) are set correctly

      However the second set of parameters (printer model 1 or 2) are not set at all, it seems as if nestling the echo commands that deep is causing problems.

      M30 "0:/sys/test/config_globals.g"
      M28 "0:/sys/test/config_globals.g"
      M29
      
      M291 T0 S4 R"Welcome" P"Welcome to your Printer. Please select the version of printer you have" K{"V1","V2"} 
      	if input = 0
      		M291 S3 R"Version V1" P"According to this, you have a V1 printer. Press OK to confirm, or cancel to re-start"
      		echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
      		echo >>"0:/sys/test/config_globals.g" "   global version = 1"
      		global version = 1
      	if input = 1
      		M291 S3 R"Version V2" P"According to this, you have a V2 printer. Press OK to confirm, or cancel to re-start"
      		echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
      		echo >>"0:/sys/test/config_globals.g" "   global version = 2"
      		global version = 2
      
      if global.version = 1
      	M291 T0 S4 R"V2 to V3" P"Do you have a A model or a B model" K{"A model","B model"} 
      		if input = 0
      			echo >>"0:/sys/test/config_globals.g" "   global printer-model = 1"
      			global printer-model = 1
      		if input = 1
      			echo >>"0:/sys/test/config_globals.g" "   global printer-model = 2"
      			global printer-model = 2
      elif global.version = 2
      	M291 T0 S4 R"V2 to V3" P"Do you have a C model or a D model" K{"C model","D model"} 
      		if input = 0
      			echo >>"0:/sys/test/config_globals.g" "   global printer-model = 1"
      			global printer-model = 1
      		if input = 1
      			echo >>"0:/sys/test/config_globals.g" "   global printer-model = 2"
      			global printer-model = 2
      
      

      The output of this file, selecting first version 1 and then printer model 1 is this:

      if !exists(global.config_version)
         global version = 1
      

      However the expected and desired code is:

      if !exists(global.config_version)
         global version = 1
         global printer-model = 1
      

      Executing the echo commands separately does work, so I am not sure why the commands are not working when used in this fashion.

      As another test, I also created two files named version1.g and version2.g, that contained the sub-scripts. In that case, the echo commands correctly wrote the global parameters to the config_globals.g

      if global.version = 1
      	M98 P"0:/sys/test/version1.g"
      elif global.version = 2
      	M98 P"0:/sys/test/version2.g"
      

      version1.g example:

      M291 T0 S4 R"V2 to V3" P"Do you have a A model or a B model" K{"A model","B model"} 
      	if input = 0
      		echo >>"0:/sys/test/config_globals.g" "   global printer-model = 1"
      		;global printer-model = 1
      	if input = 1
      		echo big120x
      		echo >>"0:/sys/test/config_globals.g" "   global printer-model = 2"
      		;global printer-model = 2 
      
      dc42undefined 1 Reply Last reply Reply Quote 0
      • dc42undefined
        dc42 administrators @ComedianTF2
        last edited by

        @ComedianTF2 what is this line supposed to do?

        echo big120x

        It's not valid and may cause execution of the macro to stop.

        Duet WiFi hardware designer and firmware engineer
        Please do not ask me for Duet support via PM or email, use the forum
        http://www.escher3d.com, https://miscsolutions.wordpress.com

        ComedianTF2undefined 1 Reply Last reply Reply Quote 0
        • ComedianTF2undefined
          ComedianTF2 @dc42
          last edited by

          @dc42 a leftover from trying to diagnose the problems, I re-wrote the macro to be significantly simpler from the one I was working on, but the problems remained.

          When removed it still doesn't work

          dc42undefined 1 Reply Last reply Reply Quote 0
          • dc42undefined
            dc42 administrators @ComedianTF2
            last edited by

            @ComedianTF2 printer-model isn't a valid variable name. Use printer_model instead.

            Duet WiFi hardware designer and firmware engineer
            Please do not ask me for Duet support via PM or email, use the forum
            http://www.escher3d.com, https://miscsolutions.wordpress.com

            ComedianTF2undefined 1 Reply Last reply Reply Quote 0
            • ComedianTF2undefined
              ComedianTF2 @dc42
              last edited by

              @dc42 The problem is not in the variable name, I tested it and it did not work with the following script:

              M30 "0:/sys/test/config_globals.g"
              M28 "0:/sys/test/config_globals.g"
              M29
               
              M291 T0 S4 R"Welcome" P"Welcome to your Printer. Please select the version of printer you have" K{"V1","V2"} 
              	if input = 0
              		M291 S3 R"Version V1" P"According to this, you have a V1 printer. Press OK to confirm, or cancel to re-start"
              		echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
              		echo >>"0:/sys/test/config_globals.g" "   global version = 1"
              		global version = 1
              	if input = 1
              		M291 S3 R"Version V2" P"According to this, you have a V2 printer. Press OK to confirm, or cancel to re-start"
              		echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
              		echo >>"0:/sys/test/config_globals.g" "   global version = 2"
              		global version = 2
               
              if global.version = 1
              	M291 T0 S4 R"V2 to V3" P"Do you have a A model or a B model" K{"A model","B model"} 
              		if input = 0
              			echo >>"0:/sys/test/config_globals.g" "   global printer_model = 1"
              			global printer_model = 1
              		if input = 1
              			echo >>"0:/sys/test/config_globals.g" "   global printer_model = 2"
              			global printer_model = 2
              elif global.version = 2
              	M291 T0 S4 R"V2 to V3" P"Do you have a C model or a D model" K{"C model","D model"} 
              		if input = 0
              			echo >>"0:/sys/test/config_globals.g" "   global printer_model = 1"
              			global printer_model = 1
              		if input = 1
              			echo >>"0:/sys/test/config_globals.g" "   global printer_model = 2"
              			global printer_model = 2
              

              That outputs (when choosing V1 and model A)

              if !exists(global.config_version)
                 global version = 1
              

              However this combination of scripts does work:

              M30 "0:/sys/test/config_globals.g"
              M28 "0:/sys/test/config_globals.g"
              M29
               
              M291 T0 S4 R"Welcome" P"Welcome to your Printer. Please select the version of printer you have" K{"V1","V2"} 
              	if input = 0
              		M291 S3 R"Version V1" P"According to this, you have a V1 printer. Press OK to confirm, or cancel to re-start"
              		echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
              		echo >>"0:/sys/test/config_globals.g" "   global version = 1"
              		;global version = 1
              	if input = 1
              		M291 S3 R"Version V2" P"According to this, you have a V2 printer. Press OK to confirm, or cancel to re-start"
              		echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
              		echo >>"0:/sys/test/config_globals.g" "   global version = 2"
              		;global version = 2
               
              if global.version = 1
              	M98 P"0:/sys/test/version1.g"
              elif global.generation = 2
              	M98 P"0:/sys/test/version2.g"
              
              
              
              M291 T0 S4 R"V2 to V3" P"Do you have a A model or a B model" K{"A model","B model"} 
              	if input = 0
              		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 1"
              		global printer_model = 1
              	if input = 1
              		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 2"
              		global printer_model = 2
              
              M291 T0 S4 R"V2 to V3" P"Do you have a C model or a D model" K{"C model","D model"} 
              	if input = 0
              		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 1"
              		global printer_model = 1
              	if input = 1
              		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 2"
              		global printer_model = 2
              

              That outputs (when choosing V1 and model A)

              if !exists(global.config_version)
                 global version = 1
                 global printer_model = 1
              

              I don't see a good reason why the script with everything contained in one file would not work, but that same script just split out over three files does work as it should

              dc42undefined 1 Reply Last reply Reply Quote 0
              • dc42undefined
                dc42 administrators @ComedianTF2
                last edited by dc42

                @ComedianTF2 I confirm that your original script doesn't work. I changed it to the following, which works:

                if fileexists("0:/sys/test/config_globals.g")
                	M30 "0:/sys/test/config_globals.g"
                 
                M291 T0 S4 R"Welcome" P"Welcome to your Printer. Please select the version of printer you have" K{"V1","V2"} 
                if input = 0
                	M291 S3 R"Version V1" P"According to this, you have a V1 printer. Press OK to confirm, or cancel to re-start"
                	echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
                	echo >>"0:/sys/test/config_globals.g" "   global version = 1"
                	global version = 1
                if input = 1
                	M291 S3 R"Version V2" P"According to this, you have a V2 printer. Press OK to confirm, or cancel to re-start"
                	echo >"0:/sys/test/config_globals.g" "if !exists(global.config_version)"
                	echo >>"0:/sys/test/config_globals.g" "   global version = 2"
                	global version = 2
                 
                if global.version = 1
                	M291 T0 S4 R"V2 to V3" P"Do you have a A model or a B model" K{"A model","B model"}
                	if input = 0
                		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 1"
                		global printer_model = 1
                	if input = 1
                		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 2"
                		global printer_model = 2
                elif global.version = 2
                	M291 T0 S4 R"V2 to V3" P"Do you have a C model or a D model" K{"C model","D model"} 
                	if input = 0
                		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 1"
                		global printer_model = 1
                	if input = 1
                		echo >>"0:/sys/test/config_globals.g" "   global printer_model = 2"
                		global printer_model = 2
                

                I think it was indenting the if-blocks more than the preceding M291 command that was causing the problem. I have created Github issue https://github.com/Duet3D/RepRapFirmware/issues/897.

                dc42 created this issue in Duet3D/RepRapFirmware

                closed Extra indentation causes script not to work #897

                Duet WiFi hardware designer and firmware engineer
                Please do not ask me for Duet support via PM or email, use the forum
                http://www.escher3d.com, https://miscsolutions.wordpress.com

                ComedianTF2undefined 1 Reply Last reply Reply Quote 1
                • ComedianTF2undefined
                  ComedianTF2 @dc42
                  last edited by

                  @dc42 Thanks for that.

                  Good to know that the choice selection for the M291 T0 S4 does not need to be indented, that makes a large difference and with that I should be able to get everything in a single file

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Unless otherwise noted, all forum content is licensed under CC-BY-SA