Skip to content

Commit 53b9aaf

Browse files
hshosesalkinium
authored andcommitted
[driver] iwr6843aop hosted linux example
1 parent 319b7a5 commit 53b9aaf

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

examples/linux/iwr6843aop/main.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright (c) 2026, Joel Schulz-Andres, Henrik Hose
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <cstdlib>
13+
#include <fstream>
14+
#include <modm/debug/logger.hpp>
15+
#include <modm/platform.hpp>
16+
#include <modm/platform/uart/static_serial_interface.hpp>
17+
#include <modm/driver/radar/iwr6843aop.hpp>
18+
#include <string>
19+
20+
using namespace modm::platform;
21+
22+
template<int N>
23+
class HostedSerialUart : public modm::platform::StaticSerialInterface<N>
24+
{
25+
public:
26+
using modm::platform::StaticSerialInterface<N>::read;
27+
28+
// blocking read seems to work nicer with the hardware
29+
static std::size_t
30+
read(uint8_t *data, std::size_t length)
31+
{
32+
std::size_t count{0};
33+
uint8_t byte{0};
34+
while (count < length)
35+
{
36+
if (not modm::platform::StaticSerialInterface<N>::read(byte)) { break; }
37+
data[count++] = byte;
38+
}
39+
return count;
40+
}
41+
42+
static bool
43+
hasError()
44+
{
45+
return false;
46+
}
47+
48+
static void
49+
clearError()
50+
{}
51+
};
52+
53+
using ControlUart = HostedSerialUart<0>;
54+
using DataUart = HostedSerialUart<1>;
55+
using Radar = modm::Iwr6843aop<ControlUart, DataUart>;
56+
57+
int
58+
main(int argc, char **argv)
59+
{
60+
const char *cfgPath = "../../../src/modm/driver/radar/iwr6843aop.cfg";
61+
if (argc > 1) { cfgPath = argv[1]; }
62+
63+
std::ifstream configFile(cfgPath, std::ios::binary);
64+
if (not configFile.good())
65+
{
66+
MODM_LOG_ERROR << "Could not open config file: " << cfgPath << modm::endl;
67+
return EXIT_FAILURE;
68+
}
69+
70+
const std::string configuration((std::istreambuf_iterator<char>(configFile)),
71+
std::istreambuf_iterator<char>());
72+
73+
SerialInterface controlPort("/dev/ttyUSB0", 115200);
74+
SerialInterface dataPort("/dev/ttyUSB1", 921600);
75+
76+
if (not ControlUart::initialize<115200>(controlPort))
77+
{
78+
MODM_LOG_ERROR << "Could not open control port: " << controlPort.getDeviceName().c_str()
79+
<< modm::endl;
80+
return EXIT_FAILURE;
81+
}
82+
83+
if (not DataUart::initialize<921600>(dataPort))
84+
{
85+
MODM_LOG_ERROR << "Could not open data port: " << dataPort.getDeviceName().c_str()
86+
<< modm::endl;
87+
controlPort.close();
88+
return EXIT_FAILURE;
89+
}
90+
91+
Radar radar;
92+
radar.setSyncInterval(modm::Duration{100});
93+
94+
MODM_LOG_INFO << "Uploading configuration to radar..." << modm::endl;
95+
if (not radar.configure(std::span<const char>{configuration.data(), configuration.size()}))
96+
{
97+
MODM_LOG_ERROR << "Radar configuration failed, error="
98+
<< static_cast<int>(radar.getLastError()) << modm::endl;
99+
controlPort.close();
100+
dataPort.close();
101+
return EXIT_FAILURE;
102+
}
103+
104+
MODM_LOG_INFO << "Configuration successful" << modm::endl;
105+
106+
Radar::FrameType frame{};
107+
modm::Timestamp lastFrameTimestamp{};
108+
bool hasLastFrameTimestamp{false};
109+
uint32_t processedFrames{0};
110+
111+
while (true)
112+
{
113+
if (not radar.processData())
114+
{
115+
MODM_LOG_ERROR << "Data processing error=" << static_cast<int>(radar.getLastError())
116+
<< ", parse_errors=" << radar.getParseErrorCount()
117+
<< ", consecutive_parse_errors=" << radar.getConsecutiveParseErrorCount()
118+
<< modm::endl;
119+
radar.clearError();
120+
modm::delay_ms(5);
121+
}
122+
123+
while (radar.getFrame(frame))
124+
{
125+
processedFrames++;
126+
127+
const auto now = modm::Clock::now();
128+
const auto age = now - frame.timestamp;
129+
130+
uint32_t intervalMs{0};
131+
if (hasLastFrameTimestamp)
132+
{
133+
intervalMs = (frame.timestamp - lastFrameTimestamp).count();
134+
}
135+
lastFrameTimestamp = frame.timestamp;
136+
hasLastFrameTimestamp = true;
137+
138+
float maxVelocity{0.f};
139+
for (std::size_t ii = 0; ii < frame.pointCount; ++ii)
140+
{
141+
float velocity = frame.points[ii].point.velocity;
142+
if (velocity < 0.f) { velocity = -velocity; }
143+
if (velocity > maxVelocity) { maxVelocity = velocity; }
144+
}
145+
146+
MODM_LOG_INFO.printf(
147+
"Frame #%lu: %lu points detected, age: %lu ms, interval: %lu ms, max velocity: "
148+
"%.2f m/s\n",
149+
static_cast<unsigned long>(frame.frameHeader.frameNumber),
150+
static_cast<unsigned long>(frame.pointCount),
151+
static_cast<unsigned long>(age.count()), static_cast<unsigned long>(intervalMs),
152+
static_cast<double>(maxVelocity));
153+
154+
if ((processedFrames % 10u) == 0u)
155+
{
156+
MODM_LOG_INFO.printf("Processed %lu frames\n",
157+
static_cast<unsigned long>(processedFrames));
158+
}
159+
}
160+
161+
modm::delay_ms(1);
162+
}
163+
164+
controlPort.close();
165+
dataPort.close();
166+
return EXIT_SUCCESS;
167+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<options>
3+
<option name="modm:target">hosted-linux</option>
4+
<option name="modm:build:build.path">../../../build/linux/iwr6843aop</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:core</module>
8+
<module>modm:platform:uart</module>
9+
<module>modm:architecture:delay</module>
10+
<module>modm:debug</module>
11+
<module>modm:driver:iwr6843aop</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>

0 commit comments

Comments
 (0)