Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 7e93c1b

Browse files
Merge pull request #72 from SEAME-pt/middleWare-zenoh-config-file-add
Middle ware zenoh config file add
2 parents f8a2af1 + 47a936e commit 7e93c1b

8 files changed

Lines changed: 193 additions & 48 deletions

File tree

MiddleWare/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ find_package(zenohcxx REQUIRED)
2222
# include_directories(${libs_SOURCE_DIR}/Communication/CAN/include)
2323
# include_directories(${libs_SOURCE_DIR}/Peripherals/INA219/include)
2424

25-
add_executable(middleWareApp
25+
add_executable(MiddleWareApp
2626
./src/main.cpp
2727
)
2828

29-
target_link_libraries(middleWareApp PRIVATE zenohcxx::zenohc)
29+
target_link_libraries(MiddleWareApp PRIVATE zenohcxx::zenohc)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"mode": "peer",
3+
"connect": {
4+
"endpoints": ["tcp/127.0.0.1:7447"]
5+
},
6+
"listen": {
7+
"endpoints": ["tcp/127.0.0.1:7448"]
8+
},
9+
"scouting": {
10+
"multicast": {
11+
"enabled": false
12+
},
13+
"gossip": {
14+
"enabled": false
15+
}
16+
}
17+
}

MiddleWare/src/main.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ int main(int argc, char** argv)
3939
std::cout << "CAN socket bound to can0 interface successfully."
4040
<< std::endl;
4141

42-
Config config = Config::create_default();
43-
auto session = Session::open(std::move(config));
42+
auto config = Config::create_default();
43+
if (argc == 2)
44+
{
45+
config = Config::from_file(argv[1]);
46+
}
47+
auto session = Session::open(std::move(config));
4448

4549
auto pubSpeed =
4650
session.declare_publisher(KeyExpr("seame/car/1/speedSensor"));
4751
auto pubBattery =
4852
session.declare_publisher(KeyExpr("seame/car/1/batterySensor"));
53+
auto pubLights = session.declare_publisher(KeyExpr("seame/car/1/lights"));
54+
auto pubGear = session.declare_publisher(KeyExpr("seame/car/1/gear"));
4955

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

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

83-
printf("Publishing battery: '%lf\n", battery);
89+
// printf("Publishing battery: '%lf\n", battery);
8490
pubBattery.put(battery_str.c_str());
8591
}
92+
else if (frame.can_id == 0x03)
93+
{
94+
char lights;
95+
96+
memcpy(&lights, frame.data, sizeof(char));
97+
98+
printf("Can received lights: ");
99+
for (int i = 7; i >= 0; i--)
100+
{
101+
printf("%d", (lights >> i) & 0x01);
102+
}
103+
printf("\n");
104+
105+
// printf("Publishing lights: '%lf\n", lights[0]);
106+
std::string light_str(1, lights);
107+
pubLights.put(light_str);
108+
}
109+
else if (frame.can_id == 0x04)
110+
{
111+
char gear;
112+
113+
memcpy(&gear, frame.data, sizeof(char));
114+
115+
// printf("Publishing gear: '%lf\n", gear[0]);
116+
std::string gear_str(1, gear);
117+
pubGear.put(std::to_string(gear_str));
118+
}
86119
usleep(10);
87120
}
88121
return 0;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"mode": "peer",
3+
"connect": {
4+
"endpoints": []
5+
},
6+
"listen": {
7+
"endpoints": ["tcp/127.0.0.1:7447"]
8+
},
9+
"scouting": {
10+
"multicast": {
11+
"enabled": false
12+
},
13+
"gossip": {
14+
"enabled": false
15+
}
16+
}
17+
}

include/InstrumentCluster.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class InstrumentCluster : public QObject
103103

104104
public:
105105
explicit InstrumentCluster(QObject* parent = nullptr);
106+
explicit InstrumentCluster(const std::string& configFile,
107+
QObject* parent = nullptr);
106108
~InstrumentCluster();
107109

108110
int getSpeed() const;

src/InstrumentCluster.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,71 @@ InstrumentCluster::InstrumentCluster(QObject* parent)
6464
{
6565
}
6666

67+
InstrumentCluster::InstrumentCluster(const std::string& configFile,
68+
QObject* parent)
69+
: QObject(parent),
70+
m_session(Session::open(std::move(Config::from_file(configFile)))),
71+
m_subSpeed(m_session.declare_subscriber(
72+
"seame/car/1/speedSensor",
73+
[this](const Sample& sample)
74+
{
75+
int speed = std::stoi(sample.get_payload().as_string());
76+
std::cout << "Sub speed" << std::endl;
77+
this->setSpeed(speed);
78+
},
79+
closures::none)),
80+
m_subBattery(m_session.declare_subscriber(
81+
"seame/car/1/batterySensor",
82+
[this](const Sample& sample)
83+
{
84+
int batteryPercentage =
85+
std::stoi(sample.get_payload().as_string());
86+
BatteryStatus battery;
87+
battery.percentage = batteryPercentage;
88+
std::cout << "Sub battery" << std::endl;
89+
this->setBattery(battery);
90+
},
91+
closures::none)),
92+
m_subLights(m_session.declare_subscriber(
93+
"seame/car/1/lights",
94+
[this](const Sample& sample)
95+
{
96+
uint8_t data =
97+
static_cast<uint8_t>(sample.get_payload().as_string()[0]);
98+
99+
LightStatus lights;
100+
lights.rightBlinker = (data & (1 << 0)) != 0;
101+
lights.leftBlinker = (data & (1 << 1)) != 0;
102+
lights.lowBeam = (data & (1 << 2)) != 0;
103+
lights.highBeam = (data & (1 << 3)) != 0;
104+
lights.frontFogLight = (data & (1 << 4)) != 0;
105+
lights.rearFogLight = (data & (1 << 5)) != 0;
106+
lights.hazardLight = (data & (1 << 6)) != 0;
107+
lights.parkingLight = (data & (1 << 7)) != 0;
108+
std::cout << "Sub lights" << std::endl;
109+
this->setLights(lights);
110+
},
111+
closures::none)),
112+
m_subGear(m_session.declare_subscriber(
113+
"seame/car/1/gear",
114+
[this](const Sample& sample)
115+
{
116+
uint8_t data =
117+
static_cast<uint8_t>(sample.get_payload().as_string()[0]);
118+
119+
GearPosition gear;
120+
gear.park = (data & (1 << 0)) != 0;
121+
gear.reverse = (data & (1 << 1)) != 0;
122+
gear.neutral = (data & (1 << 2)) != 0;
123+
gear.drive = (data & (1 << 3)) != 0;
124+
std::cout << "Sub gear" << std::endl;
125+
this->setGear(gear);
126+
},
127+
closures::none)),
128+
m_speed(0)
129+
{
130+
}
131+
67132
InstrumentCluster::~InstrumentCluster()
68133
{
69134
m_session.close();

src/main.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
#include <QQmlContext>
44
#include "InstrumentCluster.hpp"
55

6+
using namespace zenoh;
7+
68
int main(int argc, char* argv[])
79
{
810
QGuiApplication app(argc, argv);
911
QQmlApplicationEngine engine;
1012

11-
InstrumentCluster instrumentCluster;
13+
InstrumentCluster* instrumentCluster;
14+
if (argc == 2)
15+
{
16+
instrumentCluster = new InstrumentCluster(argv[1]);
17+
}
18+
else
19+
{
20+
instrumentCluster = new InstrumentCluster();
21+
}
1222
engine.rootContext()->setContextProperty("instrumentCluster",
13-
&instrumentCluster);
23+
instrumentCluster);
1424

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

26-
return app.exec();
36+
int result = app.exec();
37+
delete instrumentCluster;
38+
return result;
2739
}

ui/FootbarInfo.qml

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,47 @@ Row {
77
padding: 10
88
Column {
99

10-
Row {
11-
12-
anchors.verticalCenterOffset: -10
13-
Image {
14-
id: batteryLevel
15-
// source: {
16-
// if (canBusHandler.battery >= 80) {
17-
// "qrc:/assets/icons/battery-5.png"
18-
// } else if (canBusHandler.battery >= 60) {
19-
// "qrc:/assets/icons/battery-4.png"
20-
// } else if (canBusHandler.battery >= 40) {
21-
// "qrc:/assets/icons/battery-3.png"
22-
// } else if (canBusHandler.battery >= 20) {
23-
// "qrc:/assets/icons/battery-2.png"
24-
// } else {
25-
// "qrc:/assets/icons/battery-1.png"
26-
// }
27-
// }
28-
width: 80
29-
visible: true
30-
fillMode: Image.PreserveAspectFit
31-
source: "qrc:/assets/icons/battery-4.png"
32-
// Adjust this value to move the image up or down
33-
}
34-
}
3510
// Row {
36-
// spacing: 5
3711

38-
// Text {
39-
// font.family: "Open Sans"
40-
// //text: canBusHandler.battery
41-
// text: "100"
42-
// font.pixelSize: app.letterSize
43-
// color: "white"
44-
// }
45-
// Text {
46-
// font.family: "Open Sans"
47-
// text: "%"
48-
// font.pixelSize: app.letterSize
49-
// color: "gray"
12+
// anchors.verticalCenterOffset: -10
13+
// Image {
14+
// id: batteryLevel
15+
// // source: {
16+
// // if (canBusHandler.battery >= 80) {
17+
// // "qrc:/assets/icons/battery-5.png"
18+
// // } else if (canBusHandler.battery >= 60) {
19+
// // "qrc:/assets/icons/battery-4.png"
20+
// // } else if (canBusHandler.battery >= 40) {
21+
// // "qrc:/assets/icons/battery-3.png"
22+
// // } else if (canBusHandler.battery >= 20) {
23+
// // "qrc:/assets/icons/battery-2.png"
24+
// // } else {
25+
// // "qrc:/assets/icons/battery-1.png"
26+
// // }
27+
// // }
28+
// width: 80
29+
// visible: true
30+
// fillMode: Image.PreserveAspectFit
31+
// source: "qrc:/assets/icons/battery-4.png"
32+
// // Adjust this value to move the image up or down
5033
// }
5134
// }
35+
Row {
36+
spacing: 5
37+
38+
Text {
39+
font.family: "Open Sans"
40+
text: instrumentCluster.battery.percentage
41+
font.pixelSize: app.letterSize
42+
color: "white"
43+
}
44+
Text {
45+
font.family: "Open Sans"
46+
text: "%"
47+
font.pixelSize: app.letterSize
48+
color: "gray"
49+
}
50+
}
5251
}
5352

5453
Column {
@@ -57,7 +56,7 @@ Row {
5756
spacing: 5
5857
Text {
5958
font.family: "Open Sans"
60-
text: "256"
59+
text: instrumentCluster.battery.autonomy
6160
font.pixelSize: app.letterSize
6261
color: "white"
6362
}

0 commit comments

Comments
 (0)