INSTINCT Code Coverage Report


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