INSTINCT Code Coverage Report


Directory: src/
File: Nodes/DataProvider/GNSS/Sensors/EmlidSensor.cpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 14 56 25.0%
Functions: 4 12 33.3%
Branches: 11 62 17.7%

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 "EmlidSensor.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::EmlidSensor::EmlidSensor()
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_9600);
34
35
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() });
36 224 }
37
38 224 NAV::EmlidSensor::~EmlidSensor()
39 {
40 LOG_TRACE("{}: called", nameId());
41 224 }
42
43 336 std::string NAV::EmlidSensor::typeStatic()
44 {
45
1/2
✓ Branch 1 taken 336 times.
✗ Branch 2 not taken.
672 return "EmlidSensor";
46 }
47
48 std::string NAV::EmlidSensor::type() const
49 {
50 return typeStatic();
51 }
52
53 112 std::string NAV::EmlidSensor::category()
54 {
55
1/2
✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
224 return "Data Provider";
56 }
57
58 void NAV::EmlidSensor::guiConfig()
59 {
60 if (ImGui::InputTextWithHint("SensorPort", "/dev/ttyACM0", &_sensorPort))
61 {
62 LOG_DEBUG("{}: SensorPort changed to {}", nameId(), _sensorPort);
63 flow::ApplyChanges();
64 doDeinitialize();
65 }
66 ImGui::SameLine();
67 gui::widgets::HelpMarker("COM port where the sensor is attached to\n"
68 "- \"COM1\" (Windows format for physical and virtual (USB) serial port)\n"
69 "- \"/dev/ttyS1\" (Linux format for physical serial port)\n"
70 "- \"/dev/ttyUSB0\" (Linux format for virtual (USB) serial port)\n"
71 "- \"/dev/tty.usbserial-FTXXXXXX\" (Mac OS X format for virtual (USB) serial port)\n"
72 "- \"/dev/ttyS0\" (CYGWIN format. Usually the Windows COM port number minus 1. This would connect to COM1)");
73 }
74
75 [[nodiscard]] json NAV::EmlidSensor::save() const
76 {
77 LOG_TRACE("{}: called", nameId());
78
79 json j;
80
81 j["UartSensor"] = UartSensor::save();
82
83 return j;
84 }
85
86 void NAV::EmlidSensor::restore(json const& j)
87 {
88 LOG_TRACE("{}: called", nameId());
89
90 if (j.contains("UartSensor"))
91 {
92 UartSensor::restore(j.at("UartSensor"));
93 }
94 }
95
96 bool NAV::EmlidSensor::resetNode()
97 {
98 return true;
99 }
100
101 bool NAV::EmlidSensor::initialize()
102 {
103 LOG_TRACE("{}: called", nameId());
104
105 // connect to the sensor
106 try
107 {
108 _sensor->connect(_sensorPort, sensorBaudrate());
109
110 LOG_DEBUG("{} connected on port {} with baudrate {}", nameId(), _sensorPort, sensorBaudrate());
111 }
112 catch (...)
113 {
114 LOG_ERROR("{} could not connect", nameId());
115 return false;
116 }
117
118 _sensor->registerAsyncPacketReceivedHandler(this, asciiOrBinaryAsyncMessageReceived);
119
120 return true;
121 }
122
123 void NAV::EmlidSensor::deinitialize()
124 {
125 LOG_TRACE("{}: called", nameId());
126
127 if (!isInitialized())
128 {
129 return;
130 }
131
132 if (_sensor->isConnected())
133 {
134 try
135 {
136 _sensor->unregisterAsyncPacketReceivedHandler();
137 }
138 catch (...) // NOLINT(bugprone-empty-catch)
139 {}
140
141 _sensor->disconnect();
142 }
143 }
144
145 void NAV::EmlidSensor::asciiOrBinaryAsyncMessageReceived(void* userData, uart::protocol::Packet& p, [[maybe_unused]] size_t index)
146 {
147 auto* erSensor = static_cast<EmlidSensor*>(userData);
148
149 erSensor->invokeCallbacks(OUTPUT_PORT_INDEX_EMLID_OBS, std::make_shared<UartPacket>(p));
150 }
151