<commit_before>#include <math.h>
#include <robot_p/robothw.h>
#include <boost/math/constants/constants.hpp>

using namespace robotp;

RobotHW::RobotHW(ros::NodeHandle &nh_private)
  : jnt_state_interface()
  , jnt_vel_interface()
  , serial_()
  , ticks_per_revolution_(258)
  , input_buff_()
  , alarm_(false)

{
  // connect and register the joint state interface
  hardware_interface::JointStateHandle state_handle_left(
      "front_left_wheel", &pos_[LEFT], &vel_[LEFT], &eff_[LEFT]);
  jnt_state_interface.registerHandle(state_handle_left);

  hardware_interface::JointStateHandle state_handle_right(
      "front_right_wheel", &pos_[RIGHT], &vel_[RIGHT], &eff_[RIGHT]);
  jnt_state_interface.registerHandle(state_handle_right);

  registerInterface(&jnt_state_interface);

  // connect and register the joint position interface
  hardware_interface::JointHandle vel_handle_left(
      jnt_state_interface.getHandle("front_left_wheel"), &cmd_[LEFT]);
  jnt_vel_interface.registerHandle(vel_handle_left);

  hardware_interface::JointHandle vel_handle_right(
      jnt_state_interface.getHandle("front_right_wheel"), &cmd_[RIGHT]);
  jnt_vel_interface.registerHandle(vel_handle_right);

  registerInterface(&jnt_vel_interface);

  // initialize parameters
  std::string port;
  float Kp, Ki, Kd;

  nh_private.param<double>("ticks_per_revolution", ticks_per_revolution_, 258);

  nh_private.param<std::string>("serial_port", port, "/dev/ttyS0");
  nh_private.param<float>("Kp", Kp, 1);
  nh_private.param<float>("Ki", Kd, 0.3);
  nh_private.param<float>("Kd", Ki, 0.5);

  serial_.setPort(port);
  serial_.setBaudrate(115200);
  try {
    serial_.open();
  } catch (const std::exception& e) {
    ROS_ERROR_STREAM("[ROBOTHW]: Serial exception: " << e.what());
    return;
  }
  // serial_.setRTS(false);
  // serial_.setDTR(false);
  sendMsg(createStartMsg());
  sendMsg(createPIDMsg('P', Kp));
  sendMsg(createPIDMsg('I', Ki));
  sendMsg(createPIDMsg('D', Kd));
}

robotp::RobotHW::~RobotHW()
{
  sendMsg(createStopMsg());
}

void RobotHW::read(const ros::Time &time, const ros::Duration &period)
{
  if (serial_.available() > 0) {<commit_msg>set proper timeout to serial<commit_after>#include <math.h>
#include <robot_p/robothw.h>
#include <boost/math/constants/constants.hpp>

using namespace robotp;

RobotHW::RobotHW(ros::NodeHandle &nh_private)
  : jnt_state_interface()
  , jnt_vel_interface()
  , serial_()
  , ticks_per_revolution_(258)
  , input_buff_()
  , alarm_(false)

{
  // connect and register the joint state interface
  hardware_interface::JointStateHandle state_handle_left(
      "front_left_wheel", &pos_[LEFT], &vel_[LEFT], &eff_[LEFT]);
  jnt_state_interface.registerHandle(state_handle_left);

  hardware_interface::JointStateHandle state_handle_right(
      "front_right_wheel", &pos_[RIGHT], &vel_[RIGHT], &eff_[RIGHT]);
  jnt_state_interface.registerHandle(state_handle_right);

  registerInterface(&jnt_state_interface);

  // connect and register the joint position interface
  hardware_interface::JointHandle vel_handle_left(
      jnt_state_interface.getHandle("front_left_wheel"), &cmd_[LEFT]);
  jnt_vel_interface.registerHandle(vel_handle_left);

  hardware_interface::JointHandle vel_handle_right(
      jnt_state_interface.getHandle("front_right_wheel"), &cmd_[RIGHT]);
  jnt_vel_interface.registerHandle(vel_handle_right);

  registerInterface(&jnt_vel_interface);

  // initialize parameters
  std::string port;
  float Kp, Ki, Kd;

  nh_private.param<double>("ticks_per_revolution", ticks_per_revolution_, 258);

  nh_private.param<std::string>("serial_port", port, "/dev/ttyS0");
  nh_private.param<float>("Kp", Kp, 1);
  nh_private.param<float>("Ki", Kd, 0.3);
  nh_private.param<float>("Kd", Ki, 0.5);

  serial_.setPort(port);
  serial_.setBaudrate(115200);
  auto timeout = serial::Timeout::simpleTimeout(serial::Timeout::max());
  serial_.setTimeout(timeout);
  try {
    serial_.open();
  } catch (const std::exception& e) {
    ROS_ERROR_STREAM("[ROBOTHW]: Serial exception: " << e.what());
    return;
  }
  // serial_.setRTS(false);
  // serial_.setDTR(false);
  sendMsg(createStartMsg());
  sendMsg(createPIDMsg('P', Kp));
  sendMsg(createPIDMsg('I', Ki));
  sendMsg(createPIDMsg('D', Kd));
}

robotp::RobotHW::~RobotHW()
{
  sendMsg(createStopMsg());
}

void RobotHW::read(const ros::Time &time, const ros::Duration &period)
{
  if (serial_.available() > 0) {
{'fraction_non_alphanumeric': 0.14767836919592298, 'fraction_numerical': 0.008607021517553794, 'mean_word_length': 6.088282504012841, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5675350', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1533, 'n_words': 236}