Hello Guest it is March 28, 2024, 10:49:15 AM

Author Topic: File operations, reading a line of position data  (Read 3735 times)

0 Members and 1 Guest are viewing this topic.

File operations, reading a line of position data
« on: May 07, 2017, 06:30:53 PM »
I have probed a single surface and am able to get x,y,z positions stored in a probe file.  The next step is to be able to read this one line .csv file and retrieve the x,y,z values.  Other ways to handle the information would be writing registers and then saving to the .ini file or obtaining the variable 5071,5072, and 5072.  i would like to learn to work with the file operations at this point.  Of course the Lua learning curve is still holding me down but I have been able to cut and paste and make a lot of progress.  Properly getting the path listed and working with tables is tricky.   I though I was understanding how it is done but no joy.  The code posted here seems correct to me but it is providing a nil value for the CSVpath to start with.  This is about as simple as it gets and I can't get it done...
Thanks for any suggestions.

Keith
Code: [Select]
--program to retrieve one line of probe data from a prob file
--and display the x,y,z positions in the error line
-- it does not seem to work and I am not sure why

local ProbeYWheel={}
local inst = mc.mcGetInstance()

local CSVPath= wx.wxGetCwd() .. "\\GcodeFiles\\KFCProbeWheelFLAT.csv" --1 line .csv file with x,y,z position from probing one time

--open file and read out the data

io.input(io.open(CSVPath, "r"))

local line;
for line in io.lines(CSVPath) do
tkz=wx.wxStringTokenizer(line,",");
ProbeYWheel["0"]={}--make blank table to hold the data - Should this be "0"??
local token=tkz:GetNextToken();

ProbeYWheel["0"] ["X_Position"]=Token; --i assume the first line is line 0??
ProbeYWheel["0"] ["y_Position"]=tkz:GetNextToken();
ProbeYWheel["0"] ["Z_Position"]=tkz:GetNextToken();
end
io.close()

--read data
local XPos = ProbeYWheel["0"].X_Position --should this be "0" ??
local YPos = ProbeYWheel["0"].Y_Position
local ZPos = ProbeYWheel["0"].Z_Position

    mc.mcCntlSetLastError(inst,string.format(" X: %.4f | Y: %.4f | Z: %.4f", XPos, YPos, ZPos))

« Last Edit: May 07, 2017, 06:33:00 PM by thespindoctor »
Re: File operations, reading a line of position data
« Reply #1 on: May 07, 2017, 08:51:08 PM »
seems like the position should be 0 for the first line in the file and 0 for position in the array?

Code: [Select]

--program to retrieve one line of probe data from a prob file
--and display the x,y,z positions in the error line
-- it does not seem to work and I am not sure why


local ProbeYWheel={}
local inst = mc.mcGetInstance()

local CSVPath= wx.wxGetCwd() .. "\\GcodeFiles\\KFCProbeWheelFLAT.csv"
--open file and read out the data
io.input(io.open(CSVPath, "r"))
local line;
for line in io.lines(CSVPath) do
tkz=wx.wxStringTokenizer(line,",");
ProbeYWheel[0]={}--make blank table to hold the data
local token=tkz:GetNextToken();

ProbeYWheel[0] ["X_Position"]=Token;
ProbeYWheel[0] ["y_Position"]=tkz:GetNextToken();
ProbeYWheel[0] ["Z_Position"]=tkz:GetNextToken();
end
io.close()

--read data
local XPos = ProbeYWheel[0].X_Position
local YPos = ProbeYWheel[0].Y_Position
local ZPos = ProbeYWheel[0].Z_Position

    mc.mcCntlSetLastError(inst,string.format(" X: %.4f | Y: %.4f | Z: %.4f", XPos, YPos, ZPos))

Re: File operations, reading a line of position data
« Reply #2 on: May 08, 2017, 09:41:20 AM »
Hi Keith,

I have no idea, what you're going to do ...

... but after a quick look to your code: did you read, that lua is casesensitive?
In line 18 you declare a variable "token" and in line 20 (I guess) you'll try to access that variable as "Token".
That does not work.

I recommend using the mcLuaEditor.exe provided by mach4. The colorizing ability of the editor helps find those bugs.
Then try to debug your code.
Normally the editor tells the lines, that fail to compile and tells the lines, that stop execution of lua interpreter.
It does not provide that many features, than other ide, but of cause helps a lot in programming lua!

cheers Reinhard
Re: File operations, reading a line of position data
« Reply #3 on: May 08, 2017, 11:19:07 AM »
Yes that is a good pickup on the Token vs token, thanks I missed it.

Does the line for file path seem ok?  It cannot get anything but a nil.

Trying to use the debugger for the first time, I think it will help very much.

Thanks for responding!

Keith
Re: File operations, reading a line of position data
« Reply #4 on: May 08, 2017, 11:24:52 AM »
New verion...

Code: [Select]

local ProbeYWheel={}
local inst = mc.mcGetInstance()

local CSVPath= wx.wxGetCwd() .. "\\GcodeFiles\\KFCProbeWheelFLAT.csv" --1 line .csv file with x,y,z position from probing one time

--open file and read out the data

io.input(io.open(CSVPath, "r"))

local line;
for line in io.lines(CSVPath) do
tkz=wx.wxStringTokenizer(line,",");
ProbeYWheel[1]={}--make blank table to hold the data
local token=tkz:GetNextToken();

ProbeYWheel[1] ["X_Position"]=token;
ProbeYWheel1] ["Y_Position"]=tkz:GetNextToken();
ProbeYWheel[1] ["Z_Position"]=tkz:GetNextToken();
end
io.close()

--read data
local XPos = ProbeYWheel[1].X_Position
local YPos = ProbeYWheel[1].Y_Position
local ZPos = ProbeYWheel[1].Z_Position

    mc.mcCntlSetLastError(inst,string.format(" X: %.4f | Y: %.4f | Z: %.4f", XPos, YPos, ZPos))

Re: File operations, reading a line of position data
« Reply #5 on: May 08, 2017, 02:40:26 PM »
I am not a Lua expert so take this with a grain of salt.

It appears that wxGetCwd() returns the current working directory, not a file.  Wouldn't your io.open have to append the file name to CSVPath?

HTH
Re: File operations, reading a line of position data
« Reply #6 on: May 08, 2017, 03:52:54 PM »
working now not sure what I did to make it go

using the wx.wxGetCwd as listed and it gave the proper directory and file however at first it did not.  Crazy\

Thanks
Re: File operations, reading a line of position data
« Reply #7 on: May 08, 2017, 04:19:16 PM »
My bad, I didn't see the two .. appending the file name, thought it was a comment.

If you were to open a file, say a G code file or a macro file for editing, and then tried to run your function, wouldn't the current directory be where that last file was located. 

wouldn't mc.mcCntlGetMachDir and mc.mcProfileGetName be a surer way to get where you want?

Not sure what you are doing but maybe
mcCntlProbeFileOpen
mcCntlProbeFileClose

HTH
Re: File operations, reading a line of position data
« Reply #8 on: May 08, 2017, 08:55:18 PM »
Probably could open the probe file directly but was trying to learn general file i/o opreations.

Just had some success with regfile entries now on to .ini file work.  Gradually getting the hang of Lua and a little wx.wizards

Lots of trial and error and wx.messagebox to see what happened. 

Found a book about widgets on Amazon that might help because the docsare too difficukt for me.

Thanks!
Re: File operations, reading a line of position data
« Reply #9 on: May 08, 2017, 11:10:30 PM »
Hi Keith,

Quote
Just had some success with regfile entries now on to .ini file work.
you should work on your discipline!
mach4 has API for regfile- and inifile-entry-access, so you should treat both as taboo for direct io!

cheers Reinhard