problems with sampler/halsampler :SOLVED

More
20 Jul 2022 04:00 #247812 by thadwald
I'm trying to automate the process of  capturing realtime data using sampler. My configuration is as follows:

I'm running this on a raspberry pi 4, LinuxCNC 2.8 

sampler hal configuration:
loadrt threads name1=cmm-thread period1=2000000
loadrt sampler depth=100000 cfg=ff
addf sampler.0 cmm-thread
net y-pos-cmd => sampler.0.pin.0
net laser-fb => sampler.0.pin.1
net coolant-mist => sampler.0.enable

I then start halsampler and enable sampler in python:
sampleps = subprocess.Popen("halsampler", stdout=subprocess.PIPE)
self.gscreen.mdi_control.user_command("M7")
#<< various motion commands from python >>
self.gscreen.mdi_control.user_command("M9")
sampleps.kill()
sample = ""
        for tup in sampleps.communicate():
                sample +=str(tup)
#<<further processing of sampler data>>

I'm having a difficult time figuring out why it does not work reliably. 
For instance, it doesn't begin recording immediately after enabling it. When I observe sampler.0.curr-depth in halshow, the number bounces up and down between 1 and about 15 for about a second or so after enabling, then begins to climb at the expected rate. I have tried a few things, including adding pauses to provide more time for things to settle, but I still can't get it to work reliably. 

I have little understanding of how fifo buffers work. Am I missing something?

Also, in the case that I want to start a fresh sample, is there a way to clear the FIFO buffer, or would I need to unload and reload the sampler component?

Thanks

Please Log in or Create an account to join the conversation.

More
20 Jul 2022 14:24 - 20 Jul 2022 14:32 #247850 by thadwald
I solved it.
The issue was the asynchronous nature of subprocess.Popen, coupled with the fact that I was calling halsampler without specifiying a buffer length, and then prematurely killing it while it was still reading the fifo buffer.

The solution in this case was to finish collecting data, then include the buffer length argument when calling halsampler so that it terminates when done reading. The subsequent Popen.communicate() method blocks the python code until halsampler is terminated.

code example:
#enable sampler
self.gscreen.mdi_control.user_command("M7")
#mdi move 5 units
self.gscreen.mdi_control.go_to_position("Y",0 - 5, speed)
#turn off the sampler
self.gscreen.mdi_control.user_command("M9")
#return to home
self.gscreen.mdi_control.go_to_position("Y", 0,600)

#sampler fifo depth is mapped to hal pin
n = str(self.halcomp['fifo-cur-depth'])
sampleps = subprocess.Popen(["halsampler","-n " + n], stdout=subprocess.PIPE)
#communicate() returns tuple(stdoutdata,stderrdata)
sample = str(sampleps.communicate()[0])
Last edit: 20 Jul 2022 14:32 by thadwald. Reason: conciseness

Please Log in or Create an account to join the conversation.

Time to create page: 0.124 seconds
Powered by Kunena Forum