Integrate new driver to linuxCNC

More
24 Aug 2012 11:48 - 24 Aug 2012 11:51 #23616 by t1m0n
Hi, I reat something about hal, halpins, hal components,.. but I don't understand how I can integrate a new driver.

At the moment i use an own application (prog.c) which use a driver in userspace and a seperate library for communication with test boards over sercos.
These support functions for start bus communication, write and read. In prog.c I use only these functions for communication.

For example: I set an array:
array_OUT[4] = 255;
after write(); the testboard get this value and the LEDs on Byte4 are on.

But now, I want to use the PLC in LinuxCNC instead prog.c.
I want to set the array_OUT[4] from the PLC by wire diagramm.

What is the right way to integrate these functions for bus communication in LinuxCNC?

My idea is a driver-sercos.c which works like prog.c but the different is that there is a big list of variable declarations like:
array_IN[0] = plc_pin_0_in;
array_IN[1] = plc_pin_1_in;
array_IN[2] = plc_pin_2_in;
array_OUT[0] = plc_pin_0_out;
array_OUT[1] = plc_pin_1_out;
array_OUT[2] = plc_pin_2_out;
Then driver-sercos.c start communication and write the values. Thats my idea but I don't know how I can realise this.
Last edit: 24 Aug 2012 11:51 by t1m0n.

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

More
24 Aug 2012 11:57 #23617 by andypugh
t1m0n wrote:

Hi, I reat something about hal, halpins, hal components,.. but I don't understand how I can integrate a new driver.

It probably helps to relise that a HAL "pin" is just an are of shared-memory that can contain one of the 4 types of data supported by HAL.
When you link pins together in HAL all it means is that different blocks of code in different files access the same memory address, generally using a different variable name.

But now, I want to use the PLC in LinuxCNC instead prog.c.
I want to set the array_OUT[4] from the PLC by wire diagramm.

Are you sure you want to use the PLC (classic Ladder) for this, and not a realtime HAL component?

I think that even if you do want to use Classic Ladder you will need a Realtime HAL component too.

My idea is a driver-sercos.c which is like prog.c but there is a big list of variable declarations like:
array_IN[0] = plc_pin_0_in;
array_IN[1] = plc_pin_1_in;
array_IN[2] = plc_pin_2_in;
array_OUT[0] = plc_pin_0_out;
array_OUT[1] = plc_pin_1_out;
array_OUT[2] = plc_pin_2_out;
Then driver-sercos.c start communication and write the values. Thats my idea but I don't know how I can realise this.


I think what you want to do should be relatively easy using comp:
linuxcnc.org/docs/html/hal/comp.html

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

More
24 Aug 2012 12:24 #23619 by t1m0n
Is Classic Ladder no realtime component?
The Classic Ladder was my first choice for a LinuxCNC Tool which I can use to get values from LinuxCNC.
I saw the examples and discreptions on linuxcnc.org/docs/html/hal/comp.html but these are not very helpfull.
I think the best is "14.3. out8" but all of these are not helpfull for my problem.

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

More
24 Aug 2012 12:36 #23620 by andypugh
t1m0n wrote:

Is Classic Ladder no realtime component?

There are two parts to it, a realtime part and a userspace part. I don't know a lot more about it than that, though
I am more used to C, so write custom HAL components when needed.

I think the best is "14.3. out8" but all of these are not helpfull for my problem.

I admit I am not entirely sure what your problem is.
I think you should be able to put your existing code in the code section of a comp, add a bunch of pins at the top, and use it in HAL.
However, your description of the architecture is still a bit sketchy.

I think that Sercos / LinuxCNC might already be a solved problem.
www.bitmuster.org/projects/emc.html

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

More
24 Aug 2012 13:01 - 24 Aug 2012 13:03 #23621 by t1m0n
Yes, with sercos3 the problem is solved with this methode.
But I use a special card which is able to change the bus system by some modifications.
Later, I want to use different bus systems. Then there is no different between sercos, ethercat or profibus because this is the job of the hardware and firmware, not HAL.

Let's assume I do this with comp.
I have to understand one example ;)
The way is to write a programm in c and then modify it for comp.
The example: 14.2. sincos:
component sincos;
pin out float sin_;
pin out float cos_;
pin in float theta;
function _;
license "GPLv2 or later";
;;
#include <rtapi_math.h>
FUNCTION(_) { sin_ = sin(theta); cos_ = cos(theta); }

sin(theta): This is a c-function, right? Where is theta defined?

The FUNCTION(_) is there because this it is required for comp that it knows there is a c-function?

sin_: this is the name of the variable inside this driver and outside sincos.<num>.sin?
How I can set this variable from PLC- or is it known in LinuxCNC (i.e. Classic Ladder) during the declaration pin out float sin_; ?
What can I do with <num>? In

The last question: why rtapi_math.h instead of math.h ?
Last edit: 24 Aug 2012 13:03 by t1m0n.

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

More
25 Aug 2012 00:13 #23644 by andypugh
t1m0n wrote:

pin in float theta;

sin(theta): This is a c-function, right? Where is theta defined?

In the pin in float theta line.
It might be helpful to look at the generated C-code that comp creates, you will see that "theta" gets macro-substituted to something like inst[n]->theta

The FUNCTION(_) is there because this it is required for comp that it knows there is a c-function?

That is another macro-substitution in the generated C, the function name and the parameters that need to be passed to each instance.

Part of what comp does is deal with the complexity of the possibility of there being very many instances of each component loaded, all of which need an independent internal state.

sin_: this is the name of the variable inside this driver and outside sincos.<num>.sin?

Yes, it's a pin name, and also a variable name inside the C code section. Writing a value to sin_ in the C-code will put that value on the HAL pin for other components to use. The trailing _ is to differentiate from the built-in function but is removed when naming the HAL pin.

[quoteHow I can set this variable from PLC[/quote]You can't. it's an Output pin from the HAL component. You could read it into a Classic Ladder system, by a net command in the HAL file that connected the comp output to one of the Ladder inputs.

why rtapi_math.h instead of math.h ?

rtapi_math defines a set of thread-safe mathematical functions guaranteed to work in realtime.

I have a feeling that you are missing something fundamental about HAL, but I am not sure what it is so can't clear it up.

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

More
27 Aug 2012 08:51 #23678 by t1m0n

why rtapi_math.h instead of math.h ?

rtapi_math defines a set of thread-safe mathematical functions guaranteed to work in realtime. [/quote]
I think this will be my next problem because I want to use RT_PREEMPT instead of rtai to realize realtime. This is nessecary because the required driver and library don't use rtai_libs.

I have a feeling that you are missing something fundamental about HAL, but I am not sure what it is so can't clear it up.

Yes, thats feeling is right ;( but I want to understand it.

summary:
I want to use C-Code in linuxcnc
but you say that it is easier to generate a C-Code with Comp HAL Component Generator

With RT_PREEMPT it is possible to use userspace in realtime.
Than I can use chapter 13: linuxcnc.org/docs/html/hal/comp.html#_co...side_the_source_tree
But this only works for .comp files, not for .c files.

My sourcecode (*.c-Files) consists of 500 lines sourcecode. When I modified it to a comp-File and compiled it. The sourcecode is much bigger?
I use special-functions from a library which is from the customer of my hardware. Does the comp-compiler know where the library is? (gcc knows it because I compile with a big make-file).

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

More
27 Aug 2012 13:43 #23682 by ArcEye

My sourcecode (*.c-Files) consists of 500 lines sourcecode. When I modified it to a comp-File and compiled it. The sourcecode is much bigger?

Yeah, comp does bloat the binary, I only use it to compile actual .comp files, although you can use it for more.

I use special-functions from a library which is from the customer of my hardware. Does the comp-compiler know where the library is? (gcc knows it because I compile with a big make-file).

comp has no idea what special libraries you need.
If I want to link against a particular library, I normally just cut and paste the build string from comp that just failed ( because of unresolved references that are in the library ).
Then I add in a -L/address/of/library and -lmylibrary to the string and run that instead of comp, quicker than editing comp or writing a new makefile

regards

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

More
27 Aug 2012 16:27 - 27 Aug 2012 16:33 #23686 by ArcEye

If I want to link against a particular library, I normally just cut and paste the build string from comp that just failed ( because of unresolved references that are in the library ).
Then I add in a -L/address/of/library and -lmylibrary to the string and run that instead of comp, quicker than editing comp or writing a new makefile

I forgot I actually have a script set up to do this, amongst many other things, called pro
The relevant fragment is

# !/bin/bash
case $1 in

help) cat /pro | less;;
.
.
.
comp ) comp $2.comp

gcc -Os -g -I. -I/usr/realtime-2.6.32-122-rtai/include -I. -I/usr/realtime-2.6.32-122-rtai/include -D_FORTIFY_SOURCE=0 \
-mhard-float -DRTAI=3 -fno-fast-math -mieee-fp -fno-unsafe-math-optimizations -DRTAPI -D_GNU_SOURCE -Drealtime\
-D_FORTIFY_SOURCE=0 -D__MODULE__ -I/usr/include/emc2 -Wframe-larger-than=2560 -URTAPI -U__MODULE__ -DULAPI -Os \
-o $2 $2.c -Wl,-rpath,/lib -L/lib -lemchal -l$3;;
esac

So pro comp myfile special_lib
will produce a C file from myfile.comp then compile myfile.c, outputting the binary myfile which is additionally linked against special_lib

regards

NB There are of course bash 'end of line' \ chars in this, but the forum does funny things to lots of special characters, it thinks are escape sequences
Hopefully I have managed to put them back again by double 's
Last edit: 27 Aug 2012 16:33 by ArcEye.

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

More
27 Aug 2012 21:46 #23697 by andypugh
t1m0n wrote:

rtapi_math defines a set of thread-safe mathematical functions guaranteed to work in realtime. [/quote]
I think this will be my next problem because I want to use RT_PREEMPT instead of rtai to realize realtime. This is nessecary because the required driver and library don't use rtai_libs.[/quote]Note that it is rtapi_math, not rtai_math.
rtapi tries to create a common interface for rt-linux and rtai. I think (but I am not sure) that it also does the same for rt-preempt.

With RT_PREEMPT it is possible to use userspace in realtime.

Are you sure about that? I haven't looked at rt-preempt, but I would be surprised if you could simply run normal code as realtime code. Most realtime code is written to run to completion every time the thread it is running in is polled (every module linked to the servo thread, for example, is run in sequence every millisecond. The means no wait-loops in the code, or the whole machine stops dead.

I use special-functions from a library which is from the customer of my hardware. Does the comp-compiler know where the library is? (gcc knows it because I compile with a big make-file).

You can use #include in the C-code part of a comp to link to external files, or the "include" instruction in the part above the ;; sequence if it is important that the types be included during the pin definition phase. (it might help to look at the intermediate C-code to see why this matters).

Note that when the C-code is compiled it isn't in the same place as the .comp file. Another reason to comp into intermediate C first is to see where the files are put before being passed to the compiler in order to work out how to set up relative include paths. (This really could do with a proper fix)

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

Time to create page: 0.132 seconds
Powered by Kunena Forum