Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Autogenerated by /home/machinekit/git/machinekit/bin/instcomp on Thu Oct 6 03:22:21 2016 -- do not edit */
- #include "rtapi.h"
- #ifdef RTAPI
- #include "rtapi_app.h"
- #endif
- #include "rtapi_string.h"
- #include "rtapi_errno.h"
- #include "hal.h"
- #include "hal_priv.h"
- static int comp_id;
- static char *compname = "udp";
- #ifdef MODULE_INFO
- MODULE_INFO(machinekit, "component:udp:");
- MODULE_INFO(machinekit, "pin:pos_cmd0:float:None:in::None");
- MODULE_INFO(machinekit, "pin:pos_cmd1:float:None:in::None");
- MODULE_INFO(machinekit, "pin:pos_cmd2:float:None:in::None");
- MODULE_INFO(machinekit, "pin:pos_fb0:float:None:out::None");
- MODULE_INFO(machinekit, "pin:pos_fb1:float:None:out::None");
- MODULE_INFO(machinekit, "pin:pos_fb2:float:None:out::None");
- MODULE_INFO(machinekit, "instanceparam:pincount:int::6");
- MODULE_INFO(machinekit, "funct:_:1:");
- MODULE_INFO(machinekit, "author:test");
- MODULE_INFO(machinekit, "license:GPL");
- MODULE_LICENSE("GPL");
- #endif // MODULE_INFO
- RTAPI_TAG(HAL,HC_INSTANTIABLE);
- #define MAXCOUNT 16
- #define DEFAULTCOUNT 6
- static int pincount = 6;
- RTAPI_IP_INT(pincount, "Instance integer param 'pincount'");
- struct inst_data {
- hal_float_t *pos_cmd0;
- hal_float_t *pos_cmd1;
- hal_float_t *pos_cmd2;
- hal_float_t *pos_fb0;
- hal_float_t *pos_fb1;
- hal_float_t *pos_fb2;
- int sockfd;
- int localpincount;
- };
- static int maxpins __attribute__((unused)) = 16;
- static char *iprefix = "";
- static int _(void *arg, const hal_funct_args_t *fa);
- static int instantiate(const int argc, const char**argv);
- static int extra_inst_setup(struct inst_data* ip, const char *name, int argc, const char**argv);
- #undef TRUE
- #undef FALSE
- #undef true
- #define true (1)
- #undef false
- #define false (0)
- static int export_halobjs(struct inst_data *ip, int owner_id, const char *name)
- {
- char buf[HAL_NAME_LEN + 1];
- int r = 0;
- int j __attribute__((unused)) = 0;
- int z __attribute__((unused)) = 0;
- r = hal_pin_float_newf(HAL_IN, &(ip->pos_cmd0), owner_id,
- "%s.pos-cmd0", name);
- if(r != 0) return r;
- r = hal_pin_float_newf(HAL_IN, &(ip->pos_cmd1), owner_id,
- "%s.pos-cmd1", name);
- if(r != 0) return r;
- r = hal_pin_float_newf(HAL_IN, &(ip->pos_cmd2), owner_id,
- "%s.pos-cmd2", name);
- if(r != 0) return r;
- r = hal_pin_float_newf(HAL_OUT, &(ip->pos_fb0), owner_id,
- "%s.pos-fb0", name);
- if(r != 0) return r;
- r = hal_pin_float_newf(HAL_OUT, &(ip->pos_fb1), owner_id,
- "%s.pos-fb1", name);
- if(r != 0) return r;
- r = hal_pin_float_newf(HAL_OUT, &(ip->pos_fb2), owner_id,
- "%s.pos-fb2", name);
- if(r != 0) return r;
- // if not set by instantiate() set to default
- if(! ip->localpincount || ip->localpincount == -1)
- ip->localpincount = DEFAULTCOUNT;
- hal_print_msg(RTAPI_MSG_DBG,"export_halobjs() ip->localpincount set to %d", ip->localpincount);
- // export an extended thread function:
- hal_export_xfunct_args_t __xf = {
- .type = FS_XTHREADFUNC,
- .funct.x = _,
- .arg = ip,
- .uses_fp = 1,
- .reentrant = 0,
- .owner_id = owner_id
- };
- rtapi_snprintf(buf, sizeof(buf),"%s", name);
- r = hal_export_xfunctf(&__xf, buf, name);
- if(r != 0)
- return r;
- return 0;
- }
- // constructor - init all HAL pins, params, funct etc here
- static int instantiate(const int argc, const char**argv)
- {
- const char *name __attribute__((unused)) = argv[1];
- struct inst_data *ip;
- int r;
- int k;
- // allocate a named instance, and some HAL memory for the instance data
- int inst_id = hal_inst_create(name, comp_id, sizeof(struct inst_data), (void **)&ip);
- if (inst_id < 0)
- return -1;
- // here ip is guaranteed to point to a blob of HAL memory of size sizeof(struct inst_data).
- hal_print_msg(RTAPI_MSG_DBG,"%s inst=%s argc=%d",__FUNCTION__, name, argc);
- // Debug print of params and values
- hal_print_msg(RTAPI_MSG_DBG,"%s: int instance param: %s=%d",__FUNCTION__,"pincount", pincount);
- // if pincount=NN is passed, set local variable here, if not set to default
- int pin_param_value = pincount;
- if((pin_param_value == -1) || (pin_param_value == 0))
- pin_param_value = DEFAULTCOUNT;
- else if((pin_param_value > 0) && (pin_param_value > MAXCOUNT))
- pin_param_value = MAXCOUNT;
- ip->localpincount = pincount = pin_param_value;
- hal_print_msg(RTAPI_MSG_DBG,"ip->localpincount set to %d", pin_param_value);
- // These pins - params - functs will be owned by the instance
- // which can be separately exited with delinst
- if(strlen(iprefix))
- r = export_halobjs(ip, inst_id, iprefix);
- else
- r = export_halobjs(ip, inst_id, name);
- // if the extra_inst_setup returns non zero will abort module creation
- k = extra_inst_setup(ip, name, argc, argv);
- if(k != 0)
- return k;
- //reset pincount to -1 so that instantiation without it will result in DEFAULTCOUNT
- pincount = -1;
- return r;
- }
- int rtapi_app_main(void)
- {
- // Debug print of params and values
- hal_print_msg(RTAPI_MSG_DBG,"%s: int instance param: %s=%d",__FUNCTION__,"pincount", pincount);
- comp_id = hal_xinit(TYPE_RT, 0, 0, instantiate, NULL, compname);
- if (comp_id < 0)
- return -1;
- hal_ready(comp_id);
- return 0;
- }
- void rtapi_app_exit(void)
- {
- hal_exit(comp_id);
- }
- #undef FUNCTION
- #define FUNCTION(name) static int name(void *arg, const hal_funct_args_t *fa)
- // if the extra_inst_setup returns non zero it will abort the module creation
- #undef EXTRA_INST_SETUP
- #define EXTRA_INST_SETUP() static int extra_inst_setup(struct inst_data *ip, const char *name, int argc, const char**argv)
- #undef fperiod
- #define fperiod (period * 1e-9)
- #undef pos_cmd0
- #define pos_cmd0 (0+*ip->pos_cmd0)
- #undef pos_cmd1
- #define pos_cmd1 (0+*ip->pos_cmd1)
- #undef pos_cmd2
- #define pos_cmd2 (0+*ip->pos_cmd2)
- #undef pos_fb0
- #define pos_fb0 (*ip->pos_fb0)
- #undef pos_fb1
- #define pos_fb1 (*ip->pos_fb1)
- #undef pos_fb2
- #define pos_fb2 (*ip->pos_fb2)
- #undef sockfd
- #define sockfd (ip->sockfd)
- #undef localpincount
- #define localpincount (ip->localpincount)
- #line 24 "./udp.icomp"
- #include <arpa/inet.h>
- #include <ifaddrs.h>
- EXTRA_INST_SETUP(){
- rtapi_print("hallo\n");
- int ret;
- sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- if (sockfd < 0) {
- rtapi_print("ERROR: can't open socket: %s\n", strerror(errno));
- return -errno;
- }
- struct sockaddr_in server_addr;
- struct sockaddr_in local_addr;
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(27181);
- server_addr.sin_addr.s_addr = inet_addr("192.168.1.150");
- local_addr.sin_family = AF_INET;
- local_addr.sin_addr.s_addr = INADDR_ANY;
- ret = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in));
- if (ret < 0) {
- rtapi_print("ERROR: can't connect: %s\n", strerror(errno));
- return -errno;
- }
- struct timeval timeout;
- timeout.tv_sec = 0;
- timeout.tv_usec = 10;
- ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
- if (ret < 0) {
- rtapi_print("ERROR: can't set socket option: %s\n", strerror(errno));
- return -errno;
- }
- timeout.tv_usec = 10;
- setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
- if (ret < 0) {
- rtapi_print("ERROR: can't set socket option: %s\n", strerror(errno));
- return -errno;
- }
- return 0;
- }
- FUNCTION(_) {
- long period __attribute__((unused)) = fa_period(fa);
- struct inst_data *ip = arg;
- pos_fb0 = pos_cmd0;
- pos_fb1 = pos_cmd1;
- pos_fb2 = pos_cmd2;
- uint8_t buf[16];
- buf[0] = 0x01;
- buf[1] = 0x42;
- buf[2] = 0x00;
- buf[3] = 0x01;
- int ret = send(sockfd,buf,4,0);
- if (ret < 0) {
- rtapi_print("send fail %s\n", strerror(errno));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement