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

More
4 years 2 months ago - 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.
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 #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.
The following user(s) said Thank You: tommylight

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

More
4 years 2 months ago - 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.
Last edit: 4 years 2 months ago by rodw.
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 - 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.
Last edit: 4 years 2 months ago by Aciera. Reason: Editor messup
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 #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 or Create an account 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

 

File Attachment:

File Name: remap_12.01.py
File Size:1.5 KB
The following user(s) said Thank You: rodw

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

Time to create page: 0.290 seconds
Powered by Kunena Forum