|
| 1 | +#include <chrono> |
| 2 | +#include <map> |
| 3 | +#include <thread> |
| 4 | + |
| 5 | +#include "igvc/node.hpp" |
| 6 | +#include "rclcpp/rclcpp.hpp" |
| 7 | +#include "igvc_messages/msg/gps_feedback.hpp" |
| 8 | +#include "igvc_messages/msg/imu_feedback.hpp" |
| 9 | + |
| 10 | +#include "vectornav/Interface/Sensor.hpp" |
| 11 | + |
| 12 | +using namespace std::chrono_literals; |
| 13 | + |
| 14 | +class IGVCHardwareVectornavNode : public IGVC::Node |
| 15 | +{ |
| 16 | +public: |
| 17 | + IGVCHardwareVectornavNode() : IGVC::Node("igvc_vectornav") {} |
| 18 | + |
| 19 | + void init() override |
| 20 | + { |
| 21 | + mGpsFeedbackPublisher = this->create_publisher<igvc_messages::msg::GPSFeedback>(IGVC::Topics::GPS, 10); |
| 22 | + mImuFeedbackPublisher = this->create_publisher<igvc_messages::msg::IMUFeedback>(IGVC::Topics::IMU, 10); |
| 23 | + |
| 24 | + setDeviceState(IGVC::DeviceState::READY); |
| 25 | + |
| 26 | + mSensorConnectionThread = std::thread(&IGVCHardwareVectornavNode::sensorThreadFunction, this); |
| 27 | + mSensorMonitorThread = std::thread(&IGVCHardwareVectornavNode::monitorThreadFunction, this); |
| 28 | + } |
| 29 | + |
| 30 | + void sensorThreadFunction() |
| 31 | + { |
| 32 | + // setup output register |
| 33 | + mOutputRegister.rateDivisor = 5; // 20 Hz |
| 34 | + mOutputRegister.asyncMode = VN::Registers::System::BinaryOutput::AsyncMode{}; |
| 35 | + mOutputRegister.asyncMode->serial1 = 1; |
| 36 | + |
| 37 | + // gnss/lla |
| 38 | + mOutputRegister.gnss = VN::Registers::System::BinaryOutput::Gnss{}; |
| 39 | + mOutputRegister.gnss.gnss1Fix = 1; |
| 40 | + mOutputRegister.gnss.gnss1NumSats = 1; |
| 41 | + mOutputRegister.gnss.gnss1PosLla = 1; |
| 42 | + |
| 43 | + // ypr |
| 44 | + mOutputRegister.common = VN::Registers::System::BinaryOutput::Common{}; |
| 45 | + mOutputRegister.common.ypr = 1; |
| 46 | + |
| 47 | + while (rclcpp::ok()) |
| 48 | + { |
| 49 | + // first, if we have a sensor check if its still connected |
| 50 | + if (mSensor && !mSensor->verifySensorConnectivity()) |
| 51 | + { |
| 52 | + RCLCPP_WARN(get_logger(), "VectorNav sensor disconnected, attempting to reconnect"); |
| 53 | + mSensor.reset(); |
| 54 | + setDeviceState(IGVC::DeviceState::READY); |
| 55 | + } |
| 56 | + |
| 57 | + // if we are already connected, just sleep and continue |
| 58 | + if (mSensor) |
| 59 | + { |
| 60 | + std::this_thread::sleep_for(1s); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + // try to connect |
| 65 | + mSensor = std::make_shared<VN::Sensor>(); |
| 66 | + VN::Error err = mSensor->autoConnect("/dev/igvc_vn"); |
| 67 | + if (err != VN::Error::None) |
| 68 | + { |
| 69 | + std::string errString = VN::errorCodeToString(err); |
| 70 | + RCLCPP_WARN(get_logger(), "Failed to connect to VectorNav sensor: %s", errString.c_str()); |
| 71 | + mSensor.reset(); |
| 72 | + std::this_thread::sleep_for(3s); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + // setup |
| 77 | + mSensor->asyncOutputEnable(VN::AsyncOutputEnable::State::Disable); |
| 78 | + |
| 79 | + auto writeCmdOpt = mOutputRegister.toWriteCommand(); |
| 80 | + if (!writeCmdOpt) |
| 81 | + { |
| 82 | + RCLCPP_ERROR(get_logger(), "Failed to create VectorNav output register write command"); |
| 83 | + mSensor.reset(); |
| 84 | + std::this_thread::sleep_for(3s); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + mSensor->asyncOutputEnable(VN::AsyncOutputEnable::State::Enable); |
| 89 | + |
| 90 | + // connected |
| 91 | + RCLCPP_INFO(get_logger(), "Connected to VectorNav sensor"); |
| 92 | + setDeviceState(IGVC::DeviceState::OPERATING); |
| 93 | + mIsConnected.store(true); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + void monitorThreadFunction() |
| 98 | + { |
| 99 | + while (rclcpp::ok()) |
| 100 | + { |
| 101 | + if (!mIsConnected.load()) |
| 102 | + { |
| 103 | + std::this_thread::sleep_for(100ms); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + // read the registers |
| 108 | + VN::Sensor::CompositeDataQueueReturn compositeData = mSensor->getNextMeasurement(); |
| 109 | + if (!compositeData) |
| 110 | + { |
| 111 | + // TOOD: Log? |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (!compositeData->matchesMessage(mOutputRegister)) |
| 116 | + { |
| 117 | + // we don't care about this |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + // gps |
| 122 | + igvc_messages::msg::GPSFeedback gpsMsg; |
| 123 | + gpsMsg.latitude = compositeData->gnss.gnss1PosLla->lat; |
| 124 | + gpsMsg.longitude = compositeData->gnss.gnss1PosLla->lon; |
| 125 | + gpsMsg.altitude = compositeData->gnss.gnss1PosLla->alt; |
| 126 | + gpsMsg.num_sats = compositeData->gnss.gnss1NumSats.value_or(0); |
| 127 | + gpsMsg.gps_fix = compositeData->gnss.gnss1Fix.value_or(0); |
| 128 | + mGpsFeedbackPublisher->publish(gpsMsg); |
| 129 | + |
| 130 | + // imu |
| 131 | + igvc_messages::msg::IMUFeedback imuMsg; |
| 132 | + imuMsg.yaw = compositeData->attitude.ypr->yaw; |
| 133 | + imuMsg.pitch = compositeData->attitude.ypr->pitch; |
| 134 | + imuMsg.roll = compositeData->attitude.ypr->roll; |
| 135 | + mImuFeedbackPublisher->publish(imuMsg); |
| 136 | + |
| 137 | + std::this_thread::sleep_for(10ms); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +private: |
| 142 | + // threads |
| 143 | + std::thread mSensorConnectionThread; |
| 144 | + std::thread mSensorMonitorThread; |
| 145 | + |
| 146 | + // shared data |
| 147 | + std::atomic<bool> mIsConnected{false}; |
| 148 | + |
| 149 | + // vn stuff |
| 150 | + std::shared_ptr<VN::Sensor> mSensor; |
| 151 | + VN::Registers::System::BinaryOutput1 mOutputRegister; |
| 152 | + |
| 153 | + // publishers |
| 154 | + rclcpp::Publisher<igvc_messages::msg::GPSFeedback>::SharedPtr mGpsFeedbackPublisher; |
| 155 | + rclcpp::Publisher<igvc_messages::msg::IMUFeedback>::SharedPtr mImuFeedbackPublisher; |
| 156 | +}; |
| 157 | + |
| 158 | +int main(int argc, char *argv[]) |
| 159 | +{ |
| 160 | + IGVC::Node::create_and_run<IGVCHardwareVectornavNode>(argc, argv); |
| 161 | +} |
0 commit comments