From 8a67021b1c591daac8a2d36b35ae3028fafaf94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 14:57:16 +0100 Subject: [PATCH 01/73] feat: send and receive vSOME/IP data in vehicle controller --- apps/VehicleController/main.cpp | 66 +++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 7c732e9..eab9098 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -1,5 +1,6 @@ #include "PiRacer/PiRacer.hpp" #include "Gamepad/ShanwanGamepad.hpp" +#include "../ServiceManager/src/server.hpp" /** * A : DRIVE @@ -15,6 +16,60 @@ enum Gear { DRIVE }; +Gear gear = PARKING; +std::shared_ptr app; +std::mutex mutex; +std::condition_variable condition; + +// sending the actual data (will be gear data) to server. +void send_gear_data(int data) { + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + std::shared_ptr request; + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(JOY_GEAR_SET_MID); + + std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&data), + reinterpret_cast(&data) + sizeof(int)); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "CLIENT : DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); +} + +// When response come, print the payload from server. +void on_message(const std::shared_ptr &_response) { + std::shared_ptr payload = _response->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + } + else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } +} + +void initVSOMEIP() { + app = vsomeip::runtime::get()->create_application("gear"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_SET_MID, on_message); + std::thread sender(run); + app->start(); +} + int main() { atexit(gpioTerminate); @@ -23,9 +78,9 @@ int main() { return 1; } + initVSOMEIP(); PiRacer racer; ShanWanGamepad gamepad; - Gear gear = PARKING; float steering = 0; float throttle = 0; @@ -42,6 +97,12 @@ int main() { gear = REVERSE; else if (input.button_y) gear = PARKING; + + if (gear == PARKING || gear == NEUTRAL) { + racer.setSteeringPercent(0); + racer.setThrottlePercent(0); + } + send_gear_data(gear); } else { if (gear == PARKING || gear == NEUTRAL) @@ -60,7 +121,4 @@ int main() { racer.setThrottlePercent(throttle); } } - - return 0; } - From 78759ca09e0e86152e82484ef233cbf2bd862bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 15:30:52 +0100 Subject: [PATCH 02/73] fix: fix typos --- apps/VehicleController/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index eab9098..59513e2 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -12,7 +12,7 @@ enum Gear { PARKING, REVERSE, - NEUTRAL, + NETURAL, DRIVE }; From 8bb7bd8b7cb8301a606a1049bed293cb2a097775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 15:31:33 +0100 Subject: [PATCH 03/73] refactor: fix function name --- apps/VehicleController/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 59513e2..362001f 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -58,7 +58,7 @@ void on_message(const std::shared_ptr &_response) { } } -void initVSOMEIP() { +void init_vSOMEIP() { app = vsomeip::runtime::get()->create_application("gear"); app->init(); app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); @@ -78,7 +78,7 @@ int main() { return 1; } - initVSOMEIP(); + init_vSOMEIP(); PiRacer racer; ShanWanGamepad gamepad; From 42167395e2bf6209c97c0e6c8d6eb8b69bc9ad6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 15:32:19 +0100 Subject: [PATCH 04/73] fix: create gear setting function --- apps/VehicleController/main.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 362001f..d43ae09 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -43,6 +43,22 @@ void send_gear_data(int data) { std::this_thread::sleep_for(std::chrono::seconds(1)); } +void set_gear(ShanWanGamepadInput input, int received_value) { + if (input.button_a || received_value == 3) + gear = DRIVE; + else if (input.button_b || received_value == 2) + gear = NETURAL; + else if (input.button_x || received_value == 1) + gear = REVERSE; + else if (input.button_y || received_value == 0) + gear = PARKING; + + if (gear == PARKING || gear == NEUTRAL){ + racer.setSteeringPercent(0); + racer.setThrottlePercent(0); + } +} + // When response come, print the payload from server. void on_message(const std::shared_ptr &_response) { std::shared_ptr payload = _response->get_payload(); @@ -50,6 +66,7 @@ void on_message(const std::shared_ptr &_response) { if (payload->get_length() >= sizeof(int)) { received_value = *reinterpret_cast(payload->get_data()); + set_gear(NULL, received_value); std::cout << "SERVER: Received int: " << received_value << std::endl; } else { @@ -89,19 +106,7 @@ int main() { ShanWanGamepadInput input = gamepad.read_data(); if (input.button) { - if (input.button_a) - gear = DRIVE; - else if (input.button_b) - gear = NEUTRAL; - else if (input.button_x) - gear = REVERSE; - else if (input.button_y) - gear = PARKING; - - if (gear == PARKING || gear == NEUTRAL) { - racer.setSteeringPercent(0); - racer.setThrottlePercent(0); - } + set_gear(input, -1); send_gear_data(gear); } else { From b8fcdf04214fa3034dea02b0dab09a155eefa363 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 15:32:31 +0100 Subject: [PATCH 05/73] refactor: code refactoring --- apps/VehicleController/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index d43ae09..640726f 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -17,6 +17,8 @@ enum Gear { }; Gear gear = PARKING; +PiRacer racer; + std::shared_ptr app; std::mutex mutex; std::condition_variable condition; @@ -96,7 +98,6 @@ int main() { } init_vSOMEIP(); - PiRacer racer; ShanWanGamepad gamepad; float steering = 0; From 22a265e2763948125ad6f4b56e5b6d1a79a058ba Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Thu, 20 Feb 2025 16:08:58 +0100 Subject: [PATCH 06/73] added: HeadUnit.cpp/.hpp, SpeedClient.cpp/.hpp | updated: CMakeLists.txt --- apps/HeadUnit/CMakeLists.txt | 2 + apps/HeadUnit/HeadUnit.cpp | 70 +++++++++++++++++++++++++++++ apps/HeadUnit/HeadUnit.hpp | 42 +++++++++++++++++ apps/HeadUnit/SpeedClient.cpp | 85 +++++++++++++++++++++++++++++++++++ apps/HeadUnit/SpeedClient.hpp | 24 ++++++++++ 5 files changed, 223 insertions(+) create mode 100644 apps/HeadUnit/HeadUnit.cpp create mode 100644 apps/HeadUnit/HeadUnit.hpp create mode 100644 apps/HeadUnit/SpeedClient.cpp create mode 100644 apps/HeadUnit/SpeedClient.hpp diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index c31a9a6..484e03f 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -13,6 +13,8 @@ qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(head-unit main.cpp + HeadUnit.cpp + SpeedClient.cpp resources.qrc shared/utils/envmanager.h diff --git a/apps/HeadUnit/HeadUnit.cpp b/apps/HeadUnit/HeadUnit.cpp new file mode 100644 index 0000000..8b9268c --- /dev/null +++ b/apps/HeadUnit/HeadUnit.cpp @@ -0,0 +1,70 @@ +#include "HeadUnit.h" + +HeadUnit::HeadUnit(QObject* parent) + : QObject(parent) +{} + +const QString& HeadUnit::currentGear() const +{ + return _currentGear; +} + +const QString& HeadUnit::ambientLighting() const +{ + return _ambientLighting; +} + +int HeadUnit::speed() const +{ + return _speed; +} + +int HeadUnit::batteryPercentage() const +{ + return _batteryPercentage; +} + +bool HeadUnit::chargingState() const +{ + return _chargingState; +} + +void HeadUnit::setCurrentGear(const QString& gear) +{ + if (_currentGear != gear) { + _currentGear = gear; + emit currentGearChanged(gear); + } +} + +void HeadUnit::setAmbientLighting(const QString& color) +{ + if (_ambientLighting != color) { + _ambientLighting = color; + emit ambientLightingChanged(color); + } +} + +void HeadUnit::setSpeed(int speed) +{ + if (_speed != speed) { + _speed = speed; + emit speedChanged(speed); + } +} + +void HeadUnit::setBatteryPercentage(int batteryPercentage) +{ + if (_batteryPercentage != batteryPercentage) { + _batteryPercentage = batteryPercentage; + emit batteryPercentageChanged(batteryPercentage); + } +} + +void HeadUnit::setChargingState(bool state) +{ + if (_chargingState != state) { + _chargingState = state; + emit chargingStateChanged(state); + } +} diff --git a/apps/HeadUnit/HeadUnit.hpp b/apps/HeadUnit/HeadUnit.hpp new file mode 100644 index 0000000..0228f1f --- /dev/null +++ b/apps/HeadUnit/HeadUnit.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include + +class HeadUnit : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString currentGear READ currentGear WRITE setCurrentGear NOTIFY currentGearChanged) + Q_PROPERTY(QString ambientLighting READ ambientLighting WRITE setAmbientLighting NOTIFY ambientLightingChanged) + Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged) + Q_PROPERTY(int batteryPercentage READ batteryPercentage WRITE setBatteryPercentage NOTIFY batteryPercentageChanged) + Q_PROPERTY(int chargingState READ chargingState WRITE setChargingState NOTIFY chargingStateChanged) + +public: + explicit HeadUnit(QObject *parent = nullptr); + + const QString& currentGear() const; + const QString& ambientLighting() const; + int speed() const; + int batteryPercentage() const; + bool chargingState() const; + + void setAmbientLighting(const QString& color); + void setCurrentGear(const QString& gear); + void setSpeed(int speed); + void setBatteryPercentage(int batteryPercentage); + void setChargingState(bool state); + +signals: + void currentGearChanged(const QString& gear); + void ambientLightingChanged(const QString& color); + void speedChanged(int speed); + void batteryPercentageChanged(int batteryPercentage); + void chargingStateChanged(bool state); + +private: + QString _currentGear = "P"; + int _ambientLighting = 180; + int _speed = 0; + int _batteryPercentage = 100; + bool _chargingState = false; +}; \ No newline at end of file diff --git a/apps/HeadUnit/SpeedClient.cpp b/apps/HeadUnit/SpeedClient.cpp new file mode 100644 index 0000000..26bea5b --- /dev/null +++ b/apps/HeadUnit/SpeedClient.cpp @@ -0,0 +1,85 @@ +#include "./SpeedClient.hpp" + +client_sample::client_sample(bool _use_tcp) : + app_(vsomeip::runtime::get()->create_application("speed")), use_tcp_(_use_tcp) { +} + +bool client_sample::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&client_sample::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, SPEED_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&client_sample::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, + std::bind(&client_sample::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + app_->request_event( + VEHICLE_SERVICE_ID, + SPEED_INSTANCE_ID, + SPEED_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + app_->subscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void client_sample::start() { + app_->start(); +} + +void client_sample::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, SPEED_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); + app_->stop(); +} + +void client_sample::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); + } +} + +void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void client_sample::on_message(const std::shared_ptr& _response) { + + std::shared_ptr its_payload = _response->get_payload(); + if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 + float received_speed = 0.0f; + + // 이터레이터로 시작 위치 가져오기 + auto it = its_payload->get_data(); // 시작 위치의 이터레이터 + std::copy(it, it + sizeof(float), reinterpret_cast(&received_speed)); + + // 변환된 값 출력 + std::cout << "Received data: " << received_speed << " m/s" << std::endl; + } else { + std::cerr << "Invalid data size received!" << std::endl; + } + + +} diff --git a/apps/HeadUnit/SpeedClient.hpp b/apps/HeadUnit/SpeedClient.hpp new file mode 100644 index 0000000..8078ead --- /dev/null +++ b/apps/HeadUnit/SpeedClient.hpp @@ -0,0 +1,24 @@ +#ifndef CLIENT_SAMPLE_HPP +#define CLIENT_SAMPLE_HPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class client_sample { +public: + client_sample(bool _use_tcp); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; + bool use_tcp_; +}; + +#endif // CLIENT_SAMPLE_HPP From d943ec6a5f6124f53b6072a85b521588517ef3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 18:44:57 +0100 Subject: [PATCH 07/73] fix: fix typo --- apps/VehicleController/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 640726f..760269b 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -12,7 +12,7 @@ enum Gear { PARKING, REVERSE, - NETURAL, + NEUTRAL, DRIVE }; @@ -49,7 +49,7 @@ void set_gear(ShanWanGamepadInput input, int received_value) { if (input.button_a || received_value == 3) gear = DRIVE; else if (input.button_b || received_value == 2) - gear = NETURAL; + gear = NEUTRAL; else if (input.button_x || received_value == 1) gear = REVERSE; else if (input.button_y || received_value == 0) From 8191dbad87401817f8967a8153eb7fbe5c2f8005 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Thu, 20 Feb 2025 19:39:52 +0100 Subject: [PATCH 08/73] [FEAT] : integration fin in IC --- .../clients/HU_gear_client/gear_client.cpp | 88 +++++++++++++++++ .../clients/HU_gear_client/gear_client.hpp | 34 +++++++ .../clients/J_gear_client/gear_client.cpp | 99 +++++++++++++++++++ .../clients/J_gear_client/gear_client.hpp | 34 +++++++ .../clients/ambient_receiver/al_receiver.cpp | 36 +++++++ .../clients/ambient_receiver/al_receiver.hpp | 16 +++ .../clients/ambient_sender/client.cpp | 74 ++++++++++++++ .../clients/ambient_sender/client.hpp | 0 .../clients/battery_client/battery_client.cpp | 83 ++++++++++++++++ .../clients/battery_client/battery_client.hpp | 24 +++++ .../gear_client.cpp | 83 ++++++++++++++++ .../gear_client.hpp | 23 +++++ apps/HeadUnit/clients/headers.hpp | 28 ++++++ apps/HeadUnit/clients/server.hpp | 60 +++++++++++ .../clients/speed_client/speed_client.cpp | 84 ++++++++++++++++ .../clients/speed_client/speed_client.hpp | 27 +++++ .../clients/HU_gear_client/gear_client.cpp | 88 +++++++++++++++++ .../clients/HU_gear_client/gear_client.hpp | 34 +++++++ .../clients/J_gear_client/gear_client.cpp | 99 +++++++++++++++++++ .../clients/J_gear_client/gear_client.hpp | 34 +++++++ .../clients/ambient_receiver/al_receiver.cpp | 36 +++++++ .../clients/ambient_receiver/al_receiver.hpp | 16 +++ .../clients/ambient_sender/client.cpp | 74 ++++++++++++++ .../clients/ambient_sender/client.hpp | 0 .../clients/battery_client/battery_client.cpp | 83 ++++++++++++++++ .../clients/battery_client/battery_client.hpp | 24 +++++ .../gear_client.cpp | 83 ++++++++++++++++ .../gear_client.hpp | 23 +++++ apps/InstrumentCluster/clients/headers.hpp | 28 ++++++ apps/InstrumentCluster/clients/server.hpp | 60 +++++++++++ .../clients/speed_client/speed_client.cpp | 84 ++++++++++++++++ .../clients/speed_client/speed_client.hpp | 27 +++++ .../instrumentclustercontroller.h | 10 +- apps/InstrumentCluster/main.cpp | 40 ++++++-- .../battery_client/client-example.cpp | 19 ++-- 35 files changed, 1633 insertions(+), 22 deletions(-) create mode 100644 apps/HeadUnit/clients/HU_gear_client/gear_client.cpp create mode 100644 apps/HeadUnit/clients/HU_gear_client/gear_client.hpp create mode 100644 apps/HeadUnit/clients/J_gear_client/gear_client.cpp create mode 100644 apps/HeadUnit/clients/J_gear_client/gear_client.hpp create mode 100644 apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp create mode 100644 apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp create mode 100644 apps/HeadUnit/clients/ambient_sender/client.cpp create mode 100644 apps/HeadUnit/clients/ambient_sender/client.hpp create mode 100644 apps/HeadUnit/clients/battery_client/battery_client.cpp create mode 100644 apps/HeadUnit/clients/battery_client/battery_client.hpp create mode 100644 apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp create mode 100644 apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp create mode 100644 apps/HeadUnit/clients/headers.hpp create mode 100644 apps/HeadUnit/clients/server.hpp create mode 100644 apps/HeadUnit/clients/speed_client/speed_client.cpp create mode 100644 apps/HeadUnit/clients/speed_client/speed_client.hpp create mode 100644 apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp create mode 100644 apps/InstrumentCluster/clients/HU_gear_client/gear_client.hpp create mode 100644 apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp create mode 100644 apps/InstrumentCluster/clients/J_gear_client/gear_client.hpp create mode 100644 apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp create mode 100644 apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp create mode 100644 apps/InstrumentCluster/clients/ambient_sender/client.cpp create mode 100644 apps/InstrumentCluster/clients/ambient_sender/client.hpp create mode 100644 apps/InstrumentCluster/clients/battery_client/battery_client.cpp create mode 100644 apps/InstrumentCluster/clients/battery_client/battery_client.hpp create mode 100644 apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp create mode 100644 apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp create mode 100644 apps/InstrumentCluster/clients/headers.hpp create mode 100644 apps/InstrumentCluster/clients/server.hpp create mode 100644 apps/InstrumentCluster/clients/speed_client/speed_client.cpp create mode 100644 apps/InstrumentCluster/clients/speed_client/speed_client.hpp diff --git a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp new file mode 100644 index 0000000..cc65afb --- /dev/null +++ b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp @@ -0,0 +1,88 @@ +#include +#include +#include + +#include +#include + +#include +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +//sending the actual data (will be gear data) to server. +void run() { + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(GEAR_SET_METHOD_ID); + +//sending actual data. changed into HU input value. + int value=0; + + while (1){ + // value += 1; + if (value != 5) + {std::cout << "Input number" << std::endl; + std::cin >> value;} + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + } + std::cout << "CLIENT : DATA SENDED" << std::endl; +} + +// When response come, print the payload from server. +void on_message(const std::shared_ptr &_response) { + std::shared_ptr its_payload = _response->get_payload(); + vsomeip::length_t l = its_payload->get_length(); + + // Get payload + std::stringstream ss; + for (vsomeip::length_t i=0; iget_data()+i) << " "; + } + + std::cout << "CLIENT: Received message with Client/Session [" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " + << ss.str() << std::endl; +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + if (_is_available) { + std::this_thread::sleep_for(std::chrono::seconds(3)); + condition.notify_one(); + } +} + +int main() { + + app = vsomeip::runtime::get()->create_application("gear"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_RESPONSE_MID, on_message); + std::thread sender(run); + app->start(); +} diff --git a/apps/HeadUnit/clients/HU_gear_client/gear_client.hpp b/apps/HeadUnit/clients/HU_gear_client/gear_client.hpp new file mode 100644 index 0000000..1e24653 --- /dev/null +++ b/apps/HeadUnit/clients/HU_gear_client/gear_client.hpp @@ -0,0 +1,34 @@ +#ifndef GEAR_CLIENT_HPP +# define GEAR_CLIENT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../server.hpp" + +class gearClient { +public: + gearClient(bool _use_tcp); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; + bool use_tcp_; + + gearClient(void); + ~gearClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/HeadUnit/clients/J_gear_client/gear_client.cpp b/apps/HeadUnit/clients/J_gear_client/gear_client.cpp new file mode 100644 index 0000000..8d1b531 --- /dev/null +++ b/apps/HeadUnit/clients/J_gear_client/gear_client.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +#include +#include +#include + +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +//TODO: sending the actual data (will be gear data) to server. + +// VEHICLE_SERVICE_ID, etc IDs are defined in src/server.hpp + +// void run(int gearValue) { +void run() { + // on avaliability notify this thread that client is connected to server + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + //create request + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + //basic setting + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(JOY_GEAR_SET_MID); + +//sending actual data. changed into HU input value. + int value=0; // <- real gear value should be on value variable + while (1){ + // std::cout << "Input number" << std::endl; + // std::cin >> value; + + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + // reinterpret_cast(&gearValue), + // reinterpret_cast(&gearValue) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "CLIENT : DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + + } +} +// PARKING 0 +// REVERSE 1 +// NEUTRAL 2 +// DRIVE 3 + +// When response come, print the payload from server. +void on_message(const std::shared_ptr &_response) { + std::shared_ptr payload = _response->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + } else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } + // +} + +// 서버랑 연결이 안되어있을때 전송을 할수도있어 +// vsomeip는 /tmp/vsomeip-100 103~~~ +// 얘가 소켓이 꼬여. 그래서 데이터 안가 제대로 연결이 됐어도 그래서 그냥 두는게 나을거같다. +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + if (_is_available) { + std::this_thread::sleep_for(std::chrono::seconds(3)); + condition.notify_one(); + } +} + +int main() { + app = vsomeip::runtime::get()->create_application("gear"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_SET_MID, on_message); + std::thread sender(run); + app->start(); +} diff --git a/apps/HeadUnit/clients/J_gear_client/gear_client.hpp b/apps/HeadUnit/clients/J_gear_client/gear_client.hpp new file mode 100644 index 0000000..1e24653 --- /dev/null +++ b/apps/HeadUnit/clients/J_gear_client/gear_client.hpp @@ -0,0 +1,34 @@ +#ifndef GEAR_CLIENT_HPP +# define GEAR_CLIENT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../server.hpp" + +class gearClient { +public: + gearClient(bool _use_tcp); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; + bool use_tcp_; + + gearClient(void); + ~gearClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp b/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp new file mode 100644 index 0000000..960e8d6 --- /dev/null +++ b/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp @@ -0,0 +1,36 @@ +#include "../../headers.hpp" +#include "../../server.hpp" +#include "./al_receiver.hpp" +//받은 int값을 처리. +std::shared_ptr app; + +void alClient::on_message(const std::shared_ptr &_request) { + std::shared_ptr payload = _request->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + } else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } + this->alValue = received_value; +} + +// int main() { +// app = vsomeip::runtime::get()->create_application("ambient"); +// app->init(); +// std::this_thread::sleep_for(std::chrono::seconds(2)); +// app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); +// app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); +// app->start(); +// } +void alClient::start() { + app = vsomeip::runtime::get()->create_application("ambient"); + app->init(); + std::this_thread::sleep_for(std::chrono::seconds(2)); + app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); + app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); + app->start(); +} \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp b/apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp new file mode 100644 index 0000000..5fe3804 --- /dev/null +++ b/apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp @@ -0,0 +1,16 @@ +#ifndef AL_RECEIVER_HPP +# define AL_RECEIVER_HPP + +class alClient +{ +private : + int alValue; + void on_message(const std::shared_ptr &_request); + void start(); + +public : + alClient(void); + ~alClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_sender/client.cpp b/apps/HeadUnit/clients/ambient_sender/client.cpp new file mode 100644 index 0000000..5d12b7a --- /dev/null +++ b/apps/HeadUnit/clients/ambient_sender/client.cpp @@ -0,0 +1,74 @@ +#include "../../headers.hpp" +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +void run() { + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + //create request + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + //basic setting + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(AL_INSTANCE_ID); + request->set_method(AL_SET_METHOD_ID); + + int value=0; + while (1){ + std::cout << "Input number" << std::endl; + std::cin >> value; + + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "CLIENT : DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + } + +void on_message(const std::shared_ptr &_response) { + + std::shared_ptr its_payload = _response->get_payload(); + vsomeip::length_t l = its_payload->get_length(); + + // Get payload + std::stringstream ss; + for (vsomeip::length_t i=0; iget_data()+i) << " "; + } + + std::cout << "CLIENT: Received message with Client/Session [" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " + << ss.str() << std::endl; +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + condition.notify_one(); +} + +int main() { + app = vsomeip::runtime::get()->create_application("ambient"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(2)); + app->request_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); + app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, vsomeip::ANY_METHOD, on_message); + std::thread sender(run); + app->start(); +} \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_sender/client.hpp b/apps/HeadUnit/clients/ambient_sender/client.hpp new file mode 100644 index 0000000..e69de29 diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp new file mode 100644 index 0000000..e32237b --- /dev/null +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -0,0 +1,83 @@ +#include "./batteryClient.hpp" + +client_sample::client_sample() : + app_(vsomeip::runtime::get()->create_application("battery")){ +} + +bool client_sample::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&client_sample::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, BATTERY_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&client_sample::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, + std::bind(&client_sample::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + + app_->request_event( + VEHICLE_SERVICE_ID, + BATTERY_INSTANCE_ID, + BATTERY_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + + app_->subscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void client_sample::start() { + app_->start(); +} + +void client_sample::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, BATTERY_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); + app_->stop(); +} + +void client_sample::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); + } +} + +void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void client_sample::on_message(const std::shared_ptr& _response) { + std::shared_ptr payload = _response->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + this->batteryValue = received_value; + } else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } + +} diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp new file mode 100644 index 0000000..755ad94 --- /dev/null +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -0,0 +1,24 @@ +#ifndef BATTERY_CLIENTHPP +#define BATTERY_CLIENTHPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class batteryClient { +public: + batteryClient(); + + bool init(); + void start(); + void stop(); + +private: + int batteryValue; + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; +}; + +#endif // BATTERY_CLIENTHPP diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp new file mode 100644 index 0000000..19a8075 --- /dev/null +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -0,0 +1,83 @@ +#include "./gear_client.hpp" + +client_sample::client_sample() : + app_(vsomeip::runtime::get()->create_application("gear")) { +} + +bool client_sample::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&client_sample::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&client_sample::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, + std::bind(&client_sample::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + + app_->request_event( + VEHICLE_SERVICE_ID, + GEAR_INSTANCE_ID, + GEAR_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + + app_->subscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void client_sample::start() { + app_->start(); +} + +void client_sample::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, GEAR_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + app_->stop(); +} + +void client_sample::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + } +} + +void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void client_sample::on_message(const std::shared_ptr &_request) { + std::shared_ptr payload = _request->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "GEAR DATA RECEIVING CLIENT : Received int: " << received_value << std::endl; + this->gearValue = received_value; + } else { + std::cerr << "GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; + return; + } + +} diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp new file mode 100644 index 0000000..d9dd398 --- /dev/null +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -0,0 +1,23 @@ +#ifndef GEAR_CLIENT_HPP +#define GEAR_CLIENT_HPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class gearClient { +public: + gearClient(); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; +}; + +#endif // GEAR_CLIENT_HPP diff --git a/apps/HeadUnit/clients/headers.hpp b/apps/HeadUnit/clients/headers.hpp new file mode 100644 index 0000000..d2ab20d --- /dev/null +++ b/apps/HeadUnit/clients/headers.hpp @@ -0,0 +1,28 @@ +#ifndef HEADERS_HPP +# define HEADERS_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#endif \ No newline at end of file diff --git a/apps/HeadUnit/clients/server.hpp b/apps/HeadUnit/clients/server.hpp new file mode 100644 index 0000000..42f8433 --- /dev/null +++ b/apps/HeadUnit/clients/server.hpp @@ -0,0 +1,60 @@ +// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef SERVER_HPP +#define SERVER_HPP + +//Vehicle value. speed & battery using same eventgroup id +#define VEHICLE_SERVICE_ID 0x1234 +#define VEHICLE_EVENTGROUP_ID 0x4465 + +//speed value +#define SPEED_INSTANCE_ID 0x2001 +#define SPEED_EVENT_ID 0x8779 + +//battery value +#define BATTERY_INSTANCE_ID 0x3001 +#define BATTERY_EVENT_ID 0x8780 + +// gear value +#define GEAR_INSTANCE_ID 0x4001 +#define GEAR_EVENT_ID 0x8781 + +// Ambient value +#define AL_INSTANCE_ID 0x5001 +#define AL_EVENT_ID 0x8782 + +#define SAMPLE_INSTANCE_ID 0x5678 + +// 공통 메서드 ID +#define GET_METHOD_ID 0x0001 +#define SET_METHOD_ID 0x0002 + +#define GEAR_SET_METHOD_ID 0x0421 +#define JOY_GEAR_SET_MID 0x0423 +#define JOY_GEAR_RESPONSE_MID 0x0425 + +#define AL_SET_METHOD_ID 0x0427 + +#endif // SERVER_HPP + +//previous + +// #define SAMPLE_SERVICE_ID 0x1234 +// #define SAMPLE_INSTANCE_ID 0x5678 +// #define SAMPLE_METHOD_ID 0x0421 +// #define SAMPLE_EVENTGROUP_ID 0x4465 + + +// #define SPEED_INSTANCE_ID 0x2001 +// #define BATTERY_INSTANCE_ID 0x3001 + + +// #define SAMPLE_EVENT_ID 0x8778 + +// #define SAMPLE_GET_METHOD_ID 0x0001 +// #define SAMPLE_SET_METHOD_ID 0x0002 + +// #endif // SERVER_HPP diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp new file mode 100644 index 0000000..77b25e4 --- /dev/null +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -0,0 +1,84 @@ +#include "./speed_client.hpp" + +speedClient::speedClient() : + app_(vsomeip::runtime::get()->create_application("speed")) { +} + +bool speedClient::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&speedClient::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, SPEED_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&speedClient::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, + std::bind(&speedClient::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + app_->request_event( + VEHICLE_SERVICE_ID, + SPEED_INSTANCE_ID, + SPEED_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + app_->subscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void speedClient::start() { + app_->start(); +} + +void speedClient::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, SPEED_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); + app_->stop(); +} + +void speedClient::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); + } +} + +void speedClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void speedClient::on_message(const std::shared_ptr& _response) { + + std::shared_ptr its_payload = _response->get_payload(); + if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 + float received_speed = 0.0f; + + // 이터레이터로 시작 위치 가져오기 + auto it = its_payload->get_data(); // 시작 위치의 이터레이터 + std::copy(it, it + sizeof(float), reinterpret_cast(&received_speed)); + + // 변환된 값 출력 + std::cout << "Received data: " << received_speed << " m/s" << std::endl; + this->speedValue = received_speed; + } else { + std::cerr << "Invalid data size received!" << std::endl; + } + + +} diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp new file mode 100644 index 0000000..f2dcc93 --- /dev/null +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -0,0 +1,27 @@ +#ifndef SPEED_CLIENT_HPP +#define SPEED_CLIENT_HPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class speedClient{ +public: + speedClient(); + + bool init(); + void start(); + void stop(); + +signals: + void speedReceived(int speed); + +private: + float speedValue; + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; +}; + +#endif // speedClient_HPP diff --git a/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp b/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp new file mode 100644 index 0000000..cc65afb --- /dev/null +++ b/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp @@ -0,0 +1,88 @@ +#include +#include +#include + +#include +#include + +#include +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +//sending the actual data (will be gear data) to server. +void run() { + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(GEAR_SET_METHOD_ID); + +//sending actual data. changed into HU input value. + int value=0; + + while (1){ + // value += 1; + if (value != 5) + {std::cout << "Input number" << std::endl; + std::cin >> value;} + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + } + std::cout << "CLIENT : DATA SENDED" << std::endl; +} + +// When response come, print the payload from server. +void on_message(const std::shared_ptr &_response) { + std::shared_ptr its_payload = _response->get_payload(); + vsomeip::length_t l = its_payload->get_length(); + + // Get payload + std::stringstream ss; + for (vsomeip::length_t i=0; iget_data()+i) << " "; + } + + std::cout << "CLIENT: Received message with Client/Session [" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " + << ss.str() << std::endl; +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + if (_is_available) { + std::this_thread::sleep_for(std::chrono::seconds(3)); + condition.notify_one(); + } +} + +int main() { + + app = vsomeip::runtime::get()->create_application("gear"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_RESPONSE_MID, on_message); + std::thread sender(run); + app->start(); +} diff --git a/apps/InstrumentCluster/clients/HU_gear_client/gear_client.hpp b/apps/InstrumentCluster/clients/HU_gear_client/gear_client.hpp new file mode 100644 index 0000000..1e24653 --- /dev/null +++ b/apps/InstrumentCluster/clients/HU_gear_client/gear_client.hpp @@ -0,0 +1,34 @@ +#ifndef GEAR_CLIENT_HPP +# define GEAR_CLIENT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../server.hpp" + +class gearClient { +public: + gearClient(bool _use_tcp); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; + bool use_tcp_; + + gearClient(void); + ~gearClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp b/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp new file mode 100644 index 0000000..8d1b531 --- /dev/null +++ b/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp @@ -0,0 +1,99 @@ +#include +#include +#include + +#include +#include +#include + +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +//TODO: sending the actual data (will be gear data) to server. + +// VEHICLE_SERVICE_ID, etc IDs are defined in src/server.hpp + +// void run(int gearValue) { +void run() { + // on avaliability notify this thread that client is connected to server + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + //create request + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + //basic setting + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(JOY_GEAR_SET_MID); + +//sending actual data. changed into HU input value. + int value=0; // <- real gear value should be on value variable + while (1){ + // std::cout << "Input number" << std::endl; + // std::cin >> value; + + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + // reinterpret_cast(&gearValue), + // reinterpret_cast(&gearValue) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "CLIENT : DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + + } +} +// PARKING 0 +// REVERSE 1 +// NEUTRAL 2 +// DRIVE 3 + +// When response come, print the payload from server. +void on_message(const std::shared_ptr &_response) { + std::shared_ptr payload = _response->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + } else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } + // +} + +// 서버랑 연결이 안되어있을때 전송을 할수도있어 +// vsomeip는 /tmp/vsomeip-100 103~~~ +// 얘가 소켓이 꼬여. 그래서 데이터 안가 제대로 연결이 됐어도 그래서 그냥 두는게 나을거같다. +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + if (_is_available) { + std::this_thread::sleep_for(std::chrono::seconds(3)); + condition.notify_one(); + } +} + +int main() { + app = vsomeip::runtime::get()->create_application("gear"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_SET_MID, on_message); + std::thread sender(run); + app->start(); +} diff --git a/apps/InstrumentCluster/clients/J_gear_client/gear_client.hpp b/apps/InstrumentCluster/clients/J_gear_client/gear_client.hpp new file mode 100644 index 0000000..1e24653 --- /dev/null +++ b/apps/InstrumentCluster/clients/J_gear_client/gear_client.hpp @@ -0,0 +1,34 @@ +#ifndef GEAR_CLIENT_HPP +# define GEAR_CLIENT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../server.hpp" + +class gearClient { +public: + gearClient(bool _use_tcp); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; + bool use_tcp_; + + gearClient(void); + ~gearClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp new file mode 100644 index 0000000..960e8d6 --- /dev/null +++ b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp @@ -0,0 +1,36 @@ +#include "../../headers.hpp" +#include "../../server.hpp" +#include "./al_receiver.hpp" +//받은 int값을 처리. +std::shared_ptr app; + +void alClient::on_message(const std::shared_ptr &_request) { + std::shared_ptr payload = _request->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + } else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } + this->alValue = received_value; +} + +// int main() { +// app = vsomeip::runtime::get()->create_application("ambient"); +// app->init(); +// std::this_thread::sleep_for(std::chrono::seconds(2)); +// app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); +// app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); +// app->start(); +// } +void alClient::start() { + app = vsomeip::runtime::get()->create_application("ambient"); + app->init(); + std::this_thread::sleep_for(std::chrono::seconds(2)); + app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); + app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); + app->start(); +} \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp new file mode 100644 index 0000000..5fe3804 --- /dev/null +++ b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp @@ -0,0 +1,16 @@ +#ifndef AL_RECEIVER_HPP +# define AL_RECEIVER_HPP + +class alClient +{ +private : + int alValue; + void on_message(const std::shared_ptr &_request); + void start(); + +public : + alClient(void); + ~alClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_sender/client.cpp b/apps/InstrumentCluster/clients/ambient_sender/client.cpp new file mode 100644 index 0000000..5d12b7a --- /dev/null +++ b/apps/InstrumentCluster/clients/ambient_sender/client.cpp @@ -0,0 +1,74 @@ +#include "../../headers.hpp" +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +void run() { + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + //create request + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + //basic setting + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(AL_INSTANCE_ID); + request->set_method(AL_SET_METHOD_ID); + + int value=0; + while (1){ + std::cout << "Input number" << std::endl; + std::cin >> value; + + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "CLIENT : DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + } + +void on_message(const std::shared_ptr &_response) { + + std::shared_ptr its_payload = _response->get_payload(); + vsomeip::length_t l = its_payload->get_length(); + + // Get payload + std::stringstream ss; + for (vsomeip::length_t i=0; iget_data()+i) << " "; + } + + std::cout << "CLIENT: Received message with Client/Session [" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " + << ss.str() << std::endl; +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + condition.notify_one(); +} + +int main() { + app = vsomeip::runtime::get()->create_application("ambient"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(2)); + app->request_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); + app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, vsomeip::ANY_METHOD, on_message); + std::thread sender(run); + app->start(); +} \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_sender/client.hpp b/apps/InstrumentCluster/clients/ambient_sender/client.hpp new file mode 100644 index 0000000..e69de29 diff --git a/apps/InstrumentCluster/clients/battery_client/battery_client.cpp b/apps/InstrumentCluster/clients/battery_client/battery_client.cpp new file mode 100644 index 0000000..e32237b --- /dev/null +++ b/apps/InstrumentCluster/clients/battery_client/battery_client.cpp @@ -0,0 +1,83 @@ +#include "./batteryClient.hpp" + +client_sample::client_sample() : + app_(vsomeip::runtime::get()->create_application("battery")){ +} + +bool client_sample::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&client_sample::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, BATTERY_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&client_sample::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, + std::bind(&client_sample::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + + app_->request_event( + VEHICLE_SERVICE_ID, + BATTERY_INSTANCE_ID, + BATTERY_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + + app_->subscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void client_sample::start() { + app_->start(); +} + +void client_sample::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, BATTERY_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); + app_->stop(); +} + +void client_sample::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); + } +} + +void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void client_sample::on_message(const std::shared_ptr& _response) { + std::shared_ptr payload = _response->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; + this->batteryValue = received_value; + } else { + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; + } + +} diff --git a/apps/InstrumentCluster/clients/battery_client/battery_client.hpp b/apps/InstrumentCluster/clients/battery_client/battery_client.hpp new file mode 100644 index 0000000..755ad94 --- /dev/null +++ b/apps/InstrumentCluster/clients/battery_client/battery_client.hpp @@ -0,0 +1,24 @@ +#ifndef BATTERY_CLIENTHPP +#define BATTERY_CLIENTHPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class batteryClient { +public: + batteryClient(); + + bool init(); + void start(); + void stop(); + +private: + int batteryValue; + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; +}; + +#endif // BATTERY_CLIENTHPP diff --git a/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp new file mode 100644 index 0000000..19a8075 --- /dev/null +++ b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp @@ -0,0 +1,83 @@ +#include "./gear_client.hpp" + +client_sample::client_sample() : + app_(vsomeip::runtime::get()->create_application("gear")) { +} + +bool client_sample::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&client_sample::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&client_sample::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, + std::bind(&client_sample::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + + app_->request_event( + VEHICLE_SERVICE_ID, + GEAR_INSTANCE_ID, + GEAR_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + + app_->subscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void client_sample::start() { + app_->start(); +} + +void client_sample::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, GEAR_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + app_->stop(); +} + +void client_sample::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + } +} + +void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void client_sample::on_message(const std::shared_ptr &_request) { + std::shared_ptr payload = _request->get_payload(); + int received_value = 0; + + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "GEAR DATA RECEIVING CLIENT : Received int: " << received_value << std::endl; + this->gearValue = received_value; + } else { + std::cerr << "GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; + return; + } + +} diff --git a/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp new file mode 100644 index 0000000..d9dd398 --- /dev/null +++ b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp @@ -0,0 +1,23 @@ +#ifndef GEAR_CLIENT_HPP +#define GEAR_CLIENT_HPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class gearClient { +public: + gearClient(); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; +}; + +#endif // GEAR_CLIENT_HPP diff --git a/apps/InstrumentCluster/clients/headers.hpp b/apps/InstrumentCluster/clients/headers.hpp new file mode 100644 index 0000000..d2ab20d --- /dev/null +++ b/apps/InstrumentCluster/clients/headers.hpp @@ -0,0 +1,28 @@ +#ifndef HEADERS_HPP +# define HEADERS_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#endif \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/server.hpp b/apps/InstrumentCluster/clients/server.hpp new file mode 100644 index 0000000..42f8433 --- /dev/null +++ b/apps/InstrumentCluster/clients/server.hpp @@ -0,0 +1,60 @@ +// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef SERVER_HPP +#define SERVER_HPP + +//Vehicle value. speed & battery using same eventgroup id +#define VEHICLE_SERVICE_ID 0x1234 +#define VEHICLE_EVENTGROUP_ID 0x4465 + +//speed value +#define SPEED_INSTANCE_ID 0x2001 +#define SPEED_EVENT_ID 0x8779 + +//battery value +#define BATTERY_INSTANCE_ID 0x3001 +#define BATTERY_EVENT_ID 0x8780 + +// gear value +#define GEAR_INSTANCE_ID 0x4001 +#define GEAR_EVENT_ID 0x8781 + +// Ambient value +#define AL_INSTANCE_ID 0x5001 +#define AL_EVENT_ID 0x8782 + +#define SAMPLE_INSTANCE_ID 0x5678 + +// 공통 메서드 ID +#define GET_METHOD_ID 0x0001 +#define SET_METHOD_ID 0x0002 + +#define GEAR_SET_METHOD_ID 0x0421 +#define JOY_GEAR_SET_MID 0x0423 +#define JOY_GEAR_RESPONSE_MID 0x0425 + +#define AL_SET_METHOD_ID 0x0427 + +#endif // SERVER_HPP + +//previous + +// #define SAMPLE_SERVICE_ID 0x1234 +// #define SAMPLE_INSTANCE_ID 0x5678 +// #define SAMPLE_METHOD_ID 0x0421 +// #define SAMPLE_EVENTGROUP_ID 0x4465 + + +// #define SPEED_INSTANCE_ID 0x2001 +// #define BATTERY_INSTANCE_ID 0x3001 + + +// #define SAMPLE_EVENT_ID 0x8778 + +// #define SAMPLE_GET_METHOD_ID 0x0001 +// #define SAMPLE_SET_METHOD_ID 0x0002 + +// #endif // SERVER_HPP diff --git a/apps/InstrumentCluster/clients/speed_client/speed_client.cpp b/apps/InstrumentCluster/clients/speed_client/speed_client.cpp new file mode 100644 index 0000000..77b25e4 --- /dev/null +++ b/apps/InstrumentCluster/clients/speed_client/speed_client.cpp @@ -0,0 +1,84 @@ +#include "./speed_client.hpp" + +speedClient::speedClient() : + app_(vsomeip::runtime::get()->create_application("speed")) { +} + +bool speedClient::init() { + if (!app_->init()) { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + + // 상태 핸들러 등록 + app_->register_state_handler( + std::bind(&speedClient::on_state, this, std::placeholders::_1)); + + // 메시지 핸들러 등록 + app_->register_message_handler( + vsomeip::ANY_SERVICE, SPEED_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&speedClient::on_message, this, std::placeholders::_1)); + + // 가용성 핸들러 등록 + app_->register_availability_handler(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, + std::bind(&speedClient::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + + // 이벤트 구독 + std::set its_groups; + its_groups.insert(VEHICLE_EVENTGROUP_ID); + app_->request_event( + VEHICLE_SERVICE_ID, + SPEED_INSTANCE_ID, + SPEED_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + app_->subscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + + return true; +} + +void speedClient::start() { + app_->start(); +} + +void speedClient::stop() { + app_->clear_all_handler(); + app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); + app_->release_event(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, SPEED_EVENT_ID); + app_->release_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); + app_->stop(); +} + +void speedClient::on_state(vsomeip::state_type_e _state) { + if (_state == vsomeip::state_type_e::ST_REGISTERED) { + app_->request_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); + } +} + +void speedClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "Service [" + << std::hex << std::setfill('0') << std::setw(4) << _service << "." + << std::setw(4) << _instance << "] is " + << (_is_available ? "available." : "NOT available.") << std::endl; +} + +void speedClient::on_message(const std::shared_ptr& _response) { + + std::shared_ptr its_payload = _response->get_payload(); + if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 + float received_speed = 0.0f; + + // 이터레이터로 시작 위치 가져오기 + auto it = its_payload->get_data(); // 시작 위치의 이터레이터 + std::copy(it, it + sizeof(float), reinterpret_cast(&received_speed)); + + // 변환된 값 출력 + std::cout << "Received data: " << received_speed << " m/s" << std::endl; + this->speedValue = received_speed; + } else { + std::cerr << "Invalid data size received!" << std::endl; + } + + +} diff --git a/apps/InstrumentCluster/clients/speed_client/speed_client.hpp b/apps/InstrumentCluster/clients/speed_client/speed_client.hpp new file mode 100644 index 0000000..f2dcc93 --- /dev/null +++ b/apps/InstrumentCluster/clients/speed_client/speed_client.hpp @@ -0,0 +1,27 @@ +#ifndef SPEED_CLIENT_HPP +#define SPEED_CLIENT_HPP + +#include "../../headers.hpp" +#include "../../server.hpp" + +class speedClient{ +public: + speedClient(); + + bool init(); + void start(); + void stop(); + +signals: + void speedReceived(int speed); + +private: + float speedValue; + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; +}; + +#endif // speedClient_HPP diff --git a/apps/InstrumentCluster/instrumentclustercontroller.h b/apps/InstrumentCluster/instrumentclustercontroller.h index 9448690..44a0331 100644 --- a/apps/InstrumentCluster/instrumentclustercontroller.h +++ b/apps/InstrumentCluster/instrumentclustercontroller.h @@ -2,6 +2,10 @@ #define INSTRUMENTCLUSTERCONTROLLER_H #include +#include "./clients/speed_client/speed_client.hpp" +#include "./clients/battery_client/battery_client.hpp" +#include "./clients/gear_data_receiving_client/gear_client.hpp" +#include "./clients/ambient_receiver/al_receiver.hpp" class InstrumentClusterController : public QObject { @@ -33,10 +37,12 @@ class InstrumentClusterController : public QObject void speedChanged(int speed); void batteryPercentageChanged(int batteryPercentage); void chargingStateChanged(bool state); - -private: + + + private: QString _currentGear = "P"; QString _ambientLighting = "#000000"; + int _speed = 0; int _batteryPercentage = 100; bool _chargingState = false; diff --git a/apps/InstrumentCluster/main.cpp b/apps/InstrumentCluster/main.cpp index 06f9ea1..0ea3444 100644 --- a/apps/InstrumentCluster/main.cpp +++ b/apps/InstrumentCluster/main.cpp @@ -7,13 +7,18 @@ #include #include "instrumentclustercontroller.h" - +//TODO: 2025-02-20. For ambient light, send/recive data type change required. int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; InstrumentClusterController controller; + + speedClient speedClient(); + batteryClient batteryClient(); + gearClient gearClient(); + alClient alClient(); engine.rootContext()->setContextProperty("instrumentClusterController", &controller); @@ -27,28 +32,45 @@ int main(int argc, char *argv[]) qDebug() << "Instrument Cluster launched"; + if (speedClient.init()) + speedClient.start(); + if (batteryClient.init()) + batteryClient.start(); + if (gearClient.init()) + gearClient.start(); + + alClient.start(); + // test code QTimer *timer = new QTimer(&controller); QObject::connect(timer, &QTimer::timeout, [&controller]() { - static QStringList gears = {"P", "R", "N", "D"}; - static int gearIndex = 0; - controller.setCurrentGear(gears[gearIndex]); - gearIndex = (gearIndex + 1) % gears.size(); - + //Speed static int speed = 0; - speed = (speed + 10) % 310; + speed = speedClient.speedValue; controller.setSpeed(speed); + //Battery static int batteryPercentage = 100; - batteryPercentage = (batteryPercentage - 10) < 0 ? 100 : batteryPercentage - 10; + batteryPercentage = batteryClient.batteryValue; controller.setBatteryPercentage(batteryPercentage); static int chargingState = false; chargingState = !chargingState; controller.setChargingState(chargingState); - static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; + //Gear + static QStringList gears = {"P", "R", "N", "D"}; + static int gearIndex = 0; + gearIndex = gearClient.gearValue; // Drive = 3, NEUTRAL=2, REVERSE=1, PARKING=0 + + controller.setCurrentGear(gears[gearIndex]); + gearIndex = (gearIndex + 1) % gears.size(); + + //Ambient Light + // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; static int colorIndex = 0; + colorIndex = alClient.alValue; // <- set colorIndex to received value + controller.setAmbientLighting(colors[colorIndex]); colorIndex = (colorIndex + 1) % colors.size(); }); diff --git a/apps/ServiceManager/src/battery-src/battery_client/client-example.cpp b/apps/ServiceManager/src/battery-src/battery_client/client-example.cpp index fe7fe05..91b9349 100644 --- a/apps/ServiceManager/src/battery-src/battery_client/client-example.cpp +++ b/apps/ServiceManager/src/battery-src/battery_client/client-example.cpp @@ -81,18 +81,15 @@ void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instan } void client_sample::on_message(const std::shared_ptr& _response) { + std::shared_ptr payload = _response->get_payload(); + int received_value = 0; - std::shared_ptr its_payload = _response->get_payload(); - if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 - float received_speed = 0.0f; - - // 이터레이터로 시작 위치 가져오기 - auto it = its_payload->get_data(); // 시작 위치의 이터레이터 - std::copy(it, it + sizeof(float), reinterpret_cast(&received_speed)); - - // 변환된 값 출력 - std::cout << "Received data: " << received_speed << " m/s" << std::endl; + if (payload->get_length() >= sizeof(int)) { + received_value = *reinterpret_cast(payload->get_data()); + std::cout << "SERVER: Received int: " << received_value << std::endl; } else { - std::cerr << "Invalid data size received!" << std::endl; + std::cerr << "SERVER: Invalid payload size!" << std::endl; + return; } + } From 3c122df66139c69d54e27cc353e02eb11c26a8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 20:57:50 +0100 Subject: [PATCH 09/73] fix: include cpp libraries --- apps/VehicleController/main.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 760269b..bb02309 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -2,6 +2,15 @@ #include "Gamepad/ShanwanGamepad.hpp" #include "../ServiceManager/src/server.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + /** * A : DRIVE * B : NEUTRAL From 3db06f06d3f40e5db6a3511f81a32f9a68695568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 20:58:03 +0100 Subject: [PATCH 10/73] fix: fix typo --- apps/HeadUnit/Main.qml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index c961a77..17b5a69 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -380,7 +380,7 @@ Window { id: mouseAreaP anchors.fill: parent onClicked: { - console.log("D clicked") + console.log("P clicked") } } } From 06e6270837500c7417846dfff35cbe88a3c93652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 20 Feb 2025 20:58:35 +0100 Subject: [PATCH 11/73] feat: send gear data using vSOME/IP --- apps/HeadUnit/CMakeLists.txt | 2 ++ apps/HeadUnit/Main.qml | 4 ++++ apps/HeadUnit/shared/utils/someip.cpp | 23 +++++++++++++++++++++++ apps/HeadUnit/shared/utils/someip.h | 25 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 apps/HeadUnit/shared/utils/someip.cpp create mode 100644 apps/HeadUnit/shared/utils/someip.h diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index 484e03f..38b8c17 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -21,6 +21,8 @@ qt_add_executable(head-unit shared/utils/envmanager.cpp shared/utils/utils.h shared/utils/utils.cpp + shared/utils/someip.h + shared/utils/someip.cpp modules/spotify/spotify.h modules/spotify/spotify.cpp diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index 17b5a69..6ecd91f 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -187,6 +187,7 @@ Window { anchors.fill: parent onClicked: { console.log("D clicked") + someIP.send_gear_data(3); } } } @@ -252,6 +253,7 @@ Window { anchors.fill: parent onClicked: { console.log("N clicked") + someIP.send_gear_data(2); } } } @@ -317,6 +319,7 @@ Window { anchors.fill: parent onClicked: { console.log("R clicked") + someIP.send_gear_data(1); } } } @@ -381,6 +384,7 @@ Window { anchors.fill: parent onClicked: { console.log("P clicked") + someIP.send_gear_data(0); } } } diff --git a/apps/HeadUnit/shared/utils/someip.cpp b/apps/HeadUnit/shared/utils/someip.cpp new file mode 100644 index 0000000..20009e7 --- /dev/null +++ b/apps/HeadUnit/shared/utils/someip.cpp @@ -0,0 +1,23 @@ +#include "./someip.h" + +void SomeIP::send_gear_data(int gear) +{ + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + std::shared_ptr request; + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(GEAR_SET_METHOD_ID); + + std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&gear), + reinterpret_cast(&gear) + sizeof(int)); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); +} diff --git a/apps/HeadUnit/shared/utils/someip.h b/apps/HeadUnit/shared/utils/someip.h new file mode 100644 index 0000000..5936ad7 --- /dev/null +++ b/apps/HeadUnit/shared/utils/someip.h @@ -0,0 +1,25 @@ +#ifndef SOMEIP_H +#define SOMEIP_H + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +class SomeIP : public QObject +{ + Q_OBJECT +public: + explicit SomeIP(QObject *parent = nullptr) : QObject(parent) {} + + Q_INVOKABLE void send_gear_data(int); +}; + +#endif // SOMEIP_H From 48da02445fd2d3ec8a521cb62dc34d3f804885f7 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 24 Feb 2025 14:12:46 +0100 Subject: [PATCH 12/73] [FEAT] : add data receiving codess --- apps/HeadUnit/HeadUnit.hpp | 5 +++ apps/HeadUnit/main.cpp | 41 ++++++++++++++++++- .../instrumentclustercontroller.h | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/apps/HeadUnit/HeadUnit.hpp b/apps/HeadUnit/HeadUnit.hpp index 0228f1f..6ca7131 100644 --- a/apps/HeadUnit/HeadUnit.hpp +++ b/apps/HeadUnit/HeadUnit.hpp @@ -1,6 +1,11 @@ #pragma once #include +#include "./clients/speed_client/speed_client.hpp" +#include "./clients/battery_client/battery_client.hpp" +#include "./clients/gear_data_receiving_client/gear_client.hpp" +#include "./clients/ambient_receiver/al_receiver.hpp" + class HeadUnit : public QObject { diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index f6383c9..6b36352 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -6,7 +6,7 @@ #include "modules/spotify/spotify.h" #include "shared/utils/envmanager.h" - +#include "HeadUnit.hpp" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL); @@ -25,12 +25,49 @@ int main(int argc, char *argv[]) Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); + // QObject::connect( + // &engine, + // &QQmlApplicationEngine::objectCreationFailed, + // &app, + // []() { QCoreApplication::exit(-1); }, + // Qt::QueuedConnection); + QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, - []() { QCoreApplication::exit(-1); }, + []() { QCoreApplication::exit(-1); + static int speed = 0; + speed = speedClient.speedValue; + controller.setSpeed(speed); + + //Battery + static int batteryPercentage = 100; + batteryPercentage = batteryClient.batteryValue; + controller.setBatteryPercentage(batteryPercentage); + + static int chargingState = false; + chargingState = !chargingState; + controller.setChargingState(chargingState); + + //Gear + static QStringList gears = {"P", "R", "N", "D"}; + static int gearIndex = 0; + gearIndex = gearClient.gearValue; // Drive = 3, NEUTRAL=2, REVERSE=1, PARKING=0 + + controller.setCurrentGear(gears[gearIndex]); + gearIndex = (gearIndex + 1) % gears.size(); + + //Ambient Light + // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; + static int colorIndex = 0; + colorIndex = alClient.alValue; // <- set colorIndex to received value + + controller.setAmbientLighting(colors[colorIndex]); + colorIndex = (colorIndex + 1) % colors.size(); + }, Qt::QueuedConnection); + // engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); engine.loadFromModule("HeadUnit", "Main"); diff --git a/apps/InstrumentCluster/instrumentclustercontroller.h b/apps/InstrumentCluster/instrumentclustercontroller.h index 44a0331..e31cf3f 100644 --- a/apps/InstrumentCluster/instrumentclustercontroller.h +++ b/apps/InstrumentCluster/instrumentclustercontroller.h @@ -41,6 +41,7 @@ class InstrumentClusterController : public QObject private: QString _currentGear = "P"; + //TODO: Qstring to int. QString _ambientLighting = "#000000"; int _speed = 0; From b27fd9715944c1c3e7ea41e037b5e00445d2ef1c Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Mon, 24 Feb 2025 16:28:53 +0100 Subject: [PATCH 13/73] Rebasing 56 branch --- apps/HeadUnit/CMakeLists.txt | 3 +- apps/HeadUnit/HeadUnit.cpp | 15 +----- apps/HeadUnit/HeadUnit.h | 37 +++++++++++++ apps/HeadUnit/Main.qml | 80 +++++++++++++++-------------- apps/HeadUnit/main.cpp | 38 +++++++++++--- apps/HeadUnit/modules/home/Home.qml | 2 - apps/HeadUnit/resources.qrc | 2 - 7 files changed, 114 insertions(+), 63 deletions(-) create mode 100644 apps/HeadUnit/HeadUnit.h diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index 38b8c17..e770f98 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -14,7 +14,7 @@ qt_add_executable(head-unit main.cpp HeadUnit.cpp - SpeedClient.cpp + # SpeedClient.cpp resources.qrc shared/utils/envmanager.h @@ -44,6 +44,7 @@ qt_add_qml_module(head-unit modules/youtube/Youtube_main.qml SOURCES + HeadUnit.h modules/spotify/spotify.h modules/spotify/spotify.cpp shared/utils/envmanager.h diff --git a/apps/HeadUnit/HeadUnit.cpp b/apps/HeadUnit/HeadUnit.cpp index 8b9268c..1bf671f 100644 --- a/apps/HeadUnit/HeadUnit.cpp +++ b/apps/HeadUnit/HeadUnit.cpp @@ -9,11 +9,6 @@ const QString& HeadUnit::currentGear() const return _currentGear; } -const QString& HeadUnit::ambientLighting() const -{ - return _ambientLighting; -} - int HeadUnit::speed() const { return _speed; @@ -37,14 +32,6 @@ void HeadUnit::setCurrentGear(const QString& gear) } } -void HeadUnit::setAmbientLighting(const QString& color) -{ - if (_ambientLighting != color) { - _ambientLighting = color; - emit ambientLightingChanged(color); - } -} - void HeadUnit::setSpeed(int speed) { if (_speed != speed) { @@ -68,3 +55,5 @@ void HeadUnit::setChargingState(bool state) emit chargingStateChanged(state); } } + + diff --git a/apps/HeadUnit/HeadUnit.h b/apps/HeadUnit/HeadUnit.h new file mode 100644 index 0000000..6c5f804 --- /dev/null +++ b/apps/HeadUnit/HeadUnit.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +class HeadUnit : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString currentGear READ currentGear WRITE setCurrentGear NOTIFY currentGearChanged) + Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged) + Q_PROPERTY(int batteryPercentage READ batteryPercentage WRITE setBatteryPercentage NOTIFY batteryPercentageChanged) + Q_PROPERTY(int chargingState READ chargingState WRITE setChargingState NOTIFY chargingStateChanged) + +public: + explicit HeadUnit(QObject *parent = nullptr); + + const QString& currentGear() const; + int speed() const; + int batteryPercentage() const; + bool chargingState() const; + + void setCurrentGear(const QString& gear); + void setSpeed(int speed); + void setBatteryPercentage(int batteryPercentage); + void setChargingState(bool state); + +signals: + void currentGearChanged(const QString& gear); + void speedChanged(int speed); + void batteryPercentageChanged(int batteryPercentage); + void chargingStateChanged(bool state); + +private: + QString _currentGear = "P"; + int _speed = 0; + int _batteryPercentage = 100; + bool _chargingState = false; +}; diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index 6ecd91f..10eab97 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -7,9 +7,10 @@ import HeadUnit 1.0 Window { id: window + visible: true width: 1024 height: 600 - visible: true + title: qsTr("HeadUnit") color: "black" property int colorValue: 180 @@ -123,8 +124,10 @@ Window { z:1 ColumnLayout { + id: gearColumn anchors.centerIn: parent spacing:0 + property string activeGear: "park" Rectangle { Layout.preferredWidth: 110 @@ -135,18 +138,17 @@ Window { id: drive height: 40 x: 40 - y: 20 + y: 15 source: "qrc:/shared/images/drive" fillMode: Image.PreserveAspectFit - // anchors.centerIn: parent smooth: true mipmap: true - + opacity: 0 } ColorOverlay { anchors.fill: drive source: drive - color: myColor + color: gearColumn.activeGear === "drive" ? myColor : Qt.darker(myColor, 2.5) smooth: true } //Glowing Effect @@ -188,6 +190,7 @@ Window { onClicked: { console.log("D clicked") someIP.send_gear_data(3); + gearColumn.activeGear = "drive" } } } @@ -200,19 +203,18 @@ Window { Image { id: neutral height: 40 - x: 40 - y: 20 + x: 38 + y: 22 source: "qrc:/shared/images/neutral" fillMode: Image.PreserveAspectFit - // anchors.centerIn: parent smooth: true mipmap: true - + opacity: 0 } ColorOverlay { anchors.fill: neutral source: neutral - color: myColor + color: gearColumn.activeGear === "neutral" ? myColor : Qt.darker(myColor, 2.5) smooth: true } //Glowing Effect @@ -254,6 +256,7 @@ Window { onClicked: { console.log("N clicked") someIP.send_gear_data(2); + gearColumn.activeGear = "neutral" } } } @@ -267,18 +270,17 @@ Window { id: reverse height: 40 x: 40 - y: 30 + y: 35 source: "qrc:/shared/images/reverse" fillMode: Image.PreserveAspectFit - // anchors.centerIn: parent smooth: true mipmap: true - + opacity: 0 } ColorOverlay { anchors.fill: reverse source: reverse - color: myColor + color: gearColumn.activeGear === "reverse" ? myColor : Qt.darker(myColor, 2.5) smooth: true } //Glowing Effect @@ -320,6 +322,7 @@ Window { onClicked: { console.log("R clicked") someIP.send_gear_data(1); + gearColumn.activeGear = "reverse" } } } @@ -332,18 +335,17 @@ Window { id: park height: 40 x: 40 - y: 35 + y: 43 source: "qrc:/shared/images/park" fillMode: Image.PreserveAspectFit - // anchors.centerIn: parent smooth: true mipmap: true - + opacity: 0 } ColorOverlay { anchors.fill: park source: park - color: myColor + color: gearColumn.activeGear === "park" ? myColor : Qt.darker(myColor, 2.5) smooth: true } //Glowing Effect @@ -385,6 +387,7 @@ Window { onClicked: { console.log("P clicked") someIP.send_gear_data(0); + gearColumn.activeGear = "park" } } } @@ -408,6 +411,7 @@ Window { anchors.centerIn: parent smooth: true mipmap: true + opacity: 0 } ColorOverlay { anchors.fill: menuIcon @@ -496,26 +500,26 @@ Window { } } } - - Image { - id: gearHighlights - source: "qrc:/shared/images/gearlines" - anchors.left: parent.left - y: 50 - anchors.leftMargin: 5 - fillMode: Image.PreserveAspectFit - width: 110 - smooth: true - mipmap: true - z:2 - - } - ColorOverlay { - anchors.fill: gearHighlights - source: gearHighlights - color: myColor - smooth: true - z:2 + ColumnLayout{ + x: 25 + y: 165 + spacing: 110 + opacity: 0.7 + Rectangle{ + width:70 + height: 2 + color: myColor + } + Rectangle{ + width: 70 + height: 2 + color: myColor + } + Rectangle{ + width: 70 + height: 2 + color: myColor + } } Slider { diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 6b36352..098d8a4 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -3,25 +3,27 @@ #include #include #include - #include "modules/spotify/spotify.h" -#include "shared/utils/envmanager.h" -#include "HeadUnit.hpp" +// #include "shared/utils/envmanager.h" +#include "HeadUnit.h" + +#include + int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL); QGuiApplication app(argc, argv); + QQmlApplicationEngine engine; + + HeadUnit controller; // todo fix the location - EnvManager::instance().loadEnvFile("/home/wonjeong/head-unit/apps/HeadUnit/.env"); + // EnvManager::instance().loadEnvFile("/home/wonjeong/head-unit/apps/HeadUnit/.env"); // Register the Spotify class to be used in QML // qmlRegisterType("com.spotify", 1, 0, "Spotify"); - - QQmlApplicationEngine engine; - Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); @@ -71,6 +73,28 @@ int main(int argc, char *argv[]) // engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); engine.loadFromModule("HeadUnit", "Main"); + // test code + QTimer *timer = new QTimer(&controller); + QObject::connect(timer, &QTimer::timeout, [&controller]() { + // static QStringList gears = {"P", "R", "N", "D"}; + // static int gearIndex = 0; + // controller.setCurrentGear(gears[gearIndex]); + // gearIndex = (gearIndex + 1) % gears.size(); + + static int speed = 0; + speed = (speed + 10) % 310; + controller.setSpeed(speed); + + static int batteryPercentage = 100; + batteryPercentage = (batteryPercentage - 10) < 0 ? 100 : batteryPercentage - 10; + controller.setBatteryPercentage(batteryPercentage); + + static int chargingState = false; + chargingState = !chargingState; + controller.setChargingState(chargingState); + + }); + timer->start(1000); qDebug() << "Head Unit launched"; diff --git a/apps/HeadUnit/modules/home/Home.qml b/apps/HeadUnit/modules/home/Home.qml index 32ec069..b0890a1 100644 --- a/apps/HeadUnit/modules/home/Home.qml +++ b/apps/HeadUnit/modules/home/Home.qml @@ -6,8 +6,6 @@ import QtMultimedia Item{ id:home - width: parent.width - height: parent.height x: 0 y: 0 clip: false diff --git a/apps/HeadUnit/resources.qrc b/apps/HeadUnit/resources.qrc index 4f6fb50..3cb8c4a 100644 --- a/apps/HeadUnit/resources.qrc +++ b/apps/HeadUnit/resources.qrc @@ -28,7 +28,6 @@ modules/home/images/pi_racer.png modules/home/images/battery_charging.png shared/images/line.png - shared/images/gears.png shared/images/frame.png shared/images/background.png modules/media/images/play2.png @@ -39,6 +38,5 @@ shared/images/R.png shared/images/N.png shared/images/D.png - shared/images/gearlines.png From 139454ba486395f1ff0f7d0388223e650485829d Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 24 Feb 2025 16:43:11 +0100 Subject: [PATCH 14/73] [FEAT] : path edit --- apps/HeadUnit/clients/HU_gear_client/gear_client.cpp | 2 +- apps/HeadUnit/clients/J_gear_client/gear_client.cpp | 2 +- apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp | 4 ++-- apps/HeadUnit/clients/ambient_sender/client.cpp | 4 ++-- apps/HeadUnit/clients/battery_client/battery_client.hpp | 4 ++-- .../clients/gear_data_receiving_client/gear_client.hpp | 4 ++-- apps/HeadUnit/clients/speed_client/speed_client.hpp | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp index cc65afb..e36084c 100644 --- a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp +++ b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp @@ -6,7 +6,7 @@ #include #include -#include "../../server.hpp" +#include "../server.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; diff --git a/apps/HeadUnit/clients/J_gear_client/gear_client.cpp b/apps/HeadUnit/clients/J_gear_client/gear_client.cpp index 8d1b531..0e8c8a0 100644 --- a/apps/HeadUnit/clients/J_gear_client/gear_client.cpp +++ b/apps/HeadUnit/clients/J_gear_client/gear_client.cpp @@ -6,7 +6,7 @@ #include #include -#include "../../server.hpp" +#include "../server.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; diff --git a/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp b/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp index 960e8d6..808efa1 100644 --- a/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp +++ b/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp @@ -1,5 +1,5 @@ -#include "../../headers.hpp" -#include "../../server.hpp" +#include "../headers.hpp" +#include "../server.hpp" #include "./al_receiver.hpp" //받은 int값을 처리. std::shared_ptr app; diff --git a/apps/HeadUnit/clients/ambient_sender/client.cpp b/apps/HeadUnit/clients/ambient_sender/client.cpp index 5d12b7a..09df744 100644 --- a/apps/HeadUnit/clients/ambient_sender/client.cpp +++ b/apps/HeadUnit/clients/ambient_sender/client.cpp @@ -1,5 +1,5 @@ -#include "../../headers.hpp" -#include "../../server.hpp" +#include "../headers.hpp" +#include "../server.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp index 755ad94..5c8a468 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.hpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -1,8 +1,8 @@ #ifndef BATTERY_CLIENTHPP #define BATTERY_CLIENTHPP -#include "../../headers.hpp" -#include "../../server.hpp" +#include "../headers.hpp" +#include "../server.hpp" class batteryClient { public: diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp index d9dd398..d548d1c 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -1,8 +1,8 @@ #ifndef GEAR_CLIENT_HPP #define GEAR_CLIENT_HPP -#include "../../headers.hpp" -#include "../../server.hpp" +#include "../headers.hpp" +#include "../server.hpp" class gearClient { public: diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp index f2dcc93..3355def 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.hpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -1,8 +1,8 @@ #ifndef SPEED_CLIENT_HPP #define SPEED_CLIENT_HPP -#include "../../headers.hpp" -#include "../../server.hpp" +#include "../headers.hpp" +#include "../server.hpp" class speedClient{ public: From c565eed47f4effc8aa9481c68ec66d651d58c2fa Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Mon, 24 Feb 2025 17:24:16 +0100 Subject: [PATCH 15/73] testing --- apps/HeadUnit/CMakeLists.txt | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index e770f98..ca2b09f 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -5,7 +5,7 @@ project(HeadUnit VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) - +set(EXAMPLE_CONFIG_FILES "./vsomeip.json") find_package(Qt6 6.5 REQUIRED COMPONENTS Quick Qml) qt_standard_project_setup(REQUIRES 6.5) @@ -21,8 +21,6 @@ qt_add_executable(head-unit shared/utils/envmanager.cpp shared/utils/utils.h shared/utils/utils.cpp - shared/utils/someip.h - shared/utils/someip.cpp modules/spotify/spotify.h modules/spotify/spotify.cpp @@ -59,7 +57,21 @@ qt_add_qml_module(head-unit find_package(Qt6 REQUIRED COMPONENTS Quick WebEngineWidgets Qml) -target_link_libraries(head-unit PRIVATE Qt6::Quick Qt6::WebEngineWidgets Qt6::Qml) +find_package(vsomeip3 3.4.10 REQUIRED) +find_package( Boost 1.55 COMPONENTS system thread log REQUIRED) +include_directories( + ${Boost_INCLUDE_DIR} + ${VSOMEIP_INCLUDE_DIR} + ) + +target_link_libraries(head-unit + PRIVATE + vsomeip3 + ${Boost_LIBRARIES} + Qt6::Quick + Qt6::WebEngineWidgets + Qt6::Qml +) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick Qml Bluetooth) target_link_libraries(head-unit PRIVATE Qt6::Bluetooth) From 8fe427970ade8a650156b33d843ddb33eb5a84bf Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Mon, 24 Feb 2025 17:35:40 +0100 Subject: [PATCH 16/73] qt compile setup --- apps/HeadUnit/HeadUnit.h | 4 ++ apps/HeadUnit/HeadUnit.hpp | 47 ------------------- apps/HeadUnit/SpeedClient.cpp | 85 ----------------------------------- apps/HeadUnit/SpeedClient.hpp | 24 ---------- apps/HeadUnit/main.cpp | 11 +++++ 5 files changed, 15 insertions(+), 156 deletions(-) delete mode 100644 apps/HeadUnit/HeadUnit.hpp delete mode 100644 apps/HeadUnit/SpeedClient.cpp delete mode 100644 apps/HeadUnit/SpeedClient.hpp diff --git a/apps/HeadUnit/HeadUnit.h b/apps/HeadUnit/HeadUnit.h index 6c5f804..7f906e1 100644 --- a/apps/HeadUnit/HeadUnit.h +++ b/apps/HeadUnit/HeadUnit.h @@ -1,6 +1,10 @@ #pragma once #include +#include "./clients/speed_client/speed_client.hpp" +#include "./clients/battery_client/battery_client.hpp" +#include "./clients/gear_data_receiving_client/gear_client.hpp" +#include "./clients/ambient_receiver/al_receiver.hpp" class HeadUnit : public QObject { diff --git a/apps/HeadUnit/HeadUnit.hpp b/apps/HeadUnit/HeadUnit.hpp deleted file mode 100644 index 6ca7131..0000000 --- a/apps/HeadUnit/HeadUnit.hpp +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include -#include "./clients/speed_client/speed_client.hpp" -#include "./clients/battery_client/battery_client.hpp" -#include "./clients/gear_data_receiving_client/gear_client.hpp" -#include "./clients/ambient_receiver/al_receiver.hpp" - - -class HeadUnit : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString currentGear READ currentGear WRITE setCurrentGear NOTIFY currentGearChanged) - Q_PROPERTY(QString ambientLighting READ ambientLighting WRITE setAmbientLighting NOTIFY ambientLightingChanged) - Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged) - Q_PROPERTY(int batteryPercentage READ batteryPercentage WRITE setBatteryPercentage NOTIFY batteryPercentageChanged) - Q_PROPERTY(int chargingState READ chargingState WRITE setChargingState NOTIFY chargingStateChanged) - -public: - explicit HeadUnit(QObject *parent = nullptr); - - const QString& currentGear() const; - const QString& ambientLighting() const; - int speed() const; - int batteryPercentage() const; - bool chargingState() const; - - void setAmbientLighting(const QString& color); - void setCurrentGear(const QString& gear); - void setSpeed(int speed); - void setBatteryPercentage(int batteryPercentage); - void setChargingState(bool state); - -signals: - void currentGearChanged(const QString& gear); - void ambientLightingChanged(const QString& color); - void speedChanged(int speed); - void batteryPercentageChanged(int batteryPercentage); - void chargingStateChanged(bool state); - -private: - QString _currentGear = "P"; - int _ambientLighting = 180; - int _speed = 0; - int _batteryPercentage = 100; - bool _chargingState = false; -}; \ No newline at end of file diff --git a/apps/HeadUnit/SpeedClient.cpp b/apps/HeadUnit/SpeedClient.cpp deleted file mode 100644 index 26bea5b..0000000 --- a/apps/HeadUnit/SpeedClient.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include "./SpeedClient.hpp" - -client_sample::client_sample(bool _use_tcp) : - app_(vsomeip::runtime::get()->create_application("speed")), use_tcp_(_use_tcp) { -} - -bool client_sample::init() { - if (!app_->init()) { - std::cerr << "Couldn't initialize application" << std::endl; - return false; - } - - std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; - - // 상태 핸들러 등록 - app_->register_state_handler( - std::bind(&client_sample::on_state, this, std::placeholders::_1)); - - // 메시지 핸들러 등록 - app_->register_message_handler( - vsomeip::ANY_SERVICE, SPEED_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&client_sample::on_message, this, std::placeholders::_1)); - - // 가용성 핸들러 등록 - app_->register_availability_handler(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, - std::bind(&client_sample::on_availability, this, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); - - // 이벤트 구독 - std::set its_groups; - its_groups.insert(VEHICLE_EVENTGROUP_ID); - app_->request_event( - VEHICLE_SERVICE_ID, - SPEED_INSTANCE_ID, - SPEED_EVENT_ID, - its_groups, - vsomeip::event_type_e::ET_FIELD); - app_->subscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); - - return true; -} - -void client_sample::start() { - app_->start(); -} - -void client_sample::stop() { - app_->clear_all_handler(); - app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); - app_->release_event(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, SPEED_EVENT_ID); - app_->release_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); - app_->stop(); -} - -void client_sample::on_state(vsomeip::state_type_e _state) { - if (_state == vsomeip::state_type_e::ST_REGISTERED) { - app_->request_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); - } -} - -void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { - std::cout << "Service [" - << std::hex << std::setfill('0') << std::setw(4) << _service << "." - << std::setw(4) << _instance << "] is " - << (_is_available ? "available." : "NOT available.") << std::endl; -} - -void client_sample::on_message(const std::shared_ptr& _response) { - - std::shared_ptr its_payload = _response->get_payload(); - if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 - float received_speed = 0.0f; - - // 이터레이터로 시작 위치 가져오기 - auto it = its_payload->get_data(); // 시작 위치의 이터레이터 - std::copy(it, it + sizeof(float), reinterpret_cast(&received_speed)); - - // 변환된 값 출력 - std::cout << "Received data: " << received_speed << " m/s" << std::endl; - } else { - std::cerr << "Invalid data size received!" << std::endl; - } - - -} diff --git a/apps/HeadUnit/SpeedClient.hpp b/apps/HeadUnit/SpeedClient.hpp deleted file mode 100644 index 8078ead..0000000 --- a/apps/HeadUnit/SpeedClient.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef CLIENT_SAMPLE_HPP -#define CLIENT_SAMPLE_HPP - -#include "../../headers.hpp" -#include "../../server.hpp" - -class client_sample { -public: - client_sample(bool _use_tcp); - - bool init(); - void start(); - void stop(); - -private: - void on_state(vsomeip::state_type_e _state); - void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); - void on_message(const std::shared_ptr& _response); - - std::shared_ptr app_; - bool use_tcp_; -}; - -#endif // CLIENT_SAMPLE_HPP diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 098d8a4..8630b1c 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -27,6 +27,17 @@ int main(int argc, char *argv[]) Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); + speedClient speedClient(); + batteryClient batteryClient(); + gearClient gearClient(); + alClient alClient(); + + if (speedClient.init()) + speedClient.start(); + if (batteryClient.init()) + batteryClient.start(); + if (gearClient.init()) + gearClient.start(); // QObject::connect( // &engine, // &QQmlApplicationEngine::objectCreationFailed, From c72ebe1eb910561a32de6e371be6727f25c9d0c9 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 24 Feb 2025 17:52:19 +0100 Subject: [PATCH 17/73] qt comiple setup --- .../clients/battery_client/battery_client.hpp | 3 +- .../clients/speed_client/speed_client.hpp | 3 +- apps/InstrumentCluster/clients/vsomeip.json | 112 ++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 apps/InstrumentCluster/clients/vsomeip.json diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp index 5c8a468..46c51c9 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.hpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -11,9 +11,8 @@ class batteryClient { bool init(); void start(); void stop(); - -private: int batteryValue; +private: void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp index 3355def..bbba6bf 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.hpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -11,12 +11,11 @@ class speedClient{ bool init(); void start(); void stop(); - + float speedValue; signals: void speedReceived(int speed); private: - float speedValue; void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/InstrumentCluster/clients/vsomeip.json b/apps/InstrumentCluster/clients/vsomeip.json new file mode 100644 index 0000000..0de67a2 --- /dev/null +++ b/apps/InstrumentCluster/clients/vsomeip.json @@ -0,0 +1,112 @@ +{ + "unicast": "127.0.0.1", + "logging": { + "level": "debug", + "console": "true", + "file": { "enable": "false", "path": "/var/log/vsomeip.log" }, + "dlt": "false" + }, + "applications": [ + { + "name": "speed", + "id": "0x4322", + "services": [ + { + "service": "0x1234", + "instance": "0x2001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name":"battery", + "id": "0x4323", + "services": [ + { + "service": "0x1234", + "instance": "0x3001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "gear", + "id": "0x4324", + "services": [ + { + "service": "0x1234", + "instance": "0x4001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "ambient", + "id": "0x4325", + "services": [ + { + "service": "0x1234", + "instance": "0x5001" + } + ] + } + ], + "services": [ + { + "service": "0x1234", + "instance": "0x2001", + "reliable": { "port": "30509", "enable-magic-cookies": "false" }, + "unreliable": "31000", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8779"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x3001", + "reliable": { "port": "30510", "enable-magic-cookies": "false" }, + "unreliable": "31001", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8780"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x4001", + "reliable": { "port": "30511", "enable-magic-cookies": "false" }, + "unreliable": "31002", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8781"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x5001", + "reliable": { "port": "30512", "enable-magic-cookies": "false" }, + "unreliable": "31003" + } + ], + "routing": "service-example", + "service-discovery": { + "enable": "true", + "multicast": "224.244.224.245", + "port": "30490", + "protocol": "udp", + "initial_delay_min": "10", + "initial_delay_max": "100", + "repetitions_base_delay": "100", + "repetitions_max": "3", + "ttl": "3", + "cyclic_offer_delay": "2000", + "request_response_delay": "1500" + } +} From 95d003a0a14ed1311b8ffdbbc2f76ebf488640c2 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 24 Feb 2025 17:57:43 +0100 Subject: [PATCH 18/73] [FEAT] : gear client class name changed --- .../gear_client.cpp | 20 +++++++++---------- .../gear_client.hpp | 1 + 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp index 19a8075..6eb51d9 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -1,10 +1,10 @@ #include "./gear_client.hpp" -client_sample::client_sample() : +gearClient::gearClient() : app_(vsomeip::runtime::get()->create_application("gear")) { } -bool client_sample::init() { +bool gearClient::init() { if (!app_->init()) { std::cerr << "Couldn't initialize application" << std::endl; return false; @@ -14,16 +14,16 @@ bool client_sample::init() { // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&client_sample::on_state, this, std::placeholders::_1)); + std::bind(&gearClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&client_sample::on_message, this, std::placeholders::_1)); + std::bind(&gearClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, - std::bind(&client_sample::on_availability, this, + std::bind(&gearClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 @@ -42,11 +42,11 @@ bool client_sample::init() { return true; } -void client_sample::start() { +void gearClient::start() { app_->start(); } -void client_sample::stop() { +void gearClient::stop() { app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, GEAR_EVENT_ID); @@ -54,20 +54,20 @@ void client_sample::stop() { app_->stop(); } -void client_sample::on_state(vsomeip::state_type_e _state) { +void gearClient::on_state(vsomeip::state_type_e _state) { if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); } } -void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void gearClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void client_sample::on_message(const std::shared_ptr &_request) { +void gearClient::on_message(const std::shared_ptr &_request) { std::shared_ptr payload = _request->get_payload(); int received_value = 0; diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp index d548d1c..40e71fa 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -11,6 +11,7 @@ class gearClient { bool init(); void start(); void stop(); + int gearValue; private: void on_state(vsomeip::state_type_e _state); From 28c3b1e8bdb1e0050b11445593a2a82af6444d30 Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Mon, 24 Feb 2025 18:09:11 +0100 Subject: [PATCH 19/73] fixing clients --- apps/HeadUnit/HeadUnit.h | 5 +---- .../clients/battery_client/battery_client.cpp | 22 +++++++++---------- .../gear_client.hpp | 1 - apps/HeadUnit/main.cpp | 22 +++++++++++-------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/apps/HeadUnit/HeadUnit.h b/apps/HeadUnit/HeadUnit.h index 7f906e1..a2fe7b2 100644 --- a/apps/HeadUnit/HeadUnit.h +++ b/apps/HeadUnit/HeadUnit.h @@ -1,10 +1,7 @@ #pragma once #include -#include "./clients/speed_client/speed_client.hpp" -#include "./clients/battery_client/battery_client.hpp" -#include "./clients/gear_data_receiving_client/gear_client.hpp" -#include "./clients/ambient_receiver/al_receiver.hpp" + class HeadUnit : public QObject { diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index e32237b..52c723f 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -1,10 +1,10 @@ -#include "./batteryClient.hpp" +#include "./battery_client.hpp" -client_sample::client_sample() : +batteryClient::batteryClient() : app_(vsomeip::runtime::get()->create_application("battery")){ } -bool client_sample::init() { +bool batteryClient::init() { if (!app_->init()) { std::cerr << "Couldn't initialize application" << std::endl; return false; @@ -14,16 +14,16 @@ bool client_sample::init() { // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&client_sample::on_state, this, std::placeholders::_1)); + std::bind(&batteryClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, BATTERY_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&client_sample::on_message, this, std::placeholders::_1)); + std::bind(&batteryClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, - std::bind(&client_sample::on_availability, this, + std::bind(&batteryClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 @@ -42,11 +42,11 @@ bool client_sample::init() { return true; } -void client_sample::start() { +void batteryClient::start() { app_->start(); } -void client_sample::stop() { +void batteryClient::stop() { app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, BATTERY_EVENT_ID); @@ -54,20 +54,20 @@ void client_sample::stop() { app_->stop(); } -void client_sample::on_state(vsomeip::state_type_e _state) { +void batteryClient::on_state(vsomeip::state_type_e _state) { if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); } } -void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void batteryClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void client_sample::on_message(const std::shared_ptr& _response) { +void batteryClient::on_message(const std::shared_ptr& _response) { std::shared_ptr payload = _response->get_payload(); int received_value = 0; diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp index 40e71fa..1e5a078 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -12,7 +12,6 @@ class gearClient { void start(); void stop(); int gearValue; - private: void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 8630b1c..414f066 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -8,6 +8,10 @@ #include "HeadUnit.h" #include +#include "./clients/speed_client/speed_client.hpp" +#include "./clients/battery_client/battery_client.hpp" +#include "./clients/gear_data_receiving_client/gear_client.hpp" + int main(int argc, char *argv[]) { @@ -27,10 +31,10 @@ int main(int argc, char *argv[]) Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); - speedClient speedClient(); - batteryClient batteryClient(); - gearClient gearClient(); - alClient alClient(); + speedClient speedClient; + batteryClient batteryClient; + gearClient gearClient; + // alClient alClient; if (speedClient.init()) speedClient.start(); @@ -49,7 +53,7 @@ int main(int argc, char *argv[]) &engine, &QQmlApplicationEngine::objectCreationFailed, &app, - []() { QCoreApplication::exit(-1); + [&controller, &speedClient,&batteryClient,&gearClient]() { QCoreApplication::exit(-1); static int speed = 0; speed = speedClient.speedValue; controller.setSpeed(speed); @@ -73,11 +77,11 @@ int main(int argc, char *argv[]) //Ambient Light // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; - static int colorIndex = 0; - colorIndex = alClient.alValue; // <- set colorIndex to received value + // static int colorIndex = 0; + // colorIndex = alClient.alValue; // <- set colorIndex to received value - controller.setAmbientLighting(colors[colorIndex]); - colorIndex = (colorIndex + 1) % colors.size(); + // controller.setAmbientLighting(colors[colorIndex]); + // colorIndex = (colorIndex + 1) % colors.size(); }, Qt::QueuedConnection); From bfc2cc2f2a6c7abea9e7e55c2346903c6ec87708 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 24 Feb 2025 18:10:48 +0100 Subject: [PATCH 20/73] fixing clients --- apps/HeadUnit/clients/HU_gear_client/gear_client.cpp | 1 + apps/HeadUnit/clients/J_gear_client/gear_client.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp index e36084c..ec9e9d8 100644 --- a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp +++ b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp @@ -7,6 +7,7 @@ #include #include "../server.hpp" +#include "./gear_client.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; diff --git a/apps/HeadUnit/clients/J_gear_client/gear_client.cpp b/apps/HeadUnit/clients/J_gear_client/gear_client.cpp index 0e8c8a0..aaa94a1 100644 --- a/apps/HeadUnit/clients/J_gear_client/gear_client.cpp +++ b/apps/HeadUnit/clients/J_gear_client/gear_client.cpp @@ -7,7 +7,7 @@ #include #include "../server.hpp" - +#include "./gear_client.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; std::condition_variable condition; From 8cc5d2337003738ba45c103370405322b64778fa Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Mon, 24 Feb 2025 19:44:51 +0100 Subject: [PATCH 21/73] testing --- apps/HeadUnit/CMakeLists.txt | 7 +++ apps/HeadUnit/HeadUnit.cpp | 13 +++++ apps/HeadUnit/HeadUnit.h | 6 ++- apps/HeadUnit/Main.qml | 21 +++++--- .../clients/battery_client/battery_client.cpp | 1 - .../gear_client.cpp | 2 - .../clients/speed_client/speed_client.hpp | 5 +- apps/HeadUnit/main.cpp | 12 ++++- apps/HeadUnit/modules/home/Home.qml | 4 +- apps/ServiceManager/CMakeLists.txt | 52 +++++++++---------- 10 files changed, 82 insertions(+), 41 deletions(-) diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index ca2b09f..be70232 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -24,6 +24,10 @@ qt_add_executable(head-unit modules/spotify/spotify.h modules/spotify/spotify.cpp + HeadUnit.h + clients/speed_client/speed_client.cpp + clients/battery_client/battery_client.cpp + clients/gear_data_receiving_client/gear_client.cpp ) qt_add_qml_module(head-unit @@ -43,6 +47,9 @@ qt_add_qml_module(head-unit SOURCES HeadUnit.h + clients/speed_client/speed_client.hpp + clients/battery_client/battery_client.hpp + clients/gear_data_receiving_client/gear_client.hpp modules/spotify/spotify.h modules/spotify/spotify.cpp shared/utils/envmanager.h diff --git a/apps/HeadUnit/HeadUnit.cpp b/apps/HeadUnit/HeadUnit.cpp index 1bf671f..8f5c357 100644 --- a/apps/HeadUnit/HeadUnit.cpp +++ b/apps/HeadUnit/HeadUnit.cpp @@ -9,6 +9,11 @@ const QString& HeadUnit::currentGear() const return _currentGear; } +int HeadUnit::ambientLighting() +{ + return _ambientLighting; +} + int HeadUnit::speed() const { return _speed; @@ -32,6 +37,14 @@ void HeadUnit::setCurrentGear(const QString& gear) } } +void HeadUnit::setAmbientLighting(int colorValue) +{ + if (_ambientLighting != colorValue) { + _ambientLighting = colorValue; + emit ambientLightingChanged(colorValue); + } +} + void HeadUnit::setSpeed(int speed) { if (_speed != speed) { diff --git a/apps/HeadUnit/HeadUnit.h b/apps/HeadUnit/HeadUnit.h index a2fe7b2..befae76 100644 --- a/apps/HeadUnit/HeadUnit.h +++ b/apps/HeadUnit/HeadUnit.h @@ -2,11 +2,11 @@ #include - class HeadUnit : public QObject { Q_OBJECT Q_PROPERTY(QString currentGear READ currentGear WRITE setCurrentGear NOTIFY currentGearChanged) + Q_PROPERTY(int ambientLighting READ ambientLighting WRITE setAmbientLighting NOTIFY ambientLightingChanged) Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged) Q_PROPERTY(int batteryPercentage READ batteryPercentage WRITE setBatteryPercentage NOTIFY batteryPercentageChanged) Q_PROPERTY(int chargingState READ chargingState WRITE setChargingState NOTIFY chargingStateChanged) @@ -15,10 +15,12 @@ class HeadUnit : public QObject explicit HeadUnit(QObject *parent = nullptr); const QString& currentGear() const; + int ambientLighting(); int speed() const; int batteryPercentage() const; bool chargingState() const; + void setAmbientLighting(int colorValue); void setCurrentGear(const QString& gear); void setSpeed(int speed); void setBatteryPercentage(int batteryPercentage); @@ -26,6 +28,7 @@ class HeadUnit : public QObject signals: void currentGearChanged(const QString& gear); + void ambientLightingChanged(int colorValue); void speedChanged(int speed); void batteryPercentageChanged(int batteryPercentage); void chargingStateChanged(bool state); @@ -33,6 +36,7 @@ class HeadUnit : public QObject private: QString _currentGear = "P"; int _speed = 0; + int _ambientLighting = 0; int _batteryPercentage = 100; bool _chargingState = false; }; diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index 10eab97..166cf47 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -13,9 +13,16 @@ Window { title: qsTr("HeadUnit") color: "black" - property int colorValue: 180 - property color myColor: Qt.hsva(colorValue / 360, 1, 1, 1) + property int ambientLighting: HeadUnit.ambientLighting + property color myColor: Qt.hsva(ambientLighting / 360, 1, 1, 1) + Component.onCompleted: { + console.log("ambientLighting value:", ambientLighting) + } + + onAmbientLightingChanged: { + console.log("ambientLighting changed to:", ambientLighting) + } MediaPlayer { id: mediaPlayer audioOutput: AudioOutput {id: audioOutput} @@ -189,8 +196,8 @@ Window { anchors.fill: parent onClicked: { console.log("D clicked") - someIP.send_gear_data(3); gearColumn.activeGear = "drive" + someIP.send_gear_data(3); } } } @@ -255,8 +262,8 @@ Window { anchors.fill: parent onClicked: { console.log("N clicked") - someIP.send_gear_data(2); gearColumn.activeGear = "neutral" + someIP.send_gear_data(2); } } } @@ -321,8 +328,8 @@ Window { anchors.fill: parent onClicked: { console.log("R clicked") - someIP.send_gear_data(1); gearColumn.activeGear = "reverse" + someIP.send_gear_data(1); } } } @@ -386,8 +393,8 @@ Window { anchors.fill: parent onClicked: { console.log("P clicked") - someIP.send_gear_data(0); gearColumn.activeGear = "park" + someIP.send_gear_data(0); } } } @@ -581,7 +588,7 @@ Window { id: settings Settings { onColorValueChanged: (value) => { - colorValue = value + ambientLighting = value // Use the value here } } diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index 52c723f..d724bd7 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -10,7 +10,6 @@ bool batteryClient::init() { return false; } - std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; // 상태 핸들러 등록 app_->register_state_handler( diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp index 6eb51d9..f6f8188 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -10,8 +10,6 @@ bool gearClient::init() { return false; } - std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; - // 상태 핸들러 등록 app_->register_state_handler( std::bind(&gearClient::on_state, this, std::placeholders::_1)); diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp index bbba6bf..92c5ec2 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.hpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -1,10 +1,12 @@ #ifndef SPEED_CLIENT_HPP #define SPEED_CLIENT_HPP +#include + #include "../headers.hpp" #include "../server.hpp" -class speedClient{ +class speedClient : public QObject { public: speedClient(); @@ -12,6 +14,7 @@ class speedClient{ void start(); void stop(); float speedValue; + signals: void speedReceived(int speed); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 414f066..0d35260 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -8,6 +8,7 @@ #include "HeadUnit.h" #include +#include #include "./clients/speed_client/speed_client.hpp" #include "./clients/battery_client/battery_client.hpp" #include "./clients/gear_data_receiving_client/gear_client.hpp" @@ -88,6 +89,8 @@ int main(int argc, char *argv[]) // engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml"))); engine.loadFromModule("HeadUnit", "Main"); + engine.rootContext()->setContextProperty("HeadUnit", &controller); + // test code QTimer *timer = new QTimer(&controller); QObject::connect(timer, &QTimer::timeout, [&controller]() { @@ -107,7 +110,14 @@ int main(int argc, char *argv[]) static int chargingState = false; chargingState = !chargingState; controller.setChargingState(chargingState); - + + QVector colors = {20, 157, 100, 300, 200}; + static int colorIndex = 0; + + // colorIndex = alClient.alValue; + controller.setAmbientLighting(colors[colorIndex]); + colorIndex = (colorIndex + 1) % colors.size(); + std::cout << controller.ambientLighting() << std::endl; }); timer->start(1000); diff --git a/apps/HeadUnit/modules/home/Home.qml b/apps/HeadUnit/modules/home/Home.qml index b0890a1..59a10ed 100644 --- a/apps/HeadUnit/modules/home/Home.qml +++ b/apps/HeadUnit/modules/home/Home.qml @@ -46,7 +46,7 @@ Item{ Text { id: speed - text: "100" + text: HeadUnit.speed color: myColor font { family: "Inter" @@ -95,7 +95,7 @@ Item{ Text { id: battery - text: "100" + text: HeadUnit.batteryPercentage color: myColor Layout.rightMargin: -5 font { diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index 2913a4c..60b6d64 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -27,32 +27,32 @@ add_executable(server-draft ) target_link_libraries(server-draft vsomeip3 ${Boost_LIBRARIES}) -# add_executable(service-example ../src/main.cpp ../src/service-main.cpp ../src/service-example.cpp ../src/canreceiver.cpp ${EXAMPLE_CONFIG_FILES}) -# target_link_libraries(service-example vsomeip3 ${Boost_LIBRARIES}) - -# speed receiving client -add_executable(speed_client ../src/speed-src/speed_client/client-main.cpp ../src/speed-src/speed_client/client-example.cpp ${EXAMPLE_CONFIG_FILES}) -target_link_libraries(speed_client vsomeip3 ${Boost_LIBRARIES}) - -# battery receiving client -add_executable(battery_client ../src/battery-src/battery_client/client-main.cpp ../src/battery-src/battery_client/client-example.cpp ${EXAMPLE_CONFIG_FILES}) -target_link_libraries(battery_client vsomeip3 ${Boost_LIBRARIES}) - -# joystick input -add_executable(joystick-gear-client ../src/gear-src/J_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) -target_link_libraries(joystick-gear-client vsomeip3 ${Boost_LIBRARIES}) - -# HU input -add_executable(HU-gear-client ../src/gear-src/HU_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) -target_link_libraries(HU-gear-client vsomeip3 ${Boost_LIBRARIES}) - -# gear client -add_executable(gear_client - ../src/gear-src/gear_data_receiving_client/client-main.cpp - ../src/gear-src/gear_data_receiving_client/client-example.cpp - ${EXAMPLE_CONFIG_FILES} - ) -target_link_libraries(gear_client vsomeip3 ${Boost_LIBRARIES}) +# # add_executable(service-example ../src/main.cpp ../src/service-main.cpp ../src/service-example.cpp ../src/canreceiver.cpp ${EXAMPLE_CONFIG_FILES}) +# # target_link_libraries(service-example vsomeip3 ${Boost_LIBRARIES}) + +# # speed receiving client +# add_executable(speed_client ../src/speed-src/speed_client/client-main.cpp ../src/speed-src/speed_client/client-example.cpp ${EXAMPLE_CONFIG_FILES}) +# target_link_libraries(speed_client vsomeip3 ${Boost_LIBRARIES}) + +# # battery receiving client +# add_executable(battery_client ../src/battery-src/battery_client/client-main.cpp ../src/battery-src/battery_client/client-example.cpp ${EXAMPLE_CONFIG_FILES}) +# target_link_libraries(battery_client vsomeip3 ${Boost_LIBRARIES}) + +# # joystick input +# add_executable(joystick-gear-client ../src/gear-src/J_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) +# target_link_libraries(joystick-gear-client vsomeip3 ${Boost_LIBRARIES}) + +# # HU input +# add_executable(HU-gear-client ../src/gear-src/HU_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) +# target_link_libraries(HU-gear-client vsomeip3 ${Boost_LIBRARIES}) + +# # gear client +# add_executable(gear_client +# ../src/gear-src/gear_data_receiving_client/client-main.cpp +# ../src/gear-src/gear_data_receiving_client/client-example.cpp +# ${EXAMPLE_CONFIG_FILES} +# ) +# target_link_libraries(gear_client vsomeip3 ${Boost_LIBRARIES}) # Req/Res testing From b9aed464cd926ef2543989f82b36951107117fdb Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 24 Feb 2025 19:45:49 +0100 Subject: [PATCH 22/73] config --- apps/ServiceManager/config/vsomeip.json | 43 ++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/ServiceManager/config/vsomeip.json b/apps/ServiceManager/config/vsomeip.json index ef958ec..0de67a2 100644 --- a/apps/ServiceManager/config/vsomeip.json +++ b/apps/ServiceManager/config/vsomeip.json @@ -7,10 +7,6 @@ "dlt": "false" }, "applications": [ - { - "name": "server", - "id": "0x1235" - }, { "name": "speed", "id": "0x4322", @@ -32,6 +28,27 @@ "eventgroups": ["0x4465"] } ] + }, + { + "name": "gear", + "id": "0x4324", + "services": [ + { + "service": "0x1234", + "instance": "0x4001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "ambient", + "id": "0x4325", + "services": [ + { + "service": "0x1234", + "instance": "0x5001" + } + ] } ], "services": [ @@ -58,6 +75,24 @@ "events": ["0x8780"] } ] + }, + { + "service": "0x1234", + "instance": "0x4001", + "reliable": { "port": "30511", "enable-magic-cookies": "false" }, + "unreliable": "31002", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8781"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x5001", + "reliable": { "port": "30512", "enable-magic-cookies": "false" }, + "unreliable": "31003" } ], "routing": "service-example", From 7c411c06351c232c373a9066cf3853654fa9638b Mon Sep 17 00:00:00 2001 From: frbeyer1 Date: Mon, 24 Feb 2025 20:16:42 +0100 Subject: [PATCH 23/73] Can not load configuration module (2025_02_24) --- apps/ServiceManager/CMakeLists.txt | 10 +- apps/ServiceManager/build.sh | 2 +- .../src/battery-src/battery/battery.cpp | 122 +++++++++--------- apps/ServiceManager/src/main.cpp | 4 +- apps/ServiceManager/src/service-main.cpp | 1 + 5 files changed, 73 insertions(+), 66 deletions(-) diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index 60b6d64..dec39fb 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -3,17 +3,22 @@ cmake_minimum_required (VERSION 3.13) set (CMAKE_CXX_FLAGS "-g -std=c++0x") set(EXAMPLE_CONFIG_FILES - "../config/vsomeip.json" + "/home/frederik/Qt/Projects/DES_Head_Unit/apps/ServiceManager/config/vsomeip.json" ) find_package (vsomeip3 3.4.10 REQUIRED) find_package( Boost 1.55 COMPONENTS system thread log REQUIRED ) + include_directories ( ${Boost_INCLUDE_DIR} ${VSOMEIP_INCLUDE_DIRS} ) +message(STATUS "VSOMEIP_INCLUDE_DIRS: ${VSOMEIP_INCLUDE_DIRS}") +message(STATUS "VSOMEIP_LIBRARIES: ${VSOMEIP_LIBRARIES}") + + # Server binary file add_executable(server-draft ../src/main.cpp @@ -22,7 +27,8 @@ add_executable(server-draft ../src/speed-src/speed/speed.cpp ../src/speed-src/speed/canreceiver.cpp ../src/battery-src/battery/battery.cpp - ../src/gear-src/gear/gear.cpp + ../src/gear-src/gear/gear.cpp + ./config/vsomeip.json ${EXAMPLE_CONFIG_FILES} ) target_link_libraries(server-draft vsomeip3 ${Boost_LIBRARIES}) diff --git a/apps/ServiceManager/build.sh b/apps/ServiceManager/build.sh index b9360b8..5c7eabb 100755 --- a/apps/ServiceManager/build.sh +++ b/apps/ServiceManager/build.sh @@ -26,4 +26,4 @@ echo "CMake 빌드 완료!" # cmake -Bbuild -DCMAKE_INSTALL_PREFIX=../../install_folder -DCMAKE_PREFIX_PATH=../../install_folder . # cmake --build=build # LD_LIBRARY_PATH=../../install_folder/lib/:$PWD/build/ ./build/service-example -# LD_LIBRARY_PATH=../../install_folder/lib/:$PWD/build/ ./build/client-example \ No newline at end of file +# LD_LIBRARY_PATH=../../install_folder/lib/:$PWD/build/ ./build/client-example__ \ No newline at end of file diff --git a/apps/ServiceManager/src/battery-src/battery/battery.cpp b/apps/ServiceManager/src/battery-src/battery/battery.cpp index b32f589..ac80c09 100644 --- a/apps/ServiceManager/src/battery-src/battery/battery.cpp +++ b/apps/ServiceManager/src/battery-src/battery/battery.cpp @@ -13,9 +13,9 @@ batteryObject::batteryObject(uint32_t _cycle) : voltage(0), file(-1){ battery_thread_ = std::thread(&batteryObject::getBatteryVoltage, this); - if (!initI2C()) { - std::cout << "Failed to initialize I2C interface."; - } + // if (!initI2C()) { + // std::cout << "Failed to initialize I2C interface."; + // } } @@ -27,7 +27,7 @@ bool batteryObject::init() { return false; } //서비스에 따라서 service id 변경. - + std::cout << "SERVER : init" << std::endl; app_->register_state_handler( std::bind(&batteryObject::on_state, this, std::placeholders::_1)); @@ -193,7 +193,7 @@ void batteryObject::on_set(const std::shared_ptr &_message) { } //TODO: change it into battery service code. -/* + void batteryObject::getBatteryVoltage() { float filtered_speed = 0.0f; float weight = 0.6; @@ -209,69 +209,69 @@ void batteryObject::getBatteryVoltage() { filtered_speed += 0.2; this->speedData = filtered_speed; } - std::this_thread::sleep_for(std::chrono::milliseconds(7)); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } } -*/ - -// Destructor: Cleans up the resources (closes the I2C file descriptor) - -bool batteryObject::initI2C() { - // Open the I2C bus - if ((file = open(device, O_RDWR)) < 0) { - std::cout << "Failed to open the I2C bus"; - return false; - } - // Set the I2C address for the slave device - if (ioctl(file, I2C_SLAVE, addr) < 0) { - std::cout << "Failed to acquire bus access and/or talk to slave."; - close(file); - return false; - } - return true; -} - -uint16_t batteryObject::readRegister() { - char buf[2]; - buf[0] = reg; - - if (file < 0) { - std::cout << "I2C file is not initialized."; - return -1; - } - - // Write the register address to the I2C bus - if (write(file, buf, 1) != 1) { - std::cout << "Failed to write to the I2C bus."; - std::cout << "Error still exists"; - return -1; - } - usleep(1000); // Time delay to read back from I2C - - // Read the data from the I2C bus - if (read(file, buf, 2) != 2) { - std::cout << "Failed to read from the I2C bus."; - return -1; - } - - uint16_t readValue = (buf[0] << 8) + buf[1]; - return readValue; -} +// Destructor: Cleans up the resources (closes the I2C file descriptor) -void batteryObject::getBatteryVoltage() { - // The battery voltage is stored in register 0x02 - // uint16_t voltageRaw = readRegister(); - // int voltage = 11; - uint8_t u_voltage = 11; +// bool batteryObject::initI2C() { +// // Open the I2C bus +// if ((file = open(device, O_RDWR)) < 0) { +// std::cout << "Failed to open the I2C bus"; +// return false; +// } + +// // Set the I2C address for the slave device +// if (ioctl(file, I2C_SLAVE, addr) < 0) { +// std::cout << "Failed to acquire bus access and/or talk to slave."; +// close(file); +// return false; +// } + +// return true; +// } + +// uint16_t batteryObject::readRegister() { +// char buf[2]; +// buf[0] = reg; + +// if (file < 0) { +// std::cout << "I2C file is not initialized."; +// return -1; +// } + +// // Write the register address to the I2C bus +// if (write(file, buf, 1) != 1) { +// std::cout << "Failed to write to the I2C bus."; +// std::cout << "Error still exists"; +// return -1; +// } +// usleep(1000); // Time delay to read back from I2C + +// // Read the data from the I2C bus +// if (read(file, buf, 2) != 2) { +// std::cout << "Failed to read from the I2C bus."; +// return -1; +// } + +// uint16_t readValue = (buf[0] << 8) + buf[1]; +// return readValue; +// } + +// void batteryObject::getBatteryVoltage() { +// // The battery voltage is stored in register 0x02 +// // uint16_t voltageRaw = readRegister(); +// // int voltage = 11; +// uint8_t u_voltage = 11; - this->voltage = u_voltage; +// this->voltage = u_voltage; - // uint8_t voltage = ((voltageRaw>>3)*4.0)/1000; - std::cout << "Battery Voltage: " << this->voltage << std::endl; - // return voltage; -} \ No newline at end of file +// // uint8_t voltage = ((voltageRaw>>3)*4.0)/1000; +// std::cout << "Battery Voltage: " << this->voltage << std::endl; +// // return voltage; +// } \ No newline at end of file diff --git a/apps/ServiceManager/src/main.cpp b/apps/ServiceManager/src/main.cpp index 2ef2e78..d9afbea 100644 --- a/apps/ServiceManager/src/main.cpp +++ b/apps/ServiceManager/src/main.cpp @@ -37,7 +37,7 @@ int main(int argc, char **argv) { speedProcessInit(); exit(0); // speedProcessInit()이 종료되면, 자식 프로세스 종료 } - + sleep(2); if (speed_pid != 0) battery_pid = fork(); @@ -46,7 +46,7 @@ int main(int argc, char **argv) { batteryProcessInit(); exit(0); // batteryProcessInit()이 종료되면, 자식 프로세스 종료 } - + sleep(2); if (speed_pid != 0 && battery_pid != 0) { gear_pid = fork(); //gear 처리 프로세스 실행. diff --git a/apps/ServiceManager/src/service-main.cpp b/apps/ServiceManager/src/service-main.cpp index dd0cadd..57ae727 100644 --- a/apps/ServiceManager/src/service-main.cpp +++ b/apps/ServiceManager/src/service-main.cpp @@ -56,6 +56,7 @@ int batteryProcessInit() { #endif if (batteryInit.init()) { + std::cout << "Battery Process Started" << std::endl; batteryInit.start(); #ifdef VSOMEIP_ENABLE_SIGNAL_HANDLING batteryInit.stop(); From 0fe5751ad805f2bb501f4388072040be55a04871 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Wed, 5 Mar 2025 15:05:47 +0100 Subject: [PATCH 24/73] feat: integration --- apps/ServiceManager/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index dec39fb..6a7b4d7 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -3,7 +3,8 @@ cmake_minimum_required (VERSION 3.13) set (CMAKE_CXX_FLAGS "-g -std=c++0x") set(EXAMPLE_CONFIG_FILES - "/home/frederik/Qt/Projects/DES_Head_Unit/apps/ServiceManager/config/vsomeip.json" + # "/home/frederik/Qt/Projects/DES_Head_Unit/apps/ServiceManager/config/vsomeip.json" + ./config/vsomeip.json ) find_package (vsomeip3 3.4.10 REQUIRED) From 38de95ea28d1eb62c11a2edc6409dc56b076cb98 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Wed, 5 Mar 2025 15:36:24 +0100 Subject: [PATCH 25/73] [FEAT]: Use multithread method to receive data in headunit/main.cpp --- .../clients/battery_client/battery_client.cpp | 11 +++- .../gear_client.cpp | 12 +++- .../clients/speed_client/speed_client.cpp | 13 +++- apps/HeadUnit/main.cpp | 4 +- .../src/battery-src/battery/battery.cpp | 60 +++++++++---------- 5 files changed, 64 insertions(+), 36 deletions(-) diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index d724bd7..0126fd6 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -10,7 +10,6 @@ bool batteryClient::init() { return false; } - // 상태 핸들러 등록 app_->register_state_handler( std::bind(&batteryClient::on_state, this, std::placeholders::_1)); @@ -42,9 +41,17 @@ bool batteryClient::init() { } void batteryClient::start() { - app_->start(); + // 별도 스레드에서 실행 + std::thread vsomeip_thread([this]() { + app_->start(); + }); + vsomeip_thread.detach(); // 백그라운드 실행 } +// void batteryClient::start() { +// app_->start(); +// } + void batteryClient::stop() { app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp index f6f8188..c771186 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -40,10 +40,20 @@ bool gearClient::init() { return true; } + void gearClient::start() { - app_->start(); + // 별도 스레드에서 실행 + std::thread vsomeip_thread([this]() { + app_->start(); + }); + vsomeip_thread.detach(); // 백그라운드 실행 } + +// void gearClient::start() { +// app_->start(); +// } + void gearClient::stop() { app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp index 77b25e4..1896519 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.cpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -38,10 +38,21 @@ bool speedClient::init() { return true; } + void speedClient::start() { - app_->start(); + // 별도 스레드에서 실행 + std::thread vsomeip_thread([this]() { + app_->start(); + }); + vsomeip_thread.detach(); // 백그라운드 실행 } + + +// void speedClient::start() { +// app_->start(); +// } + void speedClient::stop() { app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 0d35260..11908b2 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -37,10 +37,10 @@ int main(int argc, char *argv[]) gearClient gearClient; // alClient alClient; - if (speedClient.init()) - speedClient.start(); if (batteryClient.init()) batteryClient.start(); + if (speedClient.init()) + speedClient.start(); if (gearClient.init()) gearClient.start(); // QObject::connect( diff --git a/apps/ServiceManager/src/battery-src/battery/battery.cpp b/apps/ServiceManager/src/battery-src/battery/battery.cpp index ac80c09..66b0e99 100644 --- a/apps/ServiceManager/src/battery-src/battery/battery.cpp +++ b/apps/ServiceManager/src/battery-src/battery/battery.cpp @@ -194,27 +194,27 @@ void batteryObject::on_set(const std::shared_ptr &_message) { //TODO: change it into battery service code. -void batteryObject::getBatteryVoltage() { - float filtered_speed = 0.0f; - float weight = 0.6; - - std::cout << "running : " << running_ << std::endl; - while (running_) { - std::unique_lock its_lock(can_mutex_); - while (!is_offered_ && running_) - battery_condition_.wait(its_lock); - while (is_offered_ && running_) - { - { - filtered_speed += 0.2; - this->speedData = filtered_speed; - } - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - - } - } +// void batteryObject::getBatteryVoltage() { +// float filtered_speed = 0.0f; +// float weight = 0.6; + +// std::cout << "running : " << running_ << std::endl; +// while (running_) { +// std::unique_lock its_lock(can_mutex_); +// while (!is_offered_ && running_) +// battery_condition_.wait(its_lock); +// while (is_offered_ && running_) +// { +// { +// filtered_speed += 0.2; +// this->speedData = filtered_speed; +// } +// std::this_thread::sleep_for(std::chrono::milliseconds(100)); + +// } +// } -} +// } // Destructor: Cleans up the resources (closes the I2C file descriptor) @@ -263,15 +263,15 @@ void batteryObject::getBatteryVoltage() { // return readValue; // } -// void batteryObject::getBatteryVoltage() { -// // The battery voltage is stored in register 0x02 -// // uint16_t voltageRaw = readRegister(); -// // int voltage = 11; -// uint8_t u_voltage = 11; +void batteryObject::getBatteryVoltage() { + // The battery voltage is stored in register 0x02 + // uint16_t voltageRaw = readRegister(); + // int voltage = 11; + uint8_t u_voltage = 11; -// this->voltage = u_voltage; + this->voltage = u_voltage; -// // uint8_t voltage = ((voltageRaw>>3)*4.0)/1000; -// std::cout << "Battery Voltage: " << this->voltage << std::endl; -// // return voltage; -// } \ No newline at end of file + // uint8_t voltage = ((voltageRaw>>3)*4.0)/1000; + std::cout << "Battery Voltage: " << this->voltage << std::endl; + // return voltage; +} \ No newline at end of file From 1e65f00ecdcb621b45a69ed9a123ace01e677062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 16:21:38 +0100 Subject: [PATCH 26/73] feat: connect class and qml --- apps/HeadUnit/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 11908b2..111867a 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -4,6 +4,7 @@ #include #include #include "modules/spotify/spotify.h" +#include "shared/utils/someip.h" // #include "shared/utils/envmanager.h" #include "HeadUnit.h" @@ -32,6 +33,9 @@ int main(int argc, char *argv[]) Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); + SomeIP someIP; + engine.rootContext()->setContextProperty("someIP", &someIP); + speedClient speedClient; batteryClient batteryClient; gearClient gearClient; From 0d8296ad1141b3171a4431564c998efc3f824532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 16:44:43 +0100 Subject: [PATCH 27/73] feat: update battery data --- .../clients/battery_client/battery_client.cpp | 11 +++++++++-- .../clients/battery_client/battery_client.hpp | 14 +++++++++++--- apps/HeadUnit/main.cpp | 4 +++- apps/HeadUnit/modules/home/Home.qml | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index 0126fd6..aaed65b 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -1,7 +1,14 @@ #include "./battery_client.hpp" -batteryClient::batteryClient() : - app_(vsomeip::runtime::get()->create_application("battery")){ +batteryClient::batteryClient(QObject *parent) + : QObject(parent), + app_(vsomeip::runtime::get()->create_application("battery")) +{ +} + +int batteryClient::getBatteryValue() +{ + return batteryValue; } bool batteryClient::init() { diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp index 46c51c9..91fbefd 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.hpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -1,18 +1,26 @@ #ifndef BATTERY_CLIENTHPP #define BATTERY_CLIENTHPP +#include + #include "../headers.hpp" #include "../server.hpp" -class batteryClient { +class batteryClient : public QObejct +{ + Q_OBJECT public: - batteryClient(); + explicit batteryClient(QObject *parent = nullptr); + + Q_INVOKABLE int getBatteryValue(); bool init(); void start(); void stop(); - int batteryValue; + private: + int batteryValue; + void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 111867a..dc8a778 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -36,8 +36,10 @@ int main(int argc, char *argv[]) SomeIP someIP; engine.rootContext()->setContextProperty("someIP", &someIP); - speedClient speedClient; batteryClient batteryClient; + engine.rootContext()->setContextProperty("batteryClient", &batteryClient); + + speedClient speedClient; gearClient gearClient; // alClient alClient; diff --git a/apps/HeadUnit/modules/home/Home.qml b/apps/HeadUnit/modules/home/Home.qml index 59a10ed..763b645 100644 --- a/apps/HeadUnit/modules/home/Home.qml +++ b/apps/HeadUnit/modules/home/Home.qml @@ -95,7 +95,7 @@ Item{ Text { id: battery - text: HeadUnit.batteryPercentage + text: batteryClient.getBatteryValue() + "" color: myColor Layout.rightMargin: -5 font { From 3569152cdf5e44a0598aea9eec59b9e932cd6fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 17:37:03 +0100 Subject: [PATCH 28/73] feat: connect battery data using signal --- apps/HeadUnit/clients/battery_client/battery_client.cpp | 9 +++++++-- apps/HeadUnit/clients/battery_client/battery_client.hpp | 6 ++++-- apps/HeadUnit/modules/home/Home.qml | 8 +++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index aaed65b..0596a9a 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -2,7 +2,8 @@ batteryClient::batteryClient(QObject *parent) : QObject(parent), - app_(vsomeip::runtime::get()->create_application("battery")) + app_(vsomeip::runtime::get()->create_application("battery")), + batteryValue(0) { } @@ -87,7 +88,11 @@ void batteryClient::on_message(const std::shared_ptr& _respons if (payload->get_length() >= sizeof(int)) { received_value = *reinterpret_cast(payload->get_data()); std::cout << "SERVER: Received int: " << received_value << std::endl; - this->batteryValue = received_value; + if (this->batteryValue != received_value) + { + this->batteryValue = received_value; + emit batteryValueChanged(this->batteryValue); + } } else { std::cerr << "SERVER: Invalid payload size!" << std::endl; return; diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp index 91fbefd..5c78834 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.hpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -9,15 +9,17 @@ class batteryClient : public QObejct { Q_OBJECT + public: explicit batteryClient(QObject *parent = nullptr); - Q_INVOKABLE int getBatteryValue(); - bool init(); void start(); void stop(); +signals: + void batteryValueChanged(int newBatteryValue); + private: int batteryValue; diff --git a/apps/HeadUnit/modules/home/Home.qml b/apps/HeadUnit/modules/home/Home.qml index 763b645..9a8570a 100644 --- a/apps/HeadUnit/modules/home/Home.qml +++ b/apps/HeadUnit/modules/home/Home.qml @@ -95,7 +95,7 @@ Item{ Text { id: battery - text: batteryClient.getBatteryValue() + "" + text: "" color: myColor Layout.rightMargin: -5 font { @@ -105,6 +105,12 @@ Item{ } Layout.alignment: Qt.AlignVCenter } + Connections { + target: batteryClient + onBatteryValueChanged: { + battery.text = newBatteryValue + ""; + } + } Text { id: percent From abae2496798322b2f9288e1c364c211495eac818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 17:52:57 +0100 Subject: [PATCH 29/73] feat: update gear data --- apps/HeadUnit/Main.qml | 65 +++++++++++++++++++ .../gear_client.cpp | 8 ++- .../gear_client.hpp | 15 ++++- apps/HeadUnit/main.cpp | 4 +- 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index 166cf47..18be001 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -182,6 +182,23 @@ Window { PropertyChanges { target: glowEffectD; radius: 0; spread: 0; opacity: 0; visible: false } } ] + Connections { + target: gearClient + onGearValueChanged: { + if (gearClient.gearValue == 3) { + glowEffectD.radius = 16; + glowEffectD.spread = 0.6; + glowEffectD.opacity = 0.5; + glowEffectD.visible = true; + } else { + glowEffectD.radius = 0; + glowEffectD.spread = 0; + glowEffectD.opacity = 0; + glowEffectD.visible = false; + } + } + } + transitions: [ Transition { NumberAnimation { @@ -248,6 +265,22 @@ Window { PropertyChanges { target: glowEffectN; radius: 0; spread: 0; opacity: 0; visible: false } } ] + Connections { + target: gearClient + onGearValueChanged: { + if (gearClient.gearValue == 2) { + glowEffectN.radius = 16; + glowEffectN.spread = 0.6; + glowEffectN.opacity = 0.5; + glowEffectN.visible = true; + } else { + glowEffectN.radius = 0; + glowEffectN.spread = 0; + glowEffectN.opacity = 0; + glowEffectN.visible = false; + } + } + } transitions: [ Transition { NumberAnimation { @@ -314,6 +347,22 @@ Window { PropertyChanges { target: glowEffectR; radius: 0; spread: 0; opacity: 0; visible: false } } ] + Connections { + target: gearClient + onGearValueChanged: { + if (gearClient.gearValue == 1) { + glowEffectR.radius = 16; + glowEffectR.spread = 0.6; + glowEffectR.opacity = 0.5; + glowEffectR.visible = true; + } else { + glowEffectR.radius = 0; + glowEffectR.spread = 0; + glowEffectR.opacity = 0; + glowEffectR.visible = false; + } + } + } transitions: [ Transition { NumberAnimation { @@ -379,6 +428,22 @@ Window { PropertyChanges { target: glowEffectP; radius: 0; spread: 0; opacity: 0; visible: false } } ] + Connections { + target: gearClient + onGearValueChanged: { + if (gearClient.gearValue == 0) { + glowEffectP.radius = 16; + glowEffectP.spread = 0.6; + glowEffectP.opacity = 0.5; + glowEffectP.visible = true; + } else { + glowEffectP.radius = 0; + glowEffectP.spread = 0; + glowEffectP.opacity = 0; + glowEffectP.visible = false; + } + } + } transitions: [ Transition { NumberAnimation { diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp index c771186..0136893 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -1,7 +1,10 @@ #include "./gear_client.hpp" -gearClient::gearClient() : - app_(vsomeip::runtime::get()->create_application("gear")) { +gearClient::gearClient(QObject *parent) + : QObject(parent), + gearValue(0), + app_(vsomeip::runtime::get()->create_application("gear")) +{ } bool gearClient::init() { @@ -83,6 +86,7 @@ void gearClient::on_message(const std::shared_ptr &_request) { received_value = *reinterpret_cast(payload->get_data()); std::cout << "GEAR DATA RECEIVING CLIENT : Received int: " << received_value << std::endl; this->gearValue = received_value; + emit gearValueChanged(received_value); } else { std::cerr << "GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; return; diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp index 1e5a078..df690b9 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -1,18 +1,27 @@ #ifndef GEAR_CLIENT_HPP #define GEAR_CLIENT_HPP +#include + #include "../headers.hpp" #include "../server.hpp" -class gearClient { +class gearClient : public QObejct +{ + Q_OBJECT public: - gearClient(); + explicit gearClient(QObject *parent = nullptr); bool init(); void start(); void stop(); - int gearValue; + +signals: + void gearValueChanged(int newGearValue); + private: + int gearValue; + void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index dc8a778..dc34ad9 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -39,8 +39,10 @@ int main(int argc, char *argv[]) batteryClient batteryClient; engine.rootContext()->setContextProperty("batteryClient", &batteryClient); - speedClient speedClient; gearClient gearClient; + engine.rootContext()->setContextProperty("gearClient", &gearClient); + + speedClient speedClient; // alClient alClient; if (batteryClient.init()) From e4c7ea04b7d97d40574d236c284c2d21ed2b0448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 18:00:50 +0100 Subject: [PATCH 30/73] fix: fixed bug --- apps/HeadUnit/Main.qml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index 18be001..7a39073 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -185,7 +185,7 @@ Window { Connections { target: gearClient onGearValueChanged: { - if (gearClient.gearValue == 3) { + if (newGearValue == 3) { glowEffectD.radius = 16; glowEffectD.spread = 0.6; glowEffectD.opacity = 0.5; @@ -268,7 +268,7 @@ Window { Connections { target: gearClient onGearValueChanged: { - if (gearClient.gearValue == 2) { + if (newGearValue == 2) { glowEffectN.radius = 16; glowEffectN.spread = 0.6; glowEffectN.opacity = 0.5; @@ -350,7 +350,7 @@ Window { Connections { target: gearClient onGearValueChanged: { - if (gearClient.gearValue == 1) { + if (newGearValue == 1) { glowEffectR.radius = 16; glowEffectR.spread = 0.6; glowEffectR.opacity = 0.5; @@ -431,7 +431,7 @@ Window { Connections { target: gearClient onGearValueChanged: { - if (gearClient.gearValue == 0) { + if (newGearValue == 0) { glowEffectP.radius = 16; glowEffectP.spread = 0.6; glowEffectP.opacity = 0.5; From f7128d132533747431be4865350ddf94a1fb1648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 18:05:34 +0100 Subject: [PATCH 31/73] feat: update speed data --- apps/HeadUnit/clients/speed_client/speed_client.cpp | 10 ++++++---- apps/HeadUnit/clients/speed_client/speed_client.hpp | 9 ++++++--- apps/HeadUnit/main.cpp | 2 +- apps/HeadUnit/modules/home/Home.qml | 8 +++++++- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp index 1896519..6e0dca0 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.cpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -1,7 +1,10 @@ #include "./speed_client.hpp" -speedClient::speedClient() : - app_(vsomeip::runtime::get()->create_application("speed")) { +speedClient::speedClient(QObject *parent) + : QObject(parent), + speedValue(0.0), + app_(vsomeip::runtime::get()->create_application("speed")) +{ } bool speedClient::init() { @@ -87,9 +90,8 @@ void speedClient::on_message(const std::shared_ptr& _response) // 변환된 값 출력 std::cout << "Received data: " << received_speed << " m/s" << std::endl; this->speedValue = received_speed; + emit speedValueChanged(received_speed); } else { std::cerr << "Invalid data size received!" << std::endl; } - - } diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp index 92c5ec2..ad12bb6 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.hpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -7,18 +7,21 @@ #include "../server.hpp" class speedClient : public QObject { + Q_OBJECT + public: - speedClient(); + explicit speedClient(QObject *parent = nullptr); bool init(); void start(); void stop(); - float speedValue; signals: - void speedReceived(int speed); + void speedValueChanged(float newSpeedValue); private: + float speedValue; + void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index dc34ad9..a5267a7 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("gearClient", &gearClient); speedClient speedClient; - // alClient alClient; + engine.rootContext()->setContextProperty("speedClient", &speedClient); if (batteryClient.init()) batteryClient.start(); diff --git a/apps/HeadUnit/modules/home/Home.qml b/apps/HeadUnit/modules/home/Home.qml index 9a8570a..3fc4eb8 100644 --- a/apps/HeadUnit/modules/home/Home.qml +++ b/apps/HeadUnit/modules/home/Home.qml @@ -46,7 +46,7 @@ Item{ Text { id: speed - text: HeadUnit.speed + text: "0" color: myColor font { family: "Inter" @@ -55,6 +55,12 @@ Item{ italic: true } } + Connections { + target: speedClient + onspeedValueChanged: { + speed.text = newSpeedValue + ""; + } + } Text { id: cms From 6838827ff21b8293937ea4d672d31fb005770e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EC=9B=90=EC=A0=95?= Date: Thu, 6 Mar 2025 18:12:40 +0100 Subject: [PATCH 32/73] refactor: code refactoring --- .../clients/battery_client/battery_client.cpp | 29 ++++++---- .../clients/battery_client/battery_client.hpp | 4 +- .../gear_client.cpp | 57 ++++++++++--------- .../gear_client.hpp | 4 +- .../clients/speed_client/speed_client.cpp | 30 +++++----- .../clients/speed_client/speed_client.hpp | 5 +- 6 files changed, 70 insertions(+), 59 deletions(-) diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index 0596a9a..bb79949 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -1,18 +1,19 @@ #include "./battery_client.hpp" -batteryClient::batteryClient(QObject *parent) +BatteryClient::BatteryClient(QObject *parent) : QObject(parent), - app_(vsomeip::runtime::get()->create_application("battery")), - batteryValue(0) + batteryValue(0), + app_(vsomeip::runtime::get()->create_application("battery")) { } -int batteryClient::getBatteryValue() +int BatteryClient::getBatteryValue() { return batteryValue; } -bool batteryClient::init() { +bool BatteryClient::init() +{ if (!app_->init()) { std::cerr << "Couldn't initialize application" << std::endl; return false; @@ -48,7 +49,8 @@ bool batteryClient::init() { return true; } -void batteryClient::start() { +void BatteryClient::start() +{ // 별도 스레드에서 실행 std::thread vsomeip_thread([this]() { app_->start(); @@ -60,7 +62,8 @@ void batteryClient::start() { // app_->start(); // } -void batteryClient::stop() { +void BatteryClient::stop() +{ app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, BATTERY_EVENT_ID); @@ -68,20 +71,23 @@ void batteryClient::stop() { app_->stop(); } -void batteryClient::on_state(vsomeip::state_type_e _state) { +void BatteryClient::on_state(vsomeip::state_type_e _state) +{ if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); } } -void batteryClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void BatteryClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) +{ std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void batteryClient::on_message(const std::shared_ptr& _response) { +void BatteryClient::on_message(const std::shared_ptr &_response) +{ std::shared_ptr payload = _response->get_payload(); int received_value = 0; @@ -91,11 +97,10 @@ void batteryClient::on_message(const std::shared_ptr& _respons if (this->batteryValue != received_value) { this->batteryValue = received_value; - emit batteryValueChanged(this->batteryValue); + emit batteryValueChanged(received_value); } } else { std::cerr << "SERVER: Invalid payload size!" << std::endl; return; } - } diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp index 5c78834..2e5155d 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.hpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -6,12 +6,12 @@ #include "../headers.hpp" #include "../server.hpp" -class batteryClient : public QObejct +class BatteryClient : public QObejct { Q_OBJECT public: - explicit batteryClient(QObject *parent = nullptr); + explicit BatteryClient(QObject *parent = nullptr); bool init(); void start(); diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp index 0136893..607c002 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -1,63 +1,64 @@ #include "./gear_client.hpp" -gearClient::gearClient(QObject *parent) +GearClient::GearClient(QObject *parent) : QObject(parent), gearValue(0), app_(vsomeip::runtime::get()->create_application("gear")) { } -bool gearClient::init() { - if (!app_->init()) { +bool GearClient::init() +{ + if (!app_->init()) + { std::cerr << "Couldn't initialize application" << std::endl; return false; } // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&gearClient::on_state, this, std::placeholders::_1)); + std::bind(&gearClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( - vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&gearClient::on_message, this, std::placeholders::_1)); + vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&gearClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, - std::bind(&gearClient::on_availability, this, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + std::bind(&gearClient::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 std::set its_groups; its_groups.insert(VEHICLE_EVENTGROUP_ID); - + app_->request_event( - VEHICLE_SERVICE_ID, - GEAR_INSTANCE_ID, - GEAR_EVENT_ID, - its_groups, - vsomeip::event_type_e::ET_FIELD); - + VEHICLE_SERVICE_ID, + GEAR_INSTANCE_ID, + GEAR_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + app_->subscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); return true; } - -void gearClient::start() { +void GearClient::start() +{ // 별도 스레드에서 실행 - std::thread vsomeip_thread([this]() { - app_->start(); - }); + std::thread vsomeip_thread([this]() + { app_->start(); }); vsomeip_thread.detach(); // 백그라운드 실행 } - -// void gearClient::start() { +// void GearClient::start() { // app_->start(); // } -void gearClient::stop() { +void GearClient::stop() +{ app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, GEAR_EVENT_ID); @@ -65,20 +66,23 @@ void gearClient::stop() { app_->stop(); } -void gearClient::on_state(vsomeip::state_type_e _state) { +void GearClient::on_state(vsomeip::state_type_e _state) +{ if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); } } -void gearClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void GearClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) +{ std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void gearClient::on_message(const std::shared_ptr &_request) { +void GearClient::on_message(const std::shared_ptr &_request) +{ std::shared_ptr payload = _request->get_payload(); int received_value = 0; @@ -91,5 +95,4 @@ void gearClient::on_message(const std::shared_ptr &_request) { std::cerr << "GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; return; } - } diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp index df690b9..b297036 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -6,11 +6,11 @@ #include "../headers.hpp" #include "../server.hpp" -class gearClient : public QObejct +class GearClient : public QObejct { Q_OBJECT public: - explicit gearClient(QObject *parent = nullptr); + explicit GearClient(QObject *parent = nullptr); bool init(); void start(); diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp index 6e0dca0..b0e95d7 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.cpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -1,13 +1,14 @@ #include "./speed_client.hpp" -speedClient::speedClient(QObject *parent) +SpeedClient::SpeedClient(QObject *parent) : QObject(parent), speedValue(0.0), app_(vsomeip::runtime::get()->create_application("speed")) { } -bool speedClient::init() { +bool SpeedClient::init() +{ if (!app_->init()) { std::cerr << "Couldn't initialize application" << std::endl; return false; @@ -41,22 +42,20 @@ bool speedClient::init() { return true; } - -void speedClient::start() { +void SpeedClient::start() +{ // 별도 스레드에서 실행 - std::thread vsomeip_thread([this]() { - app_->start(); - }); + std::thread vsomeip_thread([this]() + { app_->start(); }); vsomeip_thread.detach(); // 백그라운드 실행 } - - -// void speedClient::start() { +// void SpeedClient::start() { // app_->start(); // } -void speedClient::stop() { +void SpeedClient::stop() +{ app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, SPEED_EVENT_ID); @@ -64,20 +63,23 @@ void speedClient::stop() { app_->stop(); } -void speedClient::on_state(vsomeip::state_type_e _state) { +void SpeedClient::on_state(vsomeip::state_type_e _state) +{ if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); } } -void speedClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void SpeedClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) +{ std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void speedClient::on_message(const std::shared_ptr& _response) { +void SpeedClient::on_message(const std::shared_ptr &_response) +{ std::shared_ptr its_payload = _response->get_payload(); if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp index ad12bb6..babebe9 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.hpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -6,11 +6,12 @@ #include "../headers.hpp" #include "../server.hpp" -class speedClient : public QObject { +class SpeedClient : public QObject +{ Q_OBJECT public: - explicit speedClient(QObject *parent = nullptr); + explicit SpeedClient(QObject *parent = nullptr); bool init(); void start(); From 18a331931fe527e53e2a8a1e1feb10172b7a9a63 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Thu, 6 Mar 2025 18:35:51 +0100 Subject: [PATCH 33/73] [FEAT] : qml edit --- apps/HeadUnit/CMakeLists.txt | 2 + .../clients/battery_client/battery_client.cpp | 13 ++-- .../clients/battery_client/battery_client.hpp | 4 +- .../gear_client.cpp | 6 +- .../gear_client.hpp | 5 +- .../clients/speed_client/speed_client.cpp | 6 +- .../clients/speed_client/speed_client.hpp | 3 +- apps/HeadUnit/main.cpp | 6 +- apps/HeadUnit/modules/home/Home.qml | 2 +- apps/HeadUnit/shared/utils/server.hpp | 60 +++++++++++++++++++ apps/HeadUnit/shared/utils/someip.cpp | 4 ++ apps/HeadUnit/shared/utils/someip.h | 4 +- .../src/speed-src/speed/speed.cpp | 6 +- 13 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 apps/HeadUnit/shared/utils/server.hpp diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index be70232..3603dcc 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -54,6 +54,8 @@ qt_add_qml_module(head-unit modules/spotify/spotify.cpp shared/utils/envmanager.h shared/utils/envmanager.cpp + shared/utils/someip.cpp + shared/utils/someip.h DEPENDENCIES QtQuick QtQml diff --git a/apps/HeadUnit/clients/battery_client/battery_client.cpp b/apps/HeadUnit/clients/battery_client/battery_client.cpp index bb79949..b384d7b 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.cpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.cpp @@ -7,11 +7,6 @@ BatteryClient::BatteryClient(QObject *parent) { } -int BatteryClient::getBatteryValue() -{ - return batteryValue; -} - bool BatteryClient::init() { if (!app_->init()) { @@ -21,16 +16,16 @@ bool BatteryClient::init() // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&batteryClient::on_state, this, std::placeholders::_1)); + std::bind(&BatteryClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, BATTERY_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&batteryClient::on_message, this, std::placeholders::_1)); + std::bind(&BatteryClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, - std::bind(&batteryClient::on_availability, this, + std::bind(&BatteryClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 @@ -58,7 +53,7 @@ void BatteryClient::start() vsomeip_thread.detach(); // 백그라운드 실행 } -// void batteryClient::start() { +// void BatteryClient::start() { // app_->start(); // } diff --git a/apps/HeadUnit/clients/battery_client/battery_client.hpp b/apps/HeadUnit/clients/battery_client/battery_client.hpp index 2e5155d..d6258dc 100644 --- a/apps/HeadUnit/clients/battery_client/battery_client.hpp +++ b/apps/HeadUnit/clients/battery_client/battery_client.hpp @@ -6,7 +6,7 @@ #include "../headers.hpp" #include "../server.hpp" -class BatteryClient : public QObejct +class BatteryClient : public QObject { Q_OBJECT @@ -16,12 +16,12 @@ class BatteryClient : public QObejct bool init(); void start(); void stop(); + int batteryValue; signals: void batteryValueChanged(int newBatteryValue); private: - int batteryValue; void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp index 607c002..63405c3 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.cpp @@ -17,16 +17,16 @@ bool GearClient::init() // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&gearClient::on_state, this, std::placeholders::_1)); + std::bind(&GearClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&gearClient::on_message, this, std::placeholders::_1)); + std::bind(&GearClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, - std::bind(&gearClient::on_availability, this, + std::bind(&GearClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 diff --git a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp index b297036..ae756b9 100644 --- a/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/HeadUnit/clients/gear_data_receiving_client/gear_client.hpp @@ -6,7 +6,7 @@ #include "../headers.hpp" #include "../server.hpp" -class GearClient : public QObejct +class GearClient : public QObject { Q_OBJECT public: @@ -15,13 +15,12 @@ class GearClient : public QObejct bool init(); void start(); void stop(); + int gearValue; signals: void gearValueChanged(int newGearValue); private: - int gearValue; - void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp index b0e95d7..7501e09 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.cpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -16,16 +16,16 @@ bool SpeedClient::init() // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&speedClient::on_state, this, std::placeholders::_1)); + std::bind(&SpeedClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, SPEED_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&speedClient::on_message, this, std::placeholders::_1)); + std::bind(&SpeedClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, - std::bind(&speedClient::on_availability, this, + std::bind(&SpeedClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 diff --git a/apps/HeadUnit/clients/speed_client/speed_client.hpp b/apps/HeadUnit/clients/speed_client/speed_client.hpp index babebe9..5b0838e 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.hpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.hpp @@ -16,13 +16,12 @@ class SpeedClient : public QObject bool init(); void start(); void stop(); + float speedValue; signals: void speedValueChanged(float newSpeedValue); private: - float speedValue; - void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index a5267a7..1db14a7 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -36,13 +36,13 @@ int main(int argc, char *argv[]) SomeIP someIP; engine.rootContext()->setContextProperty("someIP", &someIP); - batteryClient batteryClient; + BatteryClient batteryClient; engine.rootContext()->setContextProperty("batteryClient", &batteryClient); - gearClient gearClient; + GearClient gearClient; engine.rootContext()->setContextProperty("gearClient", &gearClient); - speedClient speedClient; + SpeedClient speedClient; engine.rootContext()->setContextProperty("speedClient", &speedClient); if (batteryClient.init()) diff --git a/apps/HeadUnit/modules/home/Home.qml b/apps/HeadUnit/modules/home/Home.qml index 3fc4eb8..bf10c10 100644 --- a/apps/HeadUnit/modules/home/Home.qml +++ b/apps/HeadUnit/modules/home/Home.qml @@ -57,7 +57,7 @@ Item{ } Connections { target: speedClient - onspeedValueChanged: { + onSpeedValueChanged: { speed.text = newSpeedValue + ""; } } diff --git a/apps/HeadUnit/shared/utils/server.hpp b/apps/HeadUnit/shared/utils/server.hpp new file mode 100644 index 0000000..42f8433 --- /dev/null +++ b/apps/HeadUnit/shared/utils/server.hpp @@ -0,0 +1,60 @@ +// Copyright (C) 2014-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#ifndef SERVER_HPP +#define SERVER_HPP + +//Vehicle value. speed & battery using same eventgroup id +#define VEHICLE_SERVICE_ID 0x1234 +#define VEHICLE_EVENTGROUP_ID 0x4465 + +//speed value +#define SPEED_INSTANCE_ID 0x2001 +#define SPEED_EVENT_ID 0x8779 + +//battery value +#define BATTERY_INSTANCE_ID 0x3001 +#define BATTERY_EVENT_ID 0x8780 + +// gear value +#define GEAR_INSTANCE_ID 0x4001 +#define GEAR_EVENT_ID 0x8781 + +// Ambient value +#define AL_INSTANCE_ID 0x5001 +#define AL_EVENT_ID 0x8782 + +#define SAMPLE_INSTANCE_ID 0x5678 + +// 공통 메서드 ID +#define GET_METHOD_ID 0x0001 +#define SET_METHOD_ID 0x0002 + +#define GEAR_SET_METHOD_ID 0x0421 +#define JOY_GEAR_SET_MID 0x0423 +#define JOY_GEAR_RESPONSE_MID 0x0425 + +#define AL_SET_METHOD_ID 0x0427 + +#endif // SERVER_HPP + +//previous + +// #define SAMPLE_SERVICE_ID 0x1234 +// #define SAMPLE_INSTANCE_ID 0x5678 +// #define SAMPLE_METHOD_ID 0x0421 +// #define SAMPLE_EVENTGROUP_ID 0x4465 + + +// #define SPEED_INSTANCE_ID 0x2001 +// #define BATTERY_INSTANCE_ID 0x3001 + + +// #define SAMPLE_EVENT_ID 0x8778 + +// #define SAMPLE_GET_METHOD_ID 0x0001 +// #define SAMPLE_SET_METHOD_ID 0x0002 + +// #endif // SERVER_HPP diff --git a/apps/HeadUnit/shared/utils/someip.cpp b/apps/HeadUnit/shared/utils/someip.cpp index 20009e7..c87d9c5 100644 --- a/apps/HeadUnit/shared/utils/someip.cpp +++ b/apps/HeadUnit/shared/utils/someip.cpp @@ -1,5 +1,9 @@ #include "./someip.h" +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + void SomeIP::send_gear_data(int gear) { std::unique_lock its_lock(mutex); diff --git a/apps/HeadUnit/shared/utils/someip.h b/apps/HeadUnit/shared/utils/someip.h index 5936ad7..b747922 100644 --- a/apps/HeadUnit/shared/utils/someip.h +++ b/apps/HeadUnit/shared/utils/someip.h @@ -1,6 +1,8 @@ #ifndef SOMEIP_H #define SOMEIP_H +#include "./server.hpp" + #include #include @@ -12,7 +14,7 @@ #include #include #include - +#include class SomeIP : public QObject { Q_OBJECT diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 1826e62..63576cb 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -178,11 +178,13 @@ void speedObject::canDataReceive() { CAN_condition_.wait(its_lock); while (is_offered_ && running_) { - { + { + if (filtered_speed >= 100.0f) + filtered_speed = 0.0f; filtered_speed += 0.1; this->speedData = filtered_speed; } - std::this_thread::sleep_for(std::chrono::milliseconds(7)); + std::this_thread::sleep_for(std::chrono::milliseconds(30)); } } From e0d027fba3bb2e7c9c7e19bba894d2c4b8380b12 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Wed, 12 Mar 2025 14:16:33 +0100 Subject: [PATCH 34/73] [FEAT] : battery works --- apps/HeadUnit/CMakeLists.txt | 21 +-- apps/HeadUnit/Main.qml | 16 +- .../clients/HU_gear_client/gear_client.cpp | 8 +- .../clients/speed_client/speed_client.cpp | 2 +- apps/HeadUnit/main.cpp | 11 +- apps/HeadUnit/shared/utils/someip.cpp | 145 +++++++++++++++--- apps/HeadUnit/shared/utils/someip.h | 15 +- apps/HeadUnit/vsomeip.json | 112 ++++++++++++++ apps/ServiceManager/CMakeLists.txt | 31 ++-- .../client-example.cpp | 4 +- .../src/speed-src/speed/speed.cpp | 2 +- 11 files changed, 294 insertions(+), 73 deletions(-) create mode 100644 apps/HeadUnit/vsomeip.json diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index 3603dcc..b7e5353 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -5,8 +5,17 @@ project(HeadUnit VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) -set(EXAMPLE_CONFIG_FILES "./vsomeip.json") +set(VSOMEIP_CONFIG_FILES "./vsomeip.json") + find_package(Qt6 6.5 REQUIRED COMPONENTS Quick Qml) +find_package(Qt6 REQUIRED COMPONENTS Quick WebEngineWidgets Qml) +find_package(vsomeip3 3.4.10 REQUIRED) +find_package( Boost 1.55 COMPONENTS system thread log REQUIRED) + +include_directories( + ${Boost_INCLUDE_DIR} + ${VSOMEIP_INCLUDE_DIR} + ) qt_standard_project_setup(REQUIRES 6.5) @@ -28,6 +37,7 @@ qt_add_executable(head-unit clients/speed_client/speed_client.cpp clients/battery_client/battery_client.cpp clients/gear_data_receiving_client/gear_client.cpp + ${VSOMEIP_CONFIG_FILES} ) qt_add_qml_module(head-unit @@ -50,6 +60,7 @@ qt_add_qml_module(head-unit clients/speed_client/speed_client.hpp clients/battery_client/battery_client.hpp clients/gear_data_receiving_client/gear_client.hpp + clients/server.hpp modules/spotify/spotify.h modules/spotify/spotify.cpp shared/utils/envmanager.h @@ -65,14 +76,6 @@ qt_add_qml_module(head-unit ) -find_package(Qt6 REQUIRED COMPONENTS Quick WebEngineWidgets Qml) -find_package(vsomeip3 3.4.10 REQUIRED) -find_package( Boost 1.55 COMPONENTS system thread log REQUIRED) -include_directories( - ${Boost_INCLUDE_DIR} - ${VSOMEIP_INCLUDE_DIR} - ) - target_link_libraries(head-unit PRIVATE vsomeip3 diff --git a/apps/HeadUnit/Main.qml b/apps/HeadUnit/Main.qml index 7a39073..b257906 100644 --- a/apps/HeadUnit/Main.qml +++ b/apps/HeadUnit/Main.qml @@ -15,14 +15,6 @@ Window { property int ambientLighting: HeadUnit.ambientLighting property color myColor: Qt.hsva(ambientLighting / 360, 1, 1, 1) - - Component.onCompleted: { - console.log("ambientLighting value:", ambientLighting) - } - - onAmbientLightingChanged: { - console.log("ambientLighting changed to:", ambientLighting) - } MediaPlayer { id: mediaPlayer audioOutput: AudioOutput {id: audioOutput} @@ -214,7 +206,7 @@ Window { onClicked: { console.log("D clicked") gearColumn.activeGear = "drive" - someIP.send_gear_data(3); + someIP.set_gear_data(3); } } } @@ -296,7 +288,7 @@ Window { onClicked: { console.log("N clicked") gearColumn.activeGear = "neutral" - someIP.send_gear_data(2); + someIP.set_gear_data(2); } } } @@ -378,7 +370,7 @@ Window { onClicked: { console.log("R clicked") gearColumn.activeGear = "reverse" - someIP.send_gear_data(1); + someIP.set_gear_data(1); } } } @@ -459,7 +451,7 @@ Window { onClicked: { console.log("P clicked") gearColumn.activeGear = "park" - someIP.send_gear_data(0); + someIP.set_gear_data(0); } } } diff --git a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp index ec9e9d8..1262cdf 100644 --- a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp +++ b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp @@ -65,15 +65,9 @@ void on_message(const std::shared_ptr &_response) { } void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { - std::cout << "CLIENT: Service [" - << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance - << "] is " + std::cout << (_is_available ? "available." : "NOT available.") << std::endl; - if (_is_available) { - std::this_thread::sleep_for(std::chrono::seconds(3)); - condition.notify_one(); - } } int main() { diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp index 7501e09..0a69c42 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.cpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -90,7 +90,7 @@ void SpeedClient::on_message(const std::shared_ptr &_response) std::copy(it, it + sizeof(float), reinterpret_cast(&received_speed)); // 변환된 값 출력 - std::cout << "Received data: " << received_speed << " m/s" << std::endl; + // std::cout << "Received data: " << received_speed << " m/s" << std::endl; this->speedValue = received_speed; emit speedValueChanged(received_speed); } else { diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 1db14a7..e96e745 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -10,6 +10,7 @@ #include #include +#include #include "./clients/speed_client/speed_client.hpp" #include "./clients/battery_client/battery_client.hpp" #include "./clients/gear_data_receiving_client/gear_client.hpp" @@ -33,9 +34,8 @@ int main(int argc, char *argv[]) Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); - SomeIP someIP; - engine.rootContext()->setContextProperty("someIP", &someIP); - + // someIP.app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + // app.request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); BatteryClient batteryClient; engine.rootContext()->setContextProperty("batteryClient", &batteryClient); @@ -51,6 +51,11 @@ int main(int argc, char *argv[]) speedClient.start(); if (gearClient.init()) gearClient.start(); + + SomeIP someIP; + engine.rootContext()->setContextProperty("someIP", &someIP); + + someIP.init(); // QObject::connect( // &engine, // &QQmlApplicationEngine::objectCreationFailed, diff --git a/apps/HeadUnit/shared/utils/someip.cpp b/apps/HeadUnit/shared/utils/someip.cpp index c87d9c5..0ac2da5 100644 --- a/apps/HeadUnit/shared/utils/someip.cpp +++ b/apps/HeadUnit/shared/utils/someip.cpp @@ -1,27 +1,124 @@ #include "./someip.h" -std::shared_ptr< vsomeip::application > app; -std::mutex mutex; -std::condition_variable condition; - -void SomeIP::send_gear_data(int gear) -{ - std::unique_lock its_lock(mutex); - condition.wait(its_lock); - - std::shared_ptr request; - request = vsomeip::runtime::get()->create_request(); - request->set_service(VEHICLE_SERVICE_ID); - request->set_instance(GEAR_INSTANCE_ID); - request->set_method(GEAR_SET_METHOD_ID); - - std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); - std::vector its_payload_data( - reinterpret_cast(&gear), - reinterpret_cast(&gear) + sizeof(int)); - its_payload->set_data(its_payload_data); - request->set_payload(its_payload); - app->send(request); - std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); +// void SomeIP::send_gear_data(int gear) +// { +// std::thread sender( +// [this, &gear]() +// { +// std::cout << "Gear Value in Run function : " << gear << std::endl; +// std::shared_ptr request = vsomeip::runtime::get()->create_request(); +// if (!request) { +// std::cerr << "Failed to create request." << std::endl; +// return; +// } +// request->set_service(VEHICLE_SERVICE_ID); +// request->set_instance(GEAR_INSTANCE_ID); +// request->set_method(GEAR_SET_METHOD_ID); + +// std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); +// if (!its_payload) { +// std::cerr << "Failed to create payload." << std::endl; +// return; +// } +// std::vector its_payload_data( +// reinterpret_cast(&gear), +// reinterpret_cast(&gear) + sizeof(int)); +// its_payload->set_data(its_payload_data); +// request->set_payload(its_payload); +// app->send(request); +// std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; +// std::this_thread::sleep_for(std::chrono::seconds(1)); +// } +// ); +// app->start(); +// // sender.join(); // 스레드 종료 대기 +// } + +void SomeIP::set_gear_data(int gear) { + std::cout << "[set gear data function] value : "<< gear << std::endl; + // this->preValue = this->value; + // this->value = gearValue; + + std::shared_ptr request; + + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(GEAR_SET_METHOD_ID); + + // std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); + // if (!its_payload) { + // std::cerr << "Failed to create payload." << std::endl; + // return; + // } + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&gear), + reinterpret_cast(&gear) + sizeof(int)); + + + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + return ; +} + +void SomeIP::run() { + std::cout << "Gear Value in Run function" << std::endl; + int gear; + std::shared_ptr request = vsomeip::runtime::get()->create_request(); + if (!request) { + std::cerr << "Failed to create request." << std::endl; + return; + } + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(GEAR_SET_METHOD_ID); + + std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); + if (!its_payload) { + std::cerr << "Failed to create payload." << std::endl; + return; + } + std::cout << "waiting for the value..." << std::endl; + if (this->value != this->preValue) { + gear = this->value; + this->preValue = gear; + std::vector its_payload_data( + reinterpret_cast(&gear), + reinterpret_cast(&gear) + sizeof(int)); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + } +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << (_is_available ? "gear available." : "gear NOT available.") << std::endl; } + +void SomeIP::start() { + std::thread someIP_thread([this](){ + // std::thread sender(std::bind(&SomeIP::run, this)); + ; + app->start();}); + someIP_thread.detach(); +} + +bool SomeIP::init() { + if (!app->init()) + { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + this->start(); + return true; +} \ No newline at end of file diff --git a/apps/HeadUnit/shared/utils/someip.h b/apps/HeadUnit/shared/utils/someip.h index b747922..11eaca4 100644 --- a/apps/HeadUnit/shared/utils/someip.h +++ b/apps/HeadUnit/shared/utils/someip.h @@ -19,9 +19,20 @@ class SomeIP : public QObject { Q_OBJECT public: - explicit SomeIP(QObject *parent = nullptr) : QObject(parent) {} + // SomeIP(); + std::shared_ptr app; + bool init(); + void start(); + void run(); + + int newValue(int); + int value; + int preValue; + explicit SomeIP(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("gear")), value(0), preValue(0){ + } + Q_INVOKABLE void set_gear_data(int gearValue); + // Q_INVOKABLE void send_gear_data(int); - Q_INVOKABLE void send_gear_data(int); }; #endif // SOMEIP_H diff --git a/apps/HeadUnit/vsomeip.json b/apps/HeadUnit/vsomeip.json new file mode 100644 index 0000000..0de67a2 --- /dev/null +++ b/apps/HeadUnit/vsomeip.json @@ -0,0 +1,112 @@ +{ + "unicast": "127.0.0.1", + "logging": { + "level": "debug", + "console": "true", + "file": { "enable": "false", "path": "/var/log/vsomeip.log" }, + "dlt": "false" + }, + "applications": [ + { + "name": "speed", + "id": "0x4322", + "services": [ + { + "service": "0x1234", + "instance": "0x2001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name":"battery", + "id": "0x4323", + "services": [ + { + "service": "0x1234", + "instance": "0x3001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "gear", + "id": "0x4324", + "services": [ + { + "service": "0x1234", + "instance": "0x4001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "ambient", + "id": "0x4325", + "services": [ + { + "service": "0x1234", + "instance": "0x5001" + } + ] + } + ], + "services": [ + { + "service": "0x1234", + "instance": "0x2001", + "reliable": { "port": "30509", "enable-magic-cookies": "false" }, + "unreliable": "31000", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8779"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x3001", + "reliable": { "port": "30510", "enable-magic-cookies": "false" }, + "unreliable": "31001", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8780"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x4001", + "reliable": { "port": "30511", "enable-magic-cookies": "false" }, + "unreliable": "31002", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8781"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x5001", + "reliable": { "port": "30512", "enable-magic-cookies": "false" }, + "unreliable": "31003" + } + ], + "routing": "service-example", + "service-discovery": { + "enable": "true", + "multicast": "224.244.224.245", + "port": "30490", + "protocol": "udp", + "initial_delay_min": "10", + "initial_delay_max": "100", + "repetitions_base_delay": "100", + "repetitions_max": "3", + "ttl": "3", + "cyclic_offer_delay": "2000", + "request_response_delay": "1500" + } +} diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index 6a7b4d7..c277120 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -46,20 +46,27 @@ target_link_libraries(server-draft vsomeip3 ${Boost_LIBRARIES}) # target_link_libraries(battery_client vsomeip3 ${Boost_LIBRARIES}) # # joystick input -# add_executable(joystick-gear-client ../src/gear-src/J_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) -# target_link_libraries(joystick-gear-client vsomeip3 ${Boost_LIBRARIES}) +add_executable(joystick-gear-client ../src/gear-src/J_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) +target_link_libraries(joystick-gear-client vsomeip3 ${Boost_LIBRARIES}) # # HU input -# add_executable(HU-gear-client ../src/gear-src/HU_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) -# target_link_libraries(HU-gear-client vsomeip3 ${Boost_LIBRARIES}) - -# # gear client -# add_executable(gear_client -# ../src/gear-src/gear_data_receiving_client/client-main.cpp -# ../src/gear-src/gear_data_receiving_client/client-example.cpp -# ${EXAMPLE_CONFIG_FILES} -# ) -# target_link_libraries(gear_client vsomeip3 ${Boost_LIBRARIES}) +add_executable(HU-gear-client ../src/gear-src/HU_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) +target_link_libraries(HU-gear-client vsomeip3 ${Boost_LIBRARIES}) + +# gear client +add_executable(gear_client + ../src/gear-src/gear_data_receiving_client/client-main.cpp + ../src/gear-src/gear_data_receiving_client/client-example.cpp + ${EXAMPLE_CONFIG_FILES} + ) +target_link_libraries(gear_client vsomeip3 ${Boost_LIBRARIES}) + +add_executable(single_gear_client + ../src/gear-src/integerate_client/client-main.cpp + ../src/gear-src/integerate_client/client-example.cpp + ${EXAMPLE_CONFIG_FILES} + ) +target_link_libraries(single_gear_client vsomeip3 ${Boost_LIBRARIES}) # Req/Res testing diff --git a/apps/ServiceManager/src/gear-src/gear_data_receiving_client/client-example.cpp b/apps/ServiceManager/src/gear-src/gear_data_receiving_client/client-example.cpp index 1924520..05c804f 100644 --- a/apps/ServiceManager/src/gear-src/gear_data_receiving_client/client-example.cpp +++ b/apps/ServiceManager/src/gear-src/gear_data_receiving_client/client-example.cpp @@ -77,9 +77,9 @@ void client_sample::on_message(const std::shared_ptr &_request if (payload->get_length() >= sizeof(int)) { received_value = *reinterpret_cast(payload->get_data()); - std::cout << "GEAR DATA RECEIVING CLIENT : Received int: " << received_value << std::endl; + std::cout << "@@@@GEAR DATA RECEIVING CLIENT : Received int: " << received_value << std::endl; } else { - std::cerr << "GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; + std::cerr << "@@@@GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; return; } // this->gearValue = received_value; diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 63576cb..8c12255 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -184,7 +184,7 @@ void speedObject::canDataReceive() { filtered_speed += 0.1; this->speedData = filtered_speed; } - std::this_thread::sleep_for(std::chrono::milliseconds(30)); + std::this_thread::sleep_for(std::chrono::milliseconds(300)); } } From 03241fbfeb5a0a7e173c88bdfbbd3b47da9c037e Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Wed, 12 Mar 2025 16:26:07 +0100 Subject: [PATCH 35/73] [FEAT] : gear, ambient light added on HU and IC. Test required --- apps/HeadUnit/CMakeLists.txt | 2 + .../clients/ambient_receiver/al_receiver.cpp | 36 -------- .../clients/ambient_receiver/al_receiver.hpp | 16 ---- .../clients/ambient_sender/alsender.cpp | 47 ++++++++++ .../clients/ambient_sender/alsender.hpp | 34 +++++++ .../clients/ambient_sender/client.cpp | 74 ---------------- .../clients/ambient_sender/client.hpp | 0 apps/HeadUnit/main.cpp | 26 +++--- apps/HeadUnit/shared/utils/someip.cpp | 80 +---------------- apps/HeadUnit/shared/utils/someip.h | 6 +- apps/InstrumentCluster/CMakeLists.txt | 32 ++++++- .../clients/HU_gear_client/gear_client.cpp | 11 +-- .../clients/J_gear_client/gear_client.cpp | 4 +- .../clients/ambient_receiver/al_receiver.cpp | 46 +++++++--- .../clients/ambient_receiver/al_receiver.hpp | 35 ++++++-- .../clients/ambient_sender/client.cpp | 74 ---------------- .../clients/ambient_sender/client.hpp | 0 .../clients/battery_client/battery_client.cpp | 52 +++++++---- .../clients/battery_client/battery_client.hpp | 19 ++-- .../gear_client.cpp | 65 ++++++++------ .../gear_client.hpp | 16 +++- .../clients/speed_client/speed_client.cpp | 43 ++++++--- .../clients/speed_client/speed_client.hpp | 17 ++-- apps/InstrumentCluster/main.cpp | 48 ++++++---- .../{clients => }/vsomeip.json | 0 .../HU_gear_client/HU_gear_client.cpp | 88 +++++++++++++++++++ .../HU_gear_client/HU_gear_client.hpp | 34 +++++++ .../gear-src/J_gear_client/gear_client.cpp | 6 +- 28 files changed, 488 insertions(+), 423 deletions(-) delete mode 100644 apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp delete mode 100644 apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp create mode 100644 apps/HeadUnit/clients/ambient_sender/alsender.cpp create mode 100644 apps/HeadUnit/clients/ambient_sender/alsender.hpp delete mode 100644 apps/HeadUnit/clients/ambient_sender/client.cpp delete mode 100644 apps/HeadUnit/clients/ambient_sender/client.hpp delete mode 100644 apps/InstrumentCluster/clients/ambient_sender/client.cpp delete mode 100644 apps/InstrumentCluster/clients/ambient_sender/client.hpp rename apps/InstrumentCluster/{clients => }/vsomeip.json (100%) create mode 100644 apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.cpp create mode 100644 apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.hpp diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index b7e5353..0ac11e9 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -37,6 +37,7 @@ qt_add_executable(head-unit clients/speed_client/speed_client.cpp clients/battery_client/battery_client.cpp clients/gear_data_receiving_client/gear_client.cpp + clients/ambient_sender/alsender.cpp ${VSOMEIP_CONFIG_FILES} ) @@ -60,6 +61,7 @@ qt_add_qml_module(head-unit clients/speed_client/speed_client.hpp clients/battery_client/battery_client.hpp clients/gear_data_receiving_client/gear_client.hpp + clients/ambient_sender/alsender.hpp clients/server.hpp modules/spotify/spotify.h modules/spotify/spotify.cpp diff --git a/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp b/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp deleted file mode 100644 index 808efa1..0000000 --- a/apps/HeadUnit/clients/ambient_receiver/al_receiver.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "../headers.hpp" -#include "../server.hpp" -#include "./al_receiver.hpp" -//받은 int값을 처리. -std::shared_ptr app; - -void alClient::on_message(const std::shared_ptr &_request) { - std::shared_ptr payload = _request->get_payload(); - int received_value = 0; - - if (payload->get_length() >= sizeof(int)) { - received_value = *reinterpret_cast(payload->get_data()); - std::cout << "SERVER: Received int: " << received_value << std::endl; - } else { - std::cerr << "SERVER: Invalid payload size!" << std::endl; - return; - } - this->alValue = received_value; -} - -// int main() { -// app = vsomeip::runtime::get()->create_application("ambient"); -// app->init(); -// std::this_thread::sleep_for(std::chrono::seconds(2)); -// app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); -// app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); -// app->start(); -// } -void alClient::start() { - app = vsomeip::runtime::get()->create_application("ambient"); - app->init(); - std::this_thread::sleep_for(std::chrono::seconds(2)); - app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); - app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); - app->start(); -} \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp b/apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp deleted file mode 100644 index 5fe3804..0000000 --- a/apps/HeadUnit/clients/ambient_receiver/al_receiver.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef AL_RECEIVER_HPP -# define AL_RECEIVER_HPP - -class alClient -{ -private : - int alValue; - void on_message(const std::shared_ptr &_request); - void start(); - -public : - alClient(void); - ~alClient(void); -}; - -#endif \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_sender/alsender.cpp b/apps/HeadUnit/clients/ambient_sender/alsender.cpp new file mode 100644 index 0000000..c6a0227 --- /dev/null +++ b/apps/HeadUnit/clients/ambient_sender/alsender.cpp @@ -0,0 +1,47 @@ +#include "./alsender.hpp" + +void Alsender::set_al_data(int al) { + std::cout << "[set al data function] value : "<< al << std::endl; + std::shared_ptr request; + + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(AL_INSTANCE_ID); + request->set_method(AL_SET_METHOD_ID); + + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&al), + reinterpret_cast(&al) + sizeof(int)); + + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::cout << "HEAD UNIT : AL DATA SENDED" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + return ; +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << (_is_available ? "AL available." : "AL NOT available.") << std::endl; +} + +void Alsender::start() { + std::thread al_thread([this](){ + app->start();}); + al_thread.detach(); +} + +bool Alsender::init() { + if (!app->init()) + { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + app->register_availability_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + app->request_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); + this->start(); + return true; +} \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_sender/alsender.hpp b/apps/HeadUnit/clients/ambient_sender/alsender.hpp new file mode 100644 index 0000000..0412c76 --- /dev/null +++ b/apps/HeadUnit/clients/ambient_sender/alsender.hpp @@ -0,0 +1,34 @@ +#ifndef ALSENDER_H +#define ALSENDER_H + +#include "../server.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +class Alsender : public QObject +{ + Q_OBJECT +public: + std::shared_ptr app; + bool init(); + void start(); + + explicit Alsender(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("ambient")){ + } + // Q_INVOKABLE void set_gear_data(int gearValue); + Q_INVOKABLE void set_al_data(int); + // Q_INVOKABLE void send_gear_data(int); + +}; + +#endif // SOMEIP_H diff --git a/apps/HeadUnit/clients/ambient_sender/client.cpp b/apps/HeadUnit/clients/ambient_sender/client.cpp deleted file mode 100644 index 09df744..0000000 --- a/apps/HeadUnit/clients/ambient_sender/client.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "../headers.hpp" -#include "../server.hpp" - -std::shared_ptr< vsomeip::application > app; -std::mutex mutex; -std::condition_variable condition; - -void run() { - std::unique_lock its_lock(mutex); - condition.wait(its_lock); - - //create request - std::shared_ptr< vsomeip::message > request; - request = vsomeip::runtime::get()->create_request(); - //basic setting - request->set_service(VEHICLE_SERVICE_ID); - request->set_instance(AL_INSTANCE_ID); - request->set_method(AL_SET_METHOD_ID); - - int value=0; - while (1){ - std::cout << "Input number" << std::endl; - std::cin >> value; - - std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); - std::vector its_payload_data( - reinterpret_cast(&value), - reinterpret_cast(&value) + sizeof(int) - ); - its_payload->set_data(its_payload_data); - request->set_payload(its_payload); - app->send(request); - std::cout << "CLIENT : DATA SENDED" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - } - -void on_message(const std::shared_ptr &_response) { - - std::shared_ptr its_payload = _response->get_payload(); - vsomeip::length_t l = its_payload->get_length(); - - // Get payload - std::stringstream ss; - for (vsomeip::length_t i=0; iget_data()+i) << " "; - } - - std::cout << "CLIENT: Received message with Client/Session [" - << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" - << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " - << ss.str() << std::endl; -} - -void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { - std::cout << "CLIENT: Service [" - << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance - << "] is " - << (_is_available ? "available." : "NOT available.") - << std::endl; - condition.notify_one(); -} - -int main() { - app = vsomeip::runtime::get()->create_application("ambient"); - app->init(); - app->register_availability_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, on_availability); - std::this_thread::sleep_for(std::chrono::seconds(2)); - app->request_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); - app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, vsomeip::ANY_METHOD, on_message); - std::thread sender(run); - app->start(); -} \ No newline at end of file diff --git a/apps/HeadUnit/clients/ambient_sender/client.hpp b/apps/HeadUnit/clients/ambient_sender/client.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index e96e745..70aa8c7 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -14,6 +14,7 @@ #include "./clients/speed_client/speed_client.hpp" #include "./clients/battery_client/battery_client.hpp" #include "./clients/gear_data_receiving_client/gear_client.hpp" +#include "./clients/ambient_sender/alsender.hpp" int main(int argc, char *argv[]) @@ -34,8 +35,6 @@ int main(int argc, char *argv[]) Spotify spotify; engine.rootContext()->setContextProperty("spotify", &spotify); - // someIP.app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); - // app.request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); BatteryClient batteryClient; engine.rootContext()->setContextProperty("batteryClient", &batteryClient); @@ -54,14 +53,13 @@ int main(int argc, char *argv[]) SomeIP someIP; engine.rootContext()->setContextProperty("someIP", &someIP); - + someIP.init(); - // QObject::connect( - // &engine, - // &QQmlApplicationEngine::objectCreationFailed, - // &app, - // []() { QCoreApplication::exit(-1); }, - // Qt::QueuedConnection); + + Alsender alClient; + engine.rootContext()->setContextProperty("alClient", &alClient); + + alClient.init(); QObject::connect( &engine, @@ -89,13 +87,13 @@ int main(int argc, char *argv[]) controller.setCurrentGear(gears[gearIndex]); gearIndex = (gearIndex + 1) % gears.size(); - //Ambient Light + // Ambient Light // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; - // static int colorIndex = 0; - // colorIndex = alClient.alValue; // <- set colorIndex to received value + // static int colorIndex = 0; + // colorIndex = alClient.alValue; // <- set colorIndex to received value - // controller.setAmbientLighting(colors[colorIndex]); - // colorIndex = (colorIndex + 1) % colors.size(); + // controller.setAmbientLighting(colors[colorIndex]); + // colorIndex = (colorIndex + 1) % colors.size(); }, Qt::QueuedConnection); diff --git a/apps/HeadUnit/shared/utils/someip.cpp b/apps/HeadUnit/shared/utils/someip.cpp index 0ac2da5..0dceaf1 100644 --- a/apps/HeadUnit/shared/utils/someip.cpp +++ b/apps/HeadUnit/shared/utils/someip.cpp @@ -1,44 +1,7 @@ #include "./someip.h" -// void SomeIP::send_gear_data(int gear) -// { -// std::thread sender( -// [this, &gear]() -// { -// std::cout << "Gear Value in Run function : " << gear << std::endl; -// std::shared_ptr request = vsomeip::runtime::get()->create_request(); -// if (!request) { -// std::cerr << "Failed to create request." << std::endl; -// return; -// } -// request->set_service(VEHICLE_SERVICE_ID); -// request->set_instance(GEAR_INSTANCE_ID); -// request->set_method(GEAR_SET_METHOD_ID); - -// std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); -// if (!its_payload) { -// std::cerr << "Failed to create payload." << std::endl; -// return; -// } -// std::vector its_payload_data( -// reinterpret_cast(&gear), -// reinterpret_cast(&gear) + sizeof(int)); -// its_payload->set_data(its_payload_data); -// request->set_payload(its_payload); -// app->send(request); -// std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; -// std::this_thread::sleep_for(std::chrono::seconds(1)); -// } -// ); -// app->start(); -// // sender.join(); // 스레드 종료 대기 -// } - void SomeIP::set_gear_data(int gear) { std::cout << "[set gear data function] value : "<< gear << std::endl; - // this->preValue = this->value; - // this->value = gearValue; - std::shared_ptr request; request = vsomeip::runtime::get()->create_request(); @@ -46,11 +9,6 @@ void SomeIP::set_gear_data(int gear) { request->set_instance(GEAR_INSTANCE_ID); request->set_method(GEAR_SET_METHOD_ID); - // std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); - // if (!its_payload) { - // std::cerr << "Failed to create payload." << std::endl; - // return; - // } std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); std::vector its_payload_data( reinterpret_cast(&gear), @@ -65,46 +23,12 @@ void SomeIP::set_gear_data(int gear) { return ; } -void SomeIP::run() { - std::cout << "Gear Value in Run function" << std::endl; - int gear; - std::shared_ptr request = vsomeip::runtime::get()->create_request(); - if (!request) { - std::cerr << "Failed to create request." << std::endl; - return; - } - request->set_service(VEHICLE_SERVICE_ID); - request->set_instance(GEAR_INSTANCE_ID); - request->set_method(GEAR_SET_METHOD_ID); - - std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); - if (!its_payload) { - std::cerr << "Failed to create payload." << std::endl; - return; - } - std::cout << "waiting for the value..." << std::endl; - if (this->value != this->preValue) { - gear = this->value; - this->preValue = gear; - std::vector its_payload_data( - reinterpret_cast(&gear), - reinterpret_cast(&gear) + sizeof(int)); - its_payload->set_data(its_payload_data); - request->set_payload(its_payload); - app->send(request); - std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } -} - -void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void on_gear_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { std::cout << (_is_available ? "gear available." : "gear NOT available.") << std::endl; } void SomeIP::start() { std::thread someIP_thread([this](){ - // std::thread sender(std::bind(&SomeIP::run, this)); - ; app->start();}); someIP_thread.detach(); } @@ -115,7 +39,7 @@ bool SomeIP::init() { std::cerr << "Couldn't initialize application" << std::endl; return false; } - app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_gear_availability); std::this_thread::sleep_for(std::chrono::seconds(1)); app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); diff --git a/apps/HeadUnit/shared/utils/someip.h b/apps/HeadUnit/shared/utils/someip.h index 11eaca4..5e43860 100644 --- a/apps/HeadUnit/shared/utils/someip.h +++ b/apps/HeadUnit/shared/utils/someip.h @@ -23,12 +23,8 @@ class SomeIP : public QObject std::shared_ptr app; bool init(); void start(); - void run(); - int newValue(int); - int value; - int preValue; - explicit SomeIP(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("gear")), value(0), preValue(0){ + explicit SomeIP(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("gear")){ } Q_INVOKABLE void set_gear_data(int gearValue); // Q_INVOKABLE void send_gear_data(int); diff --git a/apps/InstrumentCluster/CMakeLists.txt b/apps/InstrumentCluster/CMakeLists.txt index 55e384d..5f10d86 100644 --- a/apps/InstrumentCluster/CMakeLists.txt +++ b/apps/InstrumentCluster/CMakeLists.txt @@ -3,13 +3,26 @@ cmake_minimum_required(VERSION 3.16) project(InstrumentCluster VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(VSOMEIP_CONFIG_FILES "./vsomeip.json") -find_package(Qt6 6.5 REQUIRED COMPONENTS Quick) +find_package(Qt6 6.5 REQUIRED COMPONENTS Quick WebEngineWidgets Qml) +find_package(vsomeip3 3.4.10 REQUIRED) +find_package( Boost 1.55 COMPONENTS system thread log REQUIRED) + + +include_directories( + ${Boost_INCLUDE_DIR} + ${VSOMEIP_INCLUDE_DIR} + ) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(instrument-cluster main.cpp + clients/speed_client/speed_client.cpp + clients/battery_client/battery_client.cpp + clients/gear_data_receiving_client/gear_client.cpp + ${VSOMEIP_CONFIG_FILES} ) qt_add_qml_module(instrument-cluster @@ -22,7 +35,14 @@ qt_add_qml_module(instrument-cluster QML_FILES Battery.qml QML_FILES Gear.qml QML_FILES - SOURCES instrumentclustercontroller.h instrumentclustercontroller.cpp + SOURCES + instrumentclustercontroller.h + instrumentclustercontroller.cpp + clients/speed_client/speed_client.hpp + clients/battery_client/battery_client.hpp + clients/gear_data_receiving_client/gear_client.hpp + clients/server.hpp + ) qt_add_resources(instrument-cluster "resources" @@ -44,7 +64,13 @@ set_target_properties(instrument-cluster PROPERTIES ) target_link_libraries(instrument-cluster - PRIVATE Qt6::Quick + PRIVATE + vsomeip3 + ${Boost_LIBRARIES} + Qt6::Quick + Qt6::WebEngineWidgets + Qt6::Qml + ) include(GNUInstallDirs) diff --git a/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp b/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp index cc65afb..1262cdf 100644 --- a/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp +++ b/apps/InstrumentCluster/clients/HU_gear_client/gear_client.cpp @@ -6,7 +6,8 @@ #include #include -#include "../../server.hpp" +#include "../server.hpp" +#include "./gear_client.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; @@ -64,15 +65,9 @@ void on_message(const std::shared_ptr &_response) { } void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { - std::cout << "CLIENT: Service [" - << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance - << "] is " + std::cout << (_is_available ? "available." : "NOT available.") << std::endl; - if (_is_available) { - std::this_thread::sleep_for(std::chrono::seconds(3)); - condition.notify_one(); - } } int main() { diff --git a/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp b/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp index 8d1b531..aaa94a1 100644 --- a/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp +++ b/apps/InstrumentCluster/clients/J_gear_client/gear_client.cpp @@ -6,8 +6,8 @@ #include #include -#include "../../server.hpp" - +#include "../server.hpp" +#include "./gear_client.hpp" std::shared_ptr< vsomeip::application > app; std::mutex mutex; std::condition_variable condition; diff --git a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp index 960e8d6..acb14fa 100644 --- a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp +++ b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp @@ -1,8 +1,8 @@ -#include "../../headers.hpp" -#include "../../server.hpp" +#include "../headers.hpp" +#include "../server.hpp" #include "./al_receiver.hpp" //받은 int값을 처리. -std::shared_ptr app; +// std::shared_ptr app; void alClient::on_message(const std::shared_ptr &_request) { std::shared_ptr payload = _request->get_payload(); @@ -18,6 +18,30 @@ void alClient::on_message(const std::shared_ptr &_request) { this->alValue = received_value; } +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << (_is_available ? "AL receiver available." : "AL receiver NOT available.") << std::endl; +} + +void alClient::start() { + std::thread al_thread([this](){ + app->start();}); + al_thread.detach(); +} + +bool alClient::init() { + if (!app->init()) + { + std::cerr << "Couldn't initialize application" << std::endl; + return false; + } + app->register_availability_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + app->request_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); + this->start(); + return true; +} + // int main() { // app = vsomeip::runtime::get()->create_application("ambient"); // app->init(); @@ -26,11 +50,11 @@ void alClient::on_message(const std::shared_ptr &_request) { // app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); // app->start(); // } -void alClient::start() { - app = vsomeip::runtime::get()->create_application("ambient"); - app->init(); - std::this_thread::sleep_for(std::chrono::seconds(2)); - app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); - app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); - app->start(); -} \ No newline at end of file +// void alClient::start() { +// app = vsomeip::runtime::get()->create_application("ambient"); +// app->init(); +// std::this_thread::sleep_for(std::chrono::seconds(2)); +// app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, AL_SET_METHOD_ID, on_message); +// app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); +// app->start(); +// } \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp index 5fe3804..83738b8 100644 --- a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp +++ b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp @@ -1,16 +1,35 @@ #ifndef AL_RECEIVER_HPP # define AL_RECEIVER_HPP -class alClient +#include "../server.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class alClient : public QObject { -private : - int alValue; - void on_message(const std::shared_ptr &_request); - void start(); - + Q_OBJECT public : - alClient(void); - ~alClient(void); + // alClient(void); + // ~alClient(void); + + std::shared_ptr app; + bool init(); + void start(); + void on_message(const std::shared_ptr &_request); + explicit Alsender(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("ambient")){ + } + Q_INVOKABLE void set_al_data(int); }; #endif \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_sender/client.cpp b/apps/InstrumentCluster/clients/ambient_sender/client.cpp deleted file mode 100644 index 5d12b7a..0000000 --- a/apps/InstrumentCluster/clients/ambient_sender/client.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "../../headers.hpp" -#include "../../server.hpp" - -std::shared_ptr< vsomeip::application > app; -std::mutex mutex; -std::condition_variable condition; - -void run() { - std::unique_lock its_lock(mutex); - condition.wait(its_lock); - - //create request - std::shared_ptr< vsomeip::message > request; - request = vsomeip::runtime::get()->create_request(); - //basic setting - request->set_service(VEHICLE_SERVICE_ID); - request->set_instance(AL_INSTANCE_ID); - request->set_method(AL_SET_METHOD_ID); - - int value=0; - while (1){ - std::cout << "Input number" << std::endl; - std::cin >> value; - - std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); - std::vector its_payload_data( - reinterpret_cast(&value), - reinterpret_cast(&value) + sizeof(int) - ); - its_payload->set_data(its_payload_data); - request->set_payload(its_payload); - app->send(request); - std::cout << "CLIENT : DATA SENDED" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - } - -void on_message(const std::shared_ptr &_response) { - - std::shared_ptr its_payload = _response->get_payload(); - vsomeip::length_t l = its_payload->get_length(); - - // Get payload - std::stringstream ss; - for (vsomeip::length_t i=0; iget_data()+i) << " "; - } - - std::cout << "CLIENT: Received message with Client/Session [" - << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" - << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " - << ss.str() << std::endl; -} - -void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { - std::cout << "CLIENT: Service [" - << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance - << "] is " - << (_is_available ? "available." : "NOT available.") - << std::endl; - condition.notify_one(); -} - -int main() { - app = vsomeip::runtime::get()->create_application("ambient"); - app->init(); - app->register_availability_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, on_availability); - std::this_thread::sleep_for(std::chrono::seconds(2)); - app->request_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); - app->register_message_handler(VEHICLE_SERVICE_ID, AL_INSTANCE_ID, vsomeip::ANY_METHOD, on_message); - std::thread sender(run); - app->start(); -} \ No newline at end of file diff --git a/apps/InstrumentCluster/clients/ambient_sender/client.hpp b/apps/InstrumentCluster/clients/ambient_sender/client.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/apps/InstrumentCluster/clients/battery_client/battery_client.cpp b/apps/InstrumentCluster/clients/battery_client/battery_client.cpp index e32237b..b384d7b 100644 --- a/apps/InstrumentCluster/clients/battery_client/battery_client.cpp +++ b/apps/InstrumentCluster/clients/battery_client/battery_client.cpp @@ -1,29 +1,31 @@ -#include "./batteryClient.hpp" +#include "./battery_client.hpp" -client_sample::client_sample() : - app_(vsomeip::runtime::get()->create_application("battery")){ +BatteryClient::BatteryClient(QObject *parent) + : QObject(parent), + batteryValue(0), + app_(vsomeip::runtime::get()->create_application("battery")) +{ } -bool client_sample::init() { +bool BatteryClient::init() +{ if (!app_->init()) { std::cerr << "Couldn't initialize application" << std::endl; return false; } - std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; - // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&client_sample::on_state, this, std::placeholders::_1)); + std::bind(&BatteryClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, BATTERY_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&client_sample::on_message, this, std::placeholders::_1)); + std::bind(&BatteryClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, - std::bind(&client_sample::on_availability, this, + std::bind(&BatteryClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 @@ -42,11 +44,21 @@ bool client_sample::init() { return true; } -void client_sample::start() { - app_->start(); +void BatteryClient::start() +{ + // 별도 스레드에서 실행 + std::thread vsomeip_thread([this]() { + app_->start(); + }); + vsomeip_thread.detach(); // 백그라운드 실행 } -void client_sample::stop() { +// void BatteryClient::start() { +// app_->start(); +// } + +void BatteryClient::stop() +{ app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID, BATTERY_EVENT_ID); @@ -54,30 +66,36 @@ void client_sample::stop() { app_->stop(); } -void client_sample::on_state(vsomeip::state_type_e _state) { +void BatteryClient::on_state(vsomeip::state_type_e _state) +{ if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, BATTERY_INSTANCE_ID); } } -void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void BatteryClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) +{ std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void client_sample::on_message(const std::shared_ptr& _response) { +void BatteryClient::on_message(const std::shared_ptr &_response) +{ std::shared_ptr payload = _response->get_payload(); int received_value = 0; if (payload->get_length() >= sizeof(int)) { received_value = *reinterpret_cast(payload->get_data()); std::cout << "SERVER: Received int: " << received_value << std::endl; - this->batteryValue = received_value; + if (this->batteryValue != received_value) + { + this->batteryValue = received_value; + emit batteryValueChanged(received_value); + } } else { std::cerr << "SERVER: Invalid payload size!" << std::endl; return; } - } diff --git a/apps/InstrumentCluster/clients/battery_client/battery_client.hpp b/apps/InstrumentCluster/clients/battery_client/battery_client.hpp index 755ad94..d6258dc 100644 --- a/apps/InstrumentCluster/clients/battery_client/battery_client.hpp +++ b/apps/InstrumentCluster/clients/battery_client/battery_client.hpp @@ -1,19 +1,28 @@ #ifndef BATTERY_CLIENTHPP #define BATTERY_CLIENTHPP -#include "../../headers.hpp" -#include "../../server.hpp" +#include + +#include "../headers.hpp" +#include "../server.hpp" + +class BatteryClient : public QObject +{ + Q_OBJECT -class batteryClient { public: - batteryClient(); + explicit BatteryClient(QObject *parent = nullptr); bool init(); void start(); void stop(); + int batteryValue; + +signals: + void batteryValueChanged(int newBatteryValue); private: - int batteryValue; + void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp index 19a8075..63405c3 100644 --- a/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp +++ b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.cpp @@ -1,52 +1,64 @@ #include "./gear_client.hpp" -client_sample::client_sample() : - app_(vsomeip::runtime::get()->create_application("gear")) { +GearClient::GearClient(QObject *parent) + : QObject(parent), + gearValue(0), + app_(vsomeip::runtime::get()->create_application("gear")) +{ } -bool client_sample::init() { - if (!app_->init()) { +bool GearClient::init() +{ + if (!app_->init()) + { std::cerr << "Couldn't initialize application" << std::endl; return false; } - std::cout << "Client settings [protocol=" << (use_tcp_ ? "TCP" : "UDP") << "]" << std::endl; - // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&client_sample::on_state, this, std::placeholders::_1)); + std::bind(&GearClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( - vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&client_sample::on_message, this, std::placeholders::_1)); + vsomeip::ANY_SERVICE, GEAR_INSTANCE_ID, vsomeip::ANY_METHOD, + std::bind(&GearClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, - std::bind(&client_sample::on_availability, this, - std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + std::bind(&GearClient::on_availability, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 std::set its_groups; its_groups.insert(VEHICLE_EVENTGROUP_ID); - + app_->request_event( - VEHICLE_SERVICE_ID, - GEAR_INSTANCE_ID, - GEAR_EVENT_ID, - its_groups, - vsomeip::event_type_e::ET_FIELD); - + VEHICLE_SERVICE_ID, + GEAR_INSTANCE_ID, + GEAR_EVENT_ID, + its_groups, + vsomeip::event_type_e::ET_FIELD); + app_->subscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); return true; } -void client_sample::start() { - app_->start(); +void GearClient::start() +{ + // 별도 스레드에서 실행 + std::thread vsomeip_thread([this]() + { app_->start(); }); + vsomeip_thread.detach(); // 백그라운드 실행 } -void client_sample::stop() { +// void GearClient::start() { +// app_->start(); +// } + +void GearClient::stop() +{ app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, GEAR_EVENT_ID); @@ -54,20 +66,23 @@ void client_sample::stop() { app_->stop(); } -void client_sample::on_state(vsomeip::state_type_e _state) { +void GearClient::on_state(vsomeip::state_type_e _state) +{ if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); } } -void client_sample::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void GearClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) +{ std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void client_sample::on_message(const std::shared_ptr &_request) { +void GearClient::on_message(const std::shared_ptr &_request) +{ std::shared_ptr payload = _request->get_payload(); int received_value = 0; @@ -75,9 +90,9 @@ void client_sample::on_message(const std::shared_ptr &_request received_value = *reinterpret_cast(payload->get_data()); std::cout << "GEAR DATA RECEIVING CLIENT : Received int: " << received_value << std::endl; this->gearValue = received_value; + emit gearValueChanged(received_value); } else { std::cerr << "GEAR DATA RECEIVING CLIENT : Invalid payload size!" << std::endl; return; } - } diff --git a/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp index d9dd398..ae756b9 100644 --- a/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp +++ b/apps/InstrumentCluster/clients/gear_data_receiving_client/gear_client.hpp @@ -1,16 +1,24 @@ #ifndef GEAR_CLIENT_HPP #define GEAR_CLIENT_HPP -#include "../../headers.hpp" -#include "../../server.hpp" +#include -class gearClient { +#include "../headers.hpp" +#include "../server.hpp" + +class GearClient : public QObject +{ + Q_OBJECT public: - gearClient(); + explicit GearClient(QObject *parent = nullptr); bool init(); void start(); void stop(); + int gearValue; + +signals: + void gearValueChanged(int newGearValue); private: void on_state(vsomeip::state_type_e _state); diff --git a/apps/InstrumentCluster/clients/speed_client/speed_client.cpp b/apps/InstrumentCluster/clients/speed_client/speed_client.cpp index 77b25e4..7501e09 100644 --- a/apps/InstrumentCluster/clients/speed_client/speed_client.cpp +++ b/apps/InstrumentCluster/clients/speed_client/speed_client.cpp @@ -1,10 +1,14 @@ #include "./speed_client.hpp" -speedClient::speedClient() : - app_(vsomeip::runtime::get()->create_application("speed")) { +SpeedClient::SpeedClient(QObject *parent) + : QObject(parent), + speedValue(0.0), + app_(vsomeip::runtime::get()->create_application("speed")) +{ } -bool speedClient::init() { +bool SpeedClient::init() +{ if (!app_->init()) { std::cerr << "Couldn't initialize application" << std::endl; return false; @@ -12,16 +16,16 @@ bool speedClient::init() { // 상태 핸들러 등록 app_->register_state_handler( - std::bind(&speedClient::on_state, this, std::placeholders::_1)); + std::bind(&SpeedClient::on_state, this, std::placeholders::_1)); // 메시지 핸들러 등록 app_->register_message_handler( vsomeip::ANY_SERVICE, SPEED_INSTANCE_ID, vsomeip::ANY_METHOD, - std::bind(&speedClient::on_message, this, std::placeholders::_1)); + std::bind(&SpeedClient::on_message, this, std::placeholders::_1)); // 가용성 핸들러 등록 app_->register_availability_handler(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, - std::bind(&speedClient::on_availability, this, + std::bind(&SpeedClient::on_availability, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); // 이벤트 구독 @@ -38,11 +42,20 @@ bool speedClient::init() { return true; } -void speedClient::start() { - app_->start(); +void SpeedClient::start() +{ + // 별도 스레드에서 실행 + std::thread vsomeip_thread([this]() + { app_->start(); }); + vsomeip_thread.detach(); // 백그라운드 실행 } -void speedClient::stop() { +// void SpeedClient::start() { +// app_->start(); +// } + +void SpeedClient::stop() +{ app_->clear_all_handler(); app_->unsubscribe(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, VEHICLE_EVENTGROUP_ID); app_->release_event(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID, SPEED_EVENT_ID); @@ -50,20 +63,23 @@ void speedClient::stop() { app_->stop(); } -void speedClient::on_state(vsomeip::state_type_e _state) { +void SpeedClient::on_state(vsomeip::state_type_e _state) +{ if (_state == vsomeip::state_type_e::ST_REGISTERED) { app_->request_service(VEHICLE_SERVICE_ID, SPEED_INSTANCE_ID); } } -void speedClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { +void SpeedClient::on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) +{ std::cout << "Service [" << std::hex << std::setfill('0') << std::setw(4) << _service << "." << std::setw(4) << _instance << "] is " << (_is_available ? "available." : "NOT available.") << std::endl; } -void speedClient::on_message(const std::shared_ptr& _response) { +void SpeedClient::on_message(const std::shared_ptr &_response) +{ std::shared_ptr its_payload = _response->get_payload(); if (its_payload->get_length() == sizeof(float)) { // 페이로드 크기로 검증 @@ -76,9 +92,8 @@ void speedClient::on_message(const std::shared_ptr& _response) // 변환된 값 출력 std::cout << "Received data: " << received_speed << " m/s" << std::endl; this->speedValue = received_speed; + emit speedValueChanged(received_speed); } else { std::cerr << "Invalid data size received!" << std::endl; } - - } diff --git a/apps/InstrumentCluster/clients/speed_client/speed_client.hpp b/apps/InstrumentCluster/clients/speed_client/speed_client.hpp index f2dcc93..5b0838e 100644 --- a/apps/InstrumentCluster/clients/speed_client/speed_client.hpp +++ b/apps/InstrumentCluster/clients/speed_client/speed_client.hpp @@ -1,22 +1,27 @@ #ifndef SPEED_CLIENT_HPP #define SPEED_CLIENT_HPP -#include "../../headers.hpp" -#include "../../server.hpp" +#include + +#include "../headers.hpp" +#include "../server.hpp" + +class SpeedClient : public QObject +{ + Q_OBJECT -class speedClient{ public: - speedClient(); + explicit SpeedClient(QObject *parent = nullptr); bool init(); void start(); void stop(); + float speedValue; signals: - void speedReceived(int speed); + void speedValueChanged(float newSpeedValue); private: - float speedValue; void on_state(vsomeip::state_type_e _state); void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); void on_message(const std::shared_ptr& _response); diff --git a/apps/InstrumentCluster/main.cpp b/apps/InstrumentCluster/main.cpp index 0ea3444..a75d9d5 100644 --- a/apps/InstrumentCluster/main.cpp +++ b/apps/InstrumentCluster/main.cpp @@ -5,6 +5,13 @@ // for test #include +#include +#include +#include "./clients/speed_client/speed_client.hpp" +#include "./clients/battery_client/battery_client.hpp" +#include "./clients/gear_data_receiving_client/gear_client.hpp" +#include "./clients/ambient_sender/alsender.hpp" + #include "instrumentclustercontroller.h" //TODO: 2025-02-20. For ambient light, send/recive data type change required. @@ -14,14 +21,24 @@ int main(int argc, char *argv[]) QQmlApplicationEngine engine; InstrumentClusterController controller; - - speedClient speedClient(); - batteryClient batteryClient(); - gearClient gearClient(); - alClient alClient(); - engine.rootContext()->setContextProperty("instrumentClusterController", &controller); + BatteryClient batteryClient; + engine.rootContext()->setContextProperty("batteryClient", &batteryClient); + + GearClient gearClient; + engine.rootContext()->setContextProperty("gearClient", &gearClient); + + SpeedClient speedClient; + engine.rootContext()->setContextProperty("speedClient", &speedClient); + + if (batteryClient.init()) + batteryClient.start(); + if (speedClient.init()) + speedClient.start(); + if (gearClient.init()) + gearClient.start(); + QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, @@ -31,19 +48,12 @@ int main(int argc, char *argv[]) engine.loadFromModule("InstrumentCluster", "Main"); qDebug() << "Instrument Cluster launched"; - - if (speedClient.init()) - speedClient.start(); - if (batteryClient.init()) - batteryClient.start(); - if (gearClient.init()) - gearClient.start(); - alClient.start(); + // alClient.start(); // test code QTimer *timer = new QTimer(&controller); - QObject::connect(timer, &QTimer::timeout, [&controller]() { + QObject::connect(timer, &QTimer::timeout, [&controller, &speedClient,&batteryClient,&gearClient]() { //Speed static int speed = 0; speed = speedClient.speedValue; @@ -68,11 +78,11 @@ int main(int argc, char *argv[]) //Ambient Light // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; - static int colorIndex = 0; - colorIndex = alClient.alValue; // <- set colorIndex to received value + // static int colorIndex = 0; + // colorIndex = alClient.alValue; // <- set colorIndex to received value - controller.setAmbientLighting(colors[colorIndex]); - colorIndex = (colorIndex + 1) % colors.size(); + // controller.setAmbientLighting(colors[colorIndex]); + // colorIndex = (colorIndex + 1) % colors.size(); }); timer->start(1000); diff --git a/apps/InstrumentCluster/clients/vsomeip.json b/apps/InstrumentCluster/vsomeip.json similarity index 100% rename from apps/InstrumentCluster/clients/vsomeip.json rename to apps/InstrumentCluster/vsomeip.json diff --git a/apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.cpp b/apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.cpp new file mode 100644 index 0000000..cc65afb --- /dev/null +++ b/apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.cpp @@ -0,0 +1,88 @@ +#include +#include +#include + +#include +#include + +#include +#include "../../server.hpp" + +std::shared_ptr< vsomeip::application > app; +std::mutex mutex; +std::condition_variable condition; + +//sending the actual data (will be gear data) to server. +void run() { + std::unique_lock its_lock(mutex); + condition.wait(its_lock); + + std::shared_ptr< vsomeip::message > request; + request = vsomeip::runtime::get()->create_request(); + request->set_service(VEHICLE_SERVICE_ID); + request->set_instance(GEAR_INSTANCE_ID); + request->set_method(GEAR_SET_METHOD_ID); + +//sending actual data. changed into HU input value. + int value=0; + + while (1){ + // value += 1; + if (value != 5) + {std::cout << "Input number" << std::endl; + std::cin >> value;} + std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); + std::vector its_payload_data( + reinterpret_cast(&value), + reinterpret_cast(&value) + sizeof(int) + ); + its_payload->set_data(its_payload_data); + request->set_payload(its_payload); + app->send(request); + std::this_thread::sleep_for(std::chrono::seconds(1)); + + } + std::cout << "CLIENT : DATA SENDED" << std::endl; +} + +// When response come, print the payload from server. +void on_message(const std::shared_ptr &_response) { + std::shared_ptr its_payload = _response->get_payload(); + vsomeip::length_t l = its_payload->get_length(); + + // Get payload + std::stringstream ss; + for (vsomeip::length_t i=0; iget_data()+i) << " "; + } + + std::cout << "CLIENT: Received message with Client/Session [" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_client() << "/" + << std::setw(4) << std::setfill('0') << std::hex << _response->get_session() << "] " + << ss.str() << std::endl; +} + +void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available) { + std::cout << "CLIENT: Service [" + << std::setw(4) << std::setfill('0') << std::hex << _service << "." << _instance + << "] is " + << (_is_available ? "available." : "NOT available.") + << std::endl; + if (_is_available) { + std::this_thread::sleep_for(std::chrono::seconds(3)); + condition.notify_one(); + } +} + +int main() { + + app = vsomeip::runtime::get()->create_application("gear"); + app->init(); + app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + std::this_thread::sleep_for(std::chrono::seconds(1)); + app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_RESPONSE_MID, on_message); + std::thread sender(run); + app->start(); +} diff --git a/apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.hpp b/apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.hpp new file mode 100644 index 0000000..1e24653 --- /dev/null +++ b/apps/ServiceManager/src/gear-src/HU_gear_client/HU_gear_client.hpp @@ -0,0 +1,34 @@ +#ifndef GEAR_CLIENT_HPP +# define GEAR_CLIENT_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../server.hpp" + +class gearClient { +public: + gearClient(bool _use_tcp); + + bool init(); + void start(); + void stop(); + +private: + void on_state(vsomeip::state_type_e _state); + void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, bool _is_available); + void on_message(const std::shared_ptr& _response); + + std::shared_ptr app_; + bool use_tcp_; + + gearClient(void); + ~gearClient(void); +}; + +#endif \ No newline at end of file diff --git a/apps/ServiceManager/src/gear-src/J_gear_client/gear_client.cpp b/apps/ServiceManager/src/gear-src/J_gear_client/gear_client.cpp index 8d1b531..6881eb2 100644 --- a/apps/ServiceManager/src/gear-src/J_gear_client/gear_client.cpp +++ b/apps/ServiceManager/src/gear-src/J_gear_client/gear_client.cpp @@ -33,15 +33,13 @@ void run() { //sending actual data. changed into HU input value. int value=0; // <- real gear value should be on value variable while (1){ - // std::cout << "Input number" << std::endl; - // std::cin >> value; + std::cout << "Input number" << std::endl; + std::cin >> value; std::shared_ptr< vsomeip::payload > its_payload = vsomeip::runtime::get()->create_payload(); std::vector its_payload_data( reinterpret_cast(&value), reinterpret_cast(&value) + sizeof(int) - // reinterpret_cast(&gearValue), - // reinterpret_cast(&gearValue) + sizeof(int) ); its_payload->set_data(its_payload_data); request->set_payload(its_payload); From f4b06d2a34ed2f26b2929cde7f9b52c6bd174558 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 16:46:21 +0100 Subject: [PATCH 36/73] [FEAT] : battery code edit --- .../clients/HU_gear_client/gear_client.cpp | 2 +- .../clients/ambient_sender/alsender.hpp | 1 + apps/InstrumentCluster/CMakeLists.txt | 2 + .../clients/ambient_receiver/al_receiver.cpp | 8 +- .../clients/ambient_receiver/al_receiver.hpp | 14 +- apps/InstrumentCluster/main.cpp | 23 ++- .../src/battery-src/battery/battery.cpp | 158 +++++++++--------- .../src/battery-src/battery/battery.hpp | 4 +- .../src/speed-src/speed/speed.cpp | 29 +++- .../src/speed-src/speed/speed.hpp | 1 + 10 files changed, 141 insertions(+), 101 deletions(-) diff --git a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp index 1262cdf..f6dee5a 100644 --- a/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp +++ b/apps/HeadUnit/clients/HU_gear_client/gear_client.cpp @@ -40,7 +40,7 @@ void run() { its_payload->set_data(its_payload_data); request->set_payload(its_payload); app->send(request); - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(80)); } std::cout << "CLIENT : DATA SENDED" << std::endl; diff --git a/apps/HeadUnit/clients/ambient_sender/alsender.hpp b/apps/HeadUnit/clients/ambient_sender/alsender.hpp index 0412c76..50ea5b7 100644 --- a/apps/HeadUnit/clients/ambient_sender/alsender.hpp +++ b/apps/HeadUnit/clients/ambient_sender/alsender.hpp @@ -1,6 +1,7 @@ #ifndef ALSENDER_H #define ALSENDER_H +#include "../headers.hpp" #include "../server.hpp" #include diff --git a/apps/InstrumentCluster/CMakeLists.txt b/apps/InstrumentCluster/CMakeLists.txt index 5f10d86..18b9552 100644 --- a/apps/InstrumentCluster/CMakeLists.txt +++ b/apps/InstrumentCluster/CMakeLists.txt @@ -19,6 +19,7 @@ qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(instrument-cluster main.cpp + clients/ambient_receiver/al_receiver.cpp clients/speed_client/speed_client.cpp clients/battery_client/battery_client.cpp clients/gear_data_receiving_client/gear_client.cpp @@ -38,6 +39,7 @@ qt_add_qml_module(instrument-cluster SOURCES instrumentclustercontroller.h instrumentclustercontroller.cpp + clients/ambient_receiver/al_receiver.hpp clients/speed_client/speed_client.hpp clients/battery_client/battery_client.hpp clients/gear_data_receiving_client/gear_client.hpp diff --git a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp index acb14fa..f28e5b6 100644 --- a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp +++ b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.cpp @@ -4,7 +4,7 @@ //받은 int값을 처리. // std::shared_ptr app; -void alClient::on_message(const std::shared_ptr &_request) { +void AlClient::on_message(const std::shared_ptr &_request) { std::shared_ptr payload = _request->get_payload(); int received_value = 0; @@ -22,13 +22,13 @@ void on_availability(vsomeip::service_t _service, vsomeip::instance_t _instance, std::cout << (_is_available ? "AL receiver available." : "AL receiver NOT available.") << std::endl; } -void alClient::start() { +void AlClient::start() { std::thread al_thread([this](){ app->start();}); al_thread.detach(); } -bool alClient::init() { +bool AlClient::init() { if (!app->init()) { std::cerr << "Couldn't initialize application" << std::endl; @@ -50,7 +50,7 @@ bool alClient::init() { // app->offer_service(VEHICLE_SERVICE_ID, AL_INSTANCE_ID); // app->start(); // } -// void alClient::start() { +// void AlClient::start() { // app = vsomeip::runtime::get()->create_application("ambient"); // app->init(); // std::this_thread::sleep_for(std::chrono::seconds(2)); diff --git a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp index 83738b8..831e7f7 100644 --- a/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp +++ b/apps/InstrumentCluster/clients/ambient_receiver/al_receiver.hpp @@ -16,20 +16,20 @@ #include #include -class alClient : public QObject +class AlClient : public QObject { Q_OBJECT public : - // alClient(void); - // ~alClient(void); - + // AlClient(void); + // ~AlClient(void); + int alValue; std::shared_ptr app; bool init(); void start(); void on_message(const std::shared_ptr &_request); - explicit Alsender(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("ambient")){ + explicit AlClient(QObject *parent = nullptr) : QObject(parent), app(vsomeip::runtime::get()->create_application("ambient")){ } - Q_INVOKABLE void set_al_data(int); + // Q_INVOKABLE void set_al_data(int); }; -#endif \ No newline at end of file +#endif diff --git a/apps/InstrumentCluster/main.cpp b/apps/InstrumentCluster/main.cpp index a75d9d5..a4baaf0 100644 --- a/apps/InstrumentCluster/main.cpp +++ b/apps/InstrumentCluster/main.cpp @@ -5,12 +5,12 @@ // for test #include -#include +// #include #include #include "./clients/speed_client/speed_client.hpp" #include "./clients/battery_client/battery_client.hpp" #include "./clients/gear_data_receiving_client/gear_client.hpp" -#include "./clients/ambient_sender/alsender.hpp" +#include "./clients/ambient_receiver/al_receiver.hpp" #include "instrumentclustercontroller.h" @@ -32,12 +32,17 @@ int main(int argc, char *argv[]) SpeedClient speedClient; engine.rootContext()->setContextProperty("speedClient", &speedClient); + AlClient ambientClient; + engine.rootContext()->setContextProperty("ambientClient", &ambientClient); + if (batteryClient.init()) batteryClient.start(); if (speedClient.init()) speedClient.start(); if (gearClient.init()) gearClient.start(); + if (ambientClient.init()) + ambientClient.start(); QObject::connect( &engine, @@ -53,7 +58,7 @@ int main(int argc, char *argv[]) // test code QTimer *timer = new QTimer(&controller); - QObject::connect(timer, &QTimer::timeout, [&controller, &speedClient,&batteryClient,&gearClient]() { + QObject::connect(timer, &QTimer::timeout, [&controller, &speedClient,&batteryClient,&gearClient, &ambientClient]() { //Speed static int speed = 0; speed = speedClient.speedValue; @@ -76,13 +81,13 @@ int main(int argc, char *argv[]) controller.setCurrentGear(gears[gearIndex]); gearIndex = (gearIndex + 1) % gears.size(); - //Ambient Light - // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; - // static int colorIndex = 0; - // colorIndex = alClient.alValue; // <- set colorIndex to received value + // Ambient Light + static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; + static int colorIndex = 0; + colorIndex = ambientClient.alValue; // <- set colorIndex to received value - // controller.setAmbientLighting(colors[colorIndex]); - // colorIndex = (colorIndex + 1) % colors.size(); + controller.setAmbientLighting(colors[colorIndex]); + colorIndex = (colorIndex + 1) % colors.size(); }); timer->start(1000); diff --git a/apps/ServiceManager/src/battery-src/battery/battery.cpp b/apps/ServiceManager/src/battery-src/battery/battery.cpp index 66b0e99..6780650 100644 --- a/apps/ServiceManager/src/battery-src/battery/battery.cpp +++ b/apps/ServiceManager/src/battery-src/battery/battery.cpp @@ -12,10 +12,10 @@ batteryObject::batteryObject(uint32_t _cycle) : speedData(0), voltage(0), file(-1){ - battery_thread_ = std::thread(&batteryObject::getBatteryVoltage, this); - // if (!initI2C()) { - // std::cout << "Failed to initialize I2C interface."; - // } + battery_thread_ = std::thread(&batteryObject::getBatteryData, this); + if (!initI2C()) { + std::cout << "Failed to initialize I2C interface."; + } } @@ -192,86 +192,94 @@ void batteryObject::on_set(const std::shared_ptr &_message) { } -//TODO: change it into battery service code. +// Destructor: Cleans up the resources (closes the I2C file descriptor) -// void batteryObject::getBatteryVoltage() { -// float filtered_speed = 0.0f; -// float weight = 0.6; +bool batteryObject::initI2C() { + // Open the I2C bus + if ((file = open(device, O_RDWR)) < 0) { + std::cout << "Failed to open the I2C bus"; + return false; + } -// std::cout << "running : " << running_ << std::endl; -// while (running_) { -// std::unique_lock its_lock(can_mutex_); -// while (!is_offered_ && running_) -// battery_condition_.wait(its_lock); -// while (is_offered_ && running_) -// { -// { -// filtered_speed += 0.2; -// this->speedData = filtered_speed; -// } -// std::this_thread::sleep_for(std::chrono::milliseconds(100)); + // Set the I2C address for the slave device + if (ioctl(file, I2C_SLAVE, addr) < 0) { + std::cout << "Failed to acquire bus access and/or talk to slave."; + close(file); + return false; + } -// } -// } + return true; +} -// } +uint16_t batteryObject::readRegister() { + char buf[2]; + buf[0] = reg; + if (file < 0) { + std::cout << "I2C file is not initialized."; + return -1; + } -// Destructor: Cleans up the resources (closes the I2C file descriptor) + // Write the register address to the I2C bus + if (write(file, buf, 1) != 1) { + std::cout << "Failed to write to the I2C bus."; + std::cout << "Error still exists"; + return -1; + } + usleep(1000); // Time delay to read back from I2C + + // Read the data from the I2C bus + if (read(file, buf, 2) != 2) { + std::cout << "Failed to read from the I2C bus."; + return -1; + } -// bool batteryObject::initI2C() { -// // Open the I2C bus -// if ((file = open(device, O_RDWR)) < 0) { -// std::cout << "Failed to open the I2C bus"; -// return false; -// } - -// // Set the I2C address for the slave device -// if (ioctl(file, I2C_SLAVE, addr) < 0) { -// std::cout << "Failed to acquire bus access and/or talk to slave."; -// close(file); -// return false; -// } - -// return true; -// } - -// uint16_t batteryObject::readRegister() { -// char buf[2]; -// buf[0] = reg; - -// if (file < 0) { -// std::cout << "I2C file is not initialized."; -// return -1; -// } - -// // Write the register address to the I2C bus -// if (write(file, buf, 1) != 1) { -// std::cout << "Failed to write to the I2C bus."; -// std::cout << "Error still exists"; -// return -1; -// } -// usleep(1000); // Time delay to read back from I2C - -// // Read the data from the I2C bus -// if (read(file, buf, 2) != 2) { -// std::cout << "Failed to read from the I2C bus."; -// return -1; -// } - -// uint16_t readValue = (buf[0] << 8) + buf[1]; -// return readValue; -// } - -void batteryObject::getBatteryVoltage() { + uint16_t readValue = (buf[0] << 8) + buf[1]; + return readValue; +} + +uint8_t batteryObject::getBatteryVoltage() { // The battery voltage is stored in register 0x02 - // uint16_t voltageRaw = readRegister(); + uint16_t voltageRaw = readRegister(); // int voltage = 11; - uint8_t u_voltage = 11; + // uint8_t u_voltage = 11; - this->voltage = u_voltage; + // this->voltage = u_voltage; - // uint8_t voltage = ((voltageRaw>>3)*4.0)/1000; + voltage = ((voltageRaw>>3)*4.0)/1000; std::cout << "Battery Voltage: " << this->voltage << std::endl; - // return voltage; -} \ No newline at end of file + return voltage; +} + +void batteryObject::getBatteryData() { + // The battery voltage is stored in register 0x02 + uint8_t battery_percent = 0; + int lowVoltage = 9; + float diffVoltage = 3.45; + + + std::cout << "running : " << running_ << std::endl; + while (running_) { + std::unique_lock its_lock(can_mutex_); + while (!is_offered_ && running_) + battery_condition_.wait(its_lock); + while (is_offered_ && running_) + { + { + uint8_t voltage = this->getBatteryVoltage(); + float battery_percent = (((voltage - lowVoltage) / diffVoltage) * 100); + if (battery_percent -100 > 0) + battery_percent = 100; + else if (battery_percent < 0) + battery_percent = 0; + this->voltage = battery_percent; + // filtered_speed += 0.2; + // this->speedData = filtered_speed; + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + } + } + +} + diff --git a/apps/ServiceManager/src/battery-src/battery/battery.hpp b/apps/ServiceManager/src/battery-src/battery/battery.hpp index cabbb51..b70e286 100644 --- a/apps/ServiceManager/src/battery-src/battery/battery.hpp +++ b/apps/ServiceManager/src/battery-src/battery/battery.hpp @@ -30,7 +30,9 @@ private : uint16_t readRegister(); // Method to read a value from a register // void getBatteryVoltage(); - void getBatteryVoltage(); + uint8_t getBatteryVoltage(); + void getBatteryData(); + void canDataReceive(); std::shared_ptr app_; diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 8c12255..d6847e5 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -171,6 +171,7 @@ void speedObject::canDataReceive() { std::cout << "can Data Receive started" << std::endl; float filtered_speed = 0.0f; float weight = 0.6; + CANReceiver canData("can0"); while (running_) { std::unique_lock its_lock(can_mutex_); @@ -179,13 +180,33 @@ void speedObject::canDataReceive() { while (is_offered_ && running_) { { - if (filtered_speed >= 100.0f) - filtered_speed = 0.0f; - filtered_speed += 0.1; + canData.canRead(); + filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); this->speedData = filtered_speed; + // if (filtered_speed >= 100.0f) + // filtered_speed = 0.0f; + // filtered_speed += 0.1; + // this->speedData = filtered_speed; } - std::this_thread::sleep_for(std::chrono::milliseconds(300)); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); } } + + // while (running_) { + // std::unique_lock its_lock(can_mutex_); + // while (!is_offered_ && running_) + // CAN_condition_.wait(its_lock); + // while (is_offered_ && running_) + // { + // { + // if (filtered_speed >= 100.0f) + // filtered_speed = 0.0f; + // filtered_speed += 0.1; + // this->speedData = filtered_speed; + // } + // std::this_thread::sleep_for(std::chrono::milliseconds(300)); + + // } + // } } diff --git a/apps/ServiceManager/src/speed-src/speed/speed.hpp b/apps/ServiceManager/src/speed-src/speed/speed.hpp index 1c8dc9a..fbd94ea 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.hpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.hpp @@ -2,6 +2,7 @@ # define SPEED_HPP #include "../../service_base/service-base.hpp" +#include "./canreceiver.hpp" class speedObject : public service_sample { From 4695b9cb47f26be89ced9cb2e9cbb99a11c9ceab Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 16:50:37 +0100 Subject: [PATCH 37/73] [FEAT] : cmakelist changed excutable --- apps/ServiceManager/CMakeLists.txt | 38 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index c277120..b2ea600 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -21,7 +21,7 @@ message(STATUS "VSOMEIP_LIBRARIES: ${VSOMEIP_LIBRARIES}") # Server binary file -add_executable(server-draft +add_executable(server ../src/main.cpp ../src/service-main.cpp ../src/service_base/service-base.cpp @@ -32,7 +32,7 @@ add_executable(server-draft ./config/vsomeip.json ${EXAMPLE_CONFIG_FILES} ) -target_link_libraries(server-draft vsomeip3 ${Boost_LIBRARIES}) +target_link_libraries(server vsomeip3 ${Boost_LIBRARIES}) # # add_executable(service-example ../src/main.cpp ../src/service-main.cpp ../src/service-example.cpp ../src/canreceiver.cpp ${EXAMPLE_CONFIG_FILES}) # # target_link_libraries(service-example vsomeip3 ${Boost_LIBRARIES}) @@ -46,27 +46,27 @@ target_link_libraries(server-draft vsomeip3 ${Boost_LIBRARIES}) # target_link_libraries(battery_client vsomeip3 ${Boost_LIBRARIES}) # # joystick input -add_executable(joystick-gear-client ../src/gear-src/J_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) -target_link_libraries(joystick-gear-client vsomeip3 ${Boost_LIBRARIES}) +# add_executable(joystick-gear-client ../src/gear-src/J_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) +# target_link_libraries(joystick-gear-client vsomeip3 ${Boost_LIBRARIES}) # # HU input -add_executable(HU-gear-client ../src/gear-src/HU_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) -target_link_libraries(HU-gear-client vsomeip3 ${Boost_LIBRARIES}) +# add_executable(HU-gear-client ../src/gear-src/HU_gear_client/gear_client.cpp ${EXAMPLE_CONFIG_FILES}) +# target_link_libraries(HU-gear-client vsomeip3 ${Boost_LIBRARIES}) # gear client -add_executable(gear_client - ../src/gear-src/gear_data_receiving_client/client-main.cpp - ../src/gear-src/gear_data_receiving_client/client-example.cpp - ${EXAMPLE_CONFIG_FILES} - ) -target_link_libraries(gear_client vsomeip3 ${Boost_LIBRARIES}) - -add_executable(single_gear_client - ../src/gear-src/integerate_client/client-main.cpp - ../src/gear-src/integerate_client/client-example.cpp - ${EXAMPLE_CONFIG_FILES} - ) -target_link_libraries(single_gear_client vsomeip3 ${Boost_LIBRARIES}) +# add_executable(gear_client +# ../src/gear-src/gear_data_receiving_client/client-main.cpp +# ../src/gear-src/gear_data_receiving_client/client-example.cpp +# ${EXAMPLE_CONFIG_FILES} +# ) +# target_link_libraries(gear_client vsomeip3 ${Boost_LIBRARIES}) + +# add_executable(single_gear_client +# ../src/gear-src/integerate_client/client-main.cpp +# ../src/gear-src/integerate_client/client-example.cpp +# ${EXAMPLE_CONFIG_FILES} +# ) +# target_link_libraries(single_gear_client vsomeip3 ${Boost_LIBRARIES}) # Req/Res testing From cdf0e0b02af9cd345afe917a7076f991ccad47e2 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 16:55:20 +0100 Subject: [PATCH 38/73] [FEAT] : path update --- apps/ServiceManager/CMakeLists.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index b2ea600..568762b 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -22,13 +22,13 @@ message(STATUS "VSOMEIP_LIBRARIES: ${VSOMEIP_LIBRARIES}") # Server binary file add_executable(server - ../src/main.cpp - ../src/service-main.cpp - ../src/service_base/service-base.cpp - ../src/speed-src/speed/speed.cpp - ../src/speed-src/speed/canreceiver.cpp - ../src/battery-src/battery/battery.cpp - ../src/gear-src/gear/gear.cpp + ./src/main.cpp + ./src/service-main.cpp + ./src/service_base/service-base.cpp + ./src/speed-src/speed/speed.cpp + ./src/speed-src/speed/canreceiver.cpp + ./src/battery-src/battery/battery.cpp + ./src/gear-src/gear/gear.cpp ./config/vsomeip.json ${EXAMPLE_CONFIG_FILES} ) From 64a910d6a83bf689594a09ebed601f7670ad2d9a Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 17:12:41 +0100 Subject: [PATCH 39/73] [FEAT] : cmakelist edit --- apps/ServiceManager/CMakeLists.txt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/apps/ServiceManager/CMakeLists.txt b/apps/ServiceManager/CMakeLists.txt index 568762b..260f471 100644 --- a/apps/ServiceManager/CMakeLists.txt +++ b/apps/ServiceManager/CMakeLists.txt @@ -7,7 +7,7 @@ set(EXAMPLE_CONFIG_FILES ./config/vsomeip.json ) -find_package (vsomeip3 3.4.10 REQUIRED) +find_package (vsomeip3 3.5.3 REQUIRED) find_package( Boost 1.55 COMPONENTS system thread log REQUIRED ) @@ -16,10 +16,6 @@ include_directories ( ${VSOMEIP_INCLUDE_DIRS} ) -message(STATUS "VSOMEIP_INCLUDE_DIRS: ${VSOMEIP_INCLUDE_DIRS}") -message(STATUS "VSOMEIP_LIBRARIES: ${VSOMEIP_LIBRARIES}") - - # Server binary file add_executable(server ./src/main.cpp From 48be885cce134ee16e6e27d83528d0fb76802338 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 17:13:57 +0100 Subject: [PATCH 40/73] [FEAT] : cmake list vsomeip version changed --- apps/HeadUnit/CMakeLists.txt | 2 +- apps/InstrumentCluster/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/HeadUnit/CMakeLists.txt b/apps/HeadUnit/CMakeLists.txt index 0ac11e9..99b4495 100644 --- a/apps/HeadUnit/CMakeLists.txt +++ b/apps/HeadUnit/CMakeLists.txt @@ -9,7 +9,7 @@ set(VSOMEIP_CONFIG_FILES "./vsomeip.json") find_package(Qt6 6.5 REQUIRED COMPONENTS Quick Qml) find_package(Qt6 REQUIRED COMPONENTS Quick WebEngineWidgets Qml) -find_package(vsomeip3 3.4.10 REQUIRED) +find_package(vsomeip3 3.5.3 REQUIRED) find_package( Boost 1.55 COMPONENTS system thread log REQUIRED) include_directories( diff --git a/apps/InstrumentCluster/CMakeLists.txt b/apps/InstrumentCluster/CMakeLists.txt index 18b9552..b85bf65 100644 --- a/apps/InstrumentCluster/CMakeLists.txt +++ b/apps/InstrumentCluster/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(VSOMEIP_CONFIG_FILES "./vsomeip.json") find_package(Qt6 6.5 REQUIRED COMPONENTS Quick WebEngineWidgets Qml) -find_package(vsomeip3 3.4.10 REQUIRED) +find_package(vsomeip3 3.5.3 REQUIRED) find_package( Boost 1.55 COMPONENTS system thread log REQUIRED) From 4b1eac7fccfe87f32dc61d620aa026ab5a423ffe Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 18:01:38 +0100 Subject: [PATCH 41/73] main edit --- apps/HeadUnit/main.cpp | 56 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 70aa8c7..708caf1 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -102,35 +102,35 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("HeadUnit", &controller); - // test code - QTimer *timer = new QTimer(&controller); - QObject::connect(timer, &QTimer::timeout, [&controller]() { - // static QStringList gears = {"P", "R", "N", "D"}; - // static int gearIndex = 0; - // controller.setCurrentGear(gears[gearIndex]); - // gearIndex = (gearIndex + 1) % gears.size(); - - static int speed = 0; - speed = (speed + 10) % 310; - controller.setSpeed(speed); - - static int batteryPercentage = 100; - batteryPercentage = (batteryPercentage - 10) < 0 ? 100 : batteryPercentage - 10; - controller.setBatteryPercentage(batteryPercentage); - - static int chargingState = false; - chargingState = !chargingState; - controller.setChargingState(chargingState); + // // test code + // QTimer *timer = new QTimer(&controller); + // QObject::connect(timer, &QTimer::timeout, [&controller]() { + // // static QStringList gears = {"P", "R", "N", "D"}; + // // static int gearIndex = 0; + // // controller.setCurrentGear(gears[gearIndex]); + // // gearIndex = (gearIndex + 1) % gears.size(); + + // // static int speed = 0; + // // speed = (speed + 10) % 310; + // // controller.setSpeed(speed); + + // // static int batteryPercentage = 100; + // // batteryPercentage = (batteryPercentage - 10) < 0 ? 100 : batteryPercentage - 10; + // // controller.setBatteryPercentage(batteryPercentage); + + // // static int chargingState = false; + // // chargingState = !chargingState; + // // controller.setChargingState(chargingState); - QVector colors = {20, 157, 100, 300, 200}; - static int colorIndex = 0; - - // colorIndex = alClient.alValue; - controller.setAmbientLighting(colors[colorIndex]); - colorIndex = (colorIndex + 1) % colors.size(); - std::cout << controller.ambientLighting() << std::endl; - }); - timer->start(1000); + // QVector colors = {20, 157, 100, 300, 200}; + // static int colorIndex = 0; + + // // colorIndex = alClient.alValue; + // controller.setAmbientLighting(colors[colorIndex]); + // colorIndex = (colorIndex + 1) % colors.size(); + // std::cout << controller.ambientLighting() << std::endl; + // }); + // timer->start(1000); qDebug() << "Head Unit launched"; From 0a73b012ab8851eaf039364217c5fbf525c2d9c7 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 18:04:13 +0100 Subject: [PATCH 42/73] main edit --- apps/HeadUnit/main.cpp | 58 +----------------------------------------- 1 file changed, 1 insertion(+), 57 deletions(-) diff --git a/apps/HeadUnit/main.cpp b/apps/HeadUnit/main.cpp index 708caf1..0df229c 100644 --- a/apps/HeadUnit/main.cpp +++ b/apps/HeadUnit/main.cpp @@ -66,34 +66,7 @@ int main(int argc, char *argv[]) &QQmlApplicationEngine::objectCreationFailed, &app, [&controller, &speedClient,&batteryClient,&gearClient]() { QCoreApplication::exit(-1); - static int speed = 0; - speed = speedClient.speedValue; - controller.setSpeed(speed); - - //Battery - static int batteryPercentage = 100; - batteryPercentage = batteryClient.batteryValue; - controller.setBatteryPercentage(batteryPercentage); - - static int chargingState = false; - chargingState = !chargingState; - controller.setChargingState(chargingState); - - //Gear - static QStringList gears = {"P", "R", "N", "D"}; - static int gearIndex = 0; - gearIndex = gearClient.gearValue; // Drive = 3, NEUTRAL=2, REVERSE=1, PARKING=0 - - controller.setCurrentGear(gears[gearIndex]); - gearIndex = (gearIndex + 1) % gears.size(); - - // Ambient Light - // static QStringList colors = {"#4deeea", "#74ee15", "#ffe700", "#f000ff", "#001eff"}; - // static int colorIndex = 0; - // colorIndex = alClient.alValue; // <- set colorIndex to received value - - // controller.setAmbientLighting(colors[colorIndex]); - // colorIndex = (colorIndex + 1) % colors.size(); + }, Qt::QueuedConnection); @@ -102,35 +75,6 @@ int main(int argc, char *argv[]) engine.rootContext()->setContextProperty("HeadUnit", &controller); - // // test code - // QTimer *timer = new QTimer(&controller); - // QObject::connect(timer, &QTimer::timeout, [&controller]() { - // // static QStringList gears = {"P", "R", "N", "D"}; - // // static int gearIndex = 0; - // // controller.setCurrentGear(gears[gearIndex]); - // // gearIndex = (gearIndex + 1) % gears.size(); - - // // static int speed = 0; - // // speed = (speed + 10) % 310; - // // controller.setSpeed(speed); - - // // static int batteryPercentage = 100; - // // batteryPercentage = (batteryPercentage - 10) < 0 ? 100 : batteryPercentage - 10; - // // controller.setBatteryPercentage(batteryPercentage); - - // // static int chargingState = false; - // // chargingState = !chargingState; - // // controller.setChargingState(chargingState); - - // QVector colors = {20, 157, 100, 300, 200}; - // static int colorIndex = 0; - - // // colorIndex = alClient.alValue; - // controller.setAmbientLighting(colors[colorIndex]); - // colorIndex = (colorIndex + 1) % colors.size(); - // std::cout << controller.ambientLighting() << std::endl; - // }); - // timer->start(1000); qDebug() << "Head Unit launched"; From 5075ecea7365749e5c156b2f0de4207493abe4ba Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 18:05:42 +0100 Subject: [PATCH 43/73] gear delay edit --- apps/HeadUnit/shared/utils/someip.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/HeadUnit/shared/utils/someip.cpp b/apps/HeadUnit/shared/utils/someip.cpp index 0dceaf1..81cce7d 100644 --- a/apps/HeadUnit/shared/utils/someip.cpp +++ b/apps/HeadUnit/shared/utils/someip.cpp @@ -19,7 +19,7 @@ void SomeIP::set_gear_data(int gear) { request->set_payload(its_payload); app->send(request); std::cout << "HEAD UNIT : GEAR DATA SENDED" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(90)); return ; } From f9a9a8bc1a335f85862f815e3a3800afd43bd0e0 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 18:37:57 +0100 Subject: [PATCH 44/73] can byte check --- apps/HeadUnit/clients/speed_client/speed_client.cpp | 2 +- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 8 ++++++++ apps/ServiceManager/src/speed-src/speed/speed.cpp | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/HeadUnit/clients/speed_client/speed_client.cpp b/apps/HeadUnit/clients/speed_client/speed_client.cpp index 0a69c42..62753fd 100644 --- a/apps/HeadUnit/clients/speed_client/speed_client.cpp +++ b/apps/HeadUnit/clients/speed_client/speed_client.cpp @@ -10,7 +10,7 @@ SpeedClient::SpeedClient(QObject *parent) bool SpeedClient::init() { if (!app_->init()) { - std::cerr << "Couldn't initialize application" << std::endl; + std::cerr << "speedclient : Couldn't initialize application" << std::endl; return false; } diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 42acef5..54c6c0f 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -69,6 +69,14 @@ float CANReceiver::getSpeed() { // Calculate speed from the first two bytes of the CAN frame data float CANReceiver::calculateSpeed(const struct can_frame* frame) { // Interpret speed as a signed 16-bit integer + std::cout << " 0 : "<data[0] << std::endl; + std::cout << " 1 : "<data[1] << std::endl; + std::cout << " 2 : "<data[2] << std::endl; + std::cout << " 3 : "<data[3] << std::endl; + std::cout << " 4 : "<data[4] << std::endl; + std::cout << " 5 : "<data[5]<< std::endl; + std::cout << " 6 : "<data[6] << std::endl; + std::cout << " 7 : "<data[7]<< std::endl; float speed = static_cast((frame->data[0] << 8) | frame->data[1]); // float speed = 120; std::cout << "Speed: " << speed << std::endl << std::flush; diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index d6847e5..9923308 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -183,6 +183,7 @@ void speedObject::canDataReceive() { canData.canRead(); filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); this->speedData = filtered_speed; + std::cout << "Speed server : in can data receive loop : filtered_speed : " << filtered_speed << std::endl; // if (filtered_speed >= 100.0f) // filtered_speed = 0.0f; // filtered_speed += 0.1; From 877b2987ab087ab0e132c5916a2721ee05812927 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 20:19:14 +0100 Subject: [PATCH 45/73] [feat] can data --- .../src/speed-src/speed/canreceiver.cpp | 100 ++++++++---------- .../src/speed-src/speed/canreceiver.hpp | 28 ++--- .../src/speed-src/speed/speed.cpp | 6 +- speed_sensor/speed_sensor.ino | 75 +++++++------ 4 files changed, 92 insertions(+), 117 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 54c6c0f..d6097af 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -2,83 +2,67 @@ #include "../../headers.hpp" // Constructor: Opens and binds the CAN socket to the specified interface -CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { - openSocket(interface_name); -} - -// Destructor: Closes the CAN socket -CANReceiver::~CANReceiver() { - if (socket_fd != -1) { - close(socket_fd); - std::cout << "CAN socket closed." << std::endl; - } -} +#include "canreceiver.hpp" +#include +#include +#include +#include +#include +#include +#include -// Opens a CAN socket and binds it to the given interface -void CANReceiver::openSocket(const std::string& interface_name) { - // Open the CAN_RAW socket - socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (socket_fd < 0) { - perror("Error while opening CAN socket"); - throw std::runtime_error("Failed to open CAN socket"); +CANReceiver::CANReceiver(const std::string& interface_name) { + if ((socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { + perror("Socket"); + throw std::runtime_error("CAN Socket creation failed"); } - // Get the interface index for the specified CAN interface - std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); + strcpy(ifr.ifr_name, interface_name.c_str()); if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { - perror("Error finding CAN interface"); - close(socket_fd); // Ensure socket is closed before throwing - throw std::runtime_error("Failed to find CAN interface"); + perror("Ioctl"); + close(socket_fd); + throw std::runtime_error("CAN Interface not found"); } addr.can_family = AF_CAN; addr.can_ifindex = ifr.ifr_ifindex; - // Bind the socket to the CAN interface - if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - perror("Error in binding CAN socket"); - close(socket_fd); // Ensure socket is closed before throwing - throw std::runtime_error("Failed to bind CAN socket"); + if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { + perror("Bind"); + close(socket_fd); + throw std::runtime_error("CAN Socket bind failed"); } - std::cout << "CAN socket bound to interface: " << interface_name << std::endl; + std::cout << "CAN Socket bound to interface: " << interface_name << std::endl; +} + +CANReceiver::~CANReceiver() { + if(socket_fd >= 0) close(socket_fd); } -// Check if a CAN frame is available (read from socket) -bool CANReceiver::canRead() { +bool CANReceiver::receive(float& speed) { + struct can_frame frame; int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); + if (nbytes < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - return false; // No data available, not an error - } - perror("Error reading CAN frame"); + perror("CAN Read Error"); return false; } - if (nbytes < sizeof(struct can_frame)) { - std::cerr << "Incomplete CAN frame" << std::endl; + + if (nbytes != sizeof(struct can_frame)) { + std::cerr << "Incomplete CAN frame received." << std::endl; return false; } - return true; -} -// Extract speed from the received CAN frame -float CANReceiver::getSpeed() { - return calculateSpeed(&frame); -} + // CAN ID 체크 (필요에 따라 특정 ID만 처리) + if (frame.can_id != 0x100) { + std::cerr << "Unknown CAN ID: " << frame.can_id << std::endl; + return false; + } -// Calculate speed from the first two bytes of the CAN frame data -float CANReceiver::calculateSpeed(const struct can_frame* frame) { - // Interpret speed as a signed 16-bit integer - std::cout << " 0 : "<data[0] << std::endl; - std::cout << " 1 : "<data[1] << std::endl; - std::cout << " 2 : "<data[2] << std::endl; - std::cout << " 3 : "<data[3] << std::endl; - std::cout << " 4 : "<data[4] << std::endl; - std::cout << " 5 : "<data[5]<< std::endl; - std::cout << " 6 : "<data[6] << std::endl; - std::cout << " 7 : "<data[7]<< std::endl; - float speed = static_cast((frame->data[0] << 8) | frame->data[1]); - // float speed = 120; - std::cout << "Speed: " << speed << std::endl << std::flush; - return speed; + // 데이터 복사 (float 형태로 정확히 처리) + memcpy(&speed, frame.data, sizeof(float)); + + std::cout << "Received Speed: " << speed << " cm/s" << std::endl; + return true; } diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index f660bb0..2b5ae5e 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -1,25 +1,19 @@ #ifndef CANRECEIVER_HPP #define CANRECEIVER_HPP -#include "../../headers.hpp" +#include +#include +#include -// CANReceiver class for handling CAN communication class CANReceiver { -public: - CANReceiver(const std::string& interface_name); // Constructor - ~CANReceiver(); // Destructor - - bool canRead(); // Check if CAN frame is available - float getSpeed(); // Get speed from the CAN frame - private: - int socket_fd; // File descriptor for the CAN socket - struct sockaddr_can addr; // CAN address - struct ifreq ifr; // Interface request structure - struct can_frame frame; // CAN frame structure + int socket_fd; + struct sockaddr_can addr; + struct ifreq ifr; - float calculateSpeed(const struct can_frame* frame); // Calculate speed from CAN frame data - void openSocket(const std::string& interface_name); // Open and bind CAN socket -}; +public: + CANReceiver(const std::string& interface_name); + ~CANReceiver(); -#endif // CANRECEIVER_HPP + bool receive(float& speed); +}; diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 9923308..22dc3e2 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -171,7 +171,7 @@ void speedObject::canDataReceive() { std::cout << "can Data Receive started" << std::endl; float filtered_speed = 0.0f; float weight = 0.6; - CANReceiver canData("can0"); + CANReceiver receiver("can0"); while (running_) { std::unique_lock its_lock(can_mutex_); @@ -180,8 +180,8 @@ void speedObject::canDataReceive() { while (is_offered_ && running_) { { - canData.canRead(); - filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); + receiver.receive(filtered_speed); + // filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); this->speedData = filtered_speed; std::cout << "Speed server : in can data receive loop : filtered_speed : " << filtered_speed << std::endl; // if (filtered_speed >= 100.0f) diff --git a/speed_sensor/speed_sensor.ino b/speed_sensor/speed_sensor.ino index 3a5bebd..b6df930 100644 --- a/speed_sensor/speed_sensor.ino +++ b/speed_sensor/speed_sensor.ino @@ -2,60 +2,57 @@ #include "mcp2515_can.h" #define sensor 3 -const int SPI_CS_PIN = 9; // default is 9 -mcp2515_can CAN(SPI_CS_PIN); // Set CS pin - +const int SPI_CS_PIN = 9; +mcp2515_can CAN(SPI_CS_PIN); volatile unsigned int pulseCount = 0; -unsigned long currentMillis = 0; -unsigned long lastMillis = 0; -const long interval = 100; -unsigned char data[8]; +unsigned long currentMillis, previousMillis = 0; +const unsigned long interval = 1000; // 1초 단위 측정 (필요에 따라 조정 가능) + +void countPulses() { + pulseCount++; +} void setup() { - SERIAL_PORT_MONITOR.begin(115200); + Serial.begin(115200); - // init can bus : baudrate = 500k always keep it same to the Rpi CAN interface + // CAN 초기화 while (CAN_OK != CAN.begin(CAN_500KBPS)) { - SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); - delay(100); + Serial.println("CAN init fail, retry..."); + delay(100); } - SERIAL_PORT_MONITOR.println("CAN init ok!"); + Serial.println("CAN init ok!"); + // 센서 핀 설정 pinMode(sensor, INPUT); attachInterrupt(digitalPinToInterrupt(sensor), countPulses, RISING); } void loop() { - - currentMillis = millis(); - - if (currentMillis - lastMillis >= interval) { - - lastMillis = currentMillis; - - float rpm = getRPM(); + static unsigned long previousMillis = 0; + unsigned long currentMillis = millis(); + + const int wheelHoles = 20; + static unsigned int pulseSnapshot = 0; + + if (currentMillis - pulseSnapshot >= 1000) { // 1초마다 RPM 계산 + noInterrupts(); // 인터럽트 임시 중지 + unsigned int pulses = pulseCount; + pulseCount = 0; // 펄스 카운트 초기화 + interrupts(); // 인터럽트 재개 + + float rotations = pulses / (float)wheelHoles; + float rpm = rotations * 60.0; // rotations per second (1초 주기) + + Serial.print("RPM: "); Serial.println(rpm); - pulseCount = 0; - memcpy(data, &rpm, sizeof(rpm)); - CAN.sendMsgBuf(0x7, 0, 4, data); // Send the data on the CAN bus + // CAN으로 전송 + byte data[4]; + memcpy(data, &rpm, sizeof(float)); + CAN.sendMsgBuf(0x7, 0, sizeof(float), data); + Serial.println("CAN BUS sendMsgBuf ok!"); - SERIAL_PORT_MONITOR.println("CAN BUS sendMsgBuf ok!"); // Print a success message + pulseSnapshot = currentMillis; } } - -void countPulses() { - pulseCount++; -} - -float getRPM() { - int wheelHoles = 20; // Number of holes in the wheel - float seconds = interval / 1000.0; // Convert interval from milliseconds to seconds - - float rotations = (float)pulseCount / wheelHoles; // Calculate number of rotations - float rotationsPerSecond = rotations / seconds; // Calculate rotations per second - float rpm = rotationsPerSecond * 60; // Convert to RPM - - return rpm; -} From e46656f11c4cb3fa02b6b315aac7dac0a505bd62 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 20:20:51 +0100 Subject: [PATCH 46/73] 1 --- apps/ServiceManager/src/speed-src/speed/canreceiver.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index 2b5ae5e..1c0b2f6 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -17,3 +17,5 @@ class CANReceiver { bool receive(float& speed); }; + +#endif \ No newline at end of file From efe26019870f2dbafba2a3de2b8550ea94aa7871 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 20:22:34 +0100 Subject: [PATCH 47/73] 2 --- apps/ServiceManager/src/speed-src/speed/canreceiver.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index 1c0b2f6..5f70822 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -4,7 +4,7 @@ #include #include #include - +#include "../../headers.hpp" class CANReceiver { private: int socket_fd; From e5c0204319b2f8fb04b7974fd2c988f1cdb30337 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 20:32:52 +0100 Subject: [PATCH 48/73] 3 --- .../src/speed-src/speed/canreceiver.cpp | 8 +++--- .../src/speed-src/speed/canreceiver.hpp | 28 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index d6097af..2346431 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -55,10 +55,10 @@ bool CANReceiver::receive(float& speed) { } // CAN ID 체크 (필요에 따라 특정 ID만 처리) - if (frame.can_id != 0x100) { - std::cerr << "Unknown CAN ID: " << frame.can_id << std::endl; - return false; - } + // if (frame.can_id != 0x100) { + // std::cerr << "Unknown CAN ID: " << frame.can_id << std::endl; + // return false; + // } // 데이터 복사 (float 형태로 정확히 처리) memcpy(&speed, frame.data, sizeof(float)); diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index 5f70822..f660bb0 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -1,21 +1,25 @@ #ifndef CANRECEIVER_HPP #define CANRECEIVER_HPP -#include -#include -#include #include "../../headers.hpp" -class CANReceiver { -private: - int socket_fd; - struct sockaddr_can addr; - struct ifreq ifr; +// CANReceiver class for handling CAN communication +class CANReceiver { public: - CANReceiver(const std::string& interface_name); - ~CANReceiver(); + CANReceiver(const std::string& interface_name); // Constructor + ~CANReceiver(); // Destructor + + bool canRead(); // Check if CAN frame is available + float getSpeed(); // Get speed from the CAN frame + +private: + int socket_fd; // File descriptor for the CAN socket + struct sockaddr_can addr; // CAN address + struct ifreq ifr; // Interface request structure + struct can_frame frame; // CAN frame structure - bool receive(float& speed); + float calculateSpeed(const struct can_frame* frame); // Calculate speed from CAN frame data + void openSocket(const std::string& interface_name); // Open and bind CAN socket }; -#endif \ No newline at end of file +#endif // CANRECEIVER_HPP From 34eae163fe57fc9efd3c3bf4dc64f43256fff44e Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 20:33:39 +0100 Subject: [PATCH 49/73] 4 --- .../src/speed-src/speed/canreceiver.hpp | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index f660bb0..5f70822 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -1,25 +1,21 @@ #ifndef CANRECEIVER_HPP #define CANRECEIVER_HPP +#include +#include +#include #include "../../headers.hpp" - -// CANReceiver class for handling CAN communication class CANReceiver { -public: - CANReceiver(const std::string& interface_name); // Constructor - ~CANReceiver(); // Destructor - - bool canRead(); // Check if CAN frame is available - float getSpeed(); // Get speed from the CAN frame - private: - int socket_fd; // File descriptor for the CAN socket - struct sockaddr_can addr; // CAN address - struct ifreq ifr; // Interface request structure - struct can_frame frame; // CAN frame structure + int socket_fd; + struct sockaddr_can addr; + struct ifreq ifr; + +public: + CANReceiver(const std::string& interface_name); + ~CANReceiver(); - float calculateSpeed(const struct can_frame* frame); // Calculate speed from CAN frame data - void openSocket(const std::string& interface_name); // Open and bind CAN socket + bool receive(float& speed); }; -#endif // CANRECEIVER_HPP +#endif \ No newline at end of file From 173a093fac9b93e096b365be934fa41015f95d91 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 21:22:51 +0100 Subject: [PATCH 50/73] 6 --- .../src/speed-src/speed/canreceiver.cpp | 117 +++++++++++------- .../src/speed-src/speed/canreceiver.hpp | 28 +++-- 2 files changed, 87 insertions(+), 58 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 2346431..4024226 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -2,67 +2,92 @@ #include "../../headers.hpp" // Constructor: Opens and binds the CAN socket to the specified interface -#include "canreceiver.hpp" -#include -#include -#include -#include -#include -#include -#include - -CANReceiver::CANReceiver(const std::string& interface_name) { - if ((socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { - perror("Socket"); - throw std::runtime_error("CAN Socket creation failed"); - } +CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { + openSocket(interface_name); +} - strcpy(ifr.ifr_name, interface_name.c_str()); - if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { - perror("Ioctl"); +// Destructor: Closes the CAN socket +CANReceiver::~CANReceiver() { + if (socket_fd != -1) { close(socket_fd); - throw std::runtime_error("CAN Interface not found"); + std::cout << "CAN socket closed." << std::endl; } +} - addr.can_family = AF_CAN; - addr.can_ifindex = ifr.ifr_ifindex; +// Opens a CAN socket and binds it to the given interface +void CANReceiver::openSocket(const std::string& interface_name) { + // Open the CAN_RAW socket - if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { - perror("Bind"); - close(socket_fd); - throw std::runtime_error("CAN Socket bind failed"); - } + try { + socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); + if (socket_fd < 0) { + perror("Error while opening CAN socket"); + throw std::runtime_error("Failed to open CAN socket"); + } - std::cout << "CAN Socket bound to interface: " << interface_name << std::endl; -} + // Get the interface index for the specified CAN interface + std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); + if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { + perror("Error finding CAN interface"); + close(socket_fd); // Ensure socket is closed before throwing + throw std::runtime_error("Failed to find CAN interface"); + } -CANReceiver::~CANReceiver() { - if(socket_fd >= 0) close(socket_fd); + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + + // Bind the socket to the CAN interface + if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + perror("Error in binding CAN socket"); + close(socket_fd); // Ensure socket is closed before throwing + throw std::runtime_error("Failed to bind CAN socket"); + } + } catch (char &e) { + std::cout << e << std::endl; + } + + std::cout << "CAN socket bound to interface: " << interface_name << std::endl; } -bool CANReceiver::receive(float& speed) { - struct can_frame frame; +// Check if a CAN frame is available (read from socket) +bool CANReceiver::canRead() { int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); - if (nbytes < 0) { - perror("CAN Read Error"); + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return false; // No data available, not an error + } + perror("Error reading CAN frame"); return false; } - - if (nbytes != sizeof(struct can_frame)) { - std::cerr << "Incomplete CAN frame received." << std::endl; + if (nbytes < sizeof(struct can_frame)) { + std::cerr << "Incomplete CAN frame" << std::endl; return false; } + return true; +} - // CAN ID 체크 (필요에 따라 특정 ID만 처리) - // if (frame.can_id != 0x100) { - // std::cerr << "Unknown CAN ID: " << frame.can_id << std::endl; - // return false; - // } +// Extract speed from the received CAN frame +float CANReceiver::getSpeed() { + return calculateSpeed(&frame); +} - // 데이터 복사 (float 형태로 정확히 처리) - memcpy(&speed, frame.data, sizeof(float)); +// Calculate speed from the first two bytes of the CAN frame data +float CANReceiver::calculateSpeed(const struct can_frame* frame) { + // Interpret speed as a signed 16-bit integer + float speed=0; + memcpy (&speed, &frame->data, sizeof(float)); + std::cout << "memcpy value : " <data[0] << std::endl; + std::cout << " 1 : "<data[1] << std::endl; + std::cout << " 2 : "<data[2] << std::endl; + std::cout << " 3 : "<data[3] << std::endl; + std::cout << " 4 : "<data[4] << std::endl; + std::cout << " 5 : "<data[5]<< std::endl; + std::cout << " 6 : "<data[6] << std::endl; + std::cout << " 7 : "<data[7]<< std::endl; + float speed = static_cast((frame->data[0] << 8) | frame->data[1]); + // float speed = 120; + std::cout << "Speed: " << speed << std::endl << std::flush; + return speed; +} \ No newline at end of file diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index 5f70822..f660bb0 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -1,21 +1,25 @@ #ifndef CANRECEIVER_HPP #define CANRECEIVER_HPP -#include -#include -#include #include "../../headers.hpp" -class CANReceiver { -private: - int socket_fd; - struct sockaddr_can addr; - struct ifreq ifr; +// CANReceiver class for handling CAN communication +class CANReceiver { public: - CANReceiver(const std::string& interface_name); - ~CANReceiver(); + CANReceiver(const std::string& interface_name); // Constructor + ~CANReceiver(); // Destructor + + bool canRead(); // Check if CAN frame is available + float getSpeed(); // Get speed from the CAN frame + +private: + int socket_fd; // File descriptor for the CAN socket + struct sockaddr_can addr; // CAN address + struct ifreq ifr; // Interface request structure + struct can_frame frame; // CAN frame structure - bool receive(float& speed); + float calculateSpeed(const struct can_frame* frame); // Calculate speed from CAN frame data + void openSocket(const std::string& interface_name); // Open and bind CAN socket }; -#endif \ No newline at end of file +#endif // CANRECEIVER_HPP From e614bdd9bf622cf56de1fc1623c6b355997e4f6a Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 21:25:06 +0100 Subject: [PATCH 51/73] 7 --- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 4 ++-- apps/ServiceManager/src/speed-src/speed/speed.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 4024226..e4d6817 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -76,7 +76,7 @@ float CANReceiver::calculateSpeed(const struct can_frame* frame) { // Interpret speed as a signed 16-bit integer float speed=0; memcpy (&speed, &frame->data, sizeof(float)); - std::cout << "memcpy value : " <data[0] << std::endl; std::cout << " 1 : "<data[1] << std::endl; @@ -86,7 +86,7 @@ float CANReceiver::calculateSpeed(const struct can_frame* frame) { std::cout << " 5 : "<data[5]<< std::endl; std::cout << " 6 : "<data[6] << std::endl; std::cout << " 7 : "<data[7]<< std::endl; - float speed = static_cast((frame->data[0] << 8) | frame->data[1]); + // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); // float speed = 120; std::cout << "Speed: " << speed << std::endl << std::flush; return speed; diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 22dc3e2..4beda92 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -180,7 +180,8 @@ void speedObject::canDataReceive() { while (is_offered_ && running_) { { - receiver.receive(filtered_speed); + receiver.canRead(); + filtered_speed = receiver.getSpeed(); // filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); this->speedData = filtered_speed; std::cout << "Speed server : in can data receive loop : filtered_speed : " << filtered_speed << std::endl; From 530d544df75a337f7b5cd40cab5bcae8a16eeb64 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Mon, 17 Mar 2025 21:40:52 +0100 Subject: [PATCH 52/73] 7 --- .../src/speed-src/speed/canreceiver.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index e4d6817..55c643f 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -75,17 +75,14 @@ float CANReceiver::getSpeed() { float CANReceiver::calculateSpeed(const struct can_frame* frame) { // Interpret speed as a signed 16-bit integer float speed=0; - memcpy (&speed, &frame->data, sizeof(float)); + memcpy (&speed, frame->data, sizeof(float)); // std::cout << "memcpy value : " <data[0] << std::endl; - std::cout << " 1 : "<data[1] << std::endl; - std::cout << " 2 : "<data[2] << std::endl; - std::cout << " 3 : "<data[3] << std::endl; - std::cout << " 4 : "<data[4] << std::endl; - std::cout << " 5 : "<data[5]<< std::endl; - std::cout << " 6 : "<data[6] << std::endl; - std::cout << " 7 : "<data[7]<< std::endl; + std::cout << " 0 : "<(frame->data[0]) << std::endl; + std::cout << " 1 : "<(frame->data[1]) << std::endl; + std::cout << " 2 : "<(frame->data[2]) << std::endl; + std::cout << " 3 : "<(frame->data[3]) << std::endl; + // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); // float speed = 120; std::cout << "Speed: " << speed << std::endl << std::flush; From 34afda9335ea4ff5229410991dc877e7aa2d72e9 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:07:58 +0100 Subject: [PATCH 53/73] 8 --- .../src/speed-src/speed/canreceiver.cpp | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 55c643f..c19b728 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -1,5 +1,7 @@ #include "./canreceiver.hpp" #include "../../headers.hpp" +#include "mcp2515_can.h" +#include // Constructor: Opens and binds the CAN socket to the specified interface CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { @@ -17,7 +19,6 @@ CANReceiver::~CANReceiver() { // Opens a CAN socket and binds it to the given interface void CANReceiver::openSocket(const std::string& interface_name) { // Open the CAN_RAW socket - try { socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (socket_fd < 0) { @@ -52,6 +53,15 @@ void CANReceiver::openSocket(const std::string& interface_name) { // Check if a CAN frame is available (read from socket) bool CANReceiver::canRead() { int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); + std::cout << "nbytes : " << nbytes << std::endl; + if (nbytes == sizeof(struct can_frame)) { + if(frame.can_id == 0x7 && frame.can_dlc == 4) { + float rpm; + memcpy(&rpm, frame.data, sizeof(float)); + std::cout << "received RPM value : " << rpm << std::endl; + } + } + if (nbytes < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { return false; // No data available, not an error @@ -75,13 +85,14 @@ float CANReceiver::getSpeed() { float CANReceiver::calculateSpeed(const struct can_frame* frame) { // Interpret speed as a signed 16-bit integer float speed=0; - memcpy (&speed, frame->data, sizeof(float)); + // memcpy (&speed, frame->data, sizeof(float)); + memcpy (&speed, frame.data, sizeof(float)); // std::cout << "memcpy value : " <(frame->data[0]) << std::endl; - std::cout << " 1 : "<(frame->data[1]) << std::endl; - std::cout << " 2 : "<(frame->data[2]) << std::endl; - std::cout << " 3 : "<(frame->data[3]) << std::endl; + // std::cout << " 0 : "<(frame->data[0]) << std::endl; + // std::cout << " 1 : "<(frame->data[1]) << std::endl; + // std::cout << " 2 : "<(frame->data[2]) << std::endl; + // std::cout << " 3 : "<(frame->data[3]) << std::endl; // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); // float speed = 120; From 8f27ea4d3f7ec7714b2ed8ed9fc3a68ba754fd8a Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:10:32 +0100 Subject: [PATCH 54/73] 9 --- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index c19b728..4c5bc5f 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -1,6 +1,6 @@ #include "./canreceiver.hpp" #include "../../headers.hpp" -#include "mcp2515_can.h" +// #include "mcp2515_can.h" #include // Constructor: Opens and binds the CAN socket to the specified interface From 76b0bee89c5cd281f09ffcf5c8d7f55be178f9e6 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:11:48 +0100 Subject: [PATCH 55/73] 10 --- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 4c5bc5f..aa3450f 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -1,7 +1,7 @@ #include "./canreceiver.hpp" #include "../../headers.hpp" // #include "mcp2515_can.h" -#include +// #include // Constructor: Opens and binds the CAN socket to the specified interface CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { From 3a149eb0ec64138525764f5073c2f33966ba9573 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:12:30 +0100 Subject: [PATCH 56/73] 11 --- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index aa3450f..21642d1 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -86,7 +86,7 @@ float CANReceiver::calculateSpeed(const struct can_frame* frame) { // Interpret speed as a signed 16-bit integer float speed=0; // memcpy (&speed, frame->data, sizeof(float)); - memcpy (&speed, frame.data, sizeof(float)); + memcpy (&speed, frame->data, sizeof(float)); // std::cout << "memcpy value : " <(frame->data[0]) << std::endl; From d301c23edd0e745f2cfe4b23b164a45c52d05816 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:26:18 +0100 Subject: [PATCH 57/73] 12 --- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 21642d1..beef33a 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -88,11 +88,8 @@ float CANReceiver::calculateSpeed(const struct can_frame* frame) { // memcpy (&speed, frame->data, sizeof(float)); memcpy (&speed, frame->data, sizeof(float)); // std::cout << "memcpy value : " <(frame->data[0]) << std::endl; - // std::cout << " 1 : "<(frame->data[1]) << std::endl; - // std::cout << " 2 : "<(frame->data[2]) << std::endl; - // std::cout << " 3 : "<(frame->data[3]) << std::endl; + for (int i = 0; i < 16; i++) + std::cout << " 0 : "<(frame->data[i]) << std::endl; // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); // float speed = 120; From 331cc08fb3ec6e468d1204a3d01621fd50136ccf Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:37:22 +0100 Subject: [PATCH 58/73] 13 --- .../src/speed-src/speed/canreceiver.cpp | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index beef33a..ba274bd 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -55,25 +55,51 @@ bool CANReceiver::canRead() { int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); std::cout << "nbytes : " << nbytes << std::endl; if (nbytes == sizeof(struct can_frame)) { + std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec << "\n"; + std::cout << "DLC: " << static_cast(frame.can_dlc) << "\n"; + + std::cout << "CAN Data: "; + for (int i = 0; i < frame.can_dlc; i++) + printf("%02X ", frame.data[i]); + std::cout << std::endl; + if(frame.can_id == 0x7 && frame.can_dlc == 4) { - float rpm; + float rpm = 0.0f; memcpy(&rpm, frame.data, sizeof(float)); - std::cout << "received RPM value : " << rpm << std::endl; - } - } - - if (nbytes < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - return false; // No data available, not an error + std::cout << "RPM: " << rpm << "\n"; + } else { + std::cout << "Received frame with unexpected CAN ID or DLC.\n"; + return false; } - perror("Error reading CAN frame"); + return true; + } else if (nbytes < 0) { + perror("CAN read failed"); return false; - } - if (nbytes < sizeof(struct can_frame)) { - std::cerr << "Incomplete CAN frame" << std::endl; + } else { + std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; return false; } - return true; + + // if (nbytes == sizeof(struct can_frame)) { + // if(frame.can_id == 0x7 && frame.can_dlc == 4) { + // float rpm; + // memcpy(&rpm, frame.data, sizeof(float)); + // std::cout << "received RPM value : " << rpm << std::endl; + // } + // } + + // if (nbytes < 0) { + // if (errno == EAGAIN || errno == EWOULDBLOCK) { + // return false; // No data available, not an error + // } + // perror("Error reading CAN frame"); + // return false; + // } + // if (nbytes < sizeof(struct can_frame)) { + // std::cerr << "Incomplete CAN frame" << std::endl; + // return false; + // } + // return true; } // Extract speed from the received CAN frame @@ -88,8 +114,8 @@ float CANReceiver::calculateSpeed(const struct can_frame* frame) { // memcpy (&speed, frame->data, sizeof(float)); memcpy (&speed, frame->data, sizeof(float)); // std::cout << "memcpy value : " <(frame->data[i]) << std::endl; + // for (int i = 0; i < 16; i++) + // std::cout << i << " : "<(frame->data[i]) << std::endl; // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); // float speed = 120; From f66c6173f06680ccafd960a4c73d1b77842545f9 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:51:28 +0100 Subject: [PATCH 59/73] 14 --- .../src/speed-src/speed/canreceiver.cpp | 262 ++++++++++++------ .../src/speed-src/speed/speed.cpp | 2 +- 2 files changed, 175 insertions(+), 89 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index ba274bd..dc9dd86 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -1,7 +1,7 @@ #include "./canreceiver.hpp" #include "../../headers.hpp" -// #include "mcp2515_can.h" -// #include + +#include "canreceiver.hpp" // Constructor: Opens and binds the CAN socket to the specified interface CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { @@ -18,33 +18,32 @@ CANReceiver::~CANReceiver() { // Opens a CAN socket and binds it to the given interface void CANReceiver::openSocket(const std::string& interface_name) { - // Open the CAN_RAW socket - try { - socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (socket_fd < 0) { - perror("Error while opening CAN socket"); - throw std::runtime_error("Failed to open CAN socket"); - } - - // Get the interface index for the specified CAN interface - std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); - if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { - perror("Error finding CAN interface"); - close(socket_fd); // Ensure socket is closed before throwing - throw std::runtime_error("Failed to find CAN interface"); - } - - addr.can_family = AF_CAN; - addr.can_ifindex = ifr.ifr_ifindex; - - // Bind the socket to the CAN interface - if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - perror("Error in binding CAN socket"); - close(socket_fd); // Ensure socket is closed before throwing - throw std::runtime_error("Failed to bind CAN socket"); - } - } catch (char &e) { - std::cout << e << std::endl; + socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); + if (socket_fd < 0) { + perror("Socket"); + throw std::runtime_error("Failed to open CAN socket"); + } + + std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); + if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { + perror("Ioctl"); + close(socket_fd); + throw std::runtime_error("Failed to get CAN interface index"); + } + + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + + // 필터 설정 (CAN ID = 0x7만 수신) + struct can_filter rfilter[1]; + rfilter[0].can_id = 0x7; + rfilter[0].can_mask = CAN_SFF_MASK; + setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); + + if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { + perror("Bind"); + close(socket_fd); + throw std::runtime_error("Failed to bind CAN socket"); } std::cout << "CAN socket bound to interface: " << interface_name << std::endl; @@ -53,72 +52,159 @@ void CANReceiver::openSocket(const std::string& interface_name) { // Check if a CAN frame is available (read from socket) bool CANReceiver::canRead() { int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); - std::cout << "nbytes : " << nbytes << std::endl; - if (nbytes == sizeof(struct can_frame)) { - std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec << "\n"; - std::cout << "DLC: " << static_cast(frame.can_dlc) << "\n"; - - std::cout << "CAN Data: "; - for (int i = 0; i < frame.can_dlc; i++) - printf("%02X ", frame.data[i]); - std::cout << std::endl; - - if(frame.can_id == 0x7 && frame.can_dlc == 4) { - float rpm = 0.0f; - memcpy(&rpm, frame.data, sizeof(float)); - std::cout << "RPM: " << rpm << "\n"; - } else { - std::cout << "Received frame with unexpected CAN ID or DLC.\n"; - return false; - } - return true; - } else if (nbytes < 0) { + + if (nbytes < 0) { perror("CAN read failed"); return false; - } else { + } + + if (nbytes != sizeof(struct can_frame)) { std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; return false; } - - // if (nbytes == sizeof(struct can_frame)) { - // if(frame.can_id == 0x7 && frame.can_dlc == 4) { - // float rpm; - // memcpy(&rpm, frame.data, sizeof(float)); - // std::cout << "received RPM value : " << rpm << std::endl; - // } - // } - - // if (nbytes < 0) { - // if (errno == EAGAIN || errno == EWOULDBLOCK) { - // return false; // No data available, not an error - // } - // perror("Error reading CAN frame"); - // return false; - // } - // if (nbytes < sizeof(struct can_frame)) { - // std::cerr << "Incomplete CAN frame" << std::endl; - // return false; - // } - // return true; + + if(frame.can_id == 0x7 && frame.can_dlc == 4) { + float rpm = calculateSpeed(&frame); + std::cout << "Received RPM: " << rpm << std::endl; + return true; + } else { + std::cerr << "Received frame with unexpected CAN ID or DLC." << std::endl; + return false; + } } -// Extract speed from the received CAN frame -float CANReceiver::getSpeed() { +// Return speed value from the received CAN frame +float CANReceiver::getSpeed() const { return calculateSpeed(&frame); } -// Calculate speed from the first two bytes of the CAN frame data -float CANReceiver::calculateSpeed(const struct can_frame* frame) { - // Interpret speed as a signed 16-bit integer - float speed=0; - // memcpy (&speed, frame->data, sizeof(float)); - memcpy (&speed, frame->data, sizeof(float)); - // std::cout << "memcpy value : " <(frame->data[i]) << std::endl; - - // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); - // float speed = 120; - std::cout << "Speed: " << speed << std::endl << std::flush; - return speed; -} \ No newline at end of file +// Calculate speed (RPM) from CAN frame data +float CANReceiver::calculateSpeed(const struct can_frame* frame) const { + float rpm = 0.0f; + memcpy(&rpm, frame->data, sizeof(float)); + return rpm; +} +//################# + +// // #include "mcp2515_can.h" +// // #include + +// // Constructor: Opens and binds the CAN socket to the specified interface +// CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { +// openSocket(interface_name); +// } + +// // Destructor: Closes the CAN socket +// CANReceiver::~CANReceiver() { +// if (socket_fd != -1) { +// close(socket_fd); +// std::cout << "CAN socket closed." << std::endl; +// } +// } + +// // Opens a CAN socket and binds it to the given interface +// void CANReceiver::openSocket(const std::string& interface_name) { +// // Open the CAN_RAW socket +// try { +// socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); +// if (socket_fd < 0) { +// perror("Error while opening CAN socket"); +// throw std::runtime_error("Failed to open CAN socket"); +// } + +// // Get the interface index for the specified CAN interface +// std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); +// if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { +// perror("Error finding CAN interface"); +// close(socket_fd); // Ensure socket is closed before throwing +// throw std::runtime_error("Failed to find CAN interface"); +// } + +// addr.can_family = AF_CAN; +// addr.can_ifindex = ifr.ifr_ifindex; + +// // Bind the socket to the CAN interface +// if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { +// perror("Error in binding CAN socket"); +// close(socket_fd); // Ensure socket is closed before throwing +// throw std::runtime_error("Failed to bind CAN socket"); +// } +// } catch (char &e) { +// std::cout << e << std::endl; +// } + +// std::cout << "CAN socket bound to interface: " << interface_name << std::endl; +// } + +// // Check if a CAN frame is available (read from socket) +// bool CANReceiver::canRead() { +// int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); +// std::cout << "nbytes : " << nbytes << std::endl; +// if (nbytes == sizeof(struct can_frame)) { +// std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec << "\n"; +// std::cout << "DLC: " << static_cast(frame.can_dlc) << "\n"; + +// std::cout << "CAN Data: "; +// for (int i = 0; i < frame.can_dlc; i++) +// printf("%02X ", frame.data[i]); +// std::cout << std::endl; + +// if(frame.can_id == 0x7 && frame.can_dlc == 4) { +// float rpm = 0.0f; +// memcpy(&rpm, frame.data, sizeof(float)); +// std::cout << "RPM: " << rpm << "\n"; +// } else { +// std::cout << "Received frame with unexpected CAN ID or DLC.\n"; +// return false; +// } +// return true; +// } else if (nbytes < 0) { +// perror("CAN read failed"); +// return false; +// } else { +// std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; +// return false; +// } + +// // if (nbytes == sizeof(struct can_frame)) { +// // if(frame.can_id == 0x7 && frame.can_dlc == 4) { +// // float rpm; +// // memcpy(&rpm, frame.data, sizeof(float)); +// // std::cout << "received RPM value : " << rpm << std::endl; +// // } +// // } + +// // if (nbytes < 0) { +// // if (errno == EAGAIN || errno == EWOULDBLOCK) { +// // return false; // No data available, not an error +// // } +// // perror("Error reading CAN frame"); +// // return false; +// // } +// // if (nbytes < sizeof(struct can_frame)) { +// // std::cerr << "Incomplete CAN frame" << std::endl; +// // return false; +// // } +// // return true; +// } + +// // Extract speed from the received CAN frame +// float CANReceiver::getSpeed() { +// return calculateSpeed(&frame); +// } + +// // Calculate speed from the first two bytes of the CAN frame data +// float CANReceiver::calculateSpeed(const struct can_frame* frame) { +// // Interpret speed as a signed 16-bit integer +// float speed=0; +// // memcpy (&speed, frame->data, sizeof(float)); +// memcpy (&speed, frame->data, sizeof(float)); +// // std::cout << "memcpy value : " <(frame->data[i]) << std::endl; + +// // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); +// // float speed = 120; +// std::cout << "Speed: " << speed << std::endl << std::flush; +// return speed; +// } \ No newline at end of file diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 4beda92..0657fd1 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -184,7 +184,7 @@ void speedObject::canDataReceive() { filtered_speed = receiver.getSpeed(); // filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); this->speedData = filtered_speed; - std::cout << "Speed server : in can data receive loop : filtered_speed : " << filtered_speed << std::endl; + std::cout << "[Speed server] in can data receive loop : filtered_speed : " << filtered_speed << std::endl; // if (filtered_speed >= 100.0f) // filtered_speed = 0.0f; // filtered_speed += 0.1; From aeff5c8b69564e613efc0fbe702ed7dff11620dd Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 15:53:54 +0100 Subject: [PATCH 60/73] 14 --- .../src/speed-src/speed/canreceiver.hpp | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index f660bb0..d5ec1ca 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -1,25 +1,57 @@ +// #ifndef CANRECEIVER_HPP +// #define CANRECEIVER_HPP + + +// // CANReceiver class for handling CAN communication +// class CANReceiver { + // public: + // CANReceiver(const std::string& interface_name); // Constructor + // ~CANReceiver(); // Destructor + + // bool canRead(); // Check if CAN frame is available + // float getSpeed(); // Get speed from the CAN frame + + // private: + // int socket_fd; // File descriptor for the CAN socket + // struct sockaddr_can addr; // CAN address + // struct ifreq ifr; // Interface request structure + // struct can_frame frame; // CAN frame structure + + // float calculateSpeed(const struct can_frame* frame) const ; // Calculate speed from CAN frame data + // void openSocket(const std::string& interface_name); // Open and bind CAN socket + // }; + +// #endif // CANRECEIVER_HPP #ifndef CANRECEIVER_HPP #define CANRECEIVER_HPP #include "../../headers.hpp" +#include +#include +#include +#include +#include +#include +#include +#include -// CANReceiver class for handling CAN communication class CANReceiver { +private: + int socket_fd; + struct sockaddr_can addr; + struct ifreq ifr; + struct can_frame frame; + public: - CANReceiver(const std::string& interface_name); // Constructor - ~CANReceiver(); // Destructor + CANReceiver(const std::string& interface_name); + ~CANReceiver(); - bool canRead(); // Check if CAN frame is available - float getSpeed(); // Get speed from the CAN frame + bool canRead(); + float getSpeed() const; private: - int socket_fd; // File descriptor for the CAN socket - struct sockaddr_can addr; // CAN address - struct ifreq ifr; // Interface request structure - struct can_frame frame; // CAN frame structure - - float calculateSpeed(const struct can_frame* frame); // Calculate speed from CAN frame data - void openSocket(const std::string& interface_name); // Open and bind CAN socket + float calculateSpeed(const struct can_frame* frame) const; + void openSocket(const std::string& interface_name); }; #endif // CANRECEIVER_HPP From f580ffff5da5265bc527e8fa475ff0c470caec4f Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 16:09:24 +0100 Subject: [PATCH 61/73] 15 --- .../src/speed-src/speed/canreceiver.cpp | 254 +++++++++++++----- .../src/speed-src/speed/canreceiver.hpp | 2 +- 2 files changed, 186 insertions(+), 70 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index dc9dd86..0931909 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -3,87 +3,87 @@ #include "canreceiver.hpp" -// Constructor: Opens and binds the CAN socket to the specified interface -CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { - openSocket(interface_name); -} +// // Constructor: Opens and binds the CAN socket to the specified interface +// CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { +// openSocket(interface_name); +// } -// Destructor: Closes the CAN socket -CANReceiver::~CANReceiver() { - if (socket_fd != -1) { - close(socket_fd); - std::cout << "CAN socket closed." << std::endl; - } -} +// // Destructor: Closes the CAN socket +// CANReceiver::~CANReceiver() { +// if (socket_fd != -1) { +// close(socket_fd); +// std::cout << "CAN socket closed." << std::endl; +// } +// } -// Opens a CAN socket and binds it to the given interface -void CANReceiver::openSocket(const std::string& interface_name) { - socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (socket_fd < 0) { - perror("Socket"); - throw std::runtime_error("Failed to open CAN socket"); - } +// // Opens a CAN socket and binds it to the given interface +// void CANReceiver::openSocket(const std::string& interface_name) { +// socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); +// if (socket_fd < 0) { +// perror("Socket"); +// throw std::runtime_error("Failed to open CAN socket"); +// } - std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); - if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { - perror("Ioctl"); - close(socket_fd); - throw std::runtime_error("Failed to get CAN interface index"); - } +// std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); +// if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { +// perror("Ioctl"); +// close(socket_fd); +// throw std::runtime_error("Failed to get CAN interface index"); +// } - addr.can_family = AF_CAN; - addr.can_ifindex = ifr.ifr_ifindex; +// addr.can_family = AF_CAN; +// addr.can_ifindex = ifr.ifr_ifindex; - // 필터 설정 (CAN ID = 0x7만 수신) - struct can_filter rfilter[1]; - rfilter[0].can_id = 0x7; - rfilter[0].can_mask = CAN_SFF_MASK; - setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); +// // 필터 설정 (CAN ID = 0x7만 수신) +// struct can_filter rfilter[1]; +// rfilter[0].can_id = 0x7; +// rfilter[0].can_mask = CAN_SFF_MASK; +// setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); - if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { - perror("Bind"); - close(socket_fd); - throw std::runtime_error("Failed to bind CAN socket"); - } +// if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { +// perror("Bind"); +// close(socket_fd); +// throw std::runtime_error("Failed to bind CAN socket"); +// } - std::cout << "CAN socket bound to interface: " << interface_name << std::endl; -} +// std::cout << "CAN socket bound to interface: " << interface_name << std::endl; +// } -// Check if a CAN frame is available (read from socket) -bool CANReceiver::canRead() { - int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); +// // Check if a CAN frame is available (read from socket) +// bool CANReceiver::canRead() { +// int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); - if (nbytes < 0) { - perror("CAN read failed"); - return false; - } +// if (nbytes < 0) { +// perror("CAN read failed"); +// return false; +// } - if (nbytes != sizeof(struct can_frame)) { - std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; - return false; - } +// if (nbytes != sizeof(struct can_frame)) { +// std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; +// return false; +// } - if(frame.can_id == 0x7 && frame.can_dlc == 4) { - float rpm = calculateSpeed(&frame); - std::cout << "Received RPM: " << rpm << std::endl; - return true; - } else { - std::cerr << "Received frame with unexpected CAN ID or DLC." << std::endl; - return false; - } -} +// if(frame.can_id == 0x7 && frame.can_dlc == 4) { +// float rpm = calculateSpeed(&frame); +// std::cout << "Received RPM: " << rpm << std::endl; +// return true; +// } else { +// std::cerr << "Received frame with unexpected CAN ID or DLC." << std::endl; +// return false; +// } +// } -// Return speed value from the received CAN frame -float CANReceiver::getSpeed() const { - return calculateSpeed(&frame); -} +// // Return speed value from the received CAN frame +// float CANReceiver::getSpeed() const { +// return calculateSpeed(&frame); +// } -// Calculate speed (RPM) from CAN frame data -float CANReceiver::calculateSpeed(const struct can_frame* frame) const { - float rpm = 0.0f; - memcpy(&rpm, frame->data, sizeof(float)); - return rpm; -} +// // Calculate speed (RPM) from CAN frame data +// float CANReceiver::calculateSpeed(const struct can_frame* frame) const { +// float rpm = 0.0f; +// memcpy(&rpm, frame->data, sizeof(float)); +// return rpm; +// } //################# // // #include "mcp2515_can.h" @@ -207,4 +207,120 @@ float CANReceiver::calculateSpeed(const struct can_frame* frame) const { // // float speed = 120; // std::cout << "Speed: " << speed << std::endl << std::flush; // return speed; -// } \ No newline at end of file +// } + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int soc; // Socket descriptor +int read_can_port; // Flag for reading from the CAN port + +int open_port(const char *port) +{ + struct ifreq ifr; // Interface request structure for socket ioctls + struct sockaddr_can addr; // Address structure for the CAN socket + + /* open socket */ + soc = socket(PF_CAN, SOCK_RAW, CAN_RAW); // Open a CAN socket + + if (soc < 0) + { + printf("error!"); + return (-1); + } + + addr.can_family = AF_CAN; // Set the family type for the address to CAN + strcpy(ifr.ifr_name, port); // Copy the port name to the ifreq structure + + // Fetch the index of the network interface into the ifreq structure using ioctl + if (ioctl(soc, SIOCGIFINDEX, &ifr) < 0) + { + printf("error!"); + return (-1); + } + + addr.can_ifindex = ifr.ifr_ifindex; // Get the interface index from the ifreq structure + fcntl(soc, F_SETFL, O_NONBLOCK); // Set the socket to non-blocking mode + + // Bind the socket to the CAN interface + if (bind(soc, (struct sockaddr *)&addr, sizeof(addr)) < 0) + { + printf("binding error!"); + return (-1); + } + + return 0; +} + +void read_port() +{ + struct can_frame frame; // Structure to hold the CAN frame data + frame.can_dlc = 2; // Set the data length code (DLC) to 2 + int recvbytes = 0; // Number of bytes received + read_can_port = 1; // Flag to continue reading from the CAN port + + while(read_can_port) // Continue reading as long as the flag is set + { + usleep(100000); // Sleep for 100ms + + struct timeval timeout = {1, 0}; // Set the timeout for the select function to 1 second + fd_set readSet; // File descriptor set for the select function + FD_ZERO(&readSet); // Clear the set + FD_SET(soc, &readSet); // Add the socket descriptor to the set + + // Use the select function to wait for data on the socket or for the timeout + if (select((soc + 1), &readSet, NULL, NULL, &timeout) >= 0) + { + if (!read_can_port) + { + printf("error!"); + break; + } + + // Check if data is available on the socket + if (FD_ISSET(soc, &readSet)) + { + // Read the data from the socket + recvbytes = read(soc, &frame, sizeof(struct can_frame)); + if(recvbytes) + { + /* ==================== Receive Data ====================*/ + // Convert the first two bytes of the CAN frame data to the disk rpm + int disk_rpm = (frame.data[0] << 8) + frame.data[1]; + + // Calculate the rpm from the disk rpm + int rpm = (int)((float)disk_rpm / 2.6); + printf("Car RPM: %d\t", rpm); + + // Calculate the speed from the rpm + float speed = (float)rpm * 0.0034; + printf("Car Speed: %.3f m/s\n", speed); + } + } + } + } +} + +int close_port() +{ + close(soc); // Close the socket + return 0; +} + + +int CANReceiver::get(void) +{ + open_port("can0"); // Open the CAN port with the name "can0" + read_port(); // Read data from the CAN port + + return 0; +} \ No newline at end of file diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index d5ec1ca..7e87a45 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -48,7 +48,7 @@ class CANReceiver { bool canRead(); float getSpeed() const; - + int get(); private: float calculateSpeed(const struct can_frame* frame) const; void openSocket(const std::string& interface_name); From 939b38907dffb4705846ca97e03d93d3f47d94c9 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 16:12:19 +0100 Subject: [PATCH 62/73] 16 --- .../src/speed-src/speed/canreceiver.cpp | 134 +++++++++--------- .../src/speed-src/speed/canreceiver.hpp | 1 + .../src/speed-src/speed/speed.cpp | 8 +- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 0931909..23076ef 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -4,86 +4,86 @@ #include "canreceiver.hpp" // // Constructor: Opens and binds the CAN socket to the specified interface -// CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { -// openSocket(interface_name); -// } - -// // Destructor: Closes the CAN socket -// CANReceiver::~CANReceiver() { -// if (socket_fd != -1) { -// close(socket_fd); -// std::cout << "CAN socket closed." << std::endl; -// } -// } +CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { + openSocket(interface_name); +} +CANReceiver::CANReceiver() : socket_fd(-1){} +// Destructor: Closes the CAN socket +CANReceiver::~CANReceiver() { + if (socket_fd != -1) { + close(socket_fd); + std::cout << "CAN socket closed." << std::endl; + } +} // // Opens a CAN socket and binds it to the given interface -// void CANReceiver::openSocket(const std::string& interface_name) { -// socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); -// if (socket_fd < 0) { -// perror("Socket"); -// throw std::runtime_error("Failed to open CAN socket"); -// } +void CANReceiver::openSocket(const std::string& interface_name) { + socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); + if (socket_fd < 0) { + perror("Socket"); + throw std::runtime_error("Failed to open CAN socket"); + } -// std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); -// if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { -// perror("Ioctl"); -// close(socket_fd); -// throw std::runtime_error("Failed to get CAN interface index"); -// } + std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); + if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { + perror("Ioctl"); + close(socket_fd); + throw std::runtime_error("Failed to get CAN interface index"); + } -// addr.can_family = AF_CAN; -// addr.can_ifindex = ifr.ifr_ifindex; + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; -// // 필터 설정 (CAN ID = 0x7만 수신) -// struct can_filter rfilter[1]; -// rfilter[0].can_id = 0x7; -// rfilter[0].can_mask = CAN_SFF_MASK; -// setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); + // 필터 설정 (CAN ID = 0x7만 수신) + struct can_filter rfilter[1]; + rfilter[0].can_id = 0x7; + rfilter[0].can_mask = CAN_SFF_MASK; + setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); -// if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { -// perror("Bind"); -// close(socket_fd); -// throw std::runtime_error("Failed to bind CAN socket"); -// } + if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { + perror("Bind"); + close(socket_fd); + throw std::runtime_error("Failed to bind CAN socket"); + } -// std::cout << "CAN socket bound to interface: " << interface_name << std::endl; -// } + std::cout << "CAN socket bound to interface: " << interface_name << std::endl; +} -// // Check if a CAN frame is available (read from socket) -// bool CANReceiver::canRead() { -// int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); +// Check if a CAN frame is available (read from socket) +bool CANReceiver::canRead() { + int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); -// if (nbytes < 0) { -// perror("CAN read failed"); -// return false; -// } + if (nbytes < 0) { + perror("CAN read failed"); + return false; + } -// if (nbytes != sizeof(struct can_frame)) { -// std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; -// return false; -// } + if (nbytes != sizeof(struct can_frame)) { + std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; + return false; + } -// if(frame.can_id == 0x7 && frame.can_dlc == 4) { -// float rpm = calculateSpeed(&frame); -// std::cout << "Received RPM: " << rpm << std::endl; -// return true; -// } else { -// std::cerr << "Received frame with unexpected CAN ID or DLC." << std::endl; -// return false; -// } -// } + if(frame.can_id == 0x7 && frame.can_dlc == 4) { + float rpm = calculateSpeed(&frame); + std::cout << "Received RPM: " << rpm << std::endl; + return true; + } else { + std::cerr << "Received frame with unexpected CAN ID or DLC." << std::endl; + return false; + } +} -// // Return speed value from the received CAN frame -// float CANReceiver::getSpeed() const { -// return calculateSpeed(&frame); -// } +// Return speed value from the received CAN frame +float CANReceiver::getSpeed() const { + return calculateSpeed(&frame); +} -// // Calculate speed (RPM) from CAN frame data -// float CANReceiver::calculateSpeed(const struct can_frame* frame) const { -// float rpm = 0.0f; -// memcpy(&rpm, frame->data, sizeof(float)); -// return rpm; -// } +// Calculate speed (RPM) from CAN frame data +float CANReceiver::calculateSpeed(const struct can_frame* frame) const { + float rpm = 0.0f; + memcpy(&rpm, frame->data, sizeof(float)); + return rpm; +} //################# // // #include "mcp2515_can.h" diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index 7e87a45..be057df 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -44,6 +44,7 @@ class CANReceiver { public: CANReceiver(const std::string& interface_name); + CANReceiver(); ~CANReceiver(); bool canRead(); diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index 0657fd1..c11cc85 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -171,7 +171,8 @@ void speedObject::canDataReceive() { std::cout << "can Data Receive started" << std::endl; float filtered_speed = 0.0f; float weight = 0.6; - CANReceiver receiver("can0"); + CANReceiver receiver; + // CANReceiver receiver("can0"); while (running_) { std::unique_lock its_lock(can_mutex_); @@ -180,8 +181,9 @@ void speedObject::canDataReceive() { while (is_offered_ && running_) { { - receiver.canRead(); - filtered_speed = receiver.getSpeed(); + receiver.get(); + // receiver.canRead(); + // filtered_speed = receiver.getSpeed(); // filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); this->speedData = filtered_speed; std::cout << "[Speed server] in can data receive loop : filtered_speed : " << filtered_speed << std::endl; From 6f4b99892686e0970bc9e31e51a5500d7be83d25 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 16:25:53 +0100 Subject: [PATCH 63/73] 123132 --- .../src/speed-src/speed/canreceiver.cpp | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 23076ef..4ad43f4 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -5,7 +5,7 @@ // // Constructor: Opens and binds the CAN socket to the specified interface CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { - openSocket(interface_name); + // openSocket(interface_name); } CANReceiver::CANReceiver() : socket_fd(-1){} // Destructor: Closes the CAN socket @@ -319,8 +319,61 @@ int close_port() int CANReceiver::get(void) { - open_port("can0"); // Open the CAN port with the name "can0" - read_port(); // Read data from the CAN port + // open_port("can0"); // Open the CAN port with the name "can0" + // read_port(); // Read data from the CAN port + // return 0; + + int s; + struct sockaddr_can addr; + struct ifreq ifr; + struct can_frame frame; + + // CAN 소켓 생성 + if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { + perror("CAN socket"); + return 1; + } + + // CAN 인터페이스(can0) 설정 + strcpy(ifr.ifr_name, "can0"); + if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { + perror("ioctl"); + close(s); + return 1; + } + + // 소켓을 인터페이스에 바인딩 + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + + if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + perror("bind"); + close(s); + return 1; + } + + std::cout << "CAN 소켓이 can0 인터페이스에 연결되었습니다.\n"; + + // 데이터 수신 루프 + while (true) { + int nbytes = read(s, &frame, sizeof(struct can_frame)); + + if (nbytes < 0) { + perror("CAN read"); + continue; + } + + // CAN 데이터 출력 + std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec; + std::cout << " DLC: " << static_cast(frame.can_dlc) << " Data: "; + + for (int i = 0; i < frame.can_dlc; i++) + printf("%02X ", frame.data[i]); + + std::cout << std::endl; + } + + close(s); return 0; } \ No newline at end of file From ff587c8b03de70ab091d8088a1c7ec741ca212ce Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 16:39:44 +0100 Subject: [PATCH 64/73] 123123 --- .../src/speed-src/speed/canreceiver.cpp | 386 ++++-------------- .../src/speed-src/speed/canreceiver.hpp | 7 +- .../src/speed-src/speed/speed.cpp | 2 +- 3 files changed, 71 insertions(+), 324 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 4ad43f4..f6265dc 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -4,326 +4,69 @@ #include "canreceiver.hpp" // // Constructor: Opens and binds the CAN socket to the specified interface -CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { - // openSocket(interface_name); -} -CANReceiver::CANReceiver() : socket_fd(-1){} -// Destructor: Closes the CAN socket -CANReceiver::~CANReceiver() { - if (socket_fd != -1) { - close(socket_fd); - std::cout << "CAN socket closed." << std::endl; - } -} - -// // Opens a CAN socket and binds it to the given interface -void CANReceiver::openSocket(const std::string& interface_name) { - socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); - if (socket_fd < 0) { - perror("Socket"); - throw std::runtime_error("Failed to open CAN socket"); - } - - std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); - if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { - perror("Ioctl"); - close(socket_fd); - throw std::runtime_error("Failed to get CAN interface index"); - } - - addr.can_family = AF_CAN; - addr.can_ifindex = ifr.ifr_ifindex; - - // 필터 설정 (CAN ID = 0x7만 수신) - struct can_filter rfilter[1]; - rfilter[0].can_id = 0x7; - rfilter[0].can_mask = CAN_SFF_MASK; - setsockopt(socket_fd, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); - - if (bind(socket_fd, reinterpret_cast(&addr), sizeof(addr)) < 0) { - perror("Bind"); - close(socket_fd); - throw std::runtime_error("Failed to bind CAN socket"); - } - - std::cout << "CAN socket bound to interface: " << interface_name << std::endl; -} - -// Check if a CAN frame is available (read from socket) -bool CANReceiver::canRead() { - int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); - - if (nbytes < 0) { - perror("CAN read failed"); - return false; - } - - if (nbytes != sizeof(struct can_frame)) { - std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; - return false; - } - if(frame.can_id == 0x7 && frame.can_dlc == 4) { - float rpm = calculateSpeed(&frame); - std::cout << "Received RPM: " << rpm << std::endl; - return true; - } else { - std::cerr << "Received frame with unexpected CAN ID or DLC." << std::endl; - return false; - } -} +// int CANReceiver::get(void) +// { +// // open_port("can0"); // Open the CAN port with the name "can0" +// // read_port(); // Read data from the CAN port + +// // return 0; -// Return speed value from the received CAN frame -float CANReceiver::getSpeed() const { - return calculateSpeed(&frame); -} +// int s; +// struct sockaddr_can addr; +// struct ifreq ifr; +// struct can_frame frame; -// Calculate speed (RPM) from CAN frame data -float CANReceiver::calculateSpeed(const struct can_frame* frame) const { - float rpm = 0.0f; - memcpy(&rpm, frame->data, sizeof(float)); - return rpm; -} -//################# +// // CAN 소켓 생성 +// if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { +// perror("CAN socket"); +// return 1; +// } -// // #include "mcp2515_can.h" -// // #include +// // CAN 인터페이스(can0) 설정 +// strcpy(ifr.ifr_name, "can0"); +// if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { +// perror("ioctl"); +// close(s); +// return 1; +// } -// // Constructor: Opens and binds the CAN socket to the specified interface -// CANReceiver::CANReceiver(const std::string& interface_name) : socket_fd(-1) { -// openSocket(interface_name); -// } +// // 소켓을 인터페이스에 바인딩 +// addr.can_family = AF_CAN; +// addr.can_ifindex = ifr.ifr_ifindex; -// // Destructor: Closes the CAN socket -// CANReceiver::~CANReceiver() { -// if (socket_fd != -1) { -// close(socket_fd); -// std::cout << "CAN socket closed." << std::endl; +// if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { +// perror("bind"); +// close(s); +// return 1; // } -// } -// // Opens a CAN socket and binds it to the given interface -// void CANReceiver::openSocket(const std::string& interface_name) { -// // Open the CAN_RAW socket -// try { -// socket_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW); -// if (socket_fd < 0) { -// perror("Error while opening CAN socket"); -// throw std::runtime_error("Failed to open CAN socket"); -// } - -// // Get the interface index for the specified CAN interface -// std::strncpy(ifr.ifr_name, interface_name.c_str(), IFNAMSIZ - 1); -// if (ioctl(socket_fd, SIOCGIFINDEX, &ifr) < 0) { -// perror("Error finding CAN interface"); -// close(socket_fd); // Ensure socket is closed before throwing -// throw std::runtime_error("Failed to find CAN interface"); -// } +// std::cout << "CAN 소켓이 can0 인터페이스에 연결되었습니다.\n"; -// addr.can_family = AF_CAN; -// addr.can_ifindex = ifr.ifr_ifindex; +// // 데이터 수신 루프 +// while (true) { +// int nbytes = read(s, &frame, sizeof(struct can_frame)); -// // Bind the socket to the CAN interface -// if (bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { -// perror("Error in binding CAN socket"); -// close(socket_fd); // Ensure socket is closed before throwing -// throw std::runtime_error("Failed to bind CAN socket"); +// if (nbytes < 0) { +// perror("CAN read"); +// continue; // } -// } catch (char &e) { -// std::cout << e << std::endl; -// } -// std::cout << "CAN socket bound to interface: " << interface_name << std::endl; -// } +// // CAN 데이터 출력 +// std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec; +// std::cout << " DLC: " << static_cast(frame.can_dlc) << " Data: "; -// // Check if a CAN frame is available (read from socket) -// bool CANReceiver::canRead() { -// int nbytes = read(socket_fd, &frame, sizeof(struct can_frame)); -// std::cout << "nbytes : " << nbytes << std::endl; -// if (nbytes == sizeof(struct can_frame)) { -// std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec << "\n"; -// std::cout << "DLC: " << static_cast(frame.can_dlc) << "\n"; - -// std::cout << "CAN Data: "; // for (int i = 0; i < frame.can_dlc; i++) // printf("%02X ", frame.data[i]); + // std::cout << std::endl; - -// if(frame.can_id == 0x7 && frame.can_dlc == 4) { -// float rpm = 0.0f; -// memcpy(&rpm, frame.data, sizeof(float)); -// std::cout << "RPM: " << rpm << "\n"; -// } else { -// std::cout << "Received frame with unexpected CAN ID or DLC.\n"; -// return false; -// } -// return true; -// } else if (nbytes < 0) { -// perror("CAN read failed"); -// return false; -// } else { -// std::cerr << "Incomplete CAN frame received (size = " << nbytes << ")\n"; -// return false; // } - -// // if (nbytes == sizeof(struct can_frame)) { -// // if(frame.can_id == 0x7 && frame.can_dlc == 4) { -// // float rpm; -// // memcpy(&rpm, frame.data, sizeof(float)); -// // std::cout << "received RPM value : " << rpm << std::endl; -// // } -// // } - -// // if (nbytes < 0) { -// // if (errno == EAGAIN || errno == EWOULDBLOCK) { -// // return false; // No data available, not an error -// // } -// // perror("Error reading CAN frame"); -// // return false; -// // } -// // if (nbytes < sizeof(struct can_frame)) { -// // std::cerr << "Incomplete CAN frame" << std::endl; -// // return false; -// // } -// // return true; -// } -// // Extract speed from the received CAN frame -// float CANReceiver::getSpeed() { -// return calculateSpeed(&frame); +// close(s); +// return 0; // } - -// // Calculate speed from the first two bytes of the CAN frame data -// float CANReceiver::calculateSpeed(const struct can_frame* frame) { -// // Interpret speed as a signed 16-bit integer -// float speed=0; -// // memcpy (&speed, frame->data, sizeof(float)); -// memcpy (&speed, frame->data, sizeof(float)); -// // std::cout << "memcpy value : " <(frame->data[i]) << std::endl; - -// // float speed = static_cast((frame->data[0] << 8) | frame->data[1]); -// // float speed = 120; -// std::cout << "Speed: " << speed << std::endl << std::flush; -// return speed; -// } - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int soc; // Socket descriptor -int read_can_port; // Flag for reading from the CAN port - -int open_port(const char *port) -{ - struct ifreq ifr; // Interface request structure for socket ioctls - struct sockaddr_can addr; // Address structure for the CAN socket - - /* open socket */ - soc = socket(PF_CAN, SOCK_RAW, CAN_RAW); // Open a CAN socket - - if (soc < 0) - { - printf("error!"); - return (-1); - } - - addr.can_family = AF_CAN; // Set the family type for the address to CAN - strcpy(ifr.ifr_name, port); // Copy the port name to the ifreq structure - - // Fetch the index of the network interface into the ifreq structure using ioctl - if (ioctl(soc, SIOCGIFINDEX, &ifr) < 0) - { - printf("error!"); - return (-1); - } - - addr.can_ifindex = ifr.ifr_ifindex; // Get the interface index from the ifreq structure - fcntl(soc, F_SETFL, O_NONBLOCK); // Set the socket to non-blocking mode - - // Bind the socket to the CAN interface - if (bind(soc, (struct sockaddr *)&addr, sizeof(addr)) < 0) - { - printf("binding error!"); - return (-1); - } - - return 0; -} - -void read_port() -{ - struct can_frame frame; // Structure to hold the CAN frame data - frame.can_dlc = 2; // Set the data length code (DLC) to 2 - int recvbytes = 0; // Number of bytes received - read_can_port = 1; // Flag to continue reading from the CAN port - - while(read_can_port) // Continue reading as long as the flag is set - { - usleep(100000); // Sleep for 100ms - - struct timeval timeout = {1, 0}; // Set the timeout for the select function to 1 second - fd_set readSet; // File descriptor set for the select function - FD_ZERO(&readSet); // Clear the set - FD_SET(soc, &readSet); // Add the socket descriptor to the set - - // Use the select function to wait for data on the socket or for the timeout - if (select((soc + 1), &readSet, NULL, NULL, &timeout) >= 0) - { - if (!read_can_port) - { - printf("error!"); - break; - } - - // Check if data is available on the socket - if (FD_ISSET(soc, &readSet)) - { - // Read the data from the socket - recvbytes = read(soc, &frame, sizeof(struct can_frame)); - if(recvbytes) - { - /* ==================== Receive Data ====================*/ - // Convert the first two bytes of the CAN frame data to the disk rpm - int disk_rpm = (frame.data[0] << 8) + frame.data[1]; - - // Calculate the rpm from the disk rpm - int rpm = (int)((float)disk_rpm / 2.6); - printf("Car RPM: %d\t", rpm); - - // Calculate the speed from the rpm - float speed = (float)rpm * 0.0034; - printf("Car Speed: %.3f m/s\n", speed); - } - } - } - } -} - -int close_port() -{ - close(soc); // Close the socket - return 0; -} - - int CANReceiver::get(void) { - // open_port("can0"); // Open the CAN port with the name "can0" - // read_port(); // Read data from the CAN port - - // return 0; - int s; struct sockaddr_can addr; struct ifreq ifr; @@ -332,7 +75,7 @@ int CANReceiver::get(void) // CAN 소켓 생성 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("CAN socket"); - return 1; + return -1; } // CAN 인터페이스(can0) 설정 @@ -340,7 +83,7 @@ int CANReceiver::get(void) if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { perror("ioctl"); close(s); - return 1; + return -1; } // 소켓을 인터페이스에 바인딩 @@ -350,30 +93,39 @@ int CANReceiver::get(void) if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("bind"); close(s); - return 1; + return -1; } std::cout << "CAN 소켓이 can0 인터페이스에 연결되었습니다.\n"; - // 데이터 수신 루프 - while (true) { - int nbytes = read(s, &frame, sizeof(struct can_frame)); + // 데이터 수신 (1회 읽기) + int nbytes = read(s, &frame, sizeof(struct can_frame)); - if (nbytes < 0) { - perror("CAN read"); - continue; - } + if (nbytes < 0) { + perror("CAN read"); + close(s); + return -1; + } - // CAN 데이터 출력 - std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec; - std::cout << " DLC: " << static_cast(frame.can_dlc) << " Data: "; + // CAN 데이터 출력 + std::cout << "CAN ID: 0x" << std::hex << frame.can_id << std::dec; + std::cout << " DLC: " << static_cast(frame.can_dlc) << " Data: "; - for (int i = 0; i < frame.can_dlc; i++) - printf("%02X ", frame.data[i]); + for (int i = 0; i < frame.can_dlc; i++) + printf("%02X ", frame.data[i]); + std::cout << std::endl; - std::cout << std::endl; + // 데이터(int형)로 변환 + int value = 0; + if(frame.can_dlc >= 4) { + memcpy(&value, frame.data, sizeof(int)); + std::cout << "수신한 int 값: " << value << std::endl; + } else { + std::cerr << "CAN 데이터 길이(DLC)가 4바이트 미만입니다." << std::endl; + close(s); + return -1; } close(s); - return 0; -} \ No newline at end of file + return value; // 정수값 반환 +} diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp index be057df..de97a92 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.hpp @@ -43,16 +43,11 @@ class CANReceiver { struct can_frame frame; public: - CANReceiver(const std::string& interface_name); + CANReceiver(); ~CANReceiver(); - bool canRead(); - float getSpeed() const; int get(); -private: - float calculateSpeed(const struct can_frame* frame) const; - void openSocket(const std::string& interface_name); }; #endif // CANRECEIVER_HPP diff --git a/apps/ServiceManager/src/speed-src/speed/speed.cpp b/apps/ServiceManager/src/speed-src/speed/speed.cpp index c11cc85..4b281c5 100644 --- a/apps/ServiceManager/src/speed-src/speed/speed.cpp +++ b/apps/ServiceManager/src/speed-src/speed/speed.cpp @@ -181,7 +181,7 @@ void speedObject::canDataReceive() { while (is_offered_ && running_) { { - receiver.get(); + filtered_speed = receiver.get(); // receiver.canRead(); // filtered_speed = receiver.getSpeed(); // filtered_speed = (1-weight)*filtered_speed + (weight)*canData.getSpeed(); From 1ace8f47bed0441f75d4daf0184a7eb0ee202288 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 16:41:25 +0100 Subject: [PATCH 65/73] 123123 --- apps/ServiceManager/src/speed-src/speed/canreceiver.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index f6265dc..26ec8c6 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -4,6 +4,8 @@ #include "canreceiver.hpp" // // Constructor: Opens and binds the CAN socket to the specified interface +CANReceiver::CANReceiver(){} +CANReceiver::~CANReceiver(){} // int CANReceiver::get(void) // { From 25deeb27b1119ea1383d79bdcf54ade4f1e790dc Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 17:51:07 +0100 Subject: [PATCH 66/73] 123123 --- .../src/speed-src/speed/canreceiver.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp index 26ec8c6..abfeb36 100644 --- a/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp +++ b/apps/ServiceManager/src/speed-src/speed/canreceiver.cpp @@ -74,6 +74,16 @@ int CANReceiver::get(void) struct ifreq ifr; struct can_frame frame; + + const float wheelDiameter = 6.7; // cm + const float pi = 3.14159; + const float wheelCircumference = wheelDiameter * pi; + const unsigned int numSlots = 20; // number of slots of the disk used for the speedsensor + const unsigned int scaleFactor = 10000; + const unsigned long scaledDistancePerPulse = (unsigned long)(wheelCircumference / numSlots * scaleFactor); + const unsigned int convertToPerSecFactor = 10; + const unsigned int sizeOfUnsignedLong = sizeof(unsigned long); + // CAN 소켓 생성 if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("CAN socket"); @@ -118,9 +128,10 @@ int CANReceiver::get(void) std::cout << std::endl; // 데이터(int형)로 변환 - int value = 0; + float value = 0; + int new_value = 0; if(frame.can_dlc >= 4) { - memcpy(&value, frame.data, sizeof(int)); + memcpy(&value, frame.data, sizeof(float)); std::cout << "수신한 int 값: " << value << std::endl; } else { std::cerr << "CAN 데이터 길이(DLC)가 4바이트 미만입니다." << std::endl; From bf07aed0ea8c431ab788c39fc93a0191fb51553c Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 18:50:39 +0100 Subject: [PATCH 67/73] yaya ture --- apps/VehicleController/Gamepad/ShanwanGamepad.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/VehicleController/Gamepad/ShanwanGamepad.cpp b/apps/VehicleController/Gamepad/ShanwanGamepad.cpp index ce75d91..91ce780 100644 --- a/apps/VehicleController/Gamepad/ShanwanGamepad.cpp +++ b/apps/VehicleController/Gamepad/ShanwanGamepad.cpp @@ -21,7 +21,7 @@ ShanWanGamepadInput ShanWanGamepad::read_data() { if (0 <= button_number) gamepad_input.button = true; if (0 <= axis_number) - gamepad_input.analog_stick = ture; + gamepad_input.analog_stick = true; // Joysticks if (axis_number == 0) From 288eae9d3e12f92ba3ed7e5b283b0144ef6ee81f Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 18:56:18 +0100 Subject: [PATCH 68/73] 123123 --- apps/VehicleController/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index bb02309..5c293e6 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -1,5 +1,5 @@ -#include "PiRacer/PiRacer.hpp" -#include "Gamepad/ShanwanGamepad.hpp" +#include "./PiRacer/PiRacer.hpp" +#include "./Gamepad/ShanwanGamepad.hpp" #include "../ServiceManager/src/server.hpp" #include @@ -77,7 +77,7 @@ void on_message(const std::shared_ptr &_response) { if (payload->get_length() >= sizeof(int)) { received_value = *reinterpret_cast(payload->get_data()); - set_gear(NULL, received_value); + // set_gear(0, received_value); std::cout << "SERVER: Received int: " << received_value << std::endl; } else { @@ -89,7 +89,7 @@ void on_message(const std::shared_ptr &_response) { void init_vSOMEIP() { app = vsomeip::runtime::get()->create_application("gear"); app->init(); - app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + // app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); std::this_thread::sleep_for(std::chrono::seconds(1)); app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); std::this_thread::sleep_for(std::chrono::seconds(1)); From 063fab05e4ec8db378cf608b5456bb14c9af8775 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 18:57:17 +0100 Subject: [PATCH 69/73] 123123 --- apps/VehicleController/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 5c293e6..5b0b0ae 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -94,7 +94,7 @@ void init_vSOMEIP() { app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); std::this_thread::sleep_for(std::chrono::seconds(1)); app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_SET_MID, on_message); - std::thread sender(run); + std::thread sender(send_gear_data); app->start(); } From 49d90730e1f0039b1c7fab520d03b75d6e06fd3d Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 18:58:46 +0100 Subject: [PATCH 70/73] 123123123 --- apps/VehicleController/main.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 5b0b0ae..95d5b23 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -87,15 +87,15 @@ void on_message(const std::shared_ptr &_response) { } void init_vSOMEIP() { - app = vsomeip::runtime::get()->create_application("gear"); - app->init(); - // app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); - std::this_thread::sleep_for(std::chrono::seconds(1)); - app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); - std::this_thread::sleep_for(std::chrono::seconds(1)); - app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_SET_MID, on_message); - std::thread sender(send_gear_data); - app->start(); + // app = vsomeip::runtime::get()->create_application("gear"); + // app->init(); + // // app->register_availability_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, on_availability); + // std::this_thread::sleep_for(std::chrono::seconds(1)); + // app->request_service(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID); + // std::this_thread::sleep_for(std::chrono::seconds(1)); + // app->register_message_handler(VEHICLE_SERVICE_ID, GEAR_INSTANCE_ID, JOY_GEAR_SET_MID, on_message); + // std::thread sender(send_gear_data); + // app->start(); } int main() { From fe8b077875e71e0075b4d4dea764b4dc5c152fa6 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 19:00:03 +0100 Subject: [PATCH 71/73] 123123123 --- apps/VehicleController/main.cpp | 42 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/apps/VehicleController/main.cpp b/apps/VehicleController/main.cpp index 95d5b23..b1a413e 100644 --- a/apps/VehicleController/main.cpp +++ b/apps/VehicleController/main.cpp @@ -33,26 +33,26 @@ std::mutex mutex; std::condition_variable condition; // sending the actual data (will be gear data) to server. -void send_gear_data(int data) { - std::unique_lock its_lock(mutex); - condition.wait(its_lock); - - std::shared_ptr request; - request = vsomeip::runtime::get()->create_request(); - request->set_service(VEHICLE_SERVICE_ID); - request->set_instance(GEAR_INSTANCE_ID); - request->set_method(JOY_GEAR_SET_MID); - - std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); - std::vector its_payload_data( - reinterpret_cast(&data), - reinterpret_cast(&data) + sizeof(int)); - its_payload->set_data(its_payload_data); - request->set_payload(its_payload); - app->send(request); - std::cout << "CLIENT : DATA SENDED" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); -} +// void send_gear_data(int data) { +// std::unique_lock its_lock(mutex); +// condition.wait(its_lock); + +// std::shared_ptr request; +// request = vsomeip::runtime::get()->create_request(); +// request->set_service(VEHICLE_SERVICE_ID); +// request->set_instance(GEAR_INSTANCE_ID); +// request->set_method(JOY_GEAR_SET_MID); + +// std::shared_ptr its_payload = vsomeip::runtime::get()->create_payload(); +// std::vector its_payload_data( +// reinterpret_cast(&data), +// reinterpret_cast(&data) + sizeof(int)); +// its_payload->set_data(its_payload_data); +// request->set_payload(its_payload); +// app->send(request); +// std::cout << "CLIENT : DATA SENDED" << std::endl; +// std::this_thread::sleep_for(std::chrono::seconds(1)); +// } void set_gear(ShanWanGamepadInput input, int received_value) { if (input.button_a || received_value == 3) @@ -117,7 +117,7 @@ int main() { if (input.button) { set_gear(input, -1); - send_gear_data(gear); + // send_gear_data(gear); } else { if (gear == PARKING || gear == NEUTRAL) From 0cfdc07458ba6367f4710ac479e9b24712bd1fcd Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 19:05:11 +0100 Subject: [PATCH 72/73] 123123 --- apps/VehicleController/CMakeLists.txt | 15 +++- apps/VehicleController/vsomeip.json | 112 ++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 apps/VehicleController/vsomeip.json diff --git a/apps/VehicleController/CMakeLists.txt b/apps/VehicleController/CMakeLists.txt index 0581f25..d06d601 100644 --- a/apps/VehicleController/CMakeLists.txt +++ b/apps/VehicleController/CMakeLists.txt @@ -8,13 +8,24 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/..) +set(EXAMPLE_CONFIG_FILES + ./vsomeip.json +) + + find_package(pigpio REQUIRED) +find_package(vsomeip3 3.5.3 REQUIRED) +find_package( Boost 1.55 COMPONENTS system thread log REQUIRED ) + include_directories( ./Adafruit_PCA9685 ./Gamepad ./PiRacer ${pigpio_INCLUDE_DIRS} + ${Boost_INCLUDE_DIR} + ${VSOMEIP_INCLUDE_DIRS} + ) set(PIRACER_SRC @@ -31,6 +42,8 @@ add_executable(vehicle-controller main.cpp ${PIRACER_SRC} ${GAMEPAD_SRC} + ${EXAMPLE_CONFIG_FILES} + ) -target_link_libraries(vehicle-controller ${pigpio_LIBRARY}) +target_link_libraries(vehicle-controller ${pigpio_LIBRARY} ${Boost_LIBRARIES}) diff --git a/apps/VehicleController/vsomeip.json b/apps/VehicleController/vsomeip.json new file mode 100644 index 0000000..0de67a2 --- /dev/null +++ b/apps/VehicleController/vsomeip.json @@ -0,0 +1,112 @@ +{ + "unicast": "127.0.0.1", + "logging": { + "level": "debug", + "console": "true", + "file": { "enable": "false", "path": "/var/log/vsomeip.log" }, + "dlt": "false" + }, + "applications": [ + { + "name": "speed", + "id": "0x4322", + "services": [ + { + "service": "0x1234", + "instance": "0x2001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name":"battery", + "id": "0x4323", + "services": [ + { + "service": "0x1234", + "instance": "0x3001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "gear", + "id": "0x4324", + "services": [ + { + "service": "0x1234", + "instance": "0x4001", + "eventgroups": ["0x4465"] + } + ] + }, + { + "name": "ambient", + "id": "0x4325", + "services": [ + { + "service": "0x1234", + "instance": "0x5001" + } + ] + } + ], + "services": [ + { + "service": "0x1234", + "instance": "0x2001", + "reliable": { "port": "30509", "enable-magic-cookies": "false" }, + "unreliable": "31000", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8779"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x3001", + "reliable": { "port": "30510", "enable-magic-cookies": "false" }, + "unreliable": "31001", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8780"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x4001", + "reliable": { "port": "30511", "enable-magic-cookies": "false" }, + "unreliable": "31002", + "eventgroups": [ + { + "eventgroup": "0x4465", + "events": ["0x8781"] + } + ] + }, + { + "service": "0x1234", + "instance": "0x5001", + "reliable": { "port": "30512", "enable-magic-cookies": "false" }, + "unreliable": "31003" + } + ], + "routing": "service-example", + "service-discovery": { + "enable": "true", + "multicast": "224.244.224.245", + "port": "30490", + "protocol": "udp", + "initial_delay_min": "10", + "initial_delay_max": "100", + "repetitions_base_delay": "100", + "repetitions_max": "3", + "ttl": "3", + "cyclic_offer_delay": "2000", + "request_response_delay": "1500" + } +} From 5d2df96c1c1762c864addad3f31a9bc9af9d1680 Mon Sep 17 00:00:00 2001 From: SihunLee-1016 Date: Tue, 18 Mar 2025 19:06:47 +0100 Subject: [PATCH 73/73] 123123231 --- apps/VehicleController/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/VehicleController/CMakeLists.txt b/apps/VehicleController/CMakeLists.txt index d06d601..83abe23 100644 --- a/apps/VehicleController/CMakeLists.txt +++ b/apps/VehicleController/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/..) +# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/..) set(EXAMPLE_CONFIG_FILES ./vsomeip.json