Linuxcnc and Python
- btaindustrial
- Offline
- New Member
Less
More
- Posts: 6
- Thank you received: 0
22 Nov 2013 02:11 - 22 Nov 2013 03:56 #41086
by btaindustrial
Linuxcnc and Python was created by btaindustrial
hello
I did a serial communication with python, it works!
but i dont know how to make it interact with the linuxcnc
how do I get it to work with linuxcnc?
example:
to enter the MDI "m3 sxxx"
take the value of "s" should be passed to the python
python and make communication with the VFD
#! /usr/bin/python
import linuxcnc
import minimalmodbus
vfd_sinus = minimalmodbus.Instrument('/dev/ttyS0', 1)
s = linuxcnc.stat()
s.poll()
#spindle speed
if s.spindle_speed < 0:
set_rpm = ((s.spindle_speed**-1)/0.30)
else :
set_rpm = (s.spindle_speed/0.30)
vfd_sinus.write_register(4, set_rpm, 0)
# on/off spindle
onoff = vfd_sinus.read_register(5, 0)
bin_onoff = bin(onoff)
spindle_cmd = s.mcodes[2]
if spindle_cmd == 3 :
set_onoff = int(bin_onoff[2:12] + '0' + '1'+ '0', 2)
if spindle_cmd == 4 :
set_onoff = int(bin_onoff[2:12] + '1' + '0'+ '0', 2)
if spindle_cmd == 5 :
set_onoff = int(bin_onoff[2:12] + '0' + '0'+ '1', 2)
vfd_sinus.write_register(5, set_onoff, 0)
I did a serial communication with python, it works!
but i dont know how to make it interact with the linuxcnc
how do I get it to work with linuxcnc?
example:
to enter the MDI "m3 sxxx"
take the value of "s" should be passed to the python
python and make communication with the VFD
#! /usr/bin/python
import linuxcnc
import minimalmodbus
vfd_sinus = minimalmodbus.Instrument('/dev/ttyS0', 1)
s = linuxcnc.stat()
s.poll()
#spindle speed
if s.spindle_speed < 0:
set_rpm = ((s.spindle_speed**-1)/0.30)
else :
set_rpm = (s.spindle_speed/0.30)
vfd_sinus.write_register(4, set_rpm, 0)
# on/off spindle
onoff = vfd_sinus.read_register(5, 0)
bin_onoff = bin(onoff)
spindle_cmd = s.mcodes[2]
if spindle_cmd == 3 :
set_onoff = int(bin_onoff[2:12] + '0' + '1'+ '0', 2)
if spindle_cmd == 4 :
set_onoff = int(bin_onoff[2:12] + '1' + '0'+ '0', 2)
if spindle_cmd == 5 :
set_onoff = int(bin_onoff[2:12] + '0' + '0'+ '1', 2)
vfd_sinus.write_register(5, set_onoff, 0)
Last edit: 22 Nov 2013 03:56 by btaindustrial.
Please Log in or Create an account to join the conversation.
22 Nov 2013 20:42 #41114
by andypugh
Replied by andypugh on topic Linuxcnc and Python
I would rather expect that to work.
The other way might be to write a userspace HAL component in Python.
The other way might be to write a userspace HAL component in Python.
Please Log in or Create an account to join the conversation.
23 Nov 2013 03:15 #41120
by nkp
Replied by nkp on topic Linuxcnc and Python
can do so:
minimod_EMC.py
=================
to HAL file :
loadusr python minimod_EMC.py
ps tried it with arduino
minimod_EMC.py
Warning: Spoiler!
#!/usr/bin/python
import linuxcnc
import minimalmodbus
import time
vfd_sinus = minimalmodbus.Instrument('/dev/ttyACM0', 1)
vfd_sinus.serial.baudrate = 19200
s = linuxcnc.stat()
try:
while 1:
s.poll()
#spindle speed
if s.spindle_speed < 0:
set_rpm = ((s.spindle_speed * -1)/0.30)
else :
set_rpm = (s.spindle_speed/(0.30))
vfd_sinus.write_register(1, 251, 0)
# on/off spindle
onoff = vfd_sinus.read_register(1, 0)
bin_onoff = bin(onoff)
spindle_cmd = s.mcodes[2]
if spindle_cmd == 3 :
set_onoff = int(bin_onoff[2:12] + '0' + '1'+ '0', 2)
if spindle_cmd == 4 :
set_onoff = int(bin_onoff[2:12] + '1' + '0'+ '0', 2)
if spindle_cmd == 5 :
set_onoff = int(bin_onoff[2:12] + '0' + '0'+ '1', 2)
else:
set_onoff = 0
vfd_sinus.write_register(2, set_onoff, 0)
time.sleep(0.1)
except KeyboardInterrupt:
raise SystemExit
import linuxcnc
import minimalmodbus
import time
vfd_sinus = minimalmodbus.Instrument('/dev/ttyACM0', 1)
vfd_sinus.serial.baudrate = 19200
s = linuxcnc.stat()
try:
while 1:
s.poll()
#spindle speed
if s.spindle_speed < 0:
set_rpm = ((s.spindle_speed * -1)/0.30)
else :
set_rpm = (s.spindle_speed/(0.30))
vfd_sinus.write_register(1, 251, 0)
# on/off spindle
onoff = vfd_sinus.read_register(1, 0)
bin_onoff = bin(onoff)
spindle_cmd = s.mcodes[2]
if spindle_cmd == 3 :
set_onoff = int(bin_onoff[2:12] + '0' + '1'+ '0', 2)
if spindle_cmd == 4 :
set_onoff = int(bin_onoff[2:12] + '1' + '0'+ '0', 2)
if spindle_cmd == 5 :
set_onoff = int(bin_onoff[2:12] + '0' + '0'+ '1', 2)
else:
set_onoff = 0
vfd_sinus.write_register(2, set_onoff, 0)
time.sleep(0.1)
except KeyboardInterrupt:
raise SystemExit
=================
to HAL file :
loadusr python minimod_EMC.py
ps tried it with arduino
The following user(s) said Thank You: btaindustrial
Please Log in or Create an account to join the conversation.
Time to create page: 0.069 seconds