2025-11-14 19:50:51 -04:00
|
|
|
#ifndef _STIMULUS_PRODUCER_H
|
|
|
|
|
#define _STIMULUS_PRODUCER_H
|
2025-10-25 18:56:30 -04:00
|
|
|
|
2025-11-06 15:03:26 -04:00
|
|
|
#include <boostAsioLinkageFix.h>
|
2025-10-25 18:56:30 -04:00
|
|
|
#include <vector>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <mutex>
|
2025-10-31 13:43:23 -04:00
|
|
|
#include <functional>
|
|
|
|
|
#include <iostream>
|
2025-11-13 01:09:30 -04:00
|
|
|
#include <chrono>
|
2025-10-31 13:43:23 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
#include <boost/asio/io_service.hpp>
|
|
|
|
|
#include <boost/asio/deadline_timer.hpp>
|
|
|
|
|
#include <spinLock.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 {
|
|
|
|
|
|
2025-11-14 23:19:32 -04:00
|
|
|
// Forward declaration
|
|
|
|
|
class StimulusBuffer;
|
|
|
|
|
|
2025-10-25 18:56:30 -04:00
|
|
|
/**
|
2025-11-14 19:50:51 -04:00
|
|
|
* StimulusProducer manages a collection of stimulus frames with simultaneity stamps.
|
2025-10-25 18:56:30 -04:00
|
|
|
*
|
2025-11-14 19:50:51 -04:00
|
|
|
* This producer is designed to hold stimulus frames that have been assembled
|
2025-10-25 18:56:30 -04:00
|
|
|
* from raw sensor data (e.g., Livox Avia point cloud data) and are ready
|
|
|
|
|
* for processing by the mind layer.
|
|
|
|
|
*
|
2025-11-14 19:50:51 -04:00
|
|
|
* The producer provides thread-safe operations for adding frames, retrieving
|
|
|
|
|
* frames, and managing the producer state.
|
2025-10-25 18:56:30 -04:00
|
|
|
*/
|
2025-11-14 19:50:51 -04:00
|
|
|
class StimulusProducer
|
2025-10-25 18:56:30 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2025-11-14 19:50:51 -04:00
|
|
|
explicit StimulusProducer(
|
2025-11-04 00:46:07 -04:00
|
|
|
const std::shared_ptr<device::DeviceAttachmentSpec>
|
|
|
|
|
&deviceAttachmentSpec,
|
2025-11-04 00:49:15 -04:00
|
|
|
boost::asio::io_service& ioService_)
|
2025-10-31 13:43:23 -04:00
|
|
|
: deviceAttachmentSpec(deviceAttachmentSpec),
|
2025-11-04 00:49:15 -04:00
|
|
|
ioService(ioService_),
|
2025-11-13 01:09:30 -04:00
|
|
|
shouldContinue(false), timer(ioService),
|
|
|
|
|
nDeferrals(0)
|
2025-10-25 23:04:12 -04:00
|
|
|
{}
|
|
|
|
|
|
2025-11-14 19:50:51 -04:00
|
|
|
virtual ~StimulusProducer() = default;
|
2025-10-25 18:56:30 -04:00
|
|
|
|
|
|
|
|
// Non-copyable, movable
|
2025-11-14 19:50:51 -04:00
|
|
|
StimulusProducer(const StimulusProducer&) = delete;
|
|
|
|
|
StimulusProducer& operator=(const StimulusProducer&) = delete;
|
|
|
|
|
StimulusProducer(StimulusProducer&&) = default;
|
|
|
|
|
StimulusProducer& operator=(StimulusProducer&&) = default;
|
2025-10-25 18:56:30 -04:00
|
|
|
|
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-14 19:50:51 -04:00
|
|
|
std::cout << __func__ << ": Starting stimulus producer for device "
|
2025-11-04 00:46:07 -04:00
|
|
|
<< deviceAttachmentSpec->deviceSelector << std::endl;
|
2025-11-01 03:32:05 -04:00
|
|
|
|
2025-11-12 15:08:44 -04:00
|
|
|
shouldContinue = true;
|
2025-11-13 01:09:30 -04:00
|
|
|
nDeferrals = 0;
|
2025-10-31 13:43:23 -04:00
|
|
|
scheduleNextTimeout();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 22:03:28 -04:00
|
|
|
virtual void stop();
|
2025-10-31 13:43:23 -04:00
|
|
|
|
2025-11-08 22:07:52 -04:00
|
|
|
void allowNextStimulusFrame()
|
|
|
|
|
{ frameAssemblyRateLimiter.release(); }
|
|
|
|
|
|
2025-11-16 02:23:53 -04:00
|
|
|
virtual std::shared_ptr<StimulusBuffer> getAttachedStimulusBuffer(
|
2025-11-14 23:19:32 -04:00
|
|
|
const std::shared_ptr<device::DeviceAttachmentSpec>& spec) const;
|
|
|
|
|
|
2025-11-15 04:02:25 -04:00
|
|
|
virtual std::shared_ptr<StimulusBuffer> getOrCreateAttachedStimulusBuffer(
|
|
|
|
|
const std::shared_ptr<device::DeviceAttachmentSpec>& deviceAttachmentSpec,
|
|
|
|
|
int histbuffMs) = 0;
|
|
|
|
|
|
2025-11-16 02:23:53 -04:00
|
|
|
// Check if any attached buffer has the specified qualeIfaceApi
|
|
|
|
|
bool hasBufferWithQualeIfaceApi(const std::string& qualeIfaceApi) const;
|
|
|
|
|
|
2025-10-31 13:54:50 -04:00
|
|
|
protected:
|
2025-11-14 20:44:37 -04:00
|
|
|
SpinLock frameAssemblyRateLimiter;
|
|
|
|
|
|
2025-10-31 13:54:50 -04:00
|
|
|
// 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-11-04 00:46:07 -04:00
|
|
|
std::shared_ptr<device::DeviceAttachmentSpec> deviceAttachmentSpec;
|
2025-11-14 23:19:32 -04:00
|
|
|
std::vector<std::shared_ptr<StimulusBuffer>> attachedStimulusBuffers;
|
2025-10-31 13:43:23 -04:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
boost::asio::io_service& ioService;
|
2025-11-14 02:23:04 -04:00
|
|
|
protected:
|
2025-11-12 15:08:44 -04:00
|
|
|
SpinLock shouldContinueLock;
|
|
|
|
|
bool shouldContinue;
|
2025-11-14 02:23:04 -04:00
|
|
|
private:
|
2025-10-31 13:43:23 -04:00
|
|
|
boost::asio::deadline_timer timer;
|
2025-11-13 01:09:30 -04:00
|
|
|
size_t nDeferrals;
|
|
|
|
|
std::chrono::high_resolution_clock::time_point deferralStartTime;
|
2025-10-31 13:43:23 -04:00
|
|
|
|
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
|
|
|
|
|
|
2025-11-14 19:50:51 -04:00
|
|
|
#endif // _STIMULUS_PRODUCER_H
|