Skip to content

Commit 6b6d87b

Browse files
committed
Add some notes
1 parent af5fbc8 commit 6b6d87b

File tree

2 files changed

+80
-19
lines changed

2 files changed

+80
-19
lines changed

infinite_sense_core/include/trigger.h

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,99 @@
33
#include <mutex>
44
#include <infinite_sense.h>
55
#include <map>
6+
67
namespace infinite_sense {
8+
9+
/**
10+
* @brief 宏定义:设置所有设备的最新触发状态。
11+
*
12+
* @param timestamp 触发时间戳。
13+
* @param status 触发状态位掩码(每一位对应一个设备)。
14+
*/
715
#define SET_LAST_TRIGGER_STATUS(timestamp, status) TriggerManger::GetInstance().SetLastTriggerStatus(timestamp, status)
16+
17+
/**
18+
* @brief 宏定义:获取指定设备的最新触发状态时间。
19+
*
20+
* @param device 要查询的设备。
21+
* @param timestamp 返回设备的最后触发时间戳。
22+
*/
823
#define GET_LAST_TRIGGER_STATUS(device, timestamp) TriggerManger::GetInstance().GetLastTriggerStatus(device, timestamp)
24+
25+
/**
26+
* @class TriggerManger
27+
* @brief 管理各传感器或设备的触发状态,并通过消息机制发布状态信息。
28+
*
29+
* 使用单例模式管理系统中所有的触发设备。支持位掩码形式更新所有设备的状态,
30+
* 并可查询特定设备的最后一次有效触发时间。
31+
*/
932
class TriggerManger {
1033
public:
34+
/**
35+
* @brief 获取 TriggerManger 单例对象。
36+
* @return TriggerManger& 单例引用。
37+
*/
1138
static TriggerManger &GetInstance() {
1239
static TriggerManger instance;
1340
return instance;
1441
}
42+
43+
// 删除拷贝构造函数和赋值运算符,确保单例语义。
1544
TriggerManger(const TriggerManger &) = delete;
1645
TriggerManger &operator=(const TriggerManger &) = delete;
17-
void SetLastTriggerStatus(const uint64_t &, const uint8_t &);
18-
bool GetLastTriggerStatus(TriggerDevice, uint64_t &);
46+
47+
/**
48+
* @brief 设置所有设备的最新触发状态。
49+
*
50+
* @param time 当前触发的时间戳。
51+
* @param status 状态掩码,每一位表示一个设备是否触发。
52+
*/
53+
void SetLastTriggerStatus(const uint64_t &time, const uint8_t &status);
54+
55+
/**
56+
* @brief 获取指定设备的最后一次触发状态及时间。
57+
*
58+
* @param dev 设备枚举。
59+
* @param time 返回该设备最后一次触发的时间戳。
60+
* @return true 如果该设备有有效的触发状态。
61+
* @return false 如果该设备无记录。
62+
*/
63+
bool GetLastTriggerStatus(TriggerDevice dev, uint64_t &time);
64+
1965
private:
66+
/**
67+
* @brief 从位掩码中获取对应设备的状态。
68+
*
69+
* @param data 状态字节。
70+
* @param index 目标设备在位掩码中的索引。
71+
* @return true 对应位置为1,表示已触发。
72+
* @return false 对应位置为0,表示未触发。
73+
*/
2074
static bool GetBool(const uint8_t data, const int index) { return (data >> index) & 1; }
75+
76+
/**
77+
* @brief 更新设备状态,并发布触发信息。
78+
*
79+
* @param dev 目标设备。
80+
* @param bit_index 设备对应的位掩码索引。
81+
* @param time 触发时间戳。
82+
*/
2183
void UpdateAndPublishDevice(TriggerDevice dev, int bit_index, uint64_t time);
84+
85+
/**
86+
* @brief 发布指定设备的状态信息。
87+
*
88+
* @param dev 目标设备。
89+
* @param time 触发时间戳。
90+
* @param status 当前触发状态。
91+
*/
2292
void PublishDeviceStatus(TriggerDevice dev, uint64_t time, bool status);
2393
TriggerManger();
2494
~TriggerManger() = default;
2595
uint8_t status_{0};
2696
std::mutex lock_{};
2797
std::map<TriggerDevice, std::tuple<bool, uint64_t>> status_map_{};
28-
std::map<TriggerDevice, std::string> device_topics_; // 设备-topic映射
98+
std::map<TriggerDevice, std::string> device_topics_;
2999
};
100+
30101
} // namespace infinite_sense

infinite_sense_core/src/trigger.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,9 @@ TriggerManger::TriggerManger() {
1414
status_map_[CAM_4] = empty;
1515
status_map_[LASER] = empty;
1616
status_map_[GPS] = empty;
17-
device_topics_ = {
18-
{IMU_1, "trigger/imu_1"},
19-
{IMU_2, "trigger/imu_2"},
20-
{CAM_1, "trigger/cam_1"},
21-
{CAM_2, "trigger/cam_2"},
22-
{CAM_3, "trigger/cam_3"},
23-
{CAM_4, "trigger/cam_4"},
24-
{LASER, "trigger/laser"},
25-
{GPS, "trigger/gps"}
26-
};
17+
device_topics_ = {{IMU_1, "trigger/imu_1"}, {IMU_2, "trigger/imu_2"}, {CAM_1, "trigger/cam_1"},
18+
{CAM_2, "trigger/cam_2"}, {CAM_3, "trigger/cam_3"}, {CAM_4, "trigger/cam_4"},
19+
{LASER, "trigger/laser"}, {GPS, "trigger/gps"}};
2720
}
2821
void TriggerManger::SetLastTriggerStatus(const uint64_t& time, const uint8_t& status) {
2922
std::lock_guard lock(lock_);
@@ -37,6 +30,7 @@ void TriggerManger::SetLastTriggerStatus(const uint64_t& time, const uint8_t& st
3730
UpdateAndPublishDevice(LASER, 6, time);
3831
UpdateAndPublishDevice(GPS, 7, time);
3932
}
33+
4034
bool TriggerManger::GetLastTriggerStatus(const TriggerDevice dev, uint64_t& time) {
4135
std::lock_guard lock(lock_);
4236
if (status_map_.find(dev) == status_map_.end()) {
@@ -57,13 +51,9 @@ void TriggerManger::PublishDeviceStatus(const TriggerDevice dev, const uint64_t
5751
uint64_t timestamp;
5852
bool status;
5953
} dev_data{time, status};
60-
Messenger::GetInstance().PubStruct(
61-
device_topics_[dev],
62-
&dev_data,
63-
sizeof(DeviceStatus));
54+
Messenger::GetInstance().PubStruct(device_topics_[dev], &dev_data, sizeof(DeviceStatus));
6455
} catch (const std::exception& e) {
65-
LOG(ERROR) << "Failed to publish " << device_topics_[dev]
66-
<< " status: " << e.what();
56+
LOG(ERROR) << "Failed to publish " << device_topics_[dev] << " status: " << e.what();
6757
}
6858
}
6959
} // namespace infinite_sense

0 commit comments

Comments
 (0)