[Home]HowToWriteNewHalComponent

LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org

hal_joystick has been depreciated from EMC 2.4

How to write a new hal component

If you don't like the encoder-decoder method because you think it is a too complicated way to do a simple task, here you find instructions on how to create a new hal component that directly perform the task. New EMC2.1 provides a very useful tool called comp. You can read about it in the [Hal manual]. Thank to comp you can create new hal components in a very simply and quickly way.

We need a component that convert a float value (joypad axis) in an integer counter (jog counts). So if we move our joypad and keep it in place so that, for example, the output is 0.03, our component has to update the counter increasing it. We also want the component to increase or decrease the counter with a speed proportional to the joypad axis value. So the machine movements will be proportional to the joypad movements.

The code to do that is very simply, you can download the source [here]. Let's explain it:

  01: component joycounts;
  02: pin in float axisvalue;
  03: pin out s32 counts;
  04: param rw float scale=10;
  05: param r float fcounts=0;
  06: function updatecounts fp;
  07: ;;

the first 6 line are the real time component definitions. Name, pins, parameters and functions. Our component joycounts will have two pins, a float input pin (for the joypad axis value) and a signed integer output (for the jog-counts). Also it will have a scale parameter (default value 10) and the read only parameter fcounts that will serve in the next code as a float point counter. The component will have only one function to update the counter.

  08: #include <rtapi_math.h>
  09: FUNCTION(updatecounts){
  10:      fcounts=fcounts+scale*axisvalue;
  11:	   counts=floor(fcounts);
  12:	}
Math functions are required because we will use the floor function. Every cycle of the thread wich the updatecounts function is attached to, the float point counter will be updated adding the value read from the axisvalue pin scaled using the scale parameter. But we need an integer counter to send its value to axis jog-counts pin. So we will give the floor value of fcounts to the counts integer pin. That's all.

To compile and install the component, just place it for example in your home folder, open a terminal and type:

  $ sudo comp --install joycounts.comp

Now you can load the component when you need it. Note: In order to compile comp modules you must install the packages emc2-dev and build-essential.

Let's examine now the hal file that uses this new component:

First load three joycounts components for X Y and Z:

  loadrt joycounts count=3

Link the joypad axis pins to the joycounts input pins:

  net velX joypad.axis.0 => joycounts.0.axisvalue
  net velY joypad.axis.1 => joycounts.1.axisvalue
  net velZ joypad.axis.3 => joycounts.2.axisvalue

Link the joycounts output to the axis jog-counts pin for X Y Z:

  net countX joycounts.0.counts => axis.0.jog-counts
  net countY joycounts.1.counts => axis.1.jog-counts
  net countZ joycounts.2.counts => axis.2.jog-counts

Enable jog for X Y and Z:

  setp axis.0.jog-enable TRUE
  setp axis.1.jog-enable TRUE
  setp axis.2.jog-enable TRUE

Set the scale value of the joycounts component for X Y and Z (a negative value will invert the axis direction):

  setp joycounts.0.scale 0.2
  setp joycounts.1.scale -0.2
  setp joycounts.2.scale 0.2

Attach realtime functions to threads:

  addf joycounts.0.updatecounts servo-thread
  addf joycounts.1.updatecounts servo-thread
  addf joycounts.2.updatecounts servo-thread

Now you can use this hal file to jog with your joypad. The complete hal file that uses the joycounts (includes the buttons functions) is downloadable [here]

Note added by Patrick Robin on Feb 9 2009: On my system, I had to add a line after the loadrt command in the file linked above to make it work. It now looks like this at the beginning of the file:

loadusr hal_joystick -d /dev/input/js0 -p joypad
loadusr -w sleep 3
loadrt joycounts count=3

This avoids a race condition were axis.0 is not yet available for commands that come after

[BACK]


LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org
This page is read-only. Follow the BasicSteps to edit pages. | View other revisions
Last edited July 7, 2012 7:22 pm by Archivist (diff)
Search:
Published under a Creative Commons License