49 #include "std_msgs/Float32.h" 52 static constexpr
float PI = 3.14159265359f;
55 using std::stringstream;
69 tuple<string, string> output;
73 get<0>(output) =
"RRR";
74 get<1>(output) =
"rate";
77 get<0>(output) =
"AAA";
78 get<1>(output) =
"incremental angle";
81 get<0>(output) =
"PPP";
82 get<1>(output) =
"integrated angle";
85 assert(!
"mode not understood");
99 output =
"incremental_angle";
102 output =
"integrated_angle";
105 assert(!
"mode not understood");
118 device->
write(
"ZZZ", 3);
122 ROS_ERROR(
"Unable to communicate with DSP-3000 device.");
129 ROS_INFO(
"Configuring for %s output.", get<1>(mode_data).c_str());
132 device->
write(get<0>(mode_data).c_str(), static_cast<int>(get<0>(mode_data).size()));
136 ROS_ERROR(
"Unable to communicate with DSP-3000 device.");
143 int main(
int argc,
char **argv)
148 ros::param::param<std::string>(
"~port", port_name,
"/dev/ttyUSB0");
157 ros::param::param<bool>(
"~invert", invert, invert);
162 n.advertise<std_msgs::Float32>(
"dsp3000_" +
get_mode_topic_name(static_cast<KvhDsp3000Mode>(mode)), 100);
168 device.
open(port_name.c_str(), 38400);
175 ROS_INFO(
"The serial port named \"%s\" is opened.", port_name.c_str());
178 char temp_buffer[128];
179 bool user_notified_of_timeout =
false;
180 int previous_errno = 0;
181 bool ignoring_buffer_overflow =
true;
182 int temp_buffer_length = 0;
183 uint8_t temporary_buffer_ignore_limit = 0U;
184 bool streaming_data =
false;
191 int const new_bytes =
192 device.readLine(&temp_buffer[temp_buffer_length],
sizeof(temp_buffer) - temp_buffer_length - 1,
TIMEOUT);
193 temp_buffer_length += new_bytes;
194 ignoring_buffer_overflow =
false;
198 if (!user_notified_of_timeout)
200 ROS_ERROR(
"Timed out while talking with DSP-3000 device.");
201 user_notified_of_timeout =
true;
207 if (!ignoring_buffer_overflow)
211 temp_buffer_length =
sizeof(temp_buffer) - 1;
215 int32_t constexpr INTERRUPTED_SYSTEM_CALL_ERRNO = 4;
216 if (INTERRUPTED_SYSTEM_CALL_ERRNO != errno && previous_errno != errno)
220 previous_errno = errno;
224 if (user_notified_of_timeout || 0 != previous_errno)
228 user_notified_of_timeout =
false;
233 streaming_data =
true;
237 bool parser_is_working =
true;
238 int previous_temp_buffer_length = temp_buffer_length;
239 while (temp_buffer_length > 0 && parser_is_working)
242 parser_is_working = previous_temp_buffer_length != parsed_data.
new_buffer_length;
244 previous_temp_buffer_length = temp_buffer_length;
255 std_msgs::Float32 dsp_out;
256 float const rotation_measurement_rad = (parsed_data.
value *
PI) / 180.0
f;
257 dsp_out.data = (invert ? -rotation_measurement_rad : rotation_measurement_rad);
263 else if (!ignoring_buffer_overflow && temporary_buffer_ignore_limit >= 100)
265 temp_buffer[temp_buffer_length] =
'\0';
266 ROS_WARN(
"Bad data. Received data \"%s\" of length %i", temp_buffer, temp_buffer_length);
270 ++temporary_buffer_ignore_limit;
KvhDsp3000Mode
Copyright Autonomous Solutions Inc. 2016.
void publish(const boost::shared_ptr< M > &message) const
string get_mode_topic_name(KvhDsp3000Mode const mode)
static constexpr int TIMEOUT
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int write(const char *data, int length=-1)
Write to the port.
tuple< string, string > get_mode_data(KvhDsp3000Mode mode)
ParseDsp3000Data parse_dsp3000(char *buffer, int buffer_size)
void open(const char *port_name, int baud_rate=115200)
Open the serial port.
bool is_sensor_data_valid
Copyright Autonomous Solutions Inc. 2016.
int main(int argc, char **argv)
static constexpr float PI
bool configure_dsp3000(SerialPort *device, KvhDsp3000Mode mode)
ROSCPP_DECL void spinOnce()
C++ serial port class for ROS.