Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: thespindoctor 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
--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))
-
seems like the position should be 0 for the first line in the file and 0 for position in the array?
--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))
-
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
-
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
-
New verion...
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))
-
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
-
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
-
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
-
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!
-
Hi Keith,
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
-
Reinhard, Thanks for the warning! Don't worry, I am using the Api for ini and reg file access. Only using direct file io for .txt and .csv files.
Thanks
Keith
-
Hi,
I reused a piece of LUA code I found in one of the LUA Examples folder that ships with Mach4.
It is a file navigation panel which allow you to browse for a file and open it. Its structure and
operation owe more to wxWigets than LUA or Mach4....
inst = mc.mcGetInstance();
-- create the wxFrame window
mainframe = wx.wxFrame( wx.NULL, -- no parent
wx.wxID_ANY, -- whatever for wxWindow ID
"DummyFrame", -- frame caption
wx.wxDefaultPosition, -- place the frame in default position
wx.wxDefaultSize, -- default frame size
wx.wxDEFAULT_FRAME_STYLE ); -- use default frame styles
-- create a panel in the frame
panel = wx.wxPanel(mainframe, wx.wxID_ANY);--We are not going to show it but we need to have this to use the File dialog
local file = wx.wxFileDialog(panel, "Select Probe File", "", "", "Text files (*.txt)|*.txt|Tap files (*.tap)|*.tap",
wx.wxFD_SAVE,wx.wxDefaultPosition,wx.wxDefaultSize, "File Dialog" );
if(file:ShowModal() == wx.wxID_OK)then;
local pathRPF = file:GetPath();
-- Edit this format statement at your own risk
mc.mcCntlProbeFileOpen(inst, pathRPF, "X%.4AXIS_X Y%.4AXIS_Y Z%.4AXIS_Z\r\n", true);
Maybe of use.
Craig
-
Thanks Craig, I have done the same thing. It is very helpful. Thatt and stealing the wx.messagebox is about the exrent of my widgets prowess!
-
Hi,
I have written a macro that reads a Gcode file line by line, makes a correction to each line, adds the corrected line
to a new file until the end of the input file.
To get the file uses a file navigation panel as above and uses the following to open:
local file = wx.wxFileDialog(panel, "Select G code File", "", "", "All files (*.*)|*.*",
wx.wxFD_SAVE,wx.wxDefaultPosition,wx.wxDefaultSize, "File Dialog" );
local pathCode;
if(file:ShowModal() == wx.wxID_OK)then;
pathCode = file:GetPath();
end;
I use this line to open the file:
local hin=assert(io.open(pathCode,"r"));
And these lines to read line by line until the end of the file:
while true do;
inline=hin:read("*L");
if inline==nil then break end;
The body of the loop has LUA string statements necessary to effect the correction I need made.
The description of the string manipulation tools can be found at:
https://www.lua.org/pil/contents.html (https://www.lua.org/pil/contents.html)
While the string functions seem a bit simplistic they can be combined to offer nearly all the ANSI string functions. They could very handily
be used to extract data points from a comma separated file for instance.
In fact this link is my primary resource for LUA coding help. Doesn't have any Mach API stuff or any wxWidgets but everything else LUA.
Craig
-
Fantastic! Thanks so much. I will study. I am sure others will be helped as well.
-
Thanks Craig, this was just the info I needed for my mach4 GUI.
Hi,
I reused a piece of LUA code I found in one of the LUA Examples folder that ships with Mach4.
It is a file navigation panel which allow you to browse for a file and open it. Its structure and
operation owe more to wxWigets than LUA or Mach4....