Developing HAL component in c

More
17 Oct 2020 11:01 - 17 Oct 2020 11:03 #186347 by MasterTom
I started a simple component: tomtom.c
I tested it with: halcmd loadrt tomtom
I got:
Waiting for component 'tomtom' to become ready..Waited 3 seconds for master. giving up.
Note: Using POSIX realtime ..........................................................................................................................................
..............


According to existing components in linuxcnc-dev/src/hal/components I created the following code:
With this code I get additional prints
Waiting for component 'tomtom' to become ready..Waited 3 seconds for master. giving up.
Note: Using POSIX realtime
> STEP 1
> STEP export pins
> DONE export pins
> STEP export hal_export_funct
> DONE export hal_export_funct
> CALL hal_ready
> DONE CALL hal_ready
..........................................
==> rtapi_app_main was successfully finished.

What is the problem?
How to really debug such code?

//
//
#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("TomTom");
MODULE_DESCRIPTION("First Test");
MODULE_LICENSE("GPL");

typedef struct {
    int counter_limit_top;
    int counter_limit_bottom;
    int increment;
    hal_bit_t *up;  /* true-->up false-->down*/
    hal_bit_t *start; //true-->run false-->stop */
    hal_s32_t *counter;
} hal_tomtom_t;

static hal_tomtom_t *p_tom_data;
static int comp_id;		/* component ID */
static void tomtom_realtimefunction(void *arg, long period);
static int _counter_value;

int rtapi_app_main(void)
{
  rtapi_print("--------------------------> STEP 1\n");
  comp_id = hal_init("Tom Test");
  if (comp_id < 0) 
  {    
    rtapi_print_msg(RTAPI_MSG_ERR,
    "Tom Test: ERROR: hal_init() failed\n");
    return -7;
  }
  p_tom_data = hal_malloc(sizeof(hal_tomtom_t));
  if (p_tom_data == 0)
  {
     rtapi_print_msg(RTAPI_MSG_ERR,
                	"Tom Test: ERROR: hal_malloc() for p_tom_dat \n");
                    hal_exit(comp_id);
	hal_exit(comp_id);
    return -11;
  }
  // init
  p_tom_data->increment = 1;
  p_tom_data->counter_limit_bottom = -100;
  p_tom_data->counter_limit_top	   =  100;
  p_tom_data->increment 		   =    1;
  
  // export pins
  rtapi_print("--------------------------> STEP export pins\n");
  int ret_val = hal_pin_bit_new("TomTomCounter.start/stop", HAL_IN, &(p_tom_data->start),comp_id);
  if (ret_val != 0)
  {
    return -12;
  }
  rtapi_print("--------------------------> DONE export pins\n");
  ret_val =   hal_pin_s32_new("TomTomCounter.Counter", HAL_OUT,&(p_tom_data->counter),comp_id);
  if (ret_val != 0)
  {
    return -14;
  }

  rtapi_print("--------------------------> STEP export hal_export_funct\n");
  ret_val = hal_export_funct("Tom Tom",tomtom_realtimefunction, &p_tom_data, 0, 0,comp_id);
  if (ret_val != 0) 
  {
    rtapi_print_msg(RTAPI_MSG_ERR,
                	"WEIGHTED_SUM: ERROR: group %d export failed\n");
                    hal_exit(comp_id);
    return -15;
  }
  rtapi_print("--------------------------> DONE export hal_export_funct\n");
  
  rtapi_print("--------------------------> CALL hal_ready\n");
  hal_ready(comp_id);
  rtapi_print("--------------------------> DONE CALL hal_ready\n");
  
  return 0;
}
void rtapi_app_exit(void)
{
    rtapi_print("--------------------------> EXIT called\n");
    hal_exit(comp_id);
}
void tomtom_realtimefunction(void *arg, long period)
{
    rtapi_print("-cyclic\n");
    return;
    p_tom_data = (hal_tomtom_t*)arg;
    if (p_tom_data->start)
        p_tom_data->counter++;
    rtapi_print_msg(RTAPI_MSG_ERR, "Hallo");
}
Last edit: 17 Oct 2020 11:03 by MasterTom. Reason: Additional question

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

More
17 Oct 2020 12:06 #186351 by dgarrett
I started a simple component: tomtom.c
...
comp_id = hal_init("Tom Test");

A likely cause:
The name of the component and the file name must agree.
The following user(s) said Thank You: MasterTom

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

More
17 Oct 2020 12:55 #186352 by MasterTom
Hi,
I'm sorry. I'm new to LinuxCNC.
Now it is working better.
But there is no call hal_export_funct.
What could be the problem?

Thanks Thomas

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

More
17 Oct 2020 13:06 #186353 by dgarrett

But there is no call hal_export_funct.


suggest:
//ret_val = hal_export_funct("Tom Tom",tomtom_realtimefunction, &p_tom_data, 0, 0,comp_id);
  ret_val = hal_export_funct("tomfunc",tomtom_realtimefunction, p_tom_data, 0, 0,comp_id);

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

More
17 Oct 2020 13:23 #186356 by MasterTom
unfortunality it doesn't get cyclic:
these are what I got from halcmd;

tom@linuxcnc:/usr/lib/linuxcnc/modules$ halcmd -V loadrt tomtom
HAL: initializing hal_lib
HAL: initializing component 'halcmd5893'
HAL: component 'halcmd5893' initialized, ID = 10
HAL: removing component 10
HAL: releasing RTAPI resourcesHAL: component 10 removed, name = 'halcmd5893'
HAL: initializing hal_lib
/usr/bin/rtapi_app load tomtom
HAL: initializing component 'halcmd5893'
smalloc_dn: shmem available 347760
HAL: component 'halcmd5893' initialized, ID = 02
Note: Using POSIX realtime
> STEP 1
> STEP export pins
> DONE export pins
> STEP export hal_export_funct
> DONE export hal_export_funct
> CALL hal_ready
> DONE CALL hal_ready
<commandline>:0: Component 'tomtom' ready
<commandline>:0: Program '/usr/bin/rtapi_app' started
smalloc_up: shmem available 347047
<commandline>:0: Realtime module 'tomtom' loaded
HAL: removing component 02
HAL: releasing RTAPI resourcesHAL: component 02 removed, name = 'halcmd5893'

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

More
17 Oct 2020 13:41 #186359 by dgarrett

unfortunality it doesn't get cyclic


try:
$ cat tst.hal # create this file
#            (adjust component and funct names as reqd)
loadrt tomtest
loadrt threads name1=t period1=25000 period2=1000000
addf tomfunc t
start

$ halrun tst.hal

Note: during debugging,if hal exits abnormally you
may need to issue $ halrun -U

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

More
17 Oct 2020 15:34 #186366 by MasterTom
Hi,

thanks for your great support. My component is working now (also under linuxcnc)
Best reagrds Thomas

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

Time to create page: 0.076 seconds
Powered by Kunena Forum