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

More
4 years 2 months ago #249436 by my1987toyota
Nice work Andypugh . I haven't messed around with DictReader yet but today this code is what I was working on.
I haven't crossed it over to the M201 code remap yet. I did it this way to simplify preliminary testing.
I hope conversion into the M201 remap wont be too complicated. But it will be a lot of typing. I am sure these codes
scream noobie.

csv file in txt format
Code:
Feeder Number,X-axis,Y axis,Z-axis,C-axis Feeder 1,X0.125,Y-0.5118,Z-0.0,C0.0 Feeder 2,X0.125,Y-1.0235,Z-0.0,C0.0 Feeder 3,X0.125,Y-1.5354,Z-0.0,C0.0 Feeder 4,X0.125,Y-2.0472,Z-0.0,C0.0 Feeder 5,X0.125,Y-2.5590,Z-0.0,C0.0 Feeder 6,X0.125,Y-3.0708,Z-0.0,C0.0 Feeder 7,X0.125,Y-3.5826,Z-0.0,C0.0 Feeder 8,X0.125,Y-4.0994,Z-0.0,C0.0 Feeder 9,X0.125,Y-4.6062,Z-0.0,C0.0 Feeder 10,X0.125,Y-5.1180,Z-0.0,C0.0 Feeder 11,X0.125,Y-5.6298,Z-0.0,C0.0 Feeder 12,X0.125,Y-6.1416,Z-0.0,C0.0 Feeder 13,X0.125,Y-6.6534,Z-0.0,C0.0 Feeder 14,X0.125,Y-7.1652,Z-0.0,C0.0 Feeder 15,X0.125,Y-7.6770,Z-0.0,C0.0 Feeder 16,X0.125,Y-8.1888,Z-0.0,C0.0 Feeder 17,X0.125,Y-8.7006,Z-0.0,C0.0 Feeder 18,X0.125,Y-9.2124,Z-0.0,C0.0 Feeder 19,X0.125,Y-9.7242,Z-0.0,C0.0 Feeder 20,X0.125,Y-10.2360,Z-0.0,C0.0 Feeder 21,X0.125,Y-10.7478,Z-0.0,C0.0 Feeder 22,X0.125,Y-11.2596,Z-0.0,C0.0 Feeder 23,X0.125,Y-11.7714,Z-0.0,C0.0 Feeder 24,X0.125,Y-12.2832,Z-0.0,C0.0 Feeder 25,X15.875,Y-0.5118,Z-0.0,C0.0 Feeder 26,X15.875,Y-1.0235,Z-0.0,C0.0 Feeder 27,X15.875,Y-1.5354,Z-0.0,C0.0 Feeder 28,X15.875,Y-2.0472,Z-0.0,C0.0 Feeder 29,X15.875,Y-2.5590,Z-0.0,C0.0 Feeder 30,X15.875,Y-3.0708,Z-0.0,C0.0 Feeder 31,X15.875,Y-3.5826,Z-0.0,C0.0 Feeder 32,X15.875,Y-4.0994,Z-0.0,C0.0 Feeder 33,X15.875,Y-4.6062,Z-0.0,C0.0 Feeder 34,X15.875,Y-5.1180,Z-0.0,C0.0 Feeder 35,X15.875,Y-5.6298,Z-0.0,C0.0 Feeder 36,X15.875,Y-6.1416,Z-0.0,C0.0 Feeder 37,X15.875,Y-6.6534,Z-0.0,C0.0 Feeder 38,X15.875,Y-7.1652,Z-0.0,C0.0 Feeder 39,X15.875,Y-7.6770,Z-0.0,C0.0 Feeder 40,X15.875,Y-8.1888,Z-0.0,C0.0 Feeder 41,X15.875,Y-8.7006,Z-0.0,C0.0 Feeder 42,X15.875,Y-9.2124,Z-0.0,C0.0 Feeder 43,X15.875,Y-9.7242,Z-0.0,C0.0 Feeder 44,X15.875,Y-10.2360,Z-0.0,C0.0 Feeder 45,X15.875,Y-10.7478,Z-0.0,C0.0 Feeder 46,X15.875,Y-11.2596,Z-0.0,C0.0 Feeder 47,X15.875,Y-11.7714,Z-0.0,C0.0 Feeder 48,X15.875,Y-12.2832,Z-0.0,C0.0

python file in text format
Code:
import csv print(' Choose "P" number ') P = input() endCoord = 'Z0.0 C0.0' prefix1 = 'G53 G0 ' prefix2 = 'G53 G1 ' feeds1 = ' F150' vacOn = 'M7' pTime = 'G4 P0.5' retract = 'G53 G1 Z0.0 F150' feedersFile = open('feeder_V1.04.csv') coordinatesReader = csv.reader(feedersFile) locationData = list(coordinatesReader) # Side 1 feeders # the first number in exampleData is row and the second number is column if P.upper() == 'P1':     print(prefix1 + locationData[1][1] + ' ' + locationData[1][2] + ' ' + endCoord)     print(prefix2 + locationData[1][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P1') if P.upper() == 'P2':     print(prefix1 + locationData[2][1] + ' ' + locationData[2][2] + ' ' + endCoord)     print(prefix2 + locationData[2][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P2') if P.upper() == 'P3':     print(prefix1 + locationData[3][1] + ' ' + locationData[3][2] + ' ' + endCoord)     print(prefix2 + locationData[3][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P3') if P.upper() == 'P4':     print(prefix1 + locationData[4][1] + ' ' + locationData[4][2] + ' ' + endCoord)     print(prefix2 + locationData[4][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P4') if P.upper() == 'P5':     print(prefix1 + locationData[5][1] + ' ' + locationData[5][2] + ' ' + endCoord)     print(prefix2 + locationData[5][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P5') if P.upper() == 'P6':     print(prefix1 + locationData[6][1] + ' ' + locationData[6][2] + ' ' + endCoord)     print(prefix2 + locationData[6][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P6') if P.upper() == 'P7':     print(prefix1 + locationData[7][1] + ' ' + locationData[7][2] + ' ' + endCoord)     print(prefix2 + locationData[7][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P7') if P.upper() == 'P8':     print(prefix1 + locationData[8][1] + ' ' + locationData[8][2] + ' ' + endCoord)     print(prefix2 + locationData[8][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P8') if P.upper() == 'P9':     print(prefix1 + locationData[9][1] + ' ' + locationData[9][2] + ' ' + endCoord)     print(prefix2 + locationData[9][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P9') if P.upper() == 'P10':     print(prefix1 + locationData[10][1] + ' ' + locationData[10][2] + ' ' + endCoord)     print(prefix2 + locationData[10][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P10') if P.upper() == 'P11':     print(prefix1 + locationData[11][1] + ' ' + locationData[11][2] + ' ' + endCoord)     print(prefix2 + locationData[11][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P11') if P.upper() == 'P12':     print(prefix1 + locationData[12][1] + ' ' + locationData[12][2] + ' ' + endCoord)     print(prefix2 + locationData[12][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P12') if P.upper() == 'P13':     print(prefix1 + locationData[13][1] + ' ' + locationData[13][2] + ' ' + endCoord)     print(prefix2 + locationData[13][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P13') if P.upper() == 'P14':     print(prefix1 + locationData[14][1] + ' ' + locationData[14][2] + ' ' + endCoord)     print(prefix2 + locationData[14][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P14') if P.upper() == 'P15':     print(prefix1 + locationData[15][1] + ' ' + locationData[15][2] + ' ' + endCoord)     print(prefix2 + locationData[15][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P15') if P.upper() == 'P16':     print(prefix1 + locationData[16][1] + ' ' + locationData[16][2] + ' ' + endCoord)     print(prefix2 + locationData[16][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P16') if P.upper() == 'P17':     print(prefix1 + locationData[17][1] + ' ' + locationData[17][2] + ' ' + endCoord)     print(prefix2 + locationData[17][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P17') if P.upper() == 'P18':     print(prefix1 + locationData[18][1] + ' ' + locationData[18][2] + ' ' + endCoord)     print(prefix2 + locationData[18][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P18') if P.upper() == 'P19':     print(prefix1 + locationData[19][1] + ' ' + locationData[19][2] + ' ' + endCoord)     print(prefix2 + locationData[19][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 19') if P.upper() == 'P20':     print(prefix1 + locationData[20][1] + ' ' + locationData[20][2] + ' ' + endCoord)     print(prefix2 + locationData[20][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P20') if P.upper() == 'P21':     print(prefix1 + locationData[21][1] + ' ' + locationData[21][2] + ' ' + endCoord)     print(prefix2 + locationData[21][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P21') if P.upper() == 'P22':     print(prefix1 + locationData[22][1] + ' ' + locationData[22][2] + ' ' + endCoord)     print(prefix2 + locationData[22][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P22') if P.upper() == 'P23':     print(prefix1 + locationData[23][1] + ' ' + locationData[23][2] + ' ' + endCoord)     print(prefix2 + locationData[23][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P23') if P.upper() == 'P24':     print(prefix1 + locationData[24][1] + ' ' + locationData[24][2] + ' ' + endCoord)     print(prefix2 + locationData[24][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P24') # Side 2 feeders if P.upper() == 'P25':     print(prefix1 + locationData[25][1] + ' ' + locationData[25][2] + ' ' + endCoord)     print(prefix2 + locationData[25][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P25') if P.upper() == 'P26':     print(prefix1 + locationData[26][1] + ' ' + locationData[26][2] + ' ' + endCoord)     print(prefix2 + locationData[26][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P26') if P.upper() == 'P27':     print(prefix1 + locationData[27][1] + ' ' + locationData[27][2] + ' ' + endCoord)     print(prefix2 + locationData[27][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P27') if P.upper() == 'P28':     print(prefix1 + locationData[28][1] + ' ' + locationData[28][2] + ' ' + endCoord)     print(prefix2 + locationData[28][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P28') if P.upper() == 'P29':     print(prefix1 + locationData[29][1] + ' ' + locationData[29][2] + ' ' + endCoord)     print(prefix2 + locationData[29][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P29') if P.upper() == 'P30':     print(prefix1 + locationData[30][1] + ' ' + locationData[30][2] + ' ' + endCoord)     print(prefix2 + locationData[30][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P30') if P.upper() == 'P31':     print(prefix1 + locationData[31][1] + ' ' + locationData[31][2] + ' ' + endCoord)     print(prefix2 + locationData[31][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P31') if P.upper() == 'P32':     print(prefix1 + locationData[32][1] + ' ' + locationData[32][2] + ' ' + endCoord)     print(prefix2 + locationData[32][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P32') if P.upper() == 'P33':     print(prefix1 + locationData[33][1] + ' ' + locationData[33][2] + ' ' + endCoord)     print(prefix2 + locationData[33][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P33') if P.upper() == 'P34':     print(prefix1 + locationData[34][1] + ' ' + locationData[34][2] + ' ' + endCoord)     print(prefix2 + locationData[34][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P34') if P.upper() == 'P35':     print(prefix1 + locationData[35][1] + ' ' + locationData[35][2] + ' ' + endCoord)     print(prefix2 + locationData[35][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P35') if P.upper() == 'P36':     print(prefix1 + locationData[36][1] + ' ' + locationData[36][2] + ' ' + endCoord)     print(prefix2 + locationData[36][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P36') if P.upper() == 'P37':     print(prefix1 + locationData[37][1] + ' ' + locationData[37][2] + ' ' + endCoord)     print(prefix2 + locationData[37][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P37') if P.upper() == 'P38':     print(prefix1 + locationData[38][1] + ' ' + locationData[38][2] + ' ' + endCoord)     print(prefix2 + locationData[38][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P38') if P.upper() == 'P39':     print(prefix1 + locationData[39][1] + ' ' + locationData[39][2] + ' ' + endCoord)     print(prefix2 + locationData[39][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P39') if P.upper() == 'P40':     print(prefix1 + locationData[40][1] + ' ' + locationData[40][2] + ' ' + endCoord)     print(prefix2 + locationData[40][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P40') if P.upper() == 'P41':     print(prefix1 + locationData[41][1] + ' ' + locationData[41][2] + ' ' + endCoord)     print(prefix2 + locationData[41][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P41') if P.upper() == 'P42':     print(prefix1 + locationData[42][1] + ' ' + locationData[42][2] + ' ' + endCoord)     print(prefix2 + locationData[42][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P42') if P.upper() == 'P43':     print(prefix1 + locationData[43][1] + ' ' + locationData[43][2] + ' ' + endCoord)     print(prefix2 + locationData[43][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P43') if P.upper() == 'P44':     print(prefix1 + locationData[44][1] + ' ' + locationData[44][2] + ' ' + endCoord)     print(prefix2 + locationData[44][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P44') if P.upper() == 'P45':     print(prefix1 + locationData[45][1] + ' ' + locationData[45][2] + ' ' + endCoord)     print(prefix2 + locationData[45][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P45') if P.upper() == 'P46':     print(prefix1 + locationData[46][1] + ' ' + locationData[46][2] + ' ' + endCoord)     print(prefix2 + locationData[46][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P46') if P.upper() == 'P47':     print(prefix1 + locationData[47][1] + ' ' + locationData[47][2] + ' ' + endCoord)     print(prefix2 + locationData[47][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P47') if P.upper() == 'P48':     print(prefix1 + locationData[48][1] + ' ' + locationData[48][2] + ' ' + endCoord)     print(prefix2 + locationData[48][3] + feeds1)     print(vacOn)     print(pTime)     print(retract)     print('M161 P48')

csv code file in txt format . just reformat to csv to use.
 

File Attachment:

File Name: feeder_V1.04.txt
File Size:1.85 KB


python file

 

File Attachment:

File Name: example_cs...er_02.py
File Size:11.9 KB

 

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

More
4 years 2 months ago - 4 years 2 months ago #249437 by my1987toyota
For those curious about how I got the code this is based off of see the book " Automate the Boring Stuff With Python" by Al Sweigart
link below to his free to read online version of the book released under Creative Commons License. See chapter 16

automatetheboringstuff.com/#toc

 
Last edit: 4 years 2 months ago by my1987toyota. Reason: updating info
The following user(s) said Thank You: tjtr33, tommylight

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

More
4 years 2 months ago - 4 years 2 months ago #249536 by my1987toyota
Well looks like I still have a lot to learn. The code works great in Python ide's but I keep getting an error in Linuxcnc when I tried to cross it over.
The error is
UnboundLocalError.local variable 'prefix1'
referenced before assignment

first I will have to look up what that means and then rework the code to correct it. I will most likely need to check into what andypugh posted
about dict.reader and rewrite accordingly.

The Faulty code is below

 

File Attachment:

File Name: remap_10.py
File Size:15.14 KB


just remove the _10 if testing in Linuxcnc otherwise Python don't care.

the csv file is in text format . just replace the extension with csv to return it to a csv file
 

File Attachment:

File Name: feeder_V1....8-11.txt
File Size:1.85 KB
Last edit: 4 years 2 months ago by my1987toyota.

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

More
4 years 2 months ago #249546 by rodw
 
Try adding
Code:
prefix1='' prefix2=''
before your if statement
The errror message is telling you whats wrong.
You are creating your variables in the if statement.
in your elif statements you are trying to manipulate a variable that has not been created which obviously will not work
 
The following user(s) said Thank You: my1987toyota

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

More
4 years 2 months ago #249547 by rodw
Actually all of your variables are being defined inside the if statement. You likely need to move this block to be above the if statement.
Code:
# 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 ' # M_code for feeder advance
In fact, you probably would move it to be above this line so its only executed once (eg as global variables) instead of each time the procedure is called.
Code:
def M201(self, **words):
The following user(s) said Thank You: tommylight, my1987toyota

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

More
4 years 2 months ago - 4 years 2 months ago #249556 by my1987toyota
Hit the nail on the head rodw.
I actually had to chase down a few other error's as well in my above code to get it to work  You are right Rod The main
one was I had to move the csv read code to be a global instead of a local. ( I didn't know anyone had replied to
my above post until after I posted this post). I am glad I didn't have to do a complete rewrite of my python code.

I had to correct a small error in this post but apparently the forum software decided to completely butcher the codes posted.
I will be posting just the codes in the next reply.
Last edit: 4 years 2 months ago by my1987toyota. Reason: correcting the mess the forum software made of my post
The following user(s) said Thank You: tommylight, rodw

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

Time to create page: 0.289 seconds
Powered by Kunena Forum