motor_serial.cc
Go to the documentation of this file.
1 
31 #include <ros/ros.h>
32 #include <serial/serial.h>
34 
35 MotorSerial::MotorSerial(const std::string& port, uint32_t baud_rate)
36  : motors(port, baud_rate, serial::Timeout::simpleTimeout(100)),
37  serial_errors(0), error_threshold(20) {
38  serial_thread = boost::thread(&MotorSerial::SerialThread, this);
39 }
40 
42  serial_thread.interrupt();
43  serial_thread.join();
44  motors.close();
45 }
46 
48  RawMotorMessage out = command.serialize();
49  ROS_DEBUG("out %02x %02x %02x %02x %02x %02x %02x %02x", out[0], out[1],
50  out[2], out[3], out[4], out[5], out[6], out[7]);
51  motors.write(out.c_array(), out.size());
52  return 0;
53 }
54 
55 int MotorSerial::transmitCommands(const std::vector<MotorMessage>& commands) {
56  for (auto& command : commands) {
57  RawMotorMessage out = command.serialize();
58  ROS_DEBUG("out %02x %02x %02x %02x %02x %02x %02x %02x", out[0], out[1],
59  out[2], out[3], out[4], out[5], out[6], out[7]);
60  motors.write(out.c_array(), out.size());
61  boost::this_thread::sleep(boost::posix_time::milliseconds(2));
62  }
63  return 0;
64 }
65 
67  MotorMessage mc;
68  if (!this->output.empty()) {
69  mc = output.front_pop();
70  }
71  return mc;
72 }
73 
75 
77 
79  try {
80  while (motors.isOpen()) {
81  boost::this_thread::interruption_point();
82  if (motors.waitReadable()) {
83  RawMotorMessage innew = {0, 0, 0, 0, 0, 0, 0, 0};
84 
85  motors.read(innew.c_array(), 1);
86  if (innew[0] != MotorMessage::delimeter) {
87  // The first byte was not the delimiter, so re-loop
89  ROS_WARN("REJECT %02x", innew[0]);
90  }
91  continue;
92  }
93 
94  // This will wait for the transmission time of 8 bytes
95  motors.waitByteTimes(innew.size());
96 
97  // Read in next 7 bytes
98  motors.read(&innew.c_array()[1], 7);
99  ROS_DEBUG("Got message %x %x %x %x %x %x %x %x", innew[0],
100  innew[1], innew[2], innew[3], innew[4], innew[5],
101  innew[6], innew[7]);
102 
103  MotorMessage mc;
104  int error_code = mc.deserialize(innew);
105  if (error_code == 0) {
106  appendOutput(mc);
107  if (mc.getType() == MotorMessage::TYPE_ERROR) {
108  ROS_ERROR("GOT error from Firm 0x%02x",
109  mc.getRegister());
110  }
111  } else {
112  if (++serial_errors > error_threshold) {
113  if (error_code == MotorMessage::ERR_UNKNOWN_REGISTER) {
114  ROS_WARN_ONCE("Message deserialize found an unrecognized firmware register");
115  } else {
116  ROS_ERROR("DESERIALIZATION ERROR! - %d", error_code);
117  }
118  }
119  }
120  }
121  }
122 
123  } catch (const boost::thread_interrupted& e) {
124  motors.close();
125  } catch (const serial::IOException& e) {
126  ROS_ERROR("%s", e.what());
127  } catch (const serial::PortNotOpenedException& e) {
128  ROS_ERROR("%s", e.what());
129  } catch (...) {
130  ROS_ERROR("Unknown Error");
131  throw;
132  }
133 }
static const uint8_t delimeter
int transmitCommand(MotorMessage command)
Definition: motor_serial.cc:47
boost::array< uint8_t, 8 > RawMotorMessage
Definition: motor_message.h:38
int transmitCommands(const std::vector< MotorMessage > &commands)
Definition: motor_serial.cc:55
serial::Serial motors
Definition: motor_serial.h:59
#define ROS_WARN(...)
bool fast_empty() const
Definition: shared_queue.h:117
int commandAvailable()
Definition: motor_serial.cc:74
bool empty() const
Definition: shared_queue.h:112
void push(const T &value)
Definition: shared_queue.h:68
MotorMessage::MessageTypes getType() const
int error_threshold
Definition: motor_serial.h:68
ROSLIB_DECL std::string command(const std::string &cmd)
MotorSerial(const std::string &port="/dev/ttyUSB0", uint32_t baud_rate=9600)
Definition: motor_serial.cc:35
#define ROS_WARN_ONCE(...)
boost::thread serial_thread
Definition: motor_serial.h:63
MotorMessage::ErrorCodes deserialize(const RawMotorMessage &serialized)
shared_queue< MotorMessage > output
Definition: motor_serial.h:61
RawMotorMessage serialize() const
MotorMessage receiveCommand()
Definition: motor_serial.cc:66
void appendOutput(MotorMessage command)
Definition: motor_serial.cc:76
int serial_errors
Definition: motor_serial.h:67
void SerialThread()
Definition: motor_serial.cc:78
#define ROS_ERROR(...)
MotorMessage::Registers getRegister() const
#define ROS_DEBUG(...)


ubiquity_motor
Author(s):
autogenerated on Mon Feb 28 2022 23:57:06