Beispiel #1
0
static void request_server_thread_code(request_server_thread_args *args)
{
  int id;
  int connection_id;
  ulapi_task_struct *request_connection_thread;
  request_connection_thread_args *request_connection_args; 

  id = args->id;

  while (true) {
    if (debug) printf("waiting for a trajectory point connection...\n");
    connection_id = ulapi_socket_get_connection_id(id);
    if (connection_id < 0) {
      fprintf(stderr, "can't get a trajectory point connecton\n");
      break;
    }
     
    if (debug) printf("got a trajectory point connection on id %d\n", connection_id);

    // spawn a connection thread
    request_connection_thread = reinterpret_cast<ulapi_task_struct *>(malloc(sizeof(*request_connection_thread)));
    request_connection_args = reinterpret_cast<request_connection_thread_args *>(malloc(sizeof(*request_connection_args)));

    ulapi_task_init(request_connection_thread);
    request_connection_args->id = connection_id;
    request_connection_args->thread = request_connection_thread;
    ulapi_task_start(request_connection_thread, reinterpret_cast<ulapi_task_code>(request_connection_thread_code), reinterpret_cast<void *>(request_connection_args), ulapi_prio_highest(), 0);
  } // while (true)

  ulapi_socket_close(id);

  if (debug) printf("server on %d done\n", id);

  return;
}
static void emove_client_write_task_code(emove_client_write_task_args *args)
{
  ulapi_task_struct *task;
  int id;
  int period_nsecs;
  enum {OUTBUF_SIZE = 1024};
  char outbuf[OUTBUF_SIZE];
  int nchars;
  nist_kitting::emove_stat emove_stat;

  task = args->task;
  id = args->id;
  period_nsecs = args->period_nsecs;
  free(args);

  while (true) {
    ros::spinOnce();
    ulapi_mutex_take(&emove_stat_mutex);
    emove_stat = emove_stat_buf;
    ulapi_mutex_give(&emove_stat_mutex);

    /*
      uint8 type
      uint8 serial_number
      uint8 state
      uint8 status
      uint8 heartbeat
      float32 period
      float32 cycle
      float32 duration

      extern char *kitting_cmd_to_string(int s);
      extern char *rcs_state_to_string(int s);
      extern char *rcs_status_to_string(int s);
    */

    ulapi_snprintf(outbuf, sizeof(outbuf), "%s %d %s %s %d %s %s %s\n",
		   kitting_cmd_to_string(emove_stat.stat.type),
		   emove_stat.stat.serial_number,
		   rcs_state_to_string(emove_stat.stat.state),
		   rcs_status_to_string(emove_stat.stat.status),
		   emove_stat.stat.heartbeat,
		   emove_stat.name.c_str(), emove_stat.line.c_str(), emove_stat.crcl.c_str());
    outbuf[sizeof(outbuf)-1] = 0;
    nchars = ulapi_socket_write(id, outbuf, strlen(outbuf));
    if (nchars <= 0) break;

    ulapi_wait(period_nsecs);
  }

  ulapi_socket_close(id);

  if (debug) printf("emove client write handler %d done\n", id);

  ulapi_task_delete(task);

  return;
}
static void ws_client_read_task_code(ws_client_read_task_args *args)
{
  ulapi_task_struct *task;
  int id;
  enum {INBUF_SIZE = 1024};
  char inbuf[INBUF_SIZE];
  char *ptr;
  char *endptr;
  int nchars;
  ros::NodeHandle nh;
  ros::Publisher pub;
  nist_kitting::ws_cmd ws_cmd;
  int serial_number = 1;

  task = args->task;
  id = args->id;
  free(args);
  
  pub = nh.advertise<nist_kitting::ws_cmd>(KITTING_WS_CMD_TOPIC, TOPIC_QUEUE_LEN);

  while (true) {
    nchars = ulapi_socket_read(id, inbuf, sizeof(inbuf));
    if (nchars <= 0) break;
    if (nchars < sizeof(inbuf)) inbuf[nchars] = 0;
    else inbuf[sizeof(inbuf)-1] = 0;

    ptr = inbuf;
    // strip off leading and trailing whitespace
    while (isspace(*ptr)) ptr++;
    endptr = ptr + strlen(ptr);
    while (isspace(*endptr)) *endptr-- = 0;
    if (0 == *ptr) continue;

    ws_cmd.cmd.type = KITTING_WS_ASSEMBLE_KIT;
    ws_cmd.assemble_kit.name = std::string(ptr);
    ws_cmd.assemble_kit.quantity = 1;
    ws_cmd.cmd.serial_number = serial_number++;
    pub.publish(ws_cmd);
  }

  ulapi_socket_close(id);

  if (debug) printf("ws client read handler %d done\n", id);

  ulapi_task_delete(task);

  return;
}
static void emove_server_task_code(emove_server_task_args *args)
{
  ulapi_task_struct *task;
  int id;
  int period_nsecs;
  ulapi_task_struct *emove_client_write_task;
  emove_client_write_task_args *emove_client_write_args; 
  int emove_connection_id;

  task = args->task;
  id = args->id;
  period_nsecs = args->period_nsecs;

  bool done = false;
  while (! done) {
    if (debug) printf("waiting for an Emove HMI connection on %d...\n", id);
    emove_connection_id = ulapi_socket_get_connection_id(id);
    if (emove_connection_id < 0) {
      fprintf(stderr, "can't get an Emove HMI connection\n");
      break;
    }
     
    if (debug) printf("got an Emove HMI connection on id %d\n", emove_connection_id);

    // spawn connection tasks for reading and writing

    emove_client_write_task = reinterpret_cast<ulapi_task_struct *>(malloc(sizeof(*emove_client_write_task)));
    emove_client_write_args = reinterpret_cast<emove_client_write_task_args *>(malloc(sizeof(*emove_client_write_args)));

    ulapi_task_init(emove_client_write_task);
    emove_client_write_args->task = emove_client_write_task;
    emove_client_write_args->id = emove_connection_id;
    emove_client_write_args->period_nsecs = period_nsecs;
    ulapi_task_start(emove_client_write_task, reinterpret_cast<ulapi_task_code>(emove_client_write_task_code), reinterpret_cast<void *>(emove_client_write_args), ulapi_prio_highest(), 0);

  } // while (true)

  ulapi_socket_close(id);

  if (debug) printf("server on %d done\n", id);

  ulapi_task_delete(task);

  return;
}
rtapi_result rtapi_socket_close(rtapi_integer id)
{
  return ulapi_socket_close(id);
}
void client_code(void *args)
{
  void *client_task;
  ulapi_integer client_id;
  client_db_struct *client_db_ptr;
  ulapi_integer debug;
  enum {BUFFERLEN = 256};
  char inbuf[BUFFERLEN];
  char outbuf[BUFFERLEN];
  ulapi_integer nchars;
  int number;
  int lastnumber = 0;

  client_task = ((client_args *) args)->client_task;
  client_id = ((client_args *) args)->client_id;
  client_db_ptr = ((client_args *) args)->client_db_ptr;
  debug = ((client_args *) args)->debug;
  free(args);

  /*
    The client thread asks for an update every second, blocks until
    it gets a response, prints it, and loops again.
  */

  for (;;) {
    ulapi_mutex_take(client_db_ptr->mutex);
    number = client_db_ptr->number;
    ulapi_mutex_give(client_db_ptr->mutex);

    if (number != lastnumber) {
      ulapi_snprintf(outbuf, sizeof(outbuf), "write %d", number);
      lastnumber = number;
    } else {
      ulapi_snprintf(outbuf, sizeof(outbuf), "read");
    }
    ulapi_socket_write(client_id, outbuf, strlen(outbuf)+1);

    nchars = ulapi_socket_read(client_id, inbuf, sizeof(inbuf)-1);

    if (-1 == nchars) {
      if (debug) printf("connection closed\n");
      break;
    }

    if (0 == nchars) {
      if (debug) printf("end of file\n");
      break;
    }
    inbuf[nchars] = 0;


    /*
      Parse and handle the message here as your application requires.
    */
    printf("%s\n", inbuf);

    ulapi_wait(1000000000);
  }

  ulapi_socket_close(client_id);
}
int main(int argc, char *argv[])
{
  int option;
  ulapi_integer port = SAMPLE_APP_DEFAULT_PORT;
  enum {BUFFERLEN = 256};
  char host[BUFFERLEN] = "localhost";
  ulapi_integer debug = 0;
  ulapi_integer client_id;
  ulapi_task_struct *client_task;
  client_args *client_args_ptr;
  client_db_struct client_db;
  char buffer[BUFFERLEN];
  char *ptr;
  int number;

  ulapi_opterr = 0;

  for (;;) {
    option = ulapi_getopt(argc, argv, ":p:h:d");
    if (option == -1)
      break;

    switch (option) {
    case 'p':
      port = atoi(ulapi_optarg);
      break;

    case 'h':
      strncpy(host, ulapi_optarg, sizeof(host));
      host[sizeof(host) - 1] = 0;
      break;

    case 'd':
      debug = 1;
      break;

    case ':':
      fprintf(stderr, "missing value for -%c\n", ulapi_optopt);
      return 1;
      break;

    default:			/* '?' */
      fprintf(stderr, "unrecognized option -%c\n", ulapi_optopt);
      return 1;
      break;
    }
  }
  if (ulapi_optind < argc) {
    fprintf(stderr, "extra non-option characters: %s\n", argv[ulapi_optind]);
    return 1;
  }

  if (ULAPI_OK != ulapi_init()) {
    fprintf(stderr, "ulapi_init error\n");
    return 1;
  }

  if (debug) ulapi_set_debug(ULAPI_DEBUG_ALL);

  if (0 != sample_app_init()) {
    fprintf(stderr, "can't init the sample app\n");
    return 1;
  }

  client_id = ulapi_socket_get_client_id(port, host);
  if (client_id < 0) {
    fprintf(stderr, "can't connect to port %d\n", (int) port);
    ulapi_exit();
    return 1;
  }
  if (debug) {
    printf("serving port %d\n", (int) port);
  }

  client_db.mutex = ulapi_mutex_new(0);
  client_db.number = 0;

  client_task = ulapi_task_new();
  client_args_ptr = reinterpret_cast<client_args *>(malloc(sizeof(client_args)));
  client_args_ptr->client_task = client_task;
  client_args_ptr->client_id = client_id;
  client_args_ptr->client_db_ptr = &client_db;
  client_args_ptr->debug = debug;
  ulapi_task_start(client_task, client_code, client_args_ptr, ulapi_prio_lowest(), 0);

  /* enter application main loop */
  while (!feof(stdin)) {

    if (NULL == fgets(buffer, sizeof(buffer), stdin)) {
      break;
    }

    ptr = buffer;
    while (isspace(*ptr)) ptr++;

    if ('q' == *ptr) break;

    if (0 == *ptr) {
      ulapi_mutex_take(client_db.mutex);
      number = client_db.number;
      ulapi_mutex_give(client_db.mutex);
      printf("%d\n", number);
      continue;
    }

    if (1 == sscanf(ptr, "%d", &number)) {
      ulapi_mutex_take(client_db.mutex);
      client_db.number = number;
      ulapi_mutex_give(client_db.mutex);
      continue;
    }
  }

  ulapi_socket_close(client_id);

  ulapi_exit();

  sample_app_exit();

  return 0;
}
int main(int argc, char *argv[])
{
  int ros_argc;
  char **ros_argv;
  std::string node_name(NODE_NAME_DEFAULT);
  int option;
  int ival;
  double dval;
  int ws_port = WS_PORT_DEFAULT;
  int emove_port = EMOVE_PORT_DEFAULT;
  double period = 1;
  int ws_server_id;
  int ws_connection_id;
  ulapi_task_struct *ws_client_read_task;
  ws_client_read_task_args *ws_client_read_args; 
  ulapi_task_struct *ws_client_write_task;
  ws_client_write_task_args *ws_client_write_args; 
  int emove_server_id;
  ulapi_task_struct emove_server_task;
  emove_server_task_args emove_server_args; 
  enum {INBUF_LEN = 1024};
  char inbuf[INBUF_LEN];

  opterr = 0;
  while (true) {
    option = getopt(argc, argv, ":i:n:p:t:Whd");
    if (option == -1) break;

    switch (option) {
    case 'i':
      inifile_name = std::string(optarg);
      break;

    case 'n':
      // first check for valid name
      if (optarg[0] == '-') {
	fprintf(stderr, "invalid node name: %s\n", optarg);
	return 1;
      }
      node_name = std::string(optarg);
      break;

    case 'w':
      ival = atoi(optarg);
      ws_port = ival;
      break;

    case 'e':
      ival = atoi(optarg);
      emove_port = ival;
      break;

    case 't':
      dval = atof(optarg);
      if (dval < FLT_EPSILON) {
	fprintf(stderr, "bad value for period: %s\n", optarg);
	return 1;
      }
      period = dval;
      break;

      // FIXME -- ipad
    case 'W':
      ws_all = true;
      break;

    case 'h':
      print_help();
      break;

    case 'd':
      debug = true;
      break;

    case ':':
      fprintf(stderr, "missing value for -%c\n", optopt);
      return 1;
      break;

    default:
      fprintf (stderr, "unrecognized option -%c\n", optopt);
      return 1;
      break;
    } // switch (option)
  }   // while (true) for getopt

  if (ULAPI_OK != ulapi_init()) {
    fprintf(stderr, "can't init ulapi\n");
    return 1;
  }

  if (! inifile_name.empty()) {
    if (0 != ini_load(inifile_name, &ws_port, &emove_port)) {
      fprintf(stderr, "error reading ini file %s\n", inifile_name.c_str());
      return 1;
    }
  }

  ulapi_mutex_init(&ws_stat_mutex, 1);
  ulapi_mutex_init(&emove_stat_mutex, 1);

  // pass everything after a '--' separator to ROS
  ros_argc = argc - optind;
  ros_argv = &argv[optind];
  ros::init(ros_argc, ros_argv, node_name);

  ros::NodeHandle nh;
  ros::Subscriber wssub;
  ros::Subscriber emovesub;
  wssub = nh.subscribe(KITTING_WS_STAT_TOPIC, TOPIC_QUEUE_LEN, ws_stat_callback);
  emovesub = nh.subscribe(KITTING_EMOVE_STAT_TOPIC, TOPIC_QUEUE_LEN, emove_stat_callback);

  /*
    Run two processes, one that serves up the Workstation-level commands
    and status, and the other that serves up the Emove status. This main
    thread will handle the Workstation level.
   */

  ws_server_id = ulapi_socket_get_server_id(ws_port);
  if (ws_server_id < 0) {
    fprintf(stderr, "can't serve WS port %d\n", ws_port);
    return 1;
  }

  emove_server_id = ulapi_socket_get_server_id(emove_port);
  if (emove_server_id < 0) {
    fprintf(stderr, "can't serve Emove port %d\n", emove_port);
    return 1;
  }

  ulapi_task_init(&emove_server_task);
  emove_server_args.task = &emove_server_task;
  emove_server_args.id = emove_server_id;
  emove_server_args.period_nsecs = (int) (period * 1.0e9);
  ulapi_task_start(&emove_server_task, reinterpret_cast<ulapi_task_code>(emove_server_task_code), reinterpret_cast<void *>(&emove_server_args), ulapi_prio_highest(), 0);

  bool done = false;
  while (! done) {
    if (debug) printf("waiting for a WS HMI connection on %d...\n", ws_server_id);
    ws_connection_id = ulapi_socket_get_connection_id(ws_server_id);
    if (ws_connection_id < 0) {
      fprintf(stderr, "can't get a WS HMI connection\n");
      break;
    }
     
    if (debug) printf("got a WS HMI connection on id %d\n", ws_connection_id);

    // spawn connection tasks for reading and writing

    ws_client_read_task = reinterpret_cast<ulapi_task_struct *>(malloc(sizeof(*ws_client_read_task)));
    ws_client_read_args = reinterpret_cast<ws_client_read_task_args *>(malloc(sizeof(*ws_client_read_args)));

    ulapi_task_init(ws_client_read_task);
    ws_client_read_args->task = ws_client_read_task;
    ws_client_read_args->id = ws_connection_id;
    ulapi_task_start(ws_client_read_task, reinterpret_cast<ulapi_task_code>(ws_client_read_task_code), reinterpret_cast<void *>(ws_client_read_args), ulapi_prio_highest(), 0);

    ws_client_write_task = reinterpret_cast<ulapi_task_struct *>(malloc(sizeof(*ws_client_write_task)));
    ws_client_write_args = reinterpret_cast<ws_client_write_task_args *>(malloc(sizeof(*ws_client_write_args)));

    ulapi_task_init(ws_client_write_task);
    ws_client_write_args->task = ws_client_write_task;
    ws_client_write_args->id = ws_connection_id;
    ws_client_write_args->period_nsecs = (int) (period * 1.0e9);
    ulapi_task_start(ws_client_write_task, reinterpret_cast<ulapi_task_code>(ws_client_write_task_code), reinterpret_cast<void *>(ws_client_write_args), ulapi_prio_highest(), 0);

  } // while (true)

  ulapi_socket_close(ws_server_id);

  if (debug) printf("server on %d done\n", ws_server_id);
  
  return 0;
}
Beispiel #9
0
static void request_connection_thread_code(request_connection_thread_args *args)
{
  void *thread;
  int id;
  enum {INBUF_LEN = 1024};
  char inbuf[INBUF_LEN];
  enum {OUTBUF_LEN = 1024};
  char outbuf[OUTBUF_LEN];
  char *ptr;
  int nchars;
  int nleft;
  int length;
  int message_type;
  ping_reply_message pingrep;
  joint_traj_pt_request_message jtreq;
  joint_traj_pt_reply_message jtrep;
  joint_info jinfo;
  cart_traj_pt_request_message ctreq;
  cart_traj_pt_reply_message ctrep;
  float f1, f2, f3, f4, f5, f6, f7;

  thread = args->thread;
  id = args->id;
  free(args);

  bool done = false;
  while (! done) {
    nchars = ulapi_socket_read(id, inbuf, sizeof(inbuf));
    if (nchars <= 0) break;
    inbuf[sizeof(inbuf)-1] = 0;
    ptr = inbuf;
    nleft = nchars;

    while (nleft > 0) {
      // get the length and type of the message
      memcpy(&length, ptr, sizeof(length));
      memcpy(&message_type, ptr + sizeof(length), sizeof(message_type));

      // switch on the message type and handle it
      switch (message_type) {
      case MESSAGE_PING:
	nchars = ulapi_socket_write(id, reinterpret_cast<char *>(&pingrep), sizeof(pingrep));
	if (nchars < 0) done = true;
	break;

      case MESSAGE_JOINT_TRAJ_PT:
	jtreq.read_joint_traj_pt_request(ptr);
	if (debug) {
	  printf("connection %d requested:\n", id);
	  jtreq.print_joint_traj_pt_request();
	}
	ulapi_mutex_take(&robot_mutex);
	for (int t = 0; t < JOINT_MAX; t++) {
	  if (! jtreq.get_pos(&f1, t)) break;
	  the_robot.set_robot_joint_pos(f1, t);
	}

	// FIXME -- testing with a delay, should thread this off
	ulapi_sleep(3);

	ulapi_mutex_give(&robot_mutex);
	jtrep.set_joint_traj_pt_reply(REPLY_SUCCESS);
	nchars = ulapi_socket_write(id, reinterpret_cast<char *>(&jtrep), sizeof(jtrep));
	if (debug) {
	  printf("replied to connection %d:\n", id);
	  jtrep.print_joint_traj_pt_reply();
	}
	if (nchars < 0) done = true;
	break;

      case MESSAGE_CART_TRAJ_PT:
	ctreq.read_cart_traj_pt_request(ptr);
	if (debug) {
	  printf("connection %d requested:\n", id);
	  ctreq.print_cart_traj_pt_request();
	}

	// FIXME -- ditto
	ulapi_sleep(3);

	ulapi_mutex_take(&robot_mutex);
	if (! ctreq.get_pos(&f1, &f2, &f3, &f4, &f5, &f6, &f7)) break;
	the_robot.set_robot_cart_pos(f1, f2, f3, f4, f5, f6, f7);
	ulapi_mutex_give(&robot_mutex);
	ctrep.set_seq_number(ctreq.get_seq_number());
	ctrep.set_cart_traj_pt_reply(REPLY_SUCCESS);
	nchars = ulapi_socket_write(id, reinterpret_cast<char *>(&ctrep), sizeof(ctrep));
	if (debug) {
	  printf("replied to connection %d:\n", id);
	  ctrep.print_cart_traj_pt_reply();
	}
	if (nchars < 0) done = true;
	break;

      default:
	// unknown message
	if (debug) printf("unknown message type: %d\n", message_type);
	break;
      } // switch (message type)
      nleft -= (sizeof(length) + length);
      ptr += (sizeof(length) + length);
    }	// while (nleft)
  }	// while (true)

  ulapi_socket_close(id);

  if (debug) printf("simple message connection handler %d done\n", id);

  free(thread);

  return;
}
Beispiel #10
0
static void state_connection_thread_code(state_connection_thread_args *args)
{
  void *thread;
  int id;
  double period;
  joint_traj_pt_state_message jsmsg;
  robot_status_message rsmsg;
  object_state_message obj_state(3);
  enum {OUTBUF_LEN = 1024};
  char outbuf[OUTBUF_LEN];
  int nchars, len;
  int which = 0;

  thread = args->thread;
  id = args->id;
  period = args->period;
  free(args);

  for (int t = 0; t < obj_state.object_number(); t++) {
    obj_state.objects[t].id = t + 1;
    obj_state.objects[t].x = t + 1.2;
    obj_state.objects[t].y = t - 3.4;
    obj_state.objects[t].z = t + 5.6;
    obj_state.objects[t].qx = 0;
    obj_state.objects[t].qy = 0;
    obj_state.objects[t].qz = sin(0.1*t);
    obj_state.objects[t].qw = cos(0.1*t);
  }

  while (true) {
    float p;
    int i;

    switch (which) {
    case 0:
      ulapi_mutex_take(&robot_mutex);
      for (int t = 0; t < JOINT_MAX; t++) {
	if (the_robot.get_robot_joint_pos(&p, t)) jsmsg.set_pos(p, t);
      }
      ulapi_mutex_give(&robot_mutex);
      nchars = ulapi_socket_write(id, reinterpret_cast<char *>(&jsmsg), sizeof(jsmsg));
      if (nchars < 0) break;
      break;

    case 1:
      ulapi_mutex_take(&robot_mutex);

      the_robot.get_drives_powered(&i);
      rsmsg.set_drives_powered(i);

      the_robot.get_e_stopped(&i);
      rsmsg.set_e_stopped(i);

      the_robot.get_in_error(&i);
      rsmsg.set_in_error(i);

      the_robot.get_in_motion(&i);
      rsmsg.set_in_motion(i);

      the_robot.get_mode(&i);
      rsmsg.set_mode(i);

      the_robot.get_motion_possible(&i);
      rsmsg.set_motion_possible(i);

      ulapi_mutex_give(&robot_mutex);

      nchars = ulapi_socket_write(id, reinterpret_cast<char *>(&rsmsg), sizeof(rsmsg));
      if (nchars < 0) break;
      break;

    case 2:
#undef DO_OBJECTS
#ifdef DO_OBJECTS
      len = obj_state.size();
      if (len <= sizeof(outbuf)) {
	obj_state.write_object_state(outbuf, sizeof(outbuf));
	nchars = ulapi_socket_write(id, outbuf, len);
      }
#endif	// DO_OBJECTS
      break;
    } // switch (which)

    if (++which > 2) which = 0;

    ulapi_wait(period * 1e9);
  } // while (true)

  ulapi_socket_close(id);

  if (debug) printf("joint state connection handler %d done\n", id);

  free(thread);

  return;
}