mirror of
https://github.com/westonrobot/ugv_sdk
synced 2023-04-08 06:32:14 +08:00
async_port: fixed return error in SetupPort()
This commit is contained in:
@@ -32,7 +32,7 @@ int main(int argc, char **argv) {
|
|||||||
std::unique_ptr<ScoutMiniOmniRobot> scout;
|
std::unique_ptr<ScoutMiniOmniRobot> scout;
|
||||||
|
|
||||||
ProtocolDectctor detector;
|
ProtocolDectctor detector;
|
||||||
detector.Connect("can0");
|
detector.Connect(device_name);
|
||||||
auto proto = detector.DetectProtocolVersion(5);
|
auto proto = detector.DetectProtocolVersion(5);
|
||||||
if (proto == ProtocolVersion::AGX_V1) {
|
if (proto == ProtocolVersion::AGX_V1) {
|
||||||
std::cout << "Detected protocol: AGX_V1" << std::endl;
|
std::cout << "Detected protocol: AGX_V1" << std::endl;
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ int main(int argc, char **argv) {
|
|||||||
std::unique_ptr<ScoutRobot> scout;
|
std::unique_ptr<ScoutRobot> scout;
|
||||||
|
|
||||||
ProtocolDectctor detector;
|
ProtocolDectctor detector;
|
||||||
detector.Connect("can0");
|
if (detector.Connect(device_name)) {
|
||||||
auto proto = detector.DetectProtocolVersion(5);
|
auto proto = detector.DetectProtocolVersion(5);
|
||||||
if (proto == ProtocolVersion::AGX_V1) {
|
if (proto == ProtocolVersion::AGX_V1) {
|
||||||
std::cout << "Detected protocol: AGX_V1" << std::endl;
|
std::cout << "Detected protocol: AGX_V1" << std::endl;
|
||||||
@@ -64,6 +64,9 @@ int main(int argc, char **argv) {
|
|||||||
std::cout << "Detected protocol: UNKONWN" << std::endl;
|
std::cout << "Detected protocol: UNKONWN" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (scout == nullptr)
|
if (scout == nullptr)
|
||||||
std::cout << "Failed to create robot object" << std::endl;
|
std::cout << "Failed to create robot object" << std::endl;
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ using namespace westonrobot;
|
|||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
ProtocolDectctor detector;
|
ProtocolDectctor detector;
|
||||||
detector.Connect("can0");
|
if (detector.Connect("can0")) {
|
||||||
|
|
||||||
auto proto = detector.DetectProtocolVersion(5);
|
auto proto = detector.DetectProtocolVersion(5);
|
||||||
|
|
||||||
if (proto == ProtocolVersion::AGX_V1) {
|
if (proto == ProtocolVersion::AGX_V1) {
|
||||||
@@ -27,6 +26,10 @@ int main(int argc, char **argv) {
|
|||||||
} else {
|
} else {
|
||||||
std::cout << "Detected protocol: UNKONWN" << std::endl;
|
std::cout << "Detected protocol: UNKONWN" << std::endl;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
std::cout << "Failed to open port" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,8 @@ class AsyncPortBase {
|
|||||||
io_thread_ = std::thread([this]() { io_context_.run(); });
|
io_thread_ = std::thread([this]() { io_context_.run(); });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
std::cerr << "Failed to setup port, please check if specified port exits "
|
std::cerr
|
||||||
|
<< "[ERROR] Failed to setup port, please check if specified port exits "
|
||||||
"or if you have proper permissions to access it"
|
"or if you have proper permissions to access it"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -18,8 +18,7 @@
|
|||||||
namespace westonrobot {
|
namespace westonrobot {
|
||||||
class ProtocolDectctor {
|
class ProtocolDectctor {
|
||||||
public:
|
public:
|
||||||
void Connect(std::string can_name);
|
bool Connect(std::string can_name);
|
||||||
void Connect(std::string uart_name, uint32_t baudrate);
|
|
||||||
|
|
||||||
ProtocolVersion DetectProtocolVersion(uint32_t timeout_sec);
|
ProtocolVersion DetectProtocolVersion(uint32_t timeout_sec);
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,10 @@ bool AsyncCAN::SetupPort() {
|
|||||||
memcpy(ifr.ifr_name, port_.c_str(), iface_name_size);
|
memcpy(ifr.ifr_name, port_.c_str(), iface_name_size);
|
||||||
|
|
||||||
const int ioctl_result = ioctl(can_fd_, SIOCGIFINDEX, &ifr);
|
const int ioctl_result = ioctl(can_fd_, SIOCGIFINDEX, &ifr);
|
||||||
if (ioctl_result < 0) StopService();
|
if (ioctl_result < 0) {
|
||||||
|
StopService();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
struct sockaddr_can addr;
|
struct sockaddr_can addr;
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
@@ -44,12 +47,15 @@ bool AsyncCAN::SetupPort() {
|
|||||||
|
|
||||||
const int bind_result =
|
const int bind_result =
|
||||||
bind(can_fd_, (struct sockaddr *)&addr, sizeof(addr));
|
bind(can_fd_, (struct sockaddr *)&addr, sizeof(addr));
|
||||||
if (bind_result < 0) StopService();
|
if (bind_result < 0) {
|
||||||
|
StopService();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
port_opened_ = true;
|
port_opened_ = true;
|
||||||
std::cout << "Start listening to port: " << port_ << std::endl;
|
std::cout << "Start listening to port: " << port_ << std::endl;
|
||||||
} catch (std::system_error &e) {
|
} catch (std::system_error &e) {
|
||||||
std::cout << e.what();
|
std::cout << e.what() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -55,9 +55,8 @@ bool AsyncSerial::SetupPort() {
|
|||||||
port_opened_ = true;
|
port_opened_ = true;
|
||||||
std::cout << "Start listening to port: " << port_ << "@" << baud_rate_
|
std::cout << "Start listening to port: " << port_ << "@" << baud_rate_
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
} catch (std::system_error &e) {
|
} catch (std::system_error &e) {
|
||||||
std::cout << e.what();
|
std::cout << e.what() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,13 +11,12 @@
|
|||||||
#include "ugv_sdk/utilities/stopwatch.hpp"
|
#include "ugv_sdk/utilities/stopwatch.hpp"
|
||||||
|
|
||||||
namespace westonrobot {
|
namespace westonrobot {
|
||||||
void ProtocolDectctor::Connect(std::string can_name) {
|
bool ProtocolDectctor::Connect(std::string can_name) {
|
||||||
can_ = std::make_shared<AsyncCAN>(can_name);
|
can_ = std::make_shared<AsyncCAN>(can_name);
|
||||||
can_->SetReceiveCallback(
|
can_->SetReceiveCallback(
|
||||||
std::bind(&ProtocolDectctor::ParseCANFrame, this, std::placeholders::_1));
|
std::bind(&ProtocolDectctor::ParseCANFrame, this, std::placeholders::_1));
|
||||||
can_->StartListening();
|
return can_->StartListening();
|
||||||
}
|
}
|
||||||
void ProtocolDectctor::Connect(std::string uart_name, uint32_t baudrate) {}
|
|
||||||
|
|
||||||
ProtocolVersion ProtocolDectctor::DetectProtocolVersion(uint32_t timeout_sec) {
|
ProtocolVersion ProtocolDectctor::DetectProtocolVersion(uint32_t timeout_sec) {
|
||||||
msg_v1_detected_ = false;
|
msg_v1_detected_ = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user