Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.
4 changes: 2 additions & 2 deletions MiddleWare/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ find_package(zenohcxx REQUIRED)
# include_directories(${libs_SOURCE_DIR}/Communication/CAN/include)
# include_directories(${libs_SOURCE_DIR}/Peripherals/INA219/include)

add_executable(middleWareApp
add_executable(MiddleWareApp
./src/main.cpp
)

target_link_libraries(middleWareApp PRIVATE zenohcxx::zenohc)
target_link_libraries(MiddleWareApp PRIVATE zenohcxx::zenohc)
17 changes: 17 additions & 0 deletions MiddleWare/config/MiddleWareConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"mode": "peer",
"connect": {
"endpoints": ["tcp/127.0.0.1:7447"]
},
"listen": {
"endpoints": ["tcp/127.0.0.1:7448"]
},
"scouting": {
"multicast": {
"enabled": false
},
"gossip": {
"enabled": false
}
}
}
41 changes: 37 additions & 4 deletions MiddleWare/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ int main(int argc, char** argv)
std::cout << "CAN socket bound to can0 interface successfully."
<< std::endl;

Config config = Config::create_default();
auto session = Session::open(std::move(config));
auto config = Config::create_default();
if (argc == 2)
{
config = Config::from_file(argv[1]);
}
auto session = Session::open(std::move(config));

auto pubSpeed =
session.declare_publisher(KeyExpr("seame/car/1/speedSensor"));
auto pubBattery =
session.declare_publisher(KeyExpr("seame/car/1/batterySensor"));
auto pubLights = session.declare_publisher(KeyExpr("seame/car/1/lights"));
auto pubGear = session.declare_publisher(KeyExpr("seame/car/1/gear"));

while (1)
{
Expand All @@ -67,7 +73,7 @@ int main(int argc, char** argv)
speed = wheelDiame * 3.14 * speed * 10 / 60;
std::string speed_str = std::to_string(speed);

printf("Publishing speed: '%d'\n", speed);
// printf("Publishing speed: '%d'\n", speed);
pubSpeed.put(speed_str.c_str());
}
else if (frame.can_id == 0x02)
Expand All @@ -80,9 +86,36 @@ int main(int argc, char** argv)
battery = std::min(100.0f, std::max(0.0f, percentage));
std::string battery_str = std::to_string(battery);

printf("Publishing battery: '%lf\n", battery);
// printf("Publishing battery: '%lf\n", battery);
pubBattery.put(battery_str.c_str());
}
else if (frame.can_id == 0x03)
{
char lights;

memcpy(&lights, frame.data, sizeof(char));

printf("Can received lights: ");
for (int i = 7; i >= 0; i--)
{
printf("%d", (lights >> i) & 0x01);
}
printf("\n");

// printf("Publishing lights: '%lf\n", lights[0]);
std::string light_str(1, lights);
pubLights.put(light_str);
}
else if (frame.can_id == 0x04)
{
char gear;

memcpy(&gear, frame.data, sizeof(char));

// printf("Publishing gear: '%lf\n", gear[0]);
std::string gear_str(1, gear);
pubGear.put(std::to_string(gear_str));
}
usleep(10);
}
return 0;
Expand Down
17 changes: 17 additions & 0 deletions config/InstrumentClusterConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"mode": "peer",
"connect": {
"endpoints": []
},
"listen": {
"endpoints": ["tcp/127.0.0.1:7447"]
},
"scouting": {
"multicast": {
"enabled": false
},
"gossip": {
"enabled": false
}
}
}
2 changes: 2 additions & 0 deletions include/InstrumentCluster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class InstrumentCluster : public QObject

public:
explicit InstrumentCluster(QObject* parent = nullptr);
explicit InstrumentCluster(const std::string& configFile,
QObject* parent = nullptr);
~InstrumentCluster();

int getSpeed() const;
Expand Down
65 changes: 65 additions & 0 deletions src/InstrumentCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,71 @@ InstrumentCluster::InstrumentCluster(QObject* parent)
{
}

InstrumentCluster::InstrumentCluster(const std::string& configFile,
QObject* parent)
: QObject(parent),
m_session(Session::open(std::move(Config::from_file(configFile)))),
m_subSpeed(m_session.declare_subscriber(
"seame/car/1/speedSensor",
[this](const Sample& sample)
{
int speed = std::stoi(sample.get_payload().as_string());
std::cout << "Sub speed" << std::endl;
this->setSpeed(speed);
},
closures::none)),
m_subBattery(m_session.declare_subscriber(
"seame/car/1/batterySensor",
[this](const Sample& sample)
{
int batteryPercentage =
std::stoi(sample.get_payload().as_string());
BatteryStatus battery;
battery.percentage = batteryPercentage;
std::cout << "Sub battery" << std::endl;
this->setBattery(battery);
},
closures::none)),
m_subLights(m_session.declare_subscriber(
"seame/car/1/lights",
[this](const Sample& sample)
{
uint8_t data =
static_cast<uint8_t>(sample.get_payload().as_string()[0]);

LightStatus lights;
lights.rightBlinker = (data & (1 << 0)) != 0;
lights.leftBlinker = (data & (1 << 1)) != 0;
lights.lowBeam = (data & (1 << 2)) != 0;
lights.highBeam = (data & (1 << 3)) != 0;
lights.frontFogLight = (data & (1 << 4)) != 0;
lights.rearFogLight = (data & (1 << 5)) != 0;
lights.hazardLight = (data & (1 << 6)) != 0;
lights.parkingLight = (data & (1 << 7)) != 0;
std::cout << "Sub lights" << std::endl;
this->setLights(lights);
},
closures::none)),
m_subGear(m_session.declare_subscriber(
"seame/car/1/gear",
[this](const Sample& sample)
{
uint8_t data =
static_cast<uint8_t>(sample.get_payload().as_string()[0]);

GearPosition gear;
gear.park = (data & (1 << 0)) != 0;
gear.reverse = (data & (1 << 1)) != 0;
gear.neutral = (data & (1 << 2)) != 0;
gear.drive = (data & (1 << 3)) != 0;
std::cout << "Sub gear" << std::endl;
this->setGear(gear);
},
closures::none)),
m_speed(0)
{
}

InstrumentCluster::~InstrumentCluster()
{
m_session.close();
Expand Down
18 changes: 15 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
#include <QQmlContext>
#include "InstrumentCluster.hpp"

using namespace zenoh;

int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;

InstrumentCluster instrumentCluster;
InstrumentCluster* instrumentCluster;
if (argc == 2)
{
instrumentCluster = new InstrumentCluster(argv[1]);
}
else
{
instrumentCluster = new InstrumentCluster();
}
engine.rootContext()->setContextProperty("instrumentCluster",
&instrumentCluster);
instrumentCluster);

const QUrl url(QStringLiteral("qrc:/Main.qml"));
QObject::connect(
Expand All @@ -23,5 +33,7 @@ int main(int argc, char* argv[])
Qt::QueuedConnection);
engine.load(url);

return app.exec();
int result = app.exec();
delete instrumentCluster;
return result;
}
77 changes: 38 additions & 39 deletions ui/FootbarInfo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,47 @@ Row {
padding: 10
Column {

Row {

anchors.verticalCenterOffset: -10
Image {
id: batteryLevel
// source: {
// if (canBusHandler.battery >= 80) {
// "qrc:/assets/icons/battery-5.png"
// } else if (canBusHandler.battery >= 60) {
// "qrc:/assets/icons/battery-4.png"
// } else if (canBusHandler.battery >= 40) {
// "qrc:/assets/icons/battery-3.png"
// } else if (canBusHandler.battery >= 20) {
// "qrc:/assets/icons/battery-2.png"
// } else {
// "qrc:/assets/icons/battery-1.png"
// }
// }
width: 80
visible: true
fillMode: Image.PreserveAspectFit
source: "qrc:/assets/icons/battery-4.png"
// Adjust this value to move the image up or down
}
}
// Row {
// spacing: 5

// Text {
// font.family: "Open Sans"
// //text: canBusHandler.battery
// text: "100"
// font.pixelSize: app.letterSize
// color: "white"
// }
// Text {
// font.family: "Open Sans"
// text: "%"
// font.pixelSize: app.letterSize
// color: "gray"
// anchors.verticalCenterOffset: -10
// Image {
// id: batteryLevel
// // source: {
// // if (canBusHandler.battery >= 80) {
// // "qrc:/assets/icons/battery-5.png"
// // } else if (canBusHandler.battery >= 60) {
// // "qrc:/assets/icons/battery-4.png"
// // } else if (canBusHandler.battery >= 40) {
// // "qrc:/assets/icons/battery-3.png"
// // } else if (canBusHandler.battery >= 20) {
// // "qrc:/assets/icons/battery-2.png"
// // } else {
// // "qrc:/assets/icons/battery-1.png"
// // }
// // }
// width: 80
// visible: true
// fillMode: Image.PreserveAspectFit
// source: "qrc:/assets/icons/battery-4.png"
// // Adjust this value to move the image up or down
// }
// }
Row {
spacing: 5

Text {
font.family: "Open Sans"
text: instrumentCluster.battery.percentage
font.pixelSize: app.letterSize
color: "white"
}
Text {
font.family: "Open Sans"
text: "%"
font.pixelSize: app.letterSize
color: "gray"
}
}
}

Column {
Expand All @@ -57,7 +56,7 @@ Row {
spacing: 5
Text {
font.family: "Open Sans"
text: "256"
text: instrumentCluster.battery.autonomy
font.pixelSize: app.letterSize
color: "white"
}
Expand Down