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

In Python sim.wait() kills the task it's waiting on. #385

Open
nikcleju opened this issue Mar 5, 2023 · 2 comments
Open

In Python sim.wait() kills the task it's waiting on. #385

nikcleju opened this issue Mar 5, 2023 · 2 comments
Labels

Comments

@nikcleju
Copy link

nikcleju commented Mar 5, 2023

I'm trying to run one of the examples via the Python API.

After some trial and error, I have something like this:

import sys
sys.path.append('/usr/lib/python3/dist-packages')

import camotics

# Create a Camotics object
sim = camotics.Simulation()

# Open project
sim.open('/usr/share/doc/camotics/examples/wave/wave.camotics')
sim.wait()

# Read g-code file
with open("/usr/share/doc/camotics/examples/wave/wave.gcode") as f:
    gcode = '\n'.join(f.readlines())

# Process g-code
print("Computing path...")
sim.compute_path(gcode=gcode)
sim.wait()

# Take a look at the path (optional)
print("Getting path...")
path = sim.get_path()
sim.wait()

# Start simulation
sim.start(threads=1, reduce=True)
sim.wait()

print("Sim finished")

surfbin = sim.get_surface(format='binary')
sim.wait()

surfpy  = sim.get_surface(format='python')
sim.wait()

sim.write_surface(filename='surf.stl')
sim.wait()

print("Finish()")

The simulation runs successfully, but the resulting surface is empty, so get_surface() and write_surface() fail. Do you have any idea why?

Basically I'm looking for a way to save the simulation result to a STL file.

@nikcleju nikcleju changed the title Python API simulation start() fails Run example with Python API, cannot get surface Mar 5, 2023
@nikcleju
Copy link
Author

nikcleju commented Mar 5, 2023

I managed to get it running by replacing the sim.wait() after sim.start() with polling if running:

sim.start(threads=4, time=100000, reduce=True)
#sim.wait()
while sim.is_running():
    sleep(0.01)

With sim.wait() it would sometimes work, and sometimes not. I still don't understand why is this happening. When exactly should I use sim.wait() and when not?

@jcoffland
Copy link
Member

The problem is that there is a mistake in wait(). It calls PyTask::join() which interrupts the task then waits for it to exit. It will be fixed in the next release. What you're doing with the while loop is fine.

@jcoffland jcoffland changed the title Run example with Python API, cannot get surface In Python sim.wait() kills the task it's waiting on. Mar 9, 2023
@jcoffland jcoffland added the bug label Mar 9, 2023
jcoffland added a commit that referenced this issue Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants