diff --git a/MiddleWare/CMakeLists.txt b/MiddleWare/CMakeLists.txt index 8e8ffa4..b7e4c90 100644 --- a/MiddleWare/CMakeLists.txt +++ b/MiddleWare/CMakeLists.txt @@ -22,8 +22,8 @@ find_package(zenohcxx REQUIRED) # include_directories(${libs_SOURCE_DIR}/Communication/CAN/include) # include_directories(${libs_SOURCE_DIR}/Peripherals/INA219/include) -add_executable(middleWareApp +add_executable(MiddleWareApp ./src/main.cpp ) -target_link_libraries(middleWareApp PRIVATE zenohcxx::zenohc) +target_link_libraries(MiddleWareApp PRIVATE zenohcxx::zenohc) diff --git a/MiddleWare/config/MiddleWareConfig.json b/MiddleWare/config/MiddleWareConfig.json new file mode 100644 index 0000000..6ec92e9 --- /dev/null +++ b/MiddleWare/config/MiddleWareConfig.json @@ -0,0 +1,17 @@ +{ + "mode": "peer", + "connect": { + "endpoints": ["tcp/127.0.0.1:7447"] + }, + "listen": { + "endpoints": ["tcp/127.0.0.1:7448"] + }, + "scouting": { + "multicast": { + "enabled": false + }, + "gossip": { + "enabled": false + } + } +} \ No newline at end of file diff --git a/MiddleWare/src/main.cpp b/MiddleWare/src/main.cpp index 2d77b77..5f1839a 100644 --- a/MiddleWare/src/main.cpp +++ b/MiddleWare/src/main.cpp @@ -39,13 +39,19 @@ int main(int argc, char** argv) std::cout << "CAN socket bound to can0 interface successfully." << std::endl; - Config config = Config::create_default(); - auto session = Session::open(std::move(config)); + auto config = Config::create_default(); + if (argc == 2) + { + config = Config::from_file(argv[1]); + } + auto session = Session::open(std::move(config)); auto pubSpeed = session.declare_publisher(KeyExpr("seame/car/1/speedSensor")); auto pubBattery = session.declare_publisher(KeyExpr("seame/car/1/batterySensor")); + auto pubLights = session.declare_publisher(KeyExpr("seame/car/1/lights")); + auto pubGear = session.declare_publisher(KeyExpr("seame/car/1/gear")); while (1) { @@ -67,7 +73,7 @@ int main(int argc, char** argv) speed = wheelDiame * 3.14 * speed * 10 / 60; std::string speed_str = std::to_string(speed); - printf("Publishing speed: '%d'\n", speed); + // printf("Publishing speed: '%d'\n", speed); pubSpeed.put(speed_str.c_str()); } else if (frame.can_id == 0x02) @@ -80,9 +86,36 @@ int main(int argc, char** argv) battery = std::min(100.0f, std::max(0.0f, percentage)); std::string battery_str = std::to_string(battery); - printf("Publishing battery: '%lf\n", battery); + // printf("Publishing battery: '%lf\n", battery); pubBattery.put(battery_str.c_str()); } + else if (frame.can_id == 0x03) + { + char lights; + + memcpy(&lights, frame.data, sizeof(char)); + + printf("Can received lights: "); + for (int i = 7; i >= 0; i--) + { + printf("%d", (lights >> i) & 0x01); + } + printf("\n"); + + // printf("Publishing lights: '%lf\n", lights[0]); + std::string light_str(1, lights); + pubLights.put(light_str); + } + else if (frame.can_id == 0x04) + { + char gear; + + memcpy(&gear, frame.data, sizeof(char)); + + // printf("Publishing gear: '%lf\n", gear[0]); + std::string gear_str(1, gear); + pubGear.put(std::to_string(gear_str)); + } usleep(10); } return 0; diff --git a/config/InstrumentClusterConfig.json b/config/InstrumentClusterConfig.json new file mode 100644 index 0000000..e63c57c --- /dev/null +++ b/config/InstrumentClusterConfig.json @@ -0,0 +1,17 @@ +{ + "mode": "peer", + "connect": { + "endpoints": [] + }, + "listen": { + "endpoints": ["tcp/127.0.0.1:7447"] + }, + "scouting": { + "multicast": { + "enabled": false + }, + "gossip": { + "enabled": false + } + } +} \ No newline at end of file diff --git a/include/InstrumentCluster.hpp b/include/InstrumentCluster.hpp index 4a94553..89f23a3 100644 --- a/include/InstrumentCluster.hpp +++ b/include/InstrumentCluster.hpp @@ -103,6 +103,8 @@ class InstrumentCluster : public QObject public: explicit InstrumentCluster(QObject* parent = nullptr); + explicit InstrumentCluster(const std::string& configFile, + QObject* parent = nullptr); ~InstrumentCluster(); int getSpeed() const; diff --git a/src/InstrumentCluster.cpp b/src/InstrumentCluster.cpp index 5a3f92f..c6911f9 100644 --- a/src/InstrumentCluster.cpp +++ b/src/InstrumentCluster.cpp @@ -64,6 +64,71 @@ InstrumentCluster::InstrumentCluster(QObject* parent) { } +InstrumentCluster::InstrumentCluster(const std::string& configFile, + QObject* parent) + : QObject(parent), + m_session(Session::open(std::move(Config::from_file(configFile)))), + m_subSpeed(m_session.declare_subscriber( + "seame/car/1/speedSensor", + [this](const Sample& sample) + { + int speed = std::stoi(sample.get_payload().as_string()); + std::cout << "Sub speed" << std::endl; + this->setSpeed(speed); + }, + closures::none)), + m_subBattery(m_session.declare_subscriber( + "seame/car/1/batterySensor", + [this](const Sample& sample) + { + int batteryPercentage = + std::stoi(sample.get_payload().as_string()); + BatteryStatus battery; + battery.percentage = batteryPercentage; + std::cout << "Sub battery" << std::endl; + this->setBattery(battery); + }, + closures::none)), + m_subLights(m_session.declare_subscriber( + "seame/car/1/lights", + [this](const Sample& sample) + { + uint8_t data = + static_cast(sample.get_payload().as_string()[0]); + + LightStatus lights; + lights.rightBlinker = (data & (1 << 0)) != 0; + lights.leftBlinker = (data & (1 << 1)) != 0; + lights.lowBeam = (data & (1 << 2)) != 0; + lights.highBeam = (data & (1 << 3)) != 0; + lights.frontFogLight = (data & (1 << 4)) != 0; + lights.rearFogLight = (data & (1 << 5)) != 0; + lights.hazardLight = (data & (1 << 6)) != 0; + lights.parkingLight = (data & (1 << 7)) != 0; + std::cout << "Sub lights" << std::endl; + this->setLights(lights); + }, + closures::none)), + m_subGear(m_session.declare_subscriber( + "seame/car/1/gear", + [this](const Sample& sample) + { + uint8_t data = + static_cast(sample.get_payload().as_string()[0]); + + GearPosition gear; + gear.park = (data & (1 << 0)) != 0; + gear.reverse = (data & (1 << 1)) != 0; + gear.neutral = (data & (1 << 2)) != 0; + gear.drive = (data & (1 << 3)) != 0; + std::cout << "Sub gear" << std::endl; + this->setGear(gear); + }, + closures::none)), + m_speed(0) +{ +} + InstrumentCluster::~InstrumentCluster() { m_session.close(); diff --git a/src/main.cpp b/src/main.cpp index da2cbcd..6cbd359 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,14 +3,24 @@ #include #include "InstrumentCluster.hpp" +using namespace zenoh; + int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - InstrumentCluster instrumentCluster; + InstrumentCluster* instrumentCluster; + if (argc == 2) + { + instrumentCluster = new InstrumentCluster(argv[1]); + } + else + { + instrumentCluster = new InstrumentCluster(); + } engine.rootContext()->setContextProperty("instrumentCluster", - &instrumentCluster); + instrumentCluster); const QUrl url(QStringLiteral("qrc:/Main.qml")); QObject::connect( @@ -23,5 +33,7 @@ int main(int argc, char* argv[]) Qt::QueuedConnection); engine.load(url); - return app.exec(); + int result = app.exec(); + delete instrumentCluster; + return result; } diff --git a/ui/FootbarInfo.qml b/ui/FootbarInfo.qml index 082723f..76cf380 100644 --- a/ui/FootbarInfo.qml +++ b/ui/FootbarInfo.qml @@ -7,48 +7,47 @@ Row { padding: 10 Column { - Row { - - anchors.verticalCenterOffset: -10 - Image { - id: batteryLevel - // source: { - // if (canBusHandler.battery >= 80) { - // "qrc:/assets/icons/battery-5.png" - // } else if (canBusHandler.battery >= 60) { - // "qrc:/assets/icons/battery-4.png" - // } else if (canBusHandler.battery >= 40) { - // "qrc:/assets/icons/battery-3.png" - // } else if (canBusHandler.battery >= 20) { - // "qrc:/assets/icons/battery-2.png" - // } else { - // "qrc:/assets/icons/battery-1.png" - // } - // } - width: 80 - visible: true - fillMode: Image.PreserveAspectFit - source: "qrc:/assets/icons/battery-4.png" - // Adjust this value to move the image up or down - } - } // Row { - // spacing: 5 - // Text { - // font.family: "Open Sans" - // //text: canBusHandler.battery - // text: "100" - // font.pixelSize: app.letterSize - // color: "white" - // } - // Text { - // font.family: "Open Sans" - // text: "%" - // font.pixelSize: app.letterSize - // color: "gray" + // anchors.verticalCenterOffset: -10 + // Image { + // id: batteryLevel + // // source: { + // // if (canBusHandler.battery >= 80) { + // // "qrc:/assets/icons/battery-5.png" + // // } else if (canBusHandler.battery >= 60) { + // // "qrc:/assets/icons/battery-4.png" + // // } else if (canBusHandler.battery >= 40) { + // // "qrc:/assets/icons/battery-3.png" + // // } else if (canBusHandler.battery >= 20) { + // // "qrc:/assets/icons/battery-2.png" + // // } else { + // // "qrc:/assets/icons/battery-1.png" + // // } + // // } + // width: 80 + // visible: true + // fillMode: Image.PreserveAspectFit + // source: "qrc:/assets/icons/battery-4.png" + // // Adjust this value to move the image up or down // } // } + Row { + spacing: 5 + + Text { + font.family: "Open Sans" + text: instrumentCluster.battery.percentage + font.pixelSize: app.letterSize + color: "white" + } + Text { + font.family: "Open Sans" + text: "%" + font.pixelSize: app.letterSize + color: "gray" + } + } } Column { @@ -57,7 +56,7 @@ Row { spacing: 5 Text { font.family: "Open Sans" - text: "256" + text: instrumentCluster.battery.autonomy font.pixelSize: app.letterSize color: "white" }