« Posts list

Spindle speed control using LinuxCNC 2.7 with a Huanyang inverter

Huanyang branded VFD drives are ubiquitous on eBay and other sites like AliExpress. I bought one some time ago with a 1.5KW spindle and have been controlling the speed manually with the difficult to use control panel on the front. It is, however, possible to control the VFD from within LinuxCNC using the M3 and M5 commands (I haven't been able to get M4, reverse rotation, working yet). What's also neat is we can get the machine to wait for the spindle to come up to speed before moving to the next line of GCode.

The first thing I tried was PDM using signals from the parallel port. This doesn't set the speed accurately at all and the PDM/speed conversion curve is hilariously inaccurate. If you want to try this method for whatever reason, there's a guide in the LinuxCNC documentation.

Most Huanyang VFDs have an RS-485 two-wire interface, so let's use this to communicate with the drive from LinuxCNC. These inverters supposedly support Modbus but aren't compliant, so a custom HAL component for LinuxCNC is required. Since LinuxCNC 2.7, this component is now bundled with the default image as a HAL component, so all we need to do is set it up properly. Easy!

Hardware required

VFD configuration

We first need to change some settings in the VFD. Most of these settings are identical to those defined in the hy_vfd man page so look there if you need more information. Running hy_vfd --help from a terminal will give you the same helptext.

Change the following registers to make the VFD listen on the RS-485 bus for control signals:

Check your VFD manual for other values for these registers. I've included a PDF of the manual [here]({{ site.files }}/hy-vfd-manual.pdf) if you've lost the one included with your VFD.

LinuxCNC Configuration

For completeness' sake, I'm making available my entire machine config [here]({{ site.files }}/hy-vfd-config-rs485.zip) to help make configuring the VFD clearer if you need it. Read on for step by step config instructions and make reference to the config files in the download if need be. It's provided purely as an example of what to add to it to get the Huanyang VFD working, so don't use it on your machine verbatim.

I didn't need most of the signals made available by the hy_vfd module so I didn't add them to the configuration. You can add them yourself by looking at the HAL meter in LinuxCNC for the names.

Note: Using stepconf again will overwrite some of your custom settings. The INI and other config files will have to be edited by hand, which is not that hard. You can always use stepconf to test things without saving and then edit the files manually later.

custom.hal

# Include your customized HAL commands here
# This file will not be overwritten when you run stepconf again

# Load the Huanyang VFD user component
loadusr -Wn spindle-vfd hy_vfd -n spindle-vfd -t 1 -d /dev/ttyUSB0 -p none -r 38400 -s 1

#net vfd-comms halui.machine.is-on => spindle-vfd.enable
setp spindle-vfd.enable 1
net spindle-fwd motion.spindle-forward => spindle-vfd.spindle-forward
net spindle-reverse motion.spindle-reverse => spindle-vfd.spindle-reverse
net spindle-speed-cmd  motion.spindle-speed-out-abs => spindle-vfd.speed-command
net spindle-on motion.spindle-on => spindle-vfd.spindle-on
net spindle-at-speed motion.spindle-at-speed => spindle-vfd.spindle-at-speed

It's worth explaining this line in more detail:

loadusr -Wn spindle-vfd hy_vfd -n spindle-vfd -t 1 -d /dev/ttyUSB0 -p none -r 38400 -s 1

All options for the hy_vfd command are explained in the user manual.

custom_postgui.hal

# Include your customized HAL commands here
# The commands in this file are run after the AXIS GUI (including PyVCP panel) starts

net spindle-at-speed => pyvcp.spindle-at-speed
net pyvcp-spindle-rpm spindle-vfd.spindle-speed-fb => pyvcp.spindle-speed
net pyvcp-modbus-ok spindle-vfd.hycomm-ok => pyvcp.hycomm-ok

custompanel.xml

We want/need to display some info about the VFD in the LinuxCNC interface, so we'll write a PyVCP panel to do so.

This panel is a stripped down version of the default supplied in the original hy-vfd module. There is a downloadable ZIP in the original forum thread containing the complete panel XML if you wish to set up extra fields. Note that you'll have to add more signals in your .hal files, and change the signal names in the PyVCP XML.

All I need from the panel is the spindle RPM, a spindle-at-speed indicator and a Modbus comm OK light. It currently looks like this (panel on the right):

LinuxCNC with custom Huanyang panel screenshot

And here's the PyVCP XML to generate it:

<?xml version='1.0' encoding='UTF-8'?>
<pyvcp>
	<labelframe text="Huanyang VFD">
		<font>("Helvetica",12)</font>
		<table>
			<tablerow/>
			<tablespan columns="2" />
			<tablesticky sticky="nsew" />
			<label>
				<text>" "</text>
				<font>("Helvetica",2)</font>
			</label>
			<tablerow/>
			<tablesticky sticky="w" />
			<label>
				<text>"Modbus Communication:"</text>
			</label>
			<tablesticky sticky="e" />
			<led>
				<halpin>"hycomm-ok"</halpin>
				<size>"10"</size>
				<on_color>"green"</on_color>
				<off_color>"red"</off_color>
			</led>
			<tablerow/>
			<tablesticky sticky="w" />
			<label>
				<text>"Spindle at speed:"</text>
			</label>
			<tablesticky sticky="e" />
			<led>
				<halpin>"spindle-at-speed"</halpin>
				<size>"10"</size>
				<on_color>"green"</on_color>
				<off_color>"red"</off_color>
			</led>
			<tablerow/>
			<label>
				<text>" "</text>
			</label>
		</table>
		<table>
			<tablesticky sticky="nsew" />
			<tablerow/>
			<tablesticky sticky="nsew" />
			<label>
				<text>"Spindle Speed (RPM)"</text>
				<font>("Helvetica",10)</font>
			</label>
			<tablerow/>
			<tablesticky sticky="nsew" />
			<label>
				<text>" "</text>
				<font>("Helvetica",2)</font>
			</label>
			<tablerow/>
			<tablesticky sticky="nsew" />
			<bar>
				<halpin>"spindle-speed"</halpin>
				<max_>24000</max_>
			</bar>
		</table>
	</labelframe>
</pyvcp>

It would be cool to have a tachometer type gauge and perhaps some more info, but this shows all the information I need.

Machine.ini

We need to get LinuxCNC to load the custom panel and wiring. To do so, modify the machine .ini file. This is the .ini file inside the folder created by Stepconf. If your machine is called Allie like mine, this will be Allie/Allie.ini.

Find the [DISPLAY] section and add the following line to load the custom control panel (assuming you called the file custompanel.xml):

PYVCP = custompanel.xml

Next, find the [HAL] section and add these lines if they're not already present:

HALFILE = custom.hal
POSTGUI_HALFILE = custom_postgui.hal

Usage

Final notes and gotchas