NEWS
LinuxCNC 2.5.2 Release
There are no translations available.

LinuxCNC 2.5.2 Update Released (changelog).
 
LinuxCNC 2.5.1 Release
There are no translations available.

LinuxCNC 2.5.1 Update Released (changelog). If the Package Manager does not prompt you to upgrade see this page.

 
LinuxCNC 2.5.0 Release
There are no translations available.

New major release (changelog). See the instructions to update your system from EMC 2.4 to LinuxCNC 2.5.
 
Home Forum Configuring LinuxCNC Advanced Configuration OpenCV vision with EMC/linuxcnc

Welcome, Guest
Username: Password: Remember me

TOPIC: OpenCV vision with EMC/linuxcnc

Re:OpenCV vision with EMC/linuxcnc 26 Май 2012 03:37 #20413

  • andypugh
  • andypugh's Avatar
  • NOW ONLINE
  • Moderator
  • Posts: 4126
  • Thank you received: 141
  • Karma: 129
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.
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)
The administrator has disabled public write access.

Re:OpenCV vision with EMC/linuxcnc 26 Май 2012 04:45 #20417

  • ArcEye
  • ArcEye's Avatar
  • OFFLINE
  • Moderator
  • Posts: 1333
  • Thank you received: 102
  • Karma: 129
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 Июл 2012 09:32 by ArcEye.
The administrator has disabled public write access.

Re:OpenCV vision with EMC/linuxcnc 26 Май 2012 05:11 #20418

  • edsimmons
  • edsimmons's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 22
  • Karma: 0
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!
The administrator has disabled public write access.

Re:OpenCV vision with EMC/linuxcnc 26 Май 2012 05:33 #20420

  • andypugh
  • andypugh's Avatar
  • NOW ONLINE
  • Moderator
  • Posts: 4126
  • Thank you received: 141
  • Karma: 129
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 :-)
The administrator has disabled public write access.

Re:OpenCV vision with EMC/linuxcnc 26 Май 2012 07:59 #20422

  • edsimmons
  • edsimmons's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 22
  • Karma: 0
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
The administrator has disabled public write access.

Re:OpenCV vision with EMC/linuxcnc 26 Май 2012 08:19 #20424

  • andypugh
  • andypugh's Avatar
  • NOW ONLINE
  • Moderator
  • Posts: 4126
  • Thank you received: 141
  • Karma: 129
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.
The administrator has disabled public write access.
Time to create page: 1.078 seconds
Powered by Kunena Forum
© 2013 LinuxCNC.org
Joomla! is Free Software released under the GNU General Public License.