Using M201 and Python to Lookup a CSV Table File and Using It's Values

More
03 Aug 2022 12:24 #248924 by my1987toyota
The Title says it all the goal of this post is to use python to
1 remap M201.
2 use a csv. file as a look up table to update part and feeder offsets.
3 Use said offsets to guide the placing head to it's correct location.
4 advance the correct feeder.

This will be a lot of work considering I haven't done much coding in python
add to the fact that since I'm using LCNC 2.8.0 I will have to work in
python 2. Never a dull moment.

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

More
03 Aug 2022 22:35 #248977 by andypugh
I think I would start with "import csv" and see what happens.

xkcd.com/353/

And, actually, it looks like a good guess:
docs.python.org/2/library/csv.html
The following user(s) said Thank You: my1987toyota

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

More
04 Aug 2022 01:55 - 04 Aug 2022 13:56 #248995 by my1987toyota
A while back I had thought about copying and renaming the tool.tbl file and another poster had commented on using csv.
At that time it was way on the back burner. forum.linuxcnc.org/20-g-code/43613-bash-...word?start=30#220957

When I started checking out stuff in the book "Automating The Boring Stuff With Python"  in chapter 14
it talks about csv and Json both have modules for python to work with them. I figured csv would be my best starting point. But I am still
getting way ahead of myself being barely a noob at best.

I thought the xkcd comic was hilarious by the way.
Last edit: 04 Aug 2022 13:56 by my1987toyota. Reason: correcting info

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

More
04 Aug 2022 02:23 - 04 Aug 2022 14:22 #248997 by my1987toyota
Something that will be added WAY down the line on my project will be a GUI to update the locations and keep track of
the status of the feeders. A lot has to happen in between for that to work so we will see. I have been told that one of
the best ways to learn a programming language is through projects . This is as good of place as any. LOL
Last edit: 04 Aug 2022 14:22 by my1987toyota.

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

More
04 Aug 2022 15:19 - 07 Aug 2022 16:20 #249047 by tjtr33
Hello again

Back in 2013 i needed data tables
and found the tool table just didnt do what i wanted

I come from Heidenhain TNC background
where data files are simple flat db files with number names and number line indexes

So i wrote simple flat db files ( whitespace delimited)
that were named as numbers
( this is a key element to use with Mcodes
eg: M1xx PNNNNNNNN QNNN format)
and wrote a python script (M122)
to read file NNNNNNNN (the P parm)
and parse the data on index line NNN ( the Q parm)

limitations:
the data file name is numeric
the line index is numeric
the data can be non numeric
the number of data entries per index have to be same

remember:
hal pins dont like strings, so hand them numeric data

I include an example etab
and a version of the Mcode that prints data read to the terminal running lcnc

you can exercise it (in MDI) from any config you like
that has in the .ini
[RS274NGC]
...
USER_M_PATH = /your/linuxcnc/maybe/-dev/mcodes

with this i get umpteen values per line in the table

hth
tomp

due to restrictions in file extents here on forum
just remove the .txt from the files

edit 07aug2022
to use sumple txt files fro data storage...
in your python script use
import sys, subprocess

then build your command string
  like 
cmdstr = "halcmd setp testEtab.0.mypin"
and add the value to be set
  like
cmdstr = cmdstr + str(myval)
then have the system exec the cmdstr
  like
   
subprocess.call(cmdstr, shell=True)

loop that recipe thru all the pins to be set

hth tomp

watch in action



 
Attachments:
Last edit: 07 Aug 2022 16:20 by tjtr33. Reason: add python subprocess invocation
The following user(s) said Thank You: my1987toyota

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

More
05 Aug 2022 19:30 #249154 by my1987toyota
  Thank you tjtr33 I have been looking for examples to get ideas from.
Through my reading on python I have come across both examples
of using csv. and txt. files. Being still very new to python I have a
ways to go. Then add in using it in Linuxcnc and you have multiple
mountains that need to be traversed. So to speak.

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

More
05 Aug 2022 19:35 - 05 Aug 2022 19:37 #249155 by rodw
One of the best Python training courses out there is one from Google which is based on their internal training courses.
developers.google.com/edu/python#:~:text...ce%20Python%20coding.

Its videos are amazing. This should get you going in 2-3 days

Then it seems there is a Python library for everything.
including csv files and ini file processing
So just Google it!
Last edit: 05 Aug 2022 19:37 by rodw.
The following user(s) said Thank You: tommylight, my1987toyota

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

More
06 Aug 2022 02:17 - 06 Aug 2022 02:26 #249172 by my1987toyota
Thanks rod because I am hitting a bit of a wall with my first efforts. Below are the files that are needed. The feeder_V1.01 was re-assigned to a
plain text document because the forum apparently will not upload csv files. If I just have it print(line) it will print it if I run LCNC from the terminal
as soon as I pair it with self.execute LCNC freaks out and gives me the error report below.

 

File Attachment:

File Name: linuxcnc.debug.txt
File Size:1 KB

File Attachment:

File Name: feeder_V1.01.txt
File Size:0 KB

File Attachment:

File Name: remap_2022-08-05.py
File Size:0 KB

File Attachment:

File Name: simple_pnp...8-05.ini
File Size:5 KB
Attachments:
Last edit: 06 Aug 2022 02:26 by my1987toyota.

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

More
07 Aug 2022 02:35 - 07 Aug 2022 02:53 #249242 by my1987toyota
Well let me try this again. I had a nice post all typed out and ready only for the forum software to completely
erase it on me .  So after a long day of trial and error and research I have finally succeeded in linuxcnc reading
the csv file and placing that info into the self execute command. I found out what was happening was after python
read the csv file it would send it in list format to the self execute command.  It won't play well with that. I ended up
having to convert it from list to string. And string is something self execute will work with just fine. I stripped my
remap and csv files down to the bare minimum so I could eliminate as many variables as possible. The below csv file
has been saved as a txt file that way if anyone wants to check out the code they just need to replace the txt extension
and put the csv extension back on it. I still have a long way to go but a win is a win.


 

File Attachment:

File Name: feeder_V1....8-06.txt
File Size:0 KB

 

File Attachment:

File Name: remap_2022-08-06.py
File Size:0 KB

 

File Attachment:

File Name: simple_pnp...8-06.ini
File Size:5 KB
Attachments:
Last edit: 07 Aug 2022 02:53 by my1987toyota. Reason: the program erased what I posted
The following user(s) said Thank You: tommylight, rodw

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

More
07 Aug 2022 12:55 #249268 by my1987toyota
After a good nights rest I now have to consider my next steps. So now I need to make multiple entries in the csv
file and see if I can choose specific ones to be executed. I am hoping to be able to isolate specific axis with it
say X or Y ect . looks like I have a bunch more research ahead of me. And then, and then, and then. LOL
I guess I will have to make some more video's of the progress along the way.

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

Time to create page: 0.252 seconds
Powered by Kunena Forum