c++ grab in position signal

More
19 Aug 2014 04:36 #50022 by bkt
I find without success some way to pass to my c++ program a signal when axis arrived to command position. Actually I use axis-remote to command the position.

I won't use a custom M1xx command but I need some type of direct signal.... example a flag or bit ..... linuxcncrsh not have this type of signal?

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

More
19 Aug 2014 14:57 - 19 Aug 2014 14:58 #50038 by ArcEye
Replied by ArcEye on topic c++ grab in position signal
Hi

The information you want is available from HAL pins

motion.in-position OUT BIT TRUE if the machine is in position (ie, not currently moving towards the commanded position).

motion.distance-to-go OUT FLOAT Distance remaining in the current move

or even just comparing

axis.N.joint-pos-cmd and axis.N.joint-pos-fb

www.linuxcnc.org/docs/devel/html/man/man9/motion.9.html

The problem is then getting that information into an external C++ program.

I personally would write the C++ program itself, so that creates a component which registers with the running Linuxcnc instance and then creates pins which are linked to the relevant motion pins.
(or just gets the values directly by interogating the values of the pins using internal functions)

Would need to know a lot more about exactly what you are doing to come up with a concrete suggestion.

If you are doing MDI through system() calls for instance using axis-remote, even changing axis-remote to return a value indicating completion might not help, as system() notoriously does not return
the return values of the called program very well.

regards
Last edit: 19 Aug 2014 14:58 by ArcEye.
The following user(s) said Thank You: bkt

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

More
19 Aug 2014 16:26 #50042 by bkt
Replied by bkt on topic c++ grab in position signal

The information you want is available from HAL pins

motion.in-position OUT BIT TRUE if the machine is in position (ie, not currently moving towards the commanded position).

motion.distance-to-go OUT FLOAT Distance remaining in the current move

or even just comparing

axis.N.joint-pos-cmd and axis.N.joint-pos-fb

www.linuxcnc.org/docs/devel/html/man/man9/motion.9.html


I know this...

If you are doing MDI through system() calls for instance using axis-remote, even changing axis-remote to return a value indicating completion might not help, as system() notoriously does not return
the return values of the called program very well.


I was not completely sure about this. Thank you for your confirmation.

The problem is then getting that information into an external C++ program.


this is the problem ..... I see that you write a linuxcnc GUI with QT ... but unfortunately I did not see and understand if and where you can see the code.

This of course would be very helpful for me.

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

More
19 Aug 2014 16:46 - 19 Aug 2014 16:48 #50043 by ArcEye
Replied by ArcEye on topic c++ grab in position signal
Still don't know why you need this, or how you will use it.

One command will not execute until the previous one completes, even directly using the MDI window of Axis.
If it does not complete, most likely there will be a following error and it will switch off anyway.
axis-remote hacks into Axis and does things without any user interaction, but does it the same way.

People doing this type of thing are usually either attempting a DNC type code injection or simply using LCNC to control something other than a standard machine
and doing all the calculations elsewhere. An example was someone using it to align an astronomical telescope.

regards
Last edit: 19 Aug 2014 16:48 by ArcEye.

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

More
19 Aug 2014 16:47 #50044 by bkt
Replied by bkt on topic c++ grab in position signal
In my c++ program, with your old suggest (thanks a lot for this), I use
"axis-remote --mdi 'G0 F100000 X100 Y100 Z100' "
in a Qthread (call axisthread) ... a void change every myparticular signal the string .... my only problem is know when linuxcnc mdi end the previous istruction.

Yes I cam make an custom M110 command that call a c++ external program that write something in a file.txt. I cam make another QThread that read every 20ms the file.txt and when it change I can emit a signal to axisthread. If there is this signal axisthread can send another std_str to linuxcnc mdi ..... but this does not sound good and I think it could cause problems.

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

More
19 Aug 2014 16:52 #50045 by bkt
Replied by bkt on topic c++ grab in position signal
I post too late ... sorry

The problem is Qthread .... it need some tipe of signal to sincronize with linuxcnc ... I think the right choice is "inpos" signal send by linuxcnc ... I not have other ideas

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

More
19 Aug 2014 17:45 - 19 Aug 2014 17:46 #50054 by ArcEye
Replied by ArcEye on topic c++ grab in position signal
I think you are correct about the thread idea, it is not ideal, especially trying to do file IO every 20ms!

The best model would be a slot / signal one where it just waits to be told the command is completed or perhaps checks a flag on a timer loop.

The only thing I can suggest is that you dissect the source code.

There is no C / C++ API for linuxcnc, if you wanted to use python, it is quite well documented, even though the python library functions are written in C!

I studied xemc and halcmd in particular. Once you strip away all the GUI bits, all you need are a few calls like
//START
comp_id = hal_init(comp_name);
hal_ready(comp_id);

//CREATE PIN(S)
r = hal_pin_bit_newf(HAL_IN, ...........................);

Then you can directly access MDI eg

static int sendMdiCmd(char *mdi)
{
  EMC_TASK_PLAN_EXECUTE emc_task_plan_execute_msg;

  strcpy(emc_task_plan_execute_msg.command, mdi);
  emc_task_plan_execute_msg.serial_number = ++emcCommandSerialNumber;
  emcCommandBuffer->write(emc_task_plan_execute_msg);

  return 0;
}

Do other stuff in a loop

//EXIT
hal_exit(comp_id);


The alternative would probably be to build you whole program into a hal userspace component, which gives the pin access you need and have it loaded and connected to relevant pins
through your configs when you launch linuxcnc. It saves you having to learn a lot of the internals.

There are no restrictions upon what you can do in a userspace component, because you have the full 'normal' set of libs available.

But I still have no idea what you are trying to do in a wider sense, so hard to know which is best

regards
Last edit: 19 Aug 2014 17:46 by ArcEye.

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

More
19 Aug 2014 22:23 - 19 Aug 2014 22:25 #50068 by ArcEye
Replied by ArcEye on topic c++ grab in position signal
ArcEye wrote:

The alternative would probably be to build you whole program into a hal userspace component, which gives the pin access you need and have it loaded and connected to relevant pins
through your configs when you launch linuxcnc. It saves you having to learn a lot of the internals.


OK here is a simple demo of what I mean

This component will open a hard coded gcode file and when the machine is switched on and the Z axis is homed, pumps the contents of the file to Axis, via axis-remote, line by line.
It waits until motion.in-position is true, before sending the next line
When it is finished it prints to stderr and exits
/*
hal file additions:-

loadusr -W mdipump

net sig1 mdipump.commandfinished motion.in-position
net sig2 mdipump.homed halui.joint.2.is-homed
net sig3 mdipump.machineon halui.machine.is-on

Need a file ~/linuxcnc/gcode.ngc.

My test file just contained this
G40 G90
G54
G21 G17
G40 G8
F2000
G01 X0 Y0 Z20
G01 X160
G01 X-160
G01 Y65
G01 Y-65
G0 Z 5

The component will wait for the machine to be switched on and the Z axis to be homed 
and will then read a gcode file line by line and pump it to axis-remote until finished.

It waits between lines until motion.in-position is TRUE before sending another one.

*/


component mdipump                     "This component reads a file and pushes MDI commands to Linuxcnc";

pin in bit commandfinished = 0        "signal last command completed - connect to motion.in-position";
pin in bit homed = 0                  "signal that machine is homed and ready for commands - connect to last axis to home - halui.joint.2.is-homed";
pin in bit machineon = 0              "signal to ensure machine is on - connect to halui.machine.is-on"; 
option singleton yes;               
option userspace yes;

author "ArcEye <arceyeATmgwareDOTcoDOTuk>";
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 <signal.h>
#include <pwd.h>

int done = 0;

void adios(int sig) { done = 1; }


void user_mainloop(void)
{
char filepath[80];
int progress = 0;
char line[80];
char cmdline[80];

    // get user home dir
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;

sprintf(filepath, "%s/linuxcnc/gcode.ngc", homedir);
FILE *fp1;
fp1 = fopen(filepath, "r");
    
signal(SIGINT, adios);
signal(SIGTERM, adios);
      
    while(!done)
        {
        usleep(250000);
        
        FOR_ALL_INSTS()  
            { 
            if(homed && machineon)
                {
                switch(progress)
                    {
                    case 0: 
                            if(!feof(fp1))
                                {
                                fgets(line, sizeof(line), fp1);  
                                fprintf(stderr,"Line:- %s ", line);// debug
                                sprintf(cmdline, "axis-remote --mdi '%s'", line);
                                system(cmdline);
                                progress = 1;
                                break;
                                }
                            else
                                {
                                done = 1;
                                fprintf(stderr,"File %s completed", filepath);
                                break;
                                }
                
                    case 1: if(!commandfinished)
                                break;
                            else
                                progress = 0;
                        
                    default:
                            break;               
                
                    }
                }
            }
        }
    exit(0);
}

You can watch it doing just this on this youtube video, set it to HD and maximised to see it properly



Hopefully will give you some ideas

regards
Last edit: 19 Aug 2014 22:25 by ArcEye.
The following user(s) said Thank You: bkt

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

More
20 Aug 2014 04:37 #50077 by bkt
Replied by bkt on topic c++ grab in position signal
certainly have taught me new things .... and you gave me a few idea. Unfortunately I'm out for 1week. When come bak I try and place the code tried.

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

More
30 Aug 2014 15:52 #50511 by bkt
Replied by bkt on topic c++ grab in position signal
the idea of ​​reading an external file to mycomponent.comp is very good but does not work in my case.
In fact in my case I write only one xyz position at a time.
This means that at each new location, the system waits the time of writing and reading of the pc. This generates a time when the axes are still waiting for a new command.
I need a lot lower this time.

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

Time to create page: 0.202 seconds
Powered by Kunena Forum