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

Simple if condition not working, what am I missing

Scheduled Pinned Locked Moved
Gcode meta commands
2
2
171
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.
  • undefined
    Psylenceo
    last edited by 28 Jul 2021, 15:40

    I wanted to use the meta-commands to adjust my babystepping for prints, however, my if statements never trigger even if they are true. So instead of testing the commands inside of a gcode over and over, I made a quick macro and get the same results. And for the life of me, I cannot figure out what I am doing wrong. Below is what I have tried.

    echo move.axes[2].babystep
    if move.axes[2].babystep = 0.050
    echo "it worked"
    echo "it didn't work"

    DWC echos the value from my Z axes babystep, and then echoes that the if statement was not true.

    set global.offset = move.axes[2].babystep
    echo global.offset
    if offset = 0.050
    echo "it worked"
    echo "it didn't work"

    Again DWC echos the value in the global offset variable, which is axes 2 babystep value, but the if statement does not see the expression as true.

    The only time I've gotten the above meta to work is by setting it to NOT equals, in which case the if statement evaluates as true.

    7/28/2021, 11:39:54 AM M122
    === Diagnostics ===
    RepRapFirmware for Duet 3 MB6HC version 3.3 (2021-06-15 21:45:47) running on Duet 3 MB6HC v0.6 or 1.0 (standalone mode)

    undefined 1 Reply Last reply 28 Jul 2021, 16:44 Reply Quote 0
    • undefined
      dc42 administrators @Psylenceo
      last edited by 28 Jul 2021, 16:44

      @psylenceo comparing floating point numbers for equality is never a good idea in programming, except in limited cases, because floating point maths is not exact. Use something like this instead:

      if abs(move.axes[2].babystep - 0.050) < 0.001)

      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

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