17 #include <asm/ioctls.h>
18 #include <linux/serial.h>
19 #include <sys/ioctl.h>
38 , usbFileDescriptor_{ -1 }
40 , usbStreamIn_{
nullptr }
41 , usbStreamOut_{
nullptr }
42 , runInThreadedMode_(
true)
49 , pollingSyncErrorCounter_(0)
50 , frameReceivedCounter_(0)
51 , frameSuccessCounter_(0)
52 , frameCrcErrorCounter_(0)
53 , frameSyncErrorCounter_(0)
54 , maxFrameSyncErrorCounts_(10000)
55 , maxCountOpenSerialPort_(10)
57 , readTimeout_(MAXIMUM_ACCEPTABLE_TIMEOUT)
79 ROS_ERROR(
"[%s] Could not establish connection with device. Init failed.",
name_.c_str());
86 ROS_ERROR(
"[%s] Could not establish connection with device. Init failed.",
name_.c_str());
91 ROS_ERROR(
"[%s] Could not bring device to INIT mode. Init failed.",
name_.c_str());
112 <<
"The following serial device has been found and initialized:");
114 <<
"Port: " <<
port_);
140 ROS_INFO(
"[%s] Driver will attempt to shut-down",
name_.c_str());
221 ROS_WARN_THROTTLE(1.0,
"[%s] Timeout reached and didn't get any valid data from the device.",
name_.c_str());
226 uint32_t stored_chars;
227 bool read_finished =
false;
238 clock_gettime(CLOCK_MONOTONIC, &t_current);
241 uint8_t possible_header;
265 t_previous = t_current;
266 clock_gettime(CLOCK_MONOTONIC, &t_current);
267 time_delta +=
diffSec(t_previous, t_current);
278 clock_gettime(CLOCK_MONOTONIC, &t_current);
284 if (stored_chars != 0)
293 ROS_DEBUG(
"[%s] Whole frame received, remaining bytes: %u",
name_.c_str(), remainder_bytes);
295 if (remainder_bytes != 0)
302 read_finished =
true;
306 t_previous = t_current;
307 clock_gettime(CLOCK_MONOTONIC, &t_current);
308 time_delta +=
diffSec(t_previous, t_current);
317 catch (std::exception& e)
319 ROS_ERROR(
"[%s] Error while reading a packet, %s",
name_.c_str(), e.what());
331 ROS_WARN(
"[%s] Reached max syncing errors. Disconnecting sensor.",
name_.c_str());
341 uint16_t crc_received = frame.crc16_ccitt;
343 if (crc_received != crc_computed)
346 ROS_WARN(
"[%s] CRC missmatch received: 0x%04x calculated: 0x%04x",
name_.c_str(), crc_received, crc_computed);
350 catch (std::exception& e)
352 ROS_ERROR(
"[%s] Error while reading a packet, %s",
name_.c_str(), e.what());
358 if (frame.data.status.app_took_too_long)
360 ROS_WARN(
"[%s] Warning force sensor is skipping measurements, Increase sinc length",
name_.c_str());
362 if (frame.data.status.overrange)
364 ROS_WARN(
"[%s] Warning measuring range exceeded",
name_.c_str());
366 if (frame.data.status.invalid_measurements)
368 ROS_ERROR(
"[%s] Warning force torque measurements are invalid, Permanent damage may occur",
name_.c_str());
370 if (frame.data.status.raw_measurements)
418 std::vector<std::string> errors;
423 errors.emplace_back(
"CRC Errors have occurred in the Serial Communication.");
427 errors.emplace_back(
"Frame Sync Errors have occurred in the Serial Communication.");
431 errors.emplace_back(
"Timeout Errors have occurred in the Serial Communication.");
435 errors.emplace_back(
"Polling Sync Errors have occurred in the Serial Communication.");
440 errors.emplace_back(
"No error flags set.");
449 bool open_port =
false;
450 unsigned int counter_open_serial_port = 0;
483 ROS_DEBUG(
"[%s] Successful initialization of first communication with baud rate %u",
name_.c_str(),
502 ROS_DEBUG(
"[%s] Successful initialization of first communication with default baud rate.",
name_.c_str());
507 ROS_ERROR(
"[%s] Failed to parse successfully the communication setup",
name_.c_str());
529 ROS_ERROR(
"[%s] Could not open serial port %s with baud rate %u",
name_.c_str(),
port_.c_str(),
533 ROS_DEBUG(
"[%s] Successful initialization of second communication with baud rate %u",
name_.c_str(),
539 ROS_ERROR(
"[%s] Failed to parse successfully the boot response",
name_.c_str());
545 ROS_ERROR(
"[%s] Device could not switch to init mode",
name_.c_str());
551 ROS_ERROR(
"[%s] Device could not switch to config mode",
name_.c_str());
556 ROS_ERROR(
"[%s] Failed to parse successfully the boot response",
name_.c_str());
567 uint8_t data_format = 0;
571 ROS_ERROR(
"[%s] Could not set the communication setup",
name_.c_str());
577 ROS_ERROR(
"[%s] Could not save the configuration to the device",
name_.c_str());
583 ROS_ERROR(
"[%s] Device could not switch to run mode",
name_.c_str());
588 ROS_DEBUG(
"[%s] Re-initializing serial-port at: %s with maximum baud rate",
name_.c_str(),
port_.c_str());
601 ROS_ERROR(
"[%s] Failed to parse successfully the boot response.",
name_.c_str());
606 ROS_ERROR(
"[%s] Failed to parse successfully the communication msgs.",
name_.c_str());
616 std::string str, tmp_str;
618 clock_gettime(CLOCK_MONOTONIC, &t_start);
620 while (
diffSec(t_start, t_current) < timeout)
623 if (tmp_str.size() > 0)
628 ROS_DEBUG(
"[%s] Found exact match: %s, time: %f",
name_.c_str(), str.c_str(),
diffSec(t_start, t_current));
632 clock_gettime(CLOCK_MONOTONIC, &t_current);
639 char buffer[numChars];
640 uint32_t stored_chars;
641 stored_chars =
usbStreamIn_.readsome(buffer,
sizeof(buffer));
642 str.assign(buffer, stored_chars);
644 return (stored_chars != 0);
649 char buffer[numChars];
650 uint32_t read_offset = 0, stored_chars;
652 clock_gettime(CLOCK_MONOTONIC, &t_start);
654 while (
diffSec(t_start, t_current) < timeout)
659 stored_chars =
usbStreamIn_.readsome((
char*)&buffer[read_offset],
sizeof(buffer) - read_offset);
660 read_offset += stored_chars;
662 clock_gettime(CLOCK_MONOTONIC, &t_current);
664 str.assign(buffer, read_offset);
666 return (read_offset != 0);
675 ROS_ERROR(
"[%s] Failed to parse Product Name of the serial device",
name_.c_str());
683 ROS_ERROR(
"[%s] Failed to parse Serial Number of the serial device",
name_.c_str());
691 ROS_ERROR(
"[%s] Failed to parse Firmware Version of the serial device",
name_.c_str());
703 struct serial_struct ser_info;
720 ROS_ERROR(
"[%s] Failed to get connection attributes.",
name_.c_str());
733 ROS_ERROR(
"[%s] Failed to set connection attributes.",
name_.c_str());
740 ROS_ERROR(
"[%s] Failed to flush the input and output streams.",
name_.c_str());
746 ser_info.flags |= ASYNC_LOW_LATENCY;
752 ROS_ERROR(
"[%s] Failed to set the descriptor flags.",
name_.c_str());
769 uint16_t crc = 0xFFFF;
777 #define lo8(x) ((x)&0xFF)
778 #define hi8(x) (((x) >> 8) & 0xFF)
782 return ((((uint16_t)
data << 8) |
hi8(crc)) ^ (uint8_t)(
data >> 4) ^ ((uint16_t)
data << 3));
795 params.sched_priority = priority;
796 if (sched_setscheduler(getpid(), SCHED_FIFO, ¶ms) != 0)
798 ROS_WARN(
"[%s] Failed to set thread priority to %d: %s.",
name_.c_str(), priority, std::strerror(errno));
811 uint32_t time_stamp_previous, time_stamp = 0;
812 double time_stamp_diff;
815 timespec t_current, t_cycle = { 0 }, t_loop;
816 bool run_processing =
true;
817 unsigned long loop_counter = 0;
824 clock_gettime(CLOCK_MONOTONIC, &t_current);
828 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
829 (this->
readDevice(frame)) ? (run_processing =
true) : (run_processing =
false);
837 time_stamp_previous = time_stamp;
838 time_stamp = frame.data.timestamp;
839 time_stamp_diff =
static_cast<double>(time_stamp - time_stamp_previous) * 1e-6;
840 if (time_stamp_previous > time_stamp)
842 ROS_WARN(
"[%s] Time stamp difference is negative: Time counter has wrapped: current time stamp=%u, previous "
843 "time stamp=%u and time "
845 name_.c_str(), time_stamp, time_stamp_previous, time_stamp_diff);
857 t_cycle.tv_nsec =
NSEC_PER_SEC * modf(time_stamp_diff * 0.9, &int_part);
858 t_cycle.tv_sec =
static_cast<uint64_t
>(int_part);
872 clock_gettime(CLOCK_MONOTONIC, &t_current);
875 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t_loop,
nullptr);
883 ROS_WARN(
"[%s] Sensor polling thread error report:",
name_.c_str());
884 ROS_WARN(
"[%s] Total iterations: %lu",
name_.c_str(), loop_counter);
932 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
936 if (!config_command.formatCommand(str))
938 ROS_ERROR(
"[%s] Could not format the config command",
name_.c_str());
969 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
972 if (!run_command.formatCommand(str))
974 ROS_ERROR(
"[%s] Could not format the run command",
name_.c_str());
991 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
994 if (!hard_reset_command.formatCommand(str))
996 ROS_ERROR(
"[%s] Could not format the hardware reset command",
name_.c_str());
1006 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1009 if (!soft_reset_command.formatCommand(str))
1011 ROS_ERROR(
"[%s] Could not format the software reset command",
name_.c_str());
1054 if (!filter_command.formatCommand(str))
1056 ROS_ERROR(
"[%s] Could not format the filter command",
name_.c_str());
1059 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1072 ROS_DEBUG_STREAM(
"[" <<
name_.c_str() <<
"] Setting Force/Torque offset: " << forceTorqueOffset << std::endl);
1075 if (!offset_command.formatCommand(str))
1077 ROS_ERROR(
"[%s] Could not format the force torque offset command",
name_.c_str());
1080 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1093 bool success =
true;
1094 for (uint32_t row = 0; row < 6; row++)
1101 if (!sensor_calibration_row_command.formatCommand(str))
1103 ROS_ERROR(
"[%s] Could not format the calibration matrix command",
name_.c_str());
1106 ROS_DEBUG_STREAM(
"[" <<
name_.c_str() <<
"] Calibration matrix line is: " << str <<
". Size is " << str.size());
1107 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1110 memset(buffer, 0,
sizeof(buffer));
1112 std::this_thread::sleep_for(std::chrono::microseconds(10000));
1125 uint8_t data_format = 0;
1130 const uint8_t& dataFormat,
const uint8_t& baudRate)
1132 ROS_DEBUG(
"[%s] Setting communication setup with baud rate: %u, data format: %u, temp comp: %u and calibration: %u",
1137 if (!comm_setup.formatCommand(str))
1139 ROS_ERROR(
"[%s] Could not format the communication setup command",
name_.c_str());
1142 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1156 if (!save_config.formatCommand(str))
1158 ROS_ERROR(
"[%s] Could not format the save config command",
name_.c_str());
1161 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1175 if (!load_config.formatCommand(str))
1177 ROS_ERROR(
"[%s] Could not format the load config command",
name_.c_str());
1180 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1194 if (!print_config.formatCommand(str))
1196 ROS_ERROR(
"[%s] Could not format the print config command",
name_.c_str());
1199 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1207 uint64_t timespec_diff_ns;
1209 clock_gettime(CLOCK_MONOTONIC, &tnow);
1212 ROS_INFO(
"[%s] Printing user configuration:",
name_.c_str());
1222 clock_gettime(CLOCK_MONOTONIC, &t_loop);
1223 timespec_diff_ns = (t_loop.tv_sec - tnow.tv_sec) * 1e9 + (t_loop.tv_nsec - tnow.tv_nsec);
1224 }
while (timespec_diff_ns < 1e9);
1231 bool success =
true;
1244 ROS_ERROR(
"[%s] Failed to flush the input read buffer.",
name_.c_str());
1266 unsigned int serial_number;
1268 stat.
add(
"Serial Number (S/N)", std::to_string(serial_number));
1295 std::bitset<16> bits(data_status.
byte);
1296 if (data_status.invalid_measurements)
1298 stat.
summary(diagnostic_msgs::DiagnosticStatus::ERROR,
"Errors in Data Flags");
1300 else if (data_status.app_took_too_long || data_status.overrange || data_status.raw_measurements)
1302 stat.
summary(diagnostic_msgs::DiagnosticStatus::WARN,
"Warnings in Data Flags");
1306 stat.
summary(diagnostic_msgs::DiagnosticStatus::OK,
"No errors in Data Flags");
1308 stat.
add(
"Sensor is skipping measurements", data_status.app_took_too_long ?
"Yes" :
"No");
1309 stat.
add(
"Measuring range exceeded", data_status.overrange ?
"Yes" :
"No");
1310 stat.
add(
"Measurements are invalid", data_status.invalid_measurements ?
"Yes" :
"No");
1311 stat.
add(
"Raw Measurements from gages", data_status.raw_measurements ?
"Yes" :
"No");
1312 stat.
add(
"Reserved 1", bits[4] ?
"Yes" :
"No");
1313 stat.
add(
"Reserved 2", bits[5] ?
"Yes" :
"No");
1314 stat.
add(
"Reserved 3", bits[6] ?
"Yes" :
"No");
1315 stat.
add(
"Reserved 4", bits[7] ?
"Yes" :
"No");
1316 stat.
add(
"Reserved 5", bits[8] ?
"Yes" :
"No");
1317 stat.
add(
"Reserved 6", bits[9] ?
"Yes" :
"No");
1318 stat.
add(
"Reserved 7", bits[10] ?
"Yes" :
"No");
1319 stat.
add(
"Reserved 8", bits[11] ?
"Yes" :
"No");
1320 stat.
add(
"Reserved 9", bits[12] ?
"Yes" :
"No");
1321 stat.
add(
"Reserved 10", bits[13] ?
"Yes" :
"No");
1322 stat.
add(
"Reserved 11", bits[14] ?
"Yes" :
"No");
1323 stat.
add(
"Reserved 12", bits[15] ?
"Yes" :
"No");
1342 ROS_ERROR(
"[%s] Didn't find the correct command in ACK",
name_.c_str());
1358 if (str.size() > 64)
1360 ROS_WARN(
"[%s] String's length exceeds permittable limit (64)",
name_.c_str());
1363 ROS_DEBUG(
"[%s] Number of chars: %zu",
name_.c_str(), str.size());
1368 char cstr[str.size() + 1];
1369 strcpy(cstr, str.c_str());
1370 for (uint8_t i = 0; i < str.size(); i++)
1373 std::this_thread::sleep_for(std::chrono::microseconds(5000));
1378 ROS_WARN(
"[%s] Serial Write or Read failed",
name_.c_str());
1388 catch (
const std::exception& e)
1401 std::unique_lock<std::recursive_mutex> lock(
serialMutex_);
1408 ROS_ERROR(
"Could not fork, error occured");
1414 ROS_DEBUG(
"Child process, pid = %u\n", getpid());
1416 char path_argument[filePath.size() + 15];
1417 int ret = sprintf(path_argument,
"-Uflash:w:%s:i", filePath.c_str());
1424 char port_argument[
port_.size() + 2];
1425 ret = sprintf(port_argument,
"-P%s",
port_.c_str());
1436 const char* argv_list[9] = {
"avrdude",
"-v",
"-patmega328p",
"-carduino", port_argument,
1437 "-b230400",
"-D", path_argument,
nullptr };
1438 ROS_DEBUG(
"Complete command for avrdude:");
1439 for (std::size_t i = 0; i <
sizeof(argv_list); i++)
1445 ret = execvp(argv_list[0],
const_cast<char* const*
>(argv_list));
1448 ROS_ERROR(
"[%s] Execution of avrdude failed: %s",
name_.c_str(), strerror(errno));
1449 kill(getpid(), SIGKILL);
1462 if (waitpid(pid, &status, WUNTRACED) > 0)
1464 if (WIFEXITED(status) && !WEXITSTATUS(status))
1466 ROS_INFO(
"[%s] avrdude returned successfully",
name_.c_str());
1470 else if (WIFEXITED(status) && WEXITSTATUS(status))
1472 if (WEXITSTATUS(status) == 127)
1481 ROS_ERROR(
"[%s] program terminated normally,"
1482 " but returned a non-zero status",
1492 ROS_ERROR(
"[%s] program didn't terminate normally",
name_.c_str());