2025-10-25 18:56:30 -04:00
|
|
|
#ifndef _STIMULUS_BUFFER_H
|
|
|
|
|
#define _STIMULUS_BUFFER_H
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <mutex>
|
2025-10-31 13:43:23 -04:00
|
|
|
#include <functional>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <boost/asio/io_service.hpp>
|
|
|
|
|
#include <boost/asio/deadline_timer.hpp>
|
|
|
|
|
#include <spinLock.h>
|
|
|
|
|
#include <asynchronousBridge.h>
|
2025-11-01 00:06:42 -04:00
|
|
|
#include <user/spMcRingBuffer.h>
|
2025-10-25 18:56:30 -04:00
|
|
|
#include "stimFrame.h"
|
2025-10-25 19:42:48 -04:00
|
|
|
#include "deviceAttachmentSpec.h"
|
2025-10-25 18:56:30 -04:00
|
|
|
|
|
|
|
|
namespace smo {
|
|
|
|
|
namespace stim_buff {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* StimulusBuffer manages a collection of stimulus frames with simultaneity stamps.
|
|
|
|
|
*
|
|
|
|
|
* This buffer is designed to hold stimulus frames that have been assembled
|
|
|
|
|
* from raw sensor data (e.g., Livox Avia point cloud data) and are ready
|
|
|
|
|
* for processing by the mind layer.
|
|
|
|
|
*
|
|
|
|
|
* The buffer provides thread-safe operations for adding frames, retrieving
|
|
|
|
|
* frames, and managing the buffer state.
|
|
|
|
|
*/
|
|
|
|
|
class StimulusBuffer
|
|
|
|
|
{
|
2025-10-25 19:32:10 -04:00
|
|
|
public:
|
|
|
|
|
class PcloudFormatDesc
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
enum class Format
|
|
|
|
|
{
|
|
|
|
|
XYZ,
|
|
|
|
|
XYZI,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Format format;
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-25 18:56:30 -04:00
|
|
|
public:
|
2025-10-25 19:42:48 -04:00
|
|
|
explicit StimulusBuffer(
|
2025-10-31 13:43:23 -04:00
|
|
|
const device::DeviceAttachmentSpec& deviceAttachmentSpec,
|
2025-11-01 00:06:42 -04:00
|
|
|
size_t nSlots,
|
|
|
|
|
const SpMcRingBuffer::InputEngineConstraints& ringBufferConstraints,
|
2025-10-31 13:43:23 -04:00
|
|
|
boost::asio::io_service& ioService)
|
|
|
|
|
: deviceAttachmentSpec(deviceAttachmentSpec),
|
2025-11-01 00:06:42 -04:00
|
|
|
ringBuffer(nSlots, ringBufferConstraints),
|
2025-10-31 13:43:23 -04:00
|
|
|
ioService(ioService),
|
|
|
|
|
shouldContinue(false), timer(ioService)
|
2025-10-25 23:04:12 -04:00
|
|
|
{}
|
|
|
|
|
|
2025-10-31 13:54:50 -04:00
|
|
|
virtual ~StimulusBuffer() = default;
|
2025-10-25 18:56:30 -04:00
|
|
|
|
|
|
|
|
// Non-copyable, movable
|
|
|
|
|
StimulusBuffer(const StimulusBuffer&) = delete;
|
|
|
|
|
StimulusBuffer& operator=(const StimulusBuffer&) = delete;
|
|
|
|
|
StimulusBuffer(StimulusBuffer&&) = default;
|
|
|
|
|
StimulusBuffer& operator=(StimulusBuffer&&) = default;
|
|
|
|
|
|
2025-10-31 13:43:23 -04:00
|
|
|
// Control methods
|
2025-11-01 22:03:28 -04:00
|
|
|
virtual void start()
|
2025-10-31 13:43:23 -04:00
|
|
|
{
|
2025-11-01 03:32:05 -04:00
|
|
|
std::cout << __func__ << ": Starting stimulus buffer for device "
|
|
|
|
|
<< deviceAttachmentSpec.deviceSelector << std::endl;
|
|
|
|
|
|
2025-10-31 13:43:23 -04:00
|
|
|
shouldContinue.store(true);
|
|
|
|
|
scheduleNextTimeout();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 22:03:28 -04:00
|
|
|
virtual void stop();
|
2025-10-31 13:43:23 -04:00
|
|
|
|
2025-10-31 13:54:50 -04:00
|
|
|
protected:
|
|
|
|
|
// Virtual functions for derived classes to override
|
|
|
|
|
virtual int getStopDelayMs() const
|
|
|
|
|
{
|
|
|
|
|
return CONFIG_STIMBUFF_FRAME_PERIOD_MS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void stimFrameProductionTimesliceInd() = 0;
|
|
|
|
|
|
2025-10-31 13:47:00 -04:00
|
|
|
private:
|
|
|
|
|
void onTimeout(const boost::system::error_code& error);
|
2025-10-31 13:43:23 -04:00
|
|
|
|
2025-10-25 23:04:12 -04:00
|
|
|
public:
|
2025-10-25 19:42:48 -04:00
|
|
|
device::DeviceAttachmentSpec deviceAttachmentSpec;
|
2025-10-25 18:56:30 -04:00
|
|
|
std::vector<StimFrame> frames_;
|
2025-10-31 13:43:23 -04:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
SpinLock frameAssemblyRateLimiter;
|
2025-11-01 00:06:42 -04:00
|
|
|
SpMcRingBuffer ringBuffer;
|
2025-10-31 13:43:23 -04:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
boost::asio::io_service& ioService;
|
|
|
|
|
std::atomic<bool> shouldContinue;
|
|
|
|
|
boost::asio::deadline_timer timer;
|
|
|
|
|
|
2025-11-02 19:08:47 -04:00
|
|
|
void scheduleNextTimeout(int delayMs = CONFIG_STIMBUFF_FRAME_PERIOD_MS);
|
2025-10-31 13:47:00 -04:00
|
|
|
};
|
2025-10-31 13:43:23 -04:00
|
|
|
|
2025-10-25 18:56:30 -04:00
|
|
|
} // namespace stim_buff
|
|
|
|
|
} // namespace smo
|
|
|
|
|
|
|
|
|
|
#endif // _STIMULUS_BUFFER_H
|