(SOLVED - maybe) How to get an underscore in a pin/parameter name?

More
14 Jun 2018 00:25 - 15 Jun 2018 00:27 #112331 by Sparky961
I'm experimenting with creating a component that defines the pins for a Mesa 7i76e so that I can perform offline development and some testing. I'm not trying to emulate the functionality at this time, just having the definitions so the config will run.

I'm butting heads with a brick wall trying to get my first definition working, as it contains an underscore character and this appears to be treated specially by halcompile.
component hm2_7i76e "Fake pins from 7i76e for testing without attached hardware";
param r s32 watchdog.timeout_ns = 5000000;
pin out bit out0;
pin in bit in0;
function _;
license "GPL";
;;
FUNCTION(_) {
    out0 = in0;
}

I want the parameter name exactly as above, but no matter what I try I can't get it to contain an underscore. Am I attempting the impossible or missing something?

The page below has not been of much help with this:
linuxcnc.org/docs/html/hal/comp.html

Thanks..
Last edit: 15 Jun 2018 00:27 by Sparky961.
The following user(s) said Thank You: Mike_Eitel

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

More
14 Jun 2018 01:06 #112334 by InMyDarkestHour
HALNAME - An extended identifier. When used to create a HAL identifier, any underscores are replaced with dashes, and any trailing dash or period is removed, so that "this_name_" will be turned into "this-name"

From your code using halcompile --install
halcmd: loadrt hm2_7i76e
Note: Using POSIX realtime
halcmd: show pin
Component Pins:
Owner   Type  Dir        Value    Name
        6   bit    IN          FALSE  hm2-7i76e.0.in0
        6   bit    OUT       FALSE  hm2-7i76e.0.out0
        6   s32  OUT       0           hm2-7i76e.0.time

So it looks like you will have to accept any underscores _ are converted to - dashes.

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

More
14 Jun 2018 01:35 #112337 by Sparky961
Can I thus reason that the real component/driver for this generates the names with underscores because it's written in C ?

Maybe this side project is a non-starter. It seemed like it would be simple.

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

More
14 Jun 2018 02:20 - 14 Jun 2018 02:27 #112338 by InMyDarkestHour
I'm guessing that the underscore is used as a delimiter between the hm2 and the type of board detected by the hm2_eth driver. Just a guess about the internals without going through the source code.
So depending whether you have a 7i76E, 7I80DB, 7I80HD, 7i92, 7i93 would dictate the string after the underscore.

If you work with a dash in the name with your experiments and then want to convert to real hardware a search and replace on hm2-7i76e to hm2_7i76e I would imagine working for the hal file ?

The below is from memory........

Or using a variable in the ini file maybe even simpler.
Maybe something like this
[MYBOARD]
#TYPE="hm2_7i76e"
TYPE="hm2-7i76e"

Depending on which line is uncommented would be the string that is used to replace [MYBOARD]TYPE

An example might be such as: I think is the correct sytax
setp [MYBOARD]TYPE.0.watchdog.timeout_ns 25000000

If I'm wrong on this, I'm pretty sure I've used something along these lines, I apologise and hope someone can correct this.
Last edit: 14 Jun 2018 02:27 by InMyDarkestHour.

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

More
14 Jun 2018 02:30 #112339 by Sparky961
Not a bad workaround, but a workaround nonetheless. I'd prefer not to have to make modifications throughout my config files. A one-line change would be acceptable.

Better would be if someone would be so kind as to provide a template component in C that does nothing except define pins. I've been trying to work with the provided "skeleton.c", but it isn't going well so far.

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

More
14 Jun 2018 02:46 #112340 by Sparky961
Ok, maybe my changes aren't the problem. I've copied "supply.c" to "hm2_7i76e.c", and without making any changes compile via:
sudo halcompile --install hm2_7i76e.c

It completes without complaining of error, but then when I try to load the component in HAL I get this:
linuxcnc@debian:~$ halrun
halcmd: loadrt hm2_7i76e 
Note: Using POSIX realtime
Waiting for component 'hm2_7i76e' to become ready............

............... and the dots continue forever...........

I'm built simple components using the component generator pseudo code and loaded them without problems, so why is this causing me grief?

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

More
14 Jun 2018 03:21 #112341 by InMyDarkestHour
Using the section [MYBOARD]
and commenting & uncommenting the lines below would give you a two line change.

But I honestly think the whole underscore to dash thing is something internal, maybe the underscore has a special meaning within some data structures that Linuxcnc keeps.

Even when you use the command halcompile --preprocess hm2_7i76e.comp and have a look at the c source
You will find a line such as:
comp_id = hal_init("hm2_7i76e");

But somewhere internally that string is being changed for referencing pins.

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

More
14 Jun 2018 03:38 #112342 by InMyDarkestHour
If you want to do it your way, this is an example I tried with the supply.c code copying it to supply_2.c and changing all references to supply to supply_2.
/********************************************************************
* Description:  supply_2.c
*               This file, 'supply_2.c', is a HAL component supply_2ing 
*               HAL pins preset to useful values like TRUE and 1.0.
*
* Author: Matt Shaver
* License: GPL Version 2
*    
* Copyright (c) 2004 All rights reserved.
*
* Last change: 
********************************************************************/
/** This file, 'supply_2.c', is a HAL component supply_2ing HAL pins preset
    to useful values like TRUE and 1.0.  I expect that it will mostly
    be used for testing.  It is a realtime component.
*/

/** Copyright (C) 2004 Matt Shaver
                       <mshaver AT users DOT sourceforge DOT net>
*/

/** This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General
    Public License as published by the Free Software Foundation.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111 USA

    THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
    ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
    TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
    harming persons must have provisions for completely removing power
    from all motors, etc, before persons enter any danger area.  All
    machinery must be designed to comply with local and national safety
    codes, and the authors of this software can not, and do not, take
    any responsibility for such compliance.

    This code was written as part of the EMC HAL project.  For more
    information, go to www.linuxcnc.org.
*/

#include "rtapi.h"		/* RTAPI realtime OS API */
#include "rtapi_app.h"		/* RTAPI realtime module decls */
#include "hal.h"		/* HAL public API decls */

/* module information */
MODULE_AUTHOR("Matt Shaver");
MODULE_DESCRIPTION("supply_2 Component for EMC HAL");
MODULE_LICENSE("GPL");
static int num_chan = 1;	/* number of channels - default = 1 */
RTAPI_MP_INT(num_chan, "number of channels");

/***********************************************************************
*                STRUCTURES AND GLOBAL VARIABLES                       *
************************************************************************/

/** This structure contains the runtime data.
*/

typedef struct {
    hal_bit_t *q;		/* pin: q output of simulated flip-flop */
    hal_bit_t *_q;		/* pin: /q output of simulated flip-flop */
    hal_float_t *variable;	/* pin: output set by param "value" */
    hal_float_t *_variable;	/* pin: output set by param "value" * -1.0 */
    hal_bit_t *d;		/* pin: d input to simulated flip-flop */
    hal_float_t *value;		/* pin: value of float pin "variable" */
} hal_supply_2_t;

/* pointer to supply_2_t struct */
static hal_supply_2_t *supply_2_array;

/* other globals */
static int comp_id;		/* component ID */

/***********************************************************************
*                  LOCAL FUNCTION DECLARATIONS                         *
************************************************************************/

static int export_supply_2(int num, hal_supply_2_t * addr);
static void update_supply_2(void *arg, long l);

/***********************************************************************
*                       INIT AND EXIT CODE                             *
************************************************************************/
#define MAX_CHAN 16

int rtapi_app_main(void)
{
    int n, retval;

    /* test for number of channels */
    if ((num_chan <= 0) || (num_chan > MAX_CHAN)) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "supply_2: ERROR: invalid num_chan: %d\n", num_chan);
	return -1;
    }
    /* have good config info, connect to the HAL */
    comp_id = hal_init("supply_2");
    if (comp_id < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR, "supply_2: ERROR: hal_init() failed\n");
	return -1;
    }
    /* allocate shared memory for supply_2 data */
    supply_2_array = hal_malloc(num_chan * sizeof(hal_supply_2_t));
    if (supply_2_array == 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "supply_2: ERROR: hal_malloc() failed\n");
	hal_exit(comp_id);
	return -1;
    }
    /* export variables and functions for each supply_2 */
    for (n = 0; n < num_chan; n++) {
	retval = export_supply_2(n, &(supply_2_array[n]));
	if (retval != 0) {
	    rtapi_print_msg(RTAPI_MSG_ERR,
		"supply_2: ERROR: var export failed\n");
	    hal_exit(comp_id);
	    return -1;
	}
    }
    rtapi_print_msg(RTAPI_MSG_INFO, "supply_2:installed %d supplies\n",
	num_chan);
    hal_ready(comp_id);
    return 0;
}

void rtapi_app_exit(void)
{
    hal_exit(comp_id);
}

/***********************************************************************
*                       REALTIME FUNCTIONS                             *
************************************************************************/

static void update_supply_2(void *arg, long l)
{
    hal_supply_2_t *supply_2;

    /* point to the data */
    supply_2 = arg;
    /* set pin = param */
    *(supply_2->q) = *(supply_2->d);
    *(supply_2->_q) = !(*(supply_2->d));
    *(supply_2->variable) = *(supply_2->value);
    *(supply_2->_variable) = *(supply_2->value) * -1.0;
    /* done */
}

/***********************************************************************
*                   LOCAL FUNCTION DEFINITIONS                         *
************************************************************************/

static int export_supply_2(int num, hal_supply_2_t * addr)
{
    int retval;
    char buf[HAL_NAME_LEN + 1];

    /* export pins */
    retval = hal_pin_bit_newf(HAL_OUT, &(addr->q), comp_id, "supply_2.%d.q", num);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_bit_newf(HAL_OUT, &(addr->_q), comp_id, "supply_2.%d._q", num);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_float_newf(HAL_OUT, &(addr->variable), comp_id,"supply_2.%d.variable", num);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_float_newf(HAL_OUT, &(addr->_variable), comp_id, "supply_2.%d._variable", num);
    if (retval != 0) {
	return retval;
    }
    /* export parameters */
    retval = hal_pin_bit_newf(HAL_IO, &(addr->d), comp_id, "supply_2.%d.d", num);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_float_newf(HAL_IO, &(addr->value), comp_id, "supply_2.%d.value", num);
    if (retval != 0) {
	return retval;
    }
    /* init all structure members */
    *(addr->q) = 0;
    *(addr->_q) = 1;
    *(addr->variable) = 0.0;
    *(addr->_variable) = 0.0;
    *(addr->d) = 0;
    *(addr->value) = 0.0;
    /* export function for this loop */
    rtapi_snprintf(buf, sizeof(buf), "supply_2.%d.update", num);
    retval =
	hal_export_funct(buf, update_supply_2, &(supply_2_array[num]), 1, 0,
	comp_id);
    if (retval != 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "supply_2: ERROR: update funct export failed\n");
	hal_exit(comp_id);
	return -1;
    }
    return 0;
}

rmurphy@stretch-cnc:~/linuxcnc-dev/src/hal/components$ halrun
halcmd: loadrt supply_2
Note: Using POSIX realtime
halcmd: show pin
Component Pins:
Owner Type Dir Value Name
6 bit OUT TRUE supply_2.0._q
6 float OUT 0 supply_2.0._variable
6 bit I/O FALSE supply_2.0.d
6 bit OUT FALSE supply_2.0.q
6 s32 OUT 0 supply_2.0.update.time
6 float I/O 0 supply_2.0.value
6 float OUT 0 supply_2.0.variable

halcmd:


I can't say for certain what affect it would have if used in a machine hal.

I would appear to unload by way of unloadrt supply_2

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

More
14 Jun 2018 03:40 - 14 Jun 2018 03:44 #112343 by Sparky961
Your comment about using the --preprocess option got me thinking that if I do that, then I should be able to modify the C source to change the dash back to an underscore. Then it should compile and install as it's supposed to using the --install option on the .C file.

From the generated hm2_7i76e.c :
    r = hal_param_s32_newf(HAL_RO, &(inst->watchdog_timeout_ns), comp_id,
        "%s.watchdog.timeout_ns", prefix);

I changed the quoted string "watchdog.timeout-ns" to "watchdog.timeout_ns".

Using the code from my first post in this thread, all went well. Except I'm getting similar behaviour as when I tried building another component. I suspect something else is wrong with the way it's builiding but have no idea what to start with.
linuxcnc@debian:~$ halrun
halcmd: loadrt hm2_7i76e 
Waiting for component 'hm2_7i76e' to become ready...Waited 3 seconds for master.  giving up.
.Note: Using POSIX realtime
Last edit: 14 Jun 2018 03:44 by Sparky961.

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

More
14 Jun 2018 04:38 - 14 Jun 2018 04:48 #112344 by InMyDarkestHour
I'd be putting my wager on the coder doing something wrong rather than the compiler doing something wrong.

Make sure that all the component names in the file match the source file name...I got the same behavior when I copied the supply.c src to supply_2.c and didn't change any reference to supply to supply_2.

But when is all said and done there is obviously a reason that an underscore is change to a dash, I'd keep to the way halcompile does things.

It honestly looks as if you are trying to make things over complicated by what can be fixed by a simple search and replace or using a section & variable in the ini & hal files.

This question would probably be better addressed via the developers list as there would be more knowledge regarding the how's and whys.

Could be a coding convention ?

Just cos a underscore can be used does not necessarily mean it should.
Last edit: 14 Jun 2018 04:48 by InMyDarkestHour.

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

Time to create page: 0.099 seconds
Powered by Kunena Forum