OpenCV vision with EMC/linuxcnc

More
26 May 2012 10:37 #20413 by andypugh
edsimmons wrote:

I'm not loving the python-ness with opencv - c++ would be infinitely preferable!

Python might make interacting with GladeVCP easier though, if you want to make a UI.

found a sensible place to start - www.linuxcnc.org/docs/2.4/html/hal_comp.html#r1_13

I guess you can create a comp then look at the compiled code to see how to make pins, etc. But I don't think comp will compile userspace components in C++ (or C)

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

More
26 May 2012 11:45 - 02 Jul 2012 16:32 #20417 by ArcEye
Hi

This link is to a previously posted basic realtime example using comp, written in C
www.linuxcnc.org/index.php/english/compo...imit=6&start=6#18975

This is a userspace socket-client component, which reads values to and from a redis database server.
I used it to read values from components without connecting to them directly.

Ignore the comms specific code and should give an idea of the structure for a C userspace comp module
and some things you can do inside it.
component redisclient;

pin in bit B0 = false;
pin in bit B1 = false;
pin in bit B2 = false;
pin in bit B3 = false;
pin in bit B4 = false;
pin in bit B5 = false;

pin in s32 D0 = 0;
pin in s32 D1 = 0;
pin in s32 D2 = 0;
pin in s32 D3 = 0;
pin in s32 D4 = 0;
pin in s32 D5 = 0;

pin in float F0 = 0;
pin in float F1 = 0;
pin in float F2 = 0;
pin in float F3 = 0;
pin in float F4 = 0;
pin in float F5 = 0;

option userspace;
option singleton;     // can't have multiple components or pin names will collide 
                                  // and overwrite each other in redis

author "ArcEye ";
license "GPL";
;;

#include <stdio.h>    /* Standard input/output definitions */
#include <stdlib.h> 
#include <stdint.h>   /* Standard types */
#include <string.h>   /* String function definitions */
#include <unistd.h>   /* UNIX standard function definitions */
#include <fcntl.h>    /* File control definitions */
#include <errno.h>    /* Error number definitions */
#include <termios.h>  /* POSIX terminal control definitions */

#include <hiredis.h>  // hiredis header

#define PORTNUM 6379  // hard coded port default for redis

redisContext *context;
redisReply *reply;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds

char buff[256];

unsigned char emc_bits[6];
long int emc_s32s[6];
float emc_floats[6];


void user_mainloop(void) 
{
int x;
    
    context = redisConnectWithTimeout("localhost", 6379, timeout);
    if (context->err) 
        {
        printf("Connection error: %sn", context->errstr);
        exit(1);
        }
   
    while(1)
        {
        usleep(1000000);//approx 1 sec

        FOR_ALL_INSTS()
            {
            int i; 
        
            emc_bits[0]= B0; emc_bits[1]= B1; emc_bits[2]= B2; emc_bits[3]= B3; emc_bits[4]= B4; emc_bits[5]= B5;
            emc_s32s[0]= D0; emc_s32s[1]= D1; emc_s32s[2]= D2; emc_s32s[3]= D3; emc_s32s[4]= D4; emc_s32s[5]= D5;
            emc_floats[0]= F0; emc_floats[1]= F1; emc_floats[2]= F2; emc_floats[3]= F3; emc_floats[4]= F4; emc_floats[5]= F5;
                
            for (i = 0; i < 6; i ++)
                {
                reply = redisCommand(context,"SET B%d %d", i, emc_bits[i]);
                // printf("SET: %dn",emc_bits[i] );
                freeReplyObject(reply);
                }
            for (i = 0; i < 6; i ++)
                {
                reply = redisCommand(context,"SET D%d %d", i, emc_s32s[i]);
                // printf("SET: %ldn", emc_s32s[i]);
                freeReplyObject(reply);
                }
            for (i = 0; i < 6; i ++)
                {
                reply = redisCommand(context,"SET F%d %f", i, emc_floats[i]);
                // printf("SET: %fn", emc_floats[i]);
                freeReplyObject(reply);
                }
            }
        }           
}

regards
Last edit: 02 Jul 2012 16:32 by ArcEye.

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

More
26 May 2012 12:11 #20418 by edsimmons

Python might make interacting with GladeVCP easier though, if you want to make a UI.


Does using c++ prevent the use of a UI, or just mean I'd need python bindings to the bits of interest?

Looking in HAL in the source code, I see a couple of userspace c modules - the modbus and gs2_vfp both look interesting. Is this more the right place?

Thanks for your help Andy.

Cheers for the code arceye!

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

More
26 May 2012 12:33 #20420 by andypugh
edsimmons wrote:

Looking in HAL in the source code, I see a couple of userspace c modules - the modbus and gs2_vfp both look interesting. Is this more the right place?

Sounds likely, but your guess is as good as mine from this point :-)

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

More
26 May 2012 14:59 #20422 by edsimmons

Sounds likely, but your guess is as good as mine from this point


Heh - well I'm there now - by the looks of it I can do what I want with comp.

This link is to a previously posted basic realtime example using comp, written in C
www.linuxcnc.org/index.php/english/compo...=6&start=6#18975

This is a userspace socket-client component, which reads values to and from a redis database server.
I used it to read values from components without connecting to them directly.
...snip


Thanks for this - it looks like it makes things very much easier for me. I'll see what can be done with making my routine in plain c++ outside linuxcnc then move it into a comp when it's sensible to start testing it.
Looking at the structure of the comp you posted, I can code it to run in a tight(ish) loop and only perform anything intensive when required, is this permissible or will this have a bad impact on system load? Obviously I'd like to shoot for less possible jitter than 1second. I'm sure this is fine, just wanted to make sure...

Andy, please could you try to clarify the problems I might run into making gladeVCP controls for this if I code the hal module in C++?

Thanks!
Ed

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

More
26 May 2012 15:19 #20424 by andypugh
edsimmons wrote:

Looking at the structure of the comp you posted, I can code it to run in a tight(ish) loop and only perform anything intensive when required, is this permissible or will this have a bad impact on system load?

I don't know what userspace routines do to hand time back to the system, though I guess that is the usleep.
However the realtime tasks will get run regardless.

Andy, please could you try to clarify the problems I might run into making gladeVCP controls for this if I code the hal module in C++?

Purely speculation, really, as all the code embedded in the GladeVCP widgets is Python.

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

More
26 May 2012 15:56 - 26 May 2012 16:17 #20425 by ArcEye
Hi

I don't know what userspace routines do to hand time back to the system, though I guess that is the usleep.
However the realtime tasks will get run regardless.

As Andy said, the usleep will give the CPU to other processes , so CPU time isn't wasted.

With the real-time scheduling kernel, a user-space program is not going to slow the system, the scheduler will always give priority to the real-time processes at the expense of user-space.

There is likely to be a quite finite amount your code can do at each poll, the example fetched 18 items of data and populated the 18 keys in the database.
So yes, if you want to complete each poll, it needs to be quite tight.

The alternative is the 'state' approach used a lot in real-time components, where each poll executes a stage and sets a flag as to which state it is at, for the next poll to pick up from.
This way you can achieve quite drawn out procedures (waiting for a signal etc) efficiently.

You can see the pure C code that comp saves you the trouble of writing, by running comp redisclient.comp.
I haven't found any need to write a module from scratch in C yet. comp is much more convenient and productive.

Andy, please could you try to clarify the problems I might run into making gladeVCP controls for this if I code the hal module in C++?
Purely speculation, really, as all the code embedded in the GladeVCP widgets is Python.

It's a big bug bear of mine that everything is so heavily reliant upon python.

However it does not really matter how the pins and params are created, they will be able to be interfaced with gladeVCP or pyVCP.
So long as you don't mind making the UI in a python based system, your modules will work with it, whatever language they were written in.


regards
Last edit: 26 May 2012 16:17 by ArcEye.

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

More
27 May 2012 06:59 #20440 by edsimmons
Thanks for the clarification - I'm pretty sure all this is possible with the right approach.

FPGA/CPLD state machine territory seems most apt for 'thought reuse.'
Am I missing the point here, if I write a loop that does nothing but check the state of pins as fast as possible then sleep (most of the time, especially during motion) and on certain conditions carries out an action, I'm not particularly worried about the time it takes to complete the action since the motion will be waiting for the outcome of the vision module. Or will a delay here screw things up?

I'm glad that the UI is possible, shame it's all python ;)

Thanks for all the input :)

Ed

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

More
27 May 2012 10:55 #20443 by ArcEye
Hi

Am I missing the point here, if I write a loop that does nothing but check the state of pins as fast as possible then sleep (most of the time, especially during motion) and on certain conditions carries out an action, I'm not particularly worried about the time it takes to complete the action since the motion will be waiting for the outcome of the vision module. Or will a delay here screw things up?

I am not familiar with the specifics of what you are trying to achieve.

User-space components are slower and not suited for interaction with other real-time processes, if a fast turn round is required, but they do allow an interface with the 'real world' using all the normal libraries etc., which is their big advantage.

All components can all be linked via the pins they expose, this is the strength of the hal system.
You can fetch data from user space and make it available to real-time modules by linking via pins.

Conversely the redis-client module links to real-time modules to fetch data, then passes the data to a database server, through which a normal user-space GUI program can access data values from within the running LinuxCNC system.

regards

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

More
27 May 2012 11:57 #20446 by cncbasher
this is one i'm looking at Ed , although it's all java
code.google.com/p/openpnp/

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

Time to create page: 0.140 seconds
Powered by Kunena Forum