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

More
4 years 2 months ago #249586 by my1987toyota
Something I will have to try and figure out is a way to update the values of the csv file
while Linuxcnc is already running. That way if you change out a feeder and the part is offset
you can enter that new location into the csv table and the program will run with the new
location without reloading Linuxcnc.

Please Log in to join the conversation.

More
4 years 2 months ago #249605 by my1987toyota
Well that didn't take long. LOL
The biggest problem was the csv reader was in the global region of the python remap script.
this time I was successful in placing it in the local M201 code so every time the M201 was called
the program would check the csv file. in effect you can update the table between M201 calls.
I will be placing the new remap python file in my next post in both readable text and a python file.
andypugh if you're reading this give this code a go and let me know what you think.

Please Log in to join the conversation.

More
4 years 2 months ago #249606 by my1987toyota
remap_11.03
Code:
#remap version 11.03 import emccanon import sys import interpreter import csv def M201(self, **words):     print(words)     P = words['p']     print(P)     feedersFile = open('feeder_V1.04.csv')     coordinatesReader = csv.reader(feedersFile)     locationData = list(coordinatesReader)     fdrPos = locationData             # these codes can be taylored to your machine if needed     endCoord = 'Z0.0 C0.0'    # G_codes to ensure Z and C axis are in there 0 position     prefix1 = 'G53 G0 '    # machine coordinate system and G0 rapid move     prefix2 = 'G53 G1 '    # machine coordinate system and G1 move     feeds1 = ' F150'    # feed value for Z to pick up part     vacOn = 'M7'    # M_code for turning on vaccum     pTime = 'G4 P0.5'   # G_code for pause time to allow vaccum to asstablish     retract = 'G53 G1 Z0.0 F150'    # Z retract speed with part attatched     fdrCycl = 'M161 P'   # M_code for feeder advance          if P < 0:         print("error, a P parameter must be passed") #side 1 feeders     elif P == 1:         self.execute(prefix1 + fdrPos[1][1] + ' ' + fdrPos[1][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos [1][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '1')     elif P == 2:         self.execute(prefix1 + fdrPos[2][1] + ' ' + fdrPos[2][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[2][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '2')     elif P == 3:         self.execute(prefix1 + fdrPos[3][1] + ' ' + fdrPos[3][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[3][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '3')     elif P == 4:         self.execute(prefix1 + fdrPos[4][1] + ' ' + fdrPos[4][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[4][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '4')     elif P == 5:         self.execute(prefix1 + fdrPos[5][1] + ' ' + fdrPos[5][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[5][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '5')     elif P == 6:         self.execute(prefix1 + fdrPos[6][1] + ' ' + fdrPos[6][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[6][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '6')     elif P == 7:         self.execute(prefix1 + fdrPos[7][1] + ' ' + fdrPos[7][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[7][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '7')     elif P == 8:         self.execute(prefix1 + fdrPos[8][1] + ' ' + fdrPos[8][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[8][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '8')     elif P == 9:         self.execute(prefix1 + fdrPos[9][1] + ' ' + fdrPos[9][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[9][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '9')     elif P == 10:         self.execute(prefix1 + fdrPos[10][1] + ' ' + fdrPos[10][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[10][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '10')     elif P == 11:         self.execute(prefix1 + fdrPos[11][1] + ' ' + fdrPos[11][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[11][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '11')     elif P == 12:         self.execute(prefix1 + fdrPos[12][1] + ' ' + [12][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[12][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '12')     elif P == 13:         self.execute(prefix1 + fdrPos[13][1] + ' ' + fdrPos[13][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[13][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '13')     elif P == 14:         self.execute(prefix1 + fdrPos[14][1] + ' ' + fdrPos[14][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[14][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '14')     elif P == 15:         self.execute(prefix1 + fdrPos[15][1] + ' ' + fdrPos[15][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[15][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '15')     elif P == 16:         self.execute(prefix1 + fdrPos[16][1] + ' ' + fdrPos[16][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[16][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '16')     elif P == 17:         self.execute(prefix1 + fdrPos[17][1] + ' ' + fdrPos[17][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[17][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '17')     elif P == 18:         self.execute(prefix1 + fdrPos[18][1] + ' ' + fdrPos[18][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[18][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '18')     elif P == 19:         self.execute(prefix1 + fdrPos[19][1] + ' ' + fdrPos[19][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[19][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '19')     elif P == 20:         self.execute(prefix1 + fdrPos[20][1] + ' ' + fdrPos[20][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[20][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '20')     elif P == 21:         self.execute(prefix1 + fdrPos[21][1] + ' ' + fdrPos[21][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[21][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '21')     elif P == 22:         self.execute(prefix1 + fdrPos[22][1] + ' ' + fdrPos[22][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[22][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '22')     elif P == 23:         self.execute(prefix1 + fdrPos[23][1] + ' ' + fdrPos[23][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[23][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '23')     elif P == 24:         self.execute(prefix1 + fdrPos[24][1] + ' ' + fdrPos[24][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[24][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '24') #side 2 feeders     elif P == 25:         self.execute(prefix1 + fdrPos[25][1] + ' ' + fdrPos[25][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[25][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '25')     elif P == 26:         self.execute(prefix1 + fdrPos[26][1] + ' ' + fdrPos[26][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[26][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '26')     elif P == 27:         self.execute(prefix1 + fdrPos[27][1] + ' ' + fdrPos[27][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[27][3] + feeds1)         self.executevacOn(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '27')     elif P == 28:         self.execute(prefix1 + fdrPos[28][1] + ' ' + fdrPos[28][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[28][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '28')     elif P == 29:         self.execute(prefix1 + fdrPos[29][1] + ' ' + fdrPos[29][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[29][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '29')     elif P == 30:         self.execute(prefix1 + fdrPos[30][1] + ' ' + fdrPos[30][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[30][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '30')     elif P == 31:         self.execute(prefix1 + fdrPos[31][1] + ' ' + fdrPos[31][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[31][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '31')     elif P == 32:         self.execute(prefix1 + fdrPos[32][1] + ' ' + fdrPos[32][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[32][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '32')     elif P == 33:         self.execute(prefix1 + fdrPos[33][1] + ' ' + fdrPos[33][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[33][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '33')     elif P == 34:         self.execute(prefix1 + fdrPos[34][1] + ' ' + fdrPos[34][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[34][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '34')     elif P == 35:         self.execute(prefix1 + fdrPos[35][1] + ' ' + fdrPos[35][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[35][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '35')     elif P == 36:         self.execute(prefix1 + fdrPos[36][1] + ' ' + fdrPos[36][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[36][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '36')     elif P == 37:         self.execute(prefix1 + fdrPos[37][1] + ' ' + fdrPos[37][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[37][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '37')     elif P == 38:         self.execute(prefix1 + fdrPos[38][1] + ' ' + fdrPos[38][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[38][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '38')     elif P == 39:         self.execute(prefix1 + fdrPos[39][1] + ' ' + fdrPos[39][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[39][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '39')     elif P == 40:         self.execute(prefix1 + fdrPos[40][1] + ' ' + fdrPos[40][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[40][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '40')     elif P == 41:         self.execute(prefix1 + fdrPos[41][1] + ' ' + fdrPos[41][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[41][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '41')     elif P == 42:         self.execute(prefix1 + fdrPos[42][1] + ' ' + fdrPos[42][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[42][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '42')     elif P == 43:         self.execute(prefix1 + fdrPos[43][1] + ' ' + fdrPos[43][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[43][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '43')     elif P == 44:         self.execute(prefix1 + fdrPos[44][1] + ' ' + fdrPos[44][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[44][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '44')     elif P == 45:         self.execute(prefix1 + fdrPos[45][1] + ' ' + fdrPos[45][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[45][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '45')     elif P == 46:         self.execute(prefix1 + fdrPos[46][1] + ' ' + fdrPos[46][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[46][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '46')     elif P == 47:         self.execute(prefix1 + fdrPos[47][1] + ' ' + fdrPos[47][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[47][3] + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '47')     elif P == 48:         self.execute(prefix1 + fdrPos[48][1] + ' ' + fdrPos[48][2] + ' ' + endCoord)         self.execute(prefix2 + fdrPos[48][3] + feeds1)         self.execut(vacOn)         self.execute(pTime)         self.execute(retract)         self.execute(fdrCycl + '48')     return interpreter.INTERP_OK

just remember to remove the _11.03 to make it usable and give it permission to run as a program
 

Attachment not found

Please Log in to join the conversation.

More
4 years 2 months ago #249607 by Aciera
Thanks for sharing.
Surely there must be a way to build these commands in just one block using the value of parameter P?

Something along these lines:

self.execute(prefix1 + fdrPos[P][1] + ' ' + fdrPos[P][2] + ' ' + endCoord)
self.execute(prefix2 + fdrPos[P][3] + feeds1)
self.execute(vacOn)
self.execute(pTime)
self.execute(retract)
self.execute(fdrCycl + P)

Please Log in to join the conversation.

More
4 years 2 months ago #249610 by my1987toyota
Their may be . I am still very new to Python ( coding in general actually ) so I went with what I could get to work. LOL.
That said I will give your suggestion a try.

Please Log in to join the conversation.

More
4 years 2 months ago #249614 by my1987toyota

Aciera post=249607 userid=25994Thanks for sharing.
Surely there must be a way to build these commands in just one block using the value of parameter P?

Something along these lines:

self.execute(prefix1 + fdrPos[P][1] + ' ' + fdrPos[P][2] + ' ' + endCoord)
self.execute(prefix2 + fdrPos[P][3] + feeds1)
self.execute(vacOn)
self.execute(pTime)
self.execute(retract)
self.execute(fdrCycl + P)

When I tried that I got an error message of " typeError: list indices must be integers, not float"
I will grant that would have drastically shortened the amount of repetitive code .  it may need
to go through some " i = P " type conversion . I will play around with your suggestion and see
what I can come up with. Thank you Aciera.

Please Log in to join the conversation.

More
4 years 2 months ago #249634 by rodw
I feel your python pain. I wrote a small script this morning to read a csv file to help me do a bank reconciliation! In the process, I found a bug thats existed for a few years that is depressing our sales... It had always been resolved by increasing our merchant fee expense.

So now you are the expert, you can do the next step which is to access my credit card processor via their API to avoid having to download the csv file! (I use Odoo ERP for our accounting and its written in Python!)

Python is generally a typeless language but sometimes it needs the type to be defined. Something like:
myint = float(myfloatValue)
Have a read of this.  www.w3schools.com/python/python_casting.asp

Also, If the csv file needs to change during run time, you might be better off looking at a sql database (mysql is easy to integrate in python) By definition sql can be modified and queried dynamically.

Please Log in to join the conversation.

More
4 years 2 months ago #249636 by Aciera
When I tried that I got an error message of

" typeError: list indices must be integers, not float"


So, your parameter P is of type "float" and to use it in the list indices you need to change that to type "integer", ie whole numbers.
Since you should only be passing whole numbers as parameter P that would not pose a problem and you could just use the int() function.

Being lazy I would try to change   
Code:
P = words['p']
to this:   
Code:
P = int(words['p'])
And see what happens.

Please Log in to join the conversation.

More
4 years 2 months ago #249684 by my1987toyota
Special thanks to Aciera for the suggestions. I actually needed to convert the float to not just an integer but a string as well.
after some fiddling around with it and sorting through errors I have a working code that is substantially shorter then my last.
I will leave the readable text version and the python file in my next post . Have fun with it, I know I did.

Please Log in to join the conversation.

More
4 years 2 months ago #249685 by my1987toyota
the remap_12.01 in text
Code:
#remap version 12.01 import emccanon import sys import interpreter import csv             # These Global Variables are mainly intended for M201 but can be used elsewhere             # these codes can be changed to meet your machines need feeds1 = 'F150'    # feed value for Z to pick up part vacOn = 'M7'    # M_code for turning on vaccum pTime = 'G4 P0.5'   # G_code for pause time to allow vaccum to stabilize def M201(self, **words):     print(words)     P = words['p']       # outputs a Float value     i = int(words['p'])  # Converts Float value to integer     s = str(i)           # Converts integer to string     feedersFile = open('feeder_V1.04.csv')     coordinatesReader = csv.reader(feedersFile)     locationData = list(coordinatesReader)     fdrPos = locationData     if P < 0:         print("error, a P parameter must be passed")     if P > 0:         print('P = ' + s + '.0') # open linuxcnc in terminal to read the float output of 'P'         print('i = ' + s)        # open linuxcnc in terminal to read the integer output of 'i'         print('s = ' + s)        # open linuxcnc in terminal to read the string output of 's'         print('Going to Feeder' + ' ' + s)         self.execute('G53 G0' + ' ' + fdrPos[i][1] + ' ' + fdrPos[i][2] + ' ' + 'Z0.0 C0.0')         self.execute('G53 G1' + ' ' + fdrPos [i][3] + ' ' + feeds1)         self.execute(vacOn)         self.execute(pTime)         self.execute('G53 G1 Z0.0 C0.0' + ' ' + feeds1)         self.execute('M161 P' + s)     return interpreter.INTERP_OK

the python file

 

Attachment not found

Please Log in to join the conversation.

Time to create page: 0.155 seconds
Powered by Kunena Forum