Auto commit at 2025-05-30 13:58:15
This commit is contained in:
parent
792d2a5c59
commit
6cd17d4a84
@ -341,11 +341,13 @@ int main(int argc, char **argv)
|
|||||||
// 添加定时器设置
|
// 添加定时器设置
|
||||||
setupTimers(node);
|
setupTimers(node);
|
||||||
|
|
||||||
// ROS 2 spin
|
// 在 ROS shutdown 时自动清理
|
||||||
|
rclcpp::on_shutdown([&]()
|
||||||
|
{ canctl.close(); });
|
||||||
|
|
||||||
|
// 事件循环
|
||||||
rclcpp::spin(node);
|
rclcpp::spin(node);
|
||||||
|
|
||||||
// 关闭 CAN 接口
|
|
||||||
canctl.close();
|
|
||||||
rclcpp::shutdown(); // 关闭 ROS 2
|
rclcpp::shutdown(); // 关闭 ROS 2
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,26 @@ Config config; // 清扫车配置文件
|
|||||||
|
|
||||||
GeneralMsg info_report; // 常规消息上报
|
GeneralMsg info_report; // 常规消息上报
|
||||||
|
|
||||||
|
std::deque<float> waterLevelHistory;
|
||||||
|
const size_t maxSamples = 10;
|
||||||
|
|
||||||
|
void updateWaterLevel(float newLevel)
|
||||||
|
{
|
||||||
|
waterLevelHistory.push_back(newLevel);
|
||||||
|
if (waterLevelHistory.size() > maxSamples)
|
||||||
|
{
|
||||||
|
waterLevelHistory.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
|
float sum = 0;
|
||||||
|
for (float v : waterLevelHistory)
|
||||||
|
sum += v;
|
||||||
|
float filteredLevel = sum / waterLevelHistory.size();
|
||||||
|
|
||||||
|
filteredLevel = std::clamp(filteredLevel, 0.0f, 180.0f);
|
||||||
|
info_report.waterLevel = static_cast<int>(std::round(filteredLevel / 1.8f));
|
||||||
|
}
|
||||||
|
|
||||||
// 解析can报文,做消息上报
|
// 解析can报文,做消息上报
|
||||||
void Msg_Handler(const mc::msg::CanFrame::SharedPtr msg)
|
void Msg_Handler(const mc::msg::CanFrame::SharedPtr msg)
|
||||||
{
|
{
|
||||||
@ -25,7 +45,7 @@ void Msg_Handler(const mc::msg::CanFrame::SharedPtr msg)
|
|||||||
case 0x1A2:
|
case 0x1A2:
|
||||||
{
|
{
|
||||||
const uint8_t gearByte = msg->data[0];
|
const uint8_t gearByte = msg->data[0];
|
||||||
info_report.gear = (gearByte & 0x01) ? 3 : ((gearByte >> 1) & 0x03);
|
info_report.gear = (gearByte & 0x01) ? 3 : ((gearByte >> 2) & 0x03);
|
||||||
|
|
||||||
const int tempRaw = (msg->data[5] << 8) | msg->data[6];
|
const int tempRaw = (msg->data[5] << 8) | msg->data[6];
|
||||||
info_report.motorTemp = static_cast<int>(tempRaw * 0.1f - 100.0f);
|
info_report.motorTemp = static_cast<int>(tempRaw * 0.1f - 100.0f);
|
||||||
@ -51,7 +71,7 @@ void Msg_Handler(const mc::msg::CanFrame::SharedPtr msg)
|
|||||||
uint16_t WaterValue = (WaterValue_h & 0x3f) * 16 + (WaterValue_l & 0x0f);
|
uint16_t WaterValue = (WaterValue_h & 0x3f) * 16 + (WaterValue_l & 0x0f);
|
||||||
float WaterLevel = WaterValue * (-0.235) + 199.75;
|
float WaterLevel = WaterValue * (-0.235) + 199.75;
|
||||||
WaterLevel = std::clamp(WaterLevel, 0.0f, 180.0f);
|
WaterLevel = std::clamp(WaterLevel, 0.0f, 180.0f);
|
||||||
info_report.waterLevel = static_cast<int>(std::round(WaterLevel / 1.8f)); // 液位百分比
|
updateWaterLevel(WaterLevel);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user