예제 #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_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;
}
예제 #3
0
rtapi_result rtapi_task_init(rtapi_task_struct *task)
{
  return ULAPI_OK == ulapi_task_init(task) ? RTAPI_OK : RTAPI_ERROR;
}
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;
}
예제 #5
0
int main(int argc, char *argv[])
{
  bool done;
  int option;
  int cmdConnection;
  int script = 1;
  char msgOut[buffSize];
  RCS_TIMER *cycleBlock = new RCS_TIMER(KUKA_DEFAULT_CYCLE);
  CRCLStatus status;
  ulapi_task_struct statusTask;
  RobotStatus robotStatus;
  GripperStatus gripperStatus;
  bool useFile = false;
  char *fileName;
  std::ifstream inFile;
  std::istream *inputSource;
  float counter = 0;

  debug = 1;
  while (true) 
    {
      option = getopt(argc, argv, ":cdf:");
      if (option == -1) break;
      switch (option) 
	{
	case 'd':
	  debug = 1;
	  break;
	case 'c':
	  script = 0;
	  break;
	case 'f':
	  script = 0;
	  useFile = true;
	  fileName = optarg;
	  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

  // this code uses the ULAPI library to provide portability
  // between different operating systems and architectures
  if (ULAPI_OK != ulapi_init())
    {
      printf ("crclClient:: can't initialize ulapi");
      return 1;
    }
  cmdConnection = ulapi_socket_get_client_id(CRCL_CMD_PORT_DEFAULT, 
					     "localhost");
  if(cmdConnection < 0)
    return -1;

  // start status thread
  ulapi_task_init(&statusTask);
  ulapi_task_start(&statusTask, 
		   reinterpret_cast<ulapi_task_code>(statusThread), 
		   reinterpret_cast<void*>(&status), 
		   ulapi_prio_lowest(), 0);
  if(!script)
    {
      if( useFile )
	{
	  inFile.open(fileName);
	  inputSource = &inFile;
	  if( !inputSource->good() )
	    {
	      printf( "unable to open %s\n", fileName );
	      exit(1);
	    }
	}
      else
	inputSource = &std::cin;
      while(inputSource->good())
	{
	  inputSource->getline(msgOut, buffSize);
	  if( msgOut[0] == ' ' || msgOut[0] == '/' )
	    continue;
	  ulapi_socket_write(cmdConnection, msgOut, strlen(msgOut));
	  printf( "Sent: %s\n", msgOut);
	  done = false;
	  if(useFile)
	    {
	      for(int ii=0; ii<5; ii++)
		cycleBlock->wait();
	    }
	  while( !done )
	    {
	      cycleBlock->wait();
	      gripperStatus = status.getGripperStatus();
	      robotStatus = status.getRobotStatus();
	      if( !useFile || status.getCurrentStatus()==CRCL_DONE )
		done = true;
	      //	      printf( "Fmod: %f\n", fmod(counter, 100.));
	      if( fmod(counter++, 100.)==0 || done )
		{
		  printf( "Status Cmd: %s Status: %s\n", 
			  getCRCLCmdString((status.getCurrentCmd()).cmd).c_str(),
			  getCRCLStatusString(status.getCurrentStatus()).c_str());
		  printf( "\x1b[32mCart Status: <%3.4f, %3.4f, %3.4f> <%3.4f, %3.4f, %3.4f>\x1b[0m\n",
			  robotStatus.pose.x, robotStatus.pose.y, 
			  robotStatus.pose.z,
			  robotStatus.pose.xrot, robotStatus.pose.yrot, 
			  robotStatus.pose.zrot);
		  printf( "\x1b[32mJoint Status: <%4.2f %4.2f %4.2f %4.2f %4.2f %4.2f>\x1b[0m\n",
			  robotStatus.joint[0],
			  robotStatus.joint[1],
			  robotStatus.joint[2],
			  robotStatus.joint[3],
			  robotStatus.joint[4],
			  robotStatus.joint[5]);
		}
	    }
	}
    }
  for(int i=0; i<1; i++ )
    {
      ulapi_strncpy(msgOut, "InitCanon", strlen("InitCanon"));
      ulapi_socket_write(cmdConnection, msgOut, strlen(msgOut));
      /*
      printf( "crclClient writing: %s\n", msgOut.c_str());
      sleep(2);
      msgOut = "MoveTo 1. 2. 3. 4. 5. 6.";
      ulapi_socket_write(cmdConnection, msgOut.c_str(), msgOut.length());
      printf( "crclClient writing: %s\n", msgOut.c_str());
      sleep(2);
      msgOut = "Dwell 1";
      ulapi_socket_write(cmdConnection, msgOut.c_str(), msgOut.length());
      printf( "crclClient writing: %s\n", msgOut.c_str());
      sleep(2);
      msgOut = "MoveTo 7. 8. 9. 10. 11. 12.";
      ulapi_socket_write(cmdConnection, msgOut.c_str(), msgOut.length());
      printf( "crclClient writing: %s\n", msgOut.c_str());
      sleep(2);
      msgOut = "Dwell 4";
      ulapi_socket_write(cmdConnection, msgOut.c_str(), msgOut.length());
      printf( "crclClient writing: %s\n", msgOut.c_str());
      sleep(2);
      msgOut = "EndCanon";
      ulapi_socket_write(cmdConnection, msgOut.c_str(), msgOut.length());
      printf( "crclClient writing: %s\n", msgOut.c_str());
      sleep(2);
      */
      cycleBlock->wait();
    }
}
예제 #6
0
int main(int argc, char *argv[])
{
  int option;
  int ival;
  double dval;
  int message_port = MESSAGE_PORT_DEFAULT;
  int state_port = STATE_PORT_DEFAULT;
  int request_server_id;
  int state_server_id;
  ulapi_task_struct request_server_thread;
  ulapi_task_struct state_server_thread;
  request_server_thread_args request_server_args; 
  state_server_thread_args state_server_args; 
  double period = 0.2;		// ROS recommends 2 Hz at least
  enum {INBUF_LEN = 1024};
  char inbuf[INBUF_LEN];

  opterr = 0;
  while (true) {
    option = getopt(argc, argv, ":m:s:t:d?");
    if (option == -1) break;

    switch (option) {
    case 'm':
      ival = atoi(optarg);
      message_port = ival;
      break;

    case 's':
      ival = atoi(optarg);
      state_port = ival;
      break;

    case 't':
      dval = atof(optarg);
      period = dval;
      break;

    case 'd':
      debug = true;
      break;

    case '?':
      print_help();
      return 0;
      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

  ulapi_mutex_init(&robot_mutex, 1);
  the_robot.print_robot_status();

  request_server_id = ulapi_socket_get_server_id(message_port);
  if (request_server_id < 0) {
    fprintf(stderr, "can't serve port %d\n", message_port);
    return 1;
  }
  ulapi_task_init(&request_server_thread);
  request_server_args.id = request_server_id;
  ulapi_task_start(&request_server_thread, reinterpret_cast<ulapi_task_code>(request_server_thread_code), reinterpret_cast<void *>(&request_server_args), ulapi_prio_highest(), 0);

  state_server_id = ulapi_socket_get_server_id(state_port);
  if (state_server_id < 0) {
    fprintf(stderr, "can't serve port %d\n", state_port);
    return 1;
  }
  ulapi_task_init(&state_server_thread);
  state_server_args.id = state_server_id;
  state_server_args.period = period;
  ulapi_task_start(&state_server_thread, reinterpret_cast<ulapi_task_code>(state_server_thread_code), reinterpret_cast<void *>(&state_server_args), ulapi_prio_highest(), 0);

  bool done = false;
  bool print_robot_info = true;

  while (! done) {
    char *ptr;
    char *endptr;
    int ival;
    float fval;
    int ret;
    int joint;

    // print prompt
    printf("> ");
    fflush(stdout);
    // get input line
    if (NULL == fgets(inbuf, sizeof(inbuf), stdin)) break;
    // skip leading whitespace
    ptr = inbuf;
    while (isspace(*ptr)) ptr++;
    // strip off trailing whitespace
    endptr = inbuf + strlen(inbuf);
    while ((isspace(*endptr) || 0 == *endptr) && endptr >= ptr) *endptr-- = 0;
    while (endptr >= ptr) *endptr = tolower(*endptr--);
    // now 'ptr' is the stripped input string

    do {
      if (0 == *ptr) {		// blank line, print state
	ulapi_mutex_take(&robot_mutex);
	if (print_robot_info) {
	  the_robot.print_robot_info();
	} else {
	  the_robot.print_robot_status();
	}
	ulapi_mutex_give(&robot_mutex);
	break;
      }

      if ('q' == *ptr) {	// quit
	done = true;
	break;
      }

      if ('d' == *ptr) {	// quit
	debug = ! debug;
	break;
      }

      if ('i' == *ptr) {
	print_robot_info = true;
      }

      if ('s' == *ptr) {
	print_robot_info = false;
      }

      if ('?' == *ptr) {	// help
	print_help();
	break;
      }

      if (! strncmp(ptr, "set", strlen("set"))) {
	ptr += strlen("set");
	if (! isspace(*ptr)) {
	  printf("need a joint and value to set\n");
	  break;
	}
	ival = strtol(ptr, &endptr, 0);
	if (endptr == ptr || isgraph(*endptr)) {
	  printf("need a joint to set\n");
	  break;
	}
	joint = ival;
	ptr = endptr;
	while (isspace(*ptr)) ptr++;

	if (! strncmp(ptr, "pos", strlen("pos"))) {
	  ptr += strlen("pos");
	  if (! isspace(*ptr)) {
	    printf("missing position value\n");
	    break;
	  }
	  fval = strtod(ptr, &endptr);
	  if (endptr == ptr || isgraph(*endptr)) {
	    printf("bad position value\n");
	    break;
	  }
	  ulapi_mutex_take(&robot_mutex);
	  ret = the_robot.set_robot_joint_pos(fval, joint-1);
	  ulapi_mutex_give(&robot_mutex);
	  if (! ret) {
	    printf("can't set joint %d position to %f\n", joint, dval);
	  }
	  break;
	}

	if (! strncmp(ptr, "pmin", strlen("pmin"))) {
	  ptr += strlen("pmin");
	  if (! isspace(*ptr)) {
	    printf("missing min value\n");
	    break;
	  }
	  fval = strtod(ptr, &endptr);
	  if (endptr == ptr || isgraph(*endptr)) {
	    printf("bad min value\n");
	    break;
	  }
	  ulapi_mutex_take(&robot_mutex);
	  ret = the_robot.set_robot_joint_min(fval, joint-1);
	  ulapi_mutex_give(&robot_mutex);
	  if (! ret) {
	    printf("can't set joint %d min value to %f\n", joint, dval);
	  }
	  break;
	}

	if (! strncmp(ptr, "pmax", strlen("pmax"))) {
	  ptr += strlen("pmax");
	  if (! isspace(*ptr)) {
	    printf("missing max value\n");
	    break;
	  }
	  fval = strtod(ptr, &endptr);
	  if (endptr == ptr || isgraph(*endptr)) {
	    printf("bad max value\n");
	    break;
	  }
	  ulapi_mutex_take(&robot_mutex);
	  ret = the_robot.set_robot_joint_max(fval, joint-1);
	  ulapi_mutex_give(&robot_mutex);
	  if (! ret) {
	    printf("can't set joint %d max value to %f\n", joint, dval);
	  }
	  break;
	}

	printf("need 'pos', 'pmin', or 'pmax' to set\n");
	break;
      }	// matches "set"
      printf("?\n");
    } while (false); // do ... wrapper
  } // while (! done)

  return 0;
}