INSTINCT Code Coverage Report


Directory: src/
File: Nodes/DataProvider/WiFi/Sensors/EspressifSensor.cpp
Date: 2025-11-25 23:34:18
Exec Total Coverage
Lines: 15 58 25.9%
Functions: 4 12 33.3%
Branches: 12 62 19.4%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 #include "EspressifSensor.hpp"
10
11 #include "util/Logger.hpp"
12
13 #include "util/Time/TimeBase.hpp"
14
15 #include "internal/gui/widgets/HelpMarker.hpp"
16
17 #include "internal/FlowManager.hpp"
18
19 #include "NodeData/General/UartPacket.hpp"
20
21 114 NAV::EspressifSensor::EspressifSensor()
22
4/8
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 114 times.
✗ Branch 5 not taken.
✓ Branch 9 taken 114 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 114 times.
✗ Branch 13 not taken.
114 : Node(typeStatic()), _sensor(typeStatic())
23 {
24 LOG_TRACE("{}: called", name);
25
26 114 _onlyRealTime = true;
27 114 _hasConfig = true;
28 114 _guiConfigDefaultWindowSize = { 360, 70 };
29
30 // TODO: Update the library to handle different baudrates
31
1/2
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
114 _selectedBaudrate = baudrate2Selection(Baudrate::BAUDRATE_115200);
32
1/2
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
114 _sensorPort = "/dev/ttyACM0";
33
34
4/8
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 114 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 114 times.
✓ Branch 10 taken 114 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
456 CreateOutputPin("UartPacket", Pin::Type::Flow, { NAV::UartPacket::type() });
35 228 }
36
37 228 NAV::EspressifSensor::~EspressifSensor()
38 {
39 LOG_TRACE("{}: called", nameId());
40 228 }
41
42 342 std::string NAV::EspressifSensor::typeStatic()
43 {
44
1/2
✓ Branch 1 taken 342 times.
✗ Branch 2 not taken.
684 return "EspressifSensor";
45 }
46
47 std::string NAV::EspressifSensor::type() const
48 {
49 return typeStatic();
50 }
51
52 114 std::string NAV::EspressifSensor::category()
53 {
54
1/2
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
228 return "Data Provider";
55 }
56
57 void NAV::EspressifSensor::guiConfig()
58 {
59 if (ImGui::InputTextWithHint("SensorPort", "/dev/ttyACM0", &_sensorPort))
60 {
61 LOG_DEBUG("{}: SensorPort changed to {}", nameId(), _sensorPort);
62 flow::ApplyChanges();
63 doDeinitialize();
64 }
65 ImGui::SameLine();
66 gui::widgets::HelpMarker("COM port where the sensor is attached to\n"
67 "- \"COM1\" (Windows format for physical and virtual (USB) serial port)\n"
68 "- \"/dev/ttyS1\" (Linux format for physical serial port)\n"
69 "- \"/dev/ttyUSB0\" (Linux format for virtual (USB) serial port)\n"
70 "- \"/dev/tty.usbserial-FTXXXXXX\" (Mac OS X format for virtual (USB) serial port)\n"
71 "- \"/dev/ttyS0\" (CYGWIN format. Usually the Windows COM port number minus 1. This would connect to COM1)");
72 }
73
74 [[nodiscard]] json NAV::EspressifSensor::save() const
75 {
76 LOG_TRACE("{}: called", nameId());
77
78 json j;
79
80 j["UartSensor"] = UartSensor::save();
81
82 return j;
83 }
84
85 void NAV::EspressifSensor::restore(json const& j)
86 {
87 LOG_TRACE("{}: called", nameId());
88
89 if (j.contains("UartSensor"))
90 {
91 UartSensor::restore(j.at("UartSensor"));
92 }
93 }
94
95 bool NAV::EspressifSensor::resetNode()
96 {
97 return true;
98 }
99
100 bool NAV::EspressifSensor::initialize()
101 {
102 LOG_TRACE("{}: called", nameId());
103
104 // connect to the sensor
105 try
106 {
107 _sensor->connect(_sensorPort, BAUDRATE_115200); // change Baudrate here
108
109 LOG_DEBUG("{} connected on port {} with baudrate {}", nameId(), _sensorPort, sensorBaudrate());
110 }
111 catch (...)
112 {
113 LOG_ERROR("{} could not connect", nameId());
114 return false;
115 }
116
117 _sensor->registerAsyncPacketReceivedHandler(this, binaryAsyncMessageReceived);
118
119 return true;
120 }
121
122 void NAV::EspressifSensor::deinitialize()
123 {
124 LOG_TRACE("{}: called", nameId());
125
126 if (_sensor->isConnected())
127 {
128 try
129 {
130 _sensor->unregisterAsyncPacketReceivedHandler();
131 }
132 catch (...) // NOLINT(bugprone-empty-catch)
133 {}
134
135 _sensor->disconnect();
136 }
137 }
138
139 void NAV::EspressifSensor::binaryAsyncMessageReceived(void* userData, uart::protocol::Packet& p, [[maybe_unused]] size_t index)
140 {
141 LOG_DATA("binaryAsyncMessageReceived");
142 auto* ubSensor = static_cast<EspressifSensor*>(userData);
143
144 auto obs = std::make_shared<UartPacket>(p);
145 obs->insTime = util::time::GetCurrentInsTime();
146
147 ubSensor->invokeCallbacks(OUTPUT_PORT_INDEX_WIFI_OBS, obs);
148 }
149