|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, 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 <modm/board.hpp> |
| 13 | +#include <modm/driver/radar/iwr6843aop.hpp> |
| 14 | + |
| 15 | +using namespace Board; |
| 16 | +using namespace modm::literals; |
| 17 | + |
| 18 | +using ControlUart = BufferedUart<UsartHal1, UartTxBuffer<1024>, UartRxBuffer<1024>>; |
| 19 | +using ControlTx = GpioB6; |
| 20 | +using ControlRx = GpioB7; |
| 21 | + |
| 22 | +using DataUart = BufferedUart<UartHal4, UartTxBuffer<256>, UartRxBuffer<16384>>; |
| 23 | +using DataTx = GpioA0; |
| 24 | +using DataRx = GpioC11; |
| 25 | + |
| 26 | +using RadarNrst = GpioC9; |
| 27 | + |
| 28 | +using Radar = modm::Iwr6843aop<ControlUart, DataUart>; |
| 29 | +Radar radar; |
| 30 | +Radar::FrameType radarFrame{}; |
| 31 | + |
| 32 | +static constexpr char RadarConfiguration[] = R"cfg( |
| 33 | +sensorStop |
| 34 | +flushCfg |
| 35 | +dfeDataOutputMode 1 |
| 36 | +channelCfg 15 7 0 |
| 37 | +adcCfg 2 1 |
| 38 | +adcbufCfg -1 0 1 1 1 |
| 39 | +profileCfg 0 60.00 38.44 7.00 63.75 0 0 35.04 1.00 255 4675.80 0 0 30 |
| 40 | +chirpCfg 0 0 0 0 0 0 0 1 |
| 41 | +chirpCfg 1 1 0 0 0 0 0 2 |
| 42 | +chirpCfg 2 2 0 0 0 0 0 4 |
| 43 | +frameCfg 0 2 60 0 95.00 1 0.00 |
| 44 | +lowPower 0 0 |
| 45 | +guiMonitor -1 1 0 0 0 0 1 |
| 46 | +cfarCfg -1 0 2 8 4 3 0 8 0 |
| 47 | +cfarCfg -1 1 0 4 2 3 1 20 1 |
| 48 | +multiObjBeamForming -1 1 0.5 |
| 49 | +clutterRemoval -1 0 |
| 50 | +aoaFovCfg -1 -90 90 -90 90 |
| 51 | +cfarFovCfg -1 0 0.1 19.0 |
| 52 | +cfarFovCfg -1 1 -8.00 8.00 |
| 53 | +calibDcRangeSig -1 0 -5 8 256 |
| 54 | +extendedMaxVelocity -1 0 |
| 55 | +lvdsStreamCfg -1 0 0 0 |
| 56 | +compRangeBiasAndRxChanPhase 0.0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 |
| 57 | +measureRangeBiasAndRxChanPhase 0 1.0 0.2 |
| 58 | +CQRxSatMonitor 0 3 5 103 0 |
| 59 | +CQSigImgMonitor 0 95 6 |
| 60 | +analogMonitor 0 0 |
| 61 | +calibData 0 0 0 |
| 62 | +sensorStart |
| 63 | +)cfg"; |
| 64 | + |
| 65 | +int |
| 66 | +main() |
| 67 | +{ |
| 68 | + Board::initialize(); |
| 69 | + Leds::setOutput(); |
| 70 | + |
| 71 | + RadarNrst::setOutput(modm::Gpio::Low); |
| 72 | + modm::delay(10ms); |
| 73 | + RadarNrst::set(); |
| 74 | + modm::delay(100ms); |
| 75 | + |
| 76 | + ControlUart::connect<ControlTx::Tx, ControlRx::Rx>(); |
| 77 | + DataUart::connect<DataTx::Tx, DataRx::Rx>(); |
| 78 | + |
| 79 | + ControlUart::initialize<Board::SystemClock, 115200_Bd>(); |
| 80 | + DataUart::initialize<Board::SystemClock, 921600_Bd>(); |
| 81 | + |
| 82 | + MODM_LOG_INFO << "IWR6843AOP example\n"; |
| 83 | + MODM_LOG_INFO << "Uploading configuration...\n"; |
| 84 | + |
| 85 | + if (not radar.configure( |
| 86 | + std::span<const char>{RadarConfiguration, sizeof(RadarConfiguration) - 1})) |
| 87 | + { |
| 88 | + MODM_LOG_ERROR << "Configuration failed, error=" << static_cast<int>(radar.getLastError()) |
| 89 | + << modm::endl; |
| 90 | + while (true) |
| 91 | + { |
| 92 | + Leds::toggle(); |
| 93 | + modm::delay(250ms); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + MODM_LOG_INFO << "Configuration successful\n"; |
| 98 | + |
| 99 | + modm::Timestamp lastFrameTimestamp{}; |
| 100 | + bool hasLastFrameTimestamp{false}; |
| 101 | + uint32_t processedFrames{0}; |
| 102 | + |
| 103 | + while (true) |
| 104 | + { |
| 105 | + if (not radar.processData()) |
| 106 | + { |
| 107 | + MODM_LOG_ERROR << "Processing error=" << static_cast<int>(radar.getLastError()) |
| 108 | + << modm::endl; |
| 109 | + radar.clearError(); |
| 110 | + modm::delay(5ms); |
| 111 | + } |
| 112 | + |
| 113 | + while (radar.getFrame(radarFrame)) |
| 114 | + { |
| 115 | + processedFrames++; |
| 116 | + LedGreen::toggle(); |
| 117 | + |
| 118 | + const auto now = modm::Clock::now(); |
| 119 | + const auto age = now - radarFrame.timestamp; |
| 120 | + |
| 121 | + uint32_t intervalMs{0}; |
| 122 | + if (hasLastFrameTimestamp) |
| 123 | + { |
| 124 | + intervalMs = (radarFrame.timestamp - lastFrameTimestamp).count(); |
| 125 | + } |
| 126 | + lastFrameTimestamp = radarFrame.timestamp; |
| 127 | + hasLastFrameTimestamp = true; |
| 128 | + |
| 129 | + float maxVelocity{0.f}; |
| 130 | + for (std::size_t ii = 0; ii < radarFrame.pointCount; ++ii) |
| 131 | + { |
| 132 | + float velocity = radarFrame.points[ii].point.velocity; |
| 133 | + if (velocity < 0.f) { velocity = -velocity; } |
| 134 | + if (velocity > maxVelocity) { maxVelocity = velocity; } |
| 135 | + } |
| 136 | + |
| 137 | + MODM_LOG_INFO.printf( |
| 138 | + "Frame #%lu: %lu points detected, age: %lu ms, interval: %lu ms, max velocity: " |
| 139 | + "%.2f m/s\n", |
| 140 | + static_cast<unsigned long>(radarFrame.frameHeader.frameNumber), |
| 141 | + static_cast<unsigned long>(radarFrame.pointCount), |
| 142 | + static_cast<unsigned long>(age.count()), static_cast<unsigned long>(intervalMs), |
| 143 | + static_cast<double>(maxVelocity)); |
| 144 | + |
| 145 | + if ((processedFrames % 10u) == 0u) |
| 146 | + { |
| 147 | + MODM_LOG_INFO.printf("Processed %lu frames\n", |
| 148 | + static_cast<unsigned long>(processedFrames)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + modm::delay(1ms); |
| 153 | + } |
| 154 | + |
| 155 | + return 0; |
| 156 | +} |
0 commit comments