37 #include <std_msgs/String.h> 48 if (str.length() > 2) {
49 if ((str.at(0) ==
'0') && (str.at(1) ==
'x')) {
52 ss << std::hex << str.substr(2);
66 if (str ==
"normal") {
68 }
else if (str ==
"listen-only") {
75 nh_(nh), nh_priv_(nh_priv), name_(name), total_drops_(0), firmware_(firmware)
97 std::stringstream ss1, ss2;
98 ss1 <<
"bitrate_" << (i + 1);
99 ss2 <<
"mode_" << (i + 1);
106 std::stringstream ss1, ss2;
107 ss1 <<
"channel_" << (i + 1) <<
"_mask_" << j;
108 ss2 <<
"channel_" << (i + 1) <<
"_match_" << j;
137 dev_->
sendMessage(channel, msg->id, msg->is_extended, msg->dlc, msg->data.elems);
142 boost::lock_guard<boost::mutex> lock(
mutex_);
143 if (channel <
pubs_.size()) {
148 msg.is_extended = extended;
149 msg.is_error = (dlc == 0x0F);
151 memcpy(msg.data.elems, data, 8);
152 if (msg.is_error && (channel <
pubs_err_.size())) {
155 pubs_[channel].publish(msg);
161 unsigned int dev_time;
167 int64_t nsec = (t2 - t0).toNSec();
180 boost::lock_guard<boost::mutex> lock(
mutex_);
189 std_msgs::String version_msg;
195 ROS_WARN(
"Detected old %s firmware version %u.%u.%u, updating to %u.%u.%u is suggested. Execute `%s` to update.",
name_.c_str(),
196 version.major(), version.minor(), version.build(),
198 "rosrun dataspeed_can_usb fw_update.bash");
204 for (
unsigned int i = 0; i < 10; i++) {
206 ROS_INFO(
"%s: Offset: %f seconds, Delay: %f seconds",
name_.c_str(), offset.toSec(), delay.toSec());
213 for (
unsigned int j = 0; j <
channels_[i].filters.size(); j++) {
214 const uint32_t mask =
channels_[i].filters[j].mask;
215 const uint32_t match =
channels_[i].filters[j].match;
217 ROS_INFO(
"%s: Ch%u, Mask: 0x%08X, Match: 0x%08X",
name_.c_str(), i + 1, mask, match);
219 ROS_WARN(
"%s: Ch%u, Mask: 0x%08X, Match: 0x%08X failed",
name_.c_str(), i + 1, mask, match);
227 ROS_INFO(
"%s: Ch%u %ukbps",
name_.c_str(), i + 1, bitrate / 1000);
229 ROS_WARN(
"%s: Ch%u %ukbps failed",
name_.c_str(), i + 1, bitrate / 1000);
237 std::stringstream ns;
238 ns <<
"can_bus_" << (i + 1);
245 pubs_.push_back(node.advertise<can_msgs::Frame>(
"can_rx", 100,
false));
247 pubs_err_.push_back(node.advertise<can_msgs::Frame>(
"can_err", 100,
false));
278 std::vector<uint32_t> rx_drops, tx_drops;
279 std::vector<uint8_t> rx_errors, tx_errors;
280 if (
dev_->
getStats(rx_drops, tx_drops, rx_errors, tx_errors,
true)) {
281 unsigned int size = std::min(rx_drops.size(), tx_drops.size());
283 for (
unsigned int i = 0; i < size; i++) {
284 total += rx_drops[i];
285 total += tx_drops[i];
289 std::stringstream ss;
290 for (
unsigned int i = 0; i < size; i++) {
291 ss <<
"Rx" << (i + 1) <<
": " << rx_drops[i] <<
", ";
292 ss <<
"Tx" << (i + 1) <<
": " << tx_drops[i] <<
", ";
294 ROS_WARN(
"Dropped CAN messages: %s", ss.str().c_str());
unsigned int numChannels() const
std::vector< ros::Subscriber > subs_
void recvDevice(unsigned int channel, uint32_t id, bool extended, uint8_t dlc, const uint8_t data[8])
static bool getParamHex(const ros::NodeHandle &nh, const std::string &key, int &i)
#define ROS_WARN_THROTTLE(rate,...)
void publish(const boost::shared_ptr< M > &message) const
std::string version() const
std::vector< ros::Publisher > pubs_err_
static uint8_t getModeFromString(const std::string &str)
void recvRos(const can_msgs::Frame::ConstPtr &msg, unsigned int channel)
void sendMessage(unsigned int channel, uint32_t id, bool extended, uint8_t dlc, const uint8_t data[8], bool flush=true)
bool sampleTimeOffset(ros::WallDuration &offset, ros::WallDuration &delay)
ros::WallTimer timer_flush_
bool getTimeStamp(uint32_t ×tamp)
std::vector< ros::Publisher > pubs_
TransportHints & tcpNoDelay(bool nodelay=true)
bool getStats(std::vector< uint32_t > &rx_drops, std::vector< uint32_t > &tx_drops, std::vector< uint8_t > &rx_errors, std::vector< uint8_t > &tx_errors, bool clear=false)
ros::Publisher pub_version_
void setRecvCallback(const Callback &callback)
WallTimer createWallTimer(WallDuration period, void(T::*callback)(const WallTimerEvent &), T *obj, bool oneshot=false, bool autostart=true) const
ros::WallTime stampDev2Ros(unsigned int dev_stamp)
void timerFlushCallback(const ros::WallTimerEvent &event)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
uint16_t versionBuild() const
bool open(const std::string &mac=std::string())
uint16_t versionMinor() const
uint16_t versionMajor() const
static const unsigned int MAX_CHANNELS
std::vector< Channel > channels_
bool getParam(const std::string &key, std::string &s) const
WallDuration & fromNSec(int64_t t)
void timerServiceCallback(const ros::WallTimerEvent &event)
ros::WallTimer timer_service_
std::string toString(bool upper=false) const
CanDriver(ros::NodeHandle &nh, ros::NodeHandle &nh_priv, lusb::UsbDevice *dev=NULL, const std::string &name=std::string("Dataspeed USB CAN Tool"), const ModuleVersion &firmware=ModuleVersion(10, 4, 0))
bool addFilter(unsigned int channel, uint32_t mask, uint32_t match)
bool setBitrate(unsigned int channel, uint32_t bitrate, uint8_t mode=0)
static const unsigned int MAX_FILTERS