1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #ifndef MEDIAPIPE_CALCULATORS_SENSORS_SENSOR_SYNC_CALCULATOR_H_ #define MEDIAPIPE_CALCULATORS_SENSORS_SENSOR_SYNC_CALCULATOR_H_
#include "mediapipe/framework/calculator_framework.h" #include <deque>
namespace mediapipe {
message SyncedState { uint64 timestamp_us = 1; int32 gaze_zone = 10; float fatigue_score = 11; float speed = 20; float steering_angle = 21; bool turn_signal_left = 22; bool turn_signal_right = 23; }
class SensorSyncCalculator : public CalculatorBase { public: static absl::Status GetContract(CalculatorContract* cc) { cc->Inputs().Tag("GAZE_ZONE").Set<int>(); cc->Inputs().Tag("FATIGUE").Set<float>(); cc->Inputs().Tag("VEHICLE_STATE").Set<VehicleState>(); cc->Outputs().Tag("SYNCED_STATE").Set<SyncedState>(); cc->Options<SensorSyncOptions>(); return absl::OkStatus(); }
absl::Status Open(CalculatorContext* cc) override { const auto& options = cc->Options<SensorSyncOptions>(); max_latency_ms_ = options.max_latency_ms(); interpolation_ = options.interpolation(); return absl::OkStatus(); }
absl::Status Process(CalculatorContext* cc) override { uint64_t current_time = cc->InputTimestamp().Value(); if (!cc->Inputs().Tag("GAZE_ZONE").IsEmpty()) { int gaze = cc->Inputs().Tag("GAZE_ZONE").Get<int>(); gaze_buffer_.push_back({current_time, gaze}); } if (!cc->Inputs().Tag("FATIGUE").IsEmpty()) { float fatigue = cc->Inputs().Tag("FATIGUE").Get<float>(); fatigue_buffer_.push_back({current_time, fatigue}); } if (!cc->Inputs().Tag("VEHICLE_STATE").IsEmpty()) { auto state = cc->Inputs().Tag("VEHICLE_STATE").Get<VehicleState>(); vehicle_buffer_.push_back({current_time, state}); } uint64_t cutoff_time = current_time - max_latency_ms_ * 1000; CleanupOldBuffer(gaze_buffer_, cutoff_time); CleanupOldBuffer(fatigue_buffer_, cutoff_time); CleanupOldBuffer(vehicle_buffer_, cutoff_time); SyncedState synced; synced.set_timestamp_us(current_time); synced.set_gaze_zone(FindNearest(gaze_buffer_, current_time)); synced.set_fatigue_score(FindNearest(fatigue_buffer_, current_time)); if (!vehicle_buffer_.empty()) { auto state = InterpolateVehicleState(current_time); synced.set_speed(state.speed()); synced.set_steering_angle(state.steering_angle()); synced.set_turn_signal_left(state.turn_signal_left()); synced.set_turn_signal_right(state.turn_signal_right()); } cc->Outputs().Tag("SYNCED_STATE").AddPacket( MakePacket<SyncedState>(synced).At(cc->InputTimestamp())); return absl::OkStatus(); }
private: int max_latency_ms_ = 100; bool interpolation_ = true; template <typename T> struct TimedData { uint64_t timestamp; T data; }; std::deque<TimedData<int>> gaze_buffer_; std::deque<TimedData<float>> fatigue_buffer_; std::deque<TimedData<VehicleState>> vehicle_buffer_; template <typename T> void CleanupOldBuffer(std::deque<TimedData<T>>& buffer, uint64_t cutoff) { while (!buffer.empty() && buffer.front().timestamp < cutoff) { buffer.pop_front(); } } template <typename T> T FindNearest(const std::deque<TimedData<T>>& buffer, uint64_t target) { if (buffer.empty()) return T(); auto it = std::lower_bound(buffer.begin(), buffer.end(), target, [](const TimedData<T>& a, uint64_t t) { return a.timestamp < t; }); if (it == buffer.end()) return buffer.back().data; if (it == buffer.begin()) return it->data; auto prev = it - 1; if (target - prev->timestamp < it->timestamp - target) { return prev->data; } return it->data; } VehicleState InterpolateVehicleState(uint64_t target) { if (vehicle_buffer_.empty()) return VehicleState(); if (vehicle_buffer_.size() == 1) return vehicle_buffer_.front().data; auto it = std::lower_bound(vehicle_buffer_.begin(), vehicle_buffer_.end(), target, [](const TimedData<VehicleState>& a, uint64_t t) { return a.timestamp < t; }); if (it == vehicle_buffer_.end()) return vehicle_buffer_.back().data; if (it == vehicle_buffer_.begin()) return it->data; auto prev = it - 1; float t = static_cast<float>(target - prev->timestamp) / (it->timestamp - prev->timestamp); VehicleState result; result.set_speed(prev->data.speed() + t * (it->data.speed() - prev->data.speed())); result.set_steering_angle(prev->data.steering_angle() + t * (it->data.steering_angle() - prev->data.steering_angle())); return result; } };
REGISTER_CALCULATOR(SensorSyncCalculator);
}
#endif
|