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  double loopRate)
37  : motors(port, baud_rate, serial::Timeout::simpleTimeout(10000)),
38  serial_loop_rate(loopRate) {
39  serial_thread = boost::thread(&MotorSerial::SerialThread, this);
40 }
41 
43  serial_thread.interrupt();
44  serial_thread.join();
45  motors.close();
46 }
47 
49  input.push(command); // add latest command to end of fifo
50  return 0;
51 }
52 
53 int MotorSerial::transmitCommands(const std::vector<MotorMessage>& commands) {
54  input.push(commands);
55  return 0;
56 }
57 
59  MotorMessage mc;
60  if (!this->output.empty()) {
61  mc = output.front_pop();
62  }
63 
64  return mc;
65 }
66 
68 
70 
72  MotorMessage mc;
73  if (!this->input.empty()) {
74  mc = input.front_pop();
75  }
76 
77  return mc;
78 }
79 
81 
83  try {
84  RawMotorMessage in;
85  bool failed_update = false;
86 
87  while (motors.isOpen()) {
88  while (motors.available() >= (failed_update ? 1 : 8)) {
89  RawMotorMessage innew;
90  motors.read(innew.c_array(), failed_update ? 1 : 8);
91 
92  // TODO use circular_buffer instead of manual shifting
93  if (!failed_update) {
94  in.swap(innew);
95  } else {
96  // Shift array contents up one
97  for (size_t i = 0; i < in.size() - 1; ++i) {
98  in[i] = in[i + 1];
99  }
100  in[in.size() - 2] = in[in.size() - 1];
101 
102  in[in.size() - 1] = innew[0];
103  }
104 
105  MotorMessage mc;
106  int error_code = mc.deserialize(in);
107 
108  if (error_code == 0) {
109  if (mc.getType() == MotorMessage::TYPE_ERROR) {
110  ROS_ERROR(
111  "GOT ERROR RESPONSE FROM PSOC FOR REGISTER 0x%02x",
112  mc.getRegister());
113  }
114  appendOutput(mc);
115  failed_update = false;
116  } else if (error_code == 1) {
117  failed_update = true;
118  char rejected[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
119  for (size_t i = 0; i < in.size() && i < 8; i++) {
120  rejected[i] = in.at(i);
121  }
122  ROS_ERROR("REJECT: %s", rejected);
123  } else {
124  failed_update = true;
125  ROS_ERROR("DESERIALIZATION ERROR! - %d", error_code);
126  }
127  }
128 
129  bool did_update = false;
130  while (inputAvailable()) {
131  did_update = true;
132 
134  ROS_DEBUG("out %02x %02x %02x %02x %02x %02x %02x %02x", out[0],
135  out[1], out[2], out[3], out[4], out[5], out[6],
136  out[7]);
137  motors.write(out.c_array(), out.size());
138  boost::this_thread::sleep(boost::posix_time::milliseconds(2));
139  }
140 
141  if (did_update) {
143  }
144 
145  boost::this_thread::interruption_point();
147  }
148 
149  } catch (const boost::thread_interrupted& e) {
150  motors.close();
151  } catch (const serial::IOException& e) {
152  ROS_ERROR("%s", e.what());
153  } catch (const serial::PortNotOpenedException& e) {
154  ROS_ERROR("%s", e.what());
155  } catch (...) {
156  ROS_ERROR("Unknown Error");
157  throw;
158  }
159 }
size_t available()
shared_queue< MotorMessage > input
Definition: motor_serial.h:62
MotorMessage::MessageTypes getType() const
int transmitCommand(MotorMessage command)
Definition: motor_serial.cc:48
MotorMessage::Registers getRegister() const
ros::Rate serial_loop_rate
Definition: motor_serial.h:67
boost::array< uint8_t, 8 > RawMotorMessage
Definition: motor_message.h:38
int transmitCommands(const std::vector< MotorMessage > &commands)
Definition: motor_serial.cc:53
serial::Serial motors
Definition: motor_serial.h:59
int commandAvailable()
Definition: motor_serial.cc:67
void push(const T &value)
Definition: shared_queue.h:68
MotorSerial(const std::string &port="/dev/ttyUSB0", uint32_t baud_rate=9600, double loopRate=100)
Definition: motor_serial.cc:35
bool fast_empty() const
Definition: shared_queue.h:117
RawMotorMessage serialize() const
boost::thread serial_thread
Definition: motor_serial.h:66
virtual const char * what() const
bool isOpen() const
MotorMessage getInputCommand()
Definition: motor_serial.cc:71
bool sleep()
shared_queue< MotorMessage > output
Definition: motor_serial.h:64
size_t read(uint8_t *buffer, size_t size)
void flushOutput()
bool empty() const
Definition: shared_queue.h:112
MotorMessage receiveCommand()
Definition: motor_serial.cc:58
int inputAvailable()
Definition: motor_serial.cc:69
void appendOutput(MotorMessage command)
Definition: motor_serial.cc:80
int deserialize(const RawMotorMessage &serialized)
void SerialThread()
Definition: motor_serial.cc:82
#define ROS_ERROR(...)
size_t write(const uint8_t *data, size_t size)
#define ROS_DEBUG(...)
virtual const char * what() const


ubiquity_motor
Author(s):
autogenerated on Mon Jun 10 2019 15:37:24