Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyboard space for pause/resume when playing ros2 bag #871

Closed
liuxw7 opened this issue Sep 13, 2021 · 1 comment
Closed

keyboard space for pause/resume when playing ros2 bag #871

liuxw7 opened this issue Sep 13, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@liuxw7
Copy link

liuxw7 commented Sep 13, 2021

modify code for player.cpp in rosbag2_transport

modify function:


void Player::play_messages_until_queue_empty(const PlayOptions & options)
{
  // 第一次加载执行线异步线程函数
  if (is_storage_completely_loaded()) {
    return;
  }
  ReplayableMessage message;

  float rate = 1.0;
  // Use rate if in valid range
  if (options.rate > 0.0) {
    rate = options.rate;
  }
  // 开始不暂停:paused = false;
  std::chrono::system_clock::time_point pause_true_time;
  std::chrono::system_clock::time_point pause_false_time;
  std::chrono::nanoseconds sum_delta_time(0);
  while (rclcpp::ok() && message_queue_.size_approx()) {
    int c = scan_keyboard();
    if (c == ' ') {
      paused_.store(!paused_.load());
      if (paused_.load() == true) {
        pause_true_time = std::chrono::system_clock::now();
      } else {
        pause_false_time = std::chrono::system_clock::now();
        std::chrono::nanoseconds delta =  std::chrono::duration_cast<std::chrono::nanoseconds>(pause_false_time - pause_true_time);
        sum_delta_time += delta;
      }
    }
    
    if (paused_.load() == true) {
      if (rclcpp::ok()) {
        auto publisher_iter = publishers_.find(message.message->topic_name);
        if (publisher_iter != publishers_.end()) {
          publisher_iter->second->publish(message.message->serialized_data);
        }
      }
      std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<long>((1.0 / rate) * 1000)));
    } else { // 未暂停
        if (message_queue_.try_dequeue(message)) { // 有数据可用
          std::this_thread::sleep_until(start_time_ + sum_delta_time +          
              std::chrono::duration_cast<std::chrono::nanoseconds>(1.0 / rate * message.time_since_start));
            auto publisher_iter = publishers_.find(message.message->topic_name);
        if (publisher_iter != publishers_.end()) {
          publisher_iter->second->publish(message.message->serialized_data);
        }
      }
    }
  }
}

add new function

int Player::scan_keyboard(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;

  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  newt.c_cc[VTIME] = 0;
  newt.c_cc[VMIN] = 1;
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  
  ch = getchar();
  
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);

  if(ch != EOF)
  {
    return ch;
  } 
  return 0;
}

in ros2 foxy, I has tested it in location. I hope it usefully for someone.

@liuxw7 liuxw7 added the bug Something isn't working label Sep 13, 2021
@emersonknapp
Copy link
Collaborator

Closing as duplicate of #55

Note: we will be using https://github.com/ros-tooling/keyboard_handler package to manage keyboard handling, to avoid putting any of that OS-specific code in rosbag2 Player.

#847 is already in progress to implement this feature - it is awaiting a full release of keyboard_handler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants