1. 优化。

main
baocm 8 months ago
parent 47336c9a07
commit 1fd0a4562d

@ -54,6 +54,9 @@ PublisherApp::PublisherApp(
, summary_topic_(nullptr) , summary_topic_(nullptr)
, summary_writer_(nullptr) , summary_writer_(nullptr)
, summary_type_(new WeighingSystem::SummaryUpdatePubSubType()) , summary_type_(new WeighingSystem::SummaryUpdatePubSubType())
, print_topic_(nullptr)
, print_writer_(nullptr)
, print_type_(new InternalSystem::PrintReqPubSubType())
, matched_(0) , matched_(0)
, samples_sent_(0) , samples_sent_(0)
, stop_(false) , stop_(false)
@ -76,6 +79,7 @@ PublisherApp::PublisherApp(
bar_type_.register_type(participant_); bar_type_.register_type(participant_);
light_type_.register_type(participant_); light_type_.register_type(participant_);
summary_type_.register_type(participant_); summary_type_.register_type(participant_);
print_type_.register_type(participant_);
// Create the publisher // Create the publisher
PublisherQos pub_qos = PUBLISHER_QOS_DEFAULT; PublisherQos pub_qos = PUBLISHER_QOS_DEFAULT;
@ -166,6 +170,33 @@ PublisherApp::PublisherApp(
{ {
throw std::runtime_error("WeighingSystem::SummaryUpdate DataWriter initialization failed"); throw std::runtime_error("WeighingSystem::SummaryUpdate DataWriter initialization failed");
} }
// Create the topic
topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos);
print_topic_ = participant_->create_topic("PrintReq", print_type_.get_type_name(), topic_qos);
if (print_topic_ == nullptr)
{
throw std::runtime_error("PrintReq Topic initialization failed");
}
// Create the data writer
writer_qos = DATAWRITER_QOS_DEFAULT;
publisher_->get_default_datawriter_qos(writer_qos);
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
writer_qos.reliability().max_blocking_time = Duration_t(1, 0);
writer_qos.durability().kind = DurabilityQosPolicyKind::VOLATILE_DURABILITY_QOS;
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
writer_qos.history().depth = 1;
writer_qos.resource_limits().max_samples = 200;
writer_qos.resource_limits().max_instances = 1;
writer_qos.resource_limits().max_samples_per_instance = 100;
writer_qos.data_sharing().off();
print_writer_ = publisher_->create_datawriter(print_topic_, writer_qos, this, StatusMask::all());
if (print_writer_ == nullptr)
{
throw std::runtime_error("InternalSystem::PrintReq DataWriter initialization failed");
}
} }
PublisherApp::~PublisherApp() PublisherApp::~PublisherApp()
@ -288,6 +319,22 @@ bool PublisherApp::light_ctrl(const std::map<std::string, std::string>& ctrl)
return true; return true;
} }
bool PublisherApp::print_receipt(const std::map<std::string, std::string>& msg)
{
InternalSystem::PrintReq req;
req.msg() = msg;
for (auto &m : req.msg())
{
std::cout << "first: " << m.first << " second: " << m.second << std::endl;
}
print_writer_->write(&req);
return true;
}
void PublisherApp::run(std::shared_ptr<mqtt::async_client> dev) void PublisherApp::run(std::shared_ptr<mqtt::async_client> dev)
{ {
uint8_t send_sum = 0; uint8_t send_sum = 0;
@ -418,6 +465,40 @@ void PublisherApp::run(std::shared_ptr<mqtt::async_client> dev)
WeighingSystem::WeightInfoOk info = std::move(DdsMsgData::WeightInfoOk_queue_.front()); WeighingSystem::WeightInfoOk info = std::move(DdsMsgData::WeightInfoOk_queue_.front());
DdsMsgData::WeightInfoOk_queue_.pop(); DdsMsgData::WeightInfoOk_queue_.pop();
dds_lock.unlock(); dds_lock.unlock();
std::map<std::string, std::string> print_msg;
auto formatFloat = [](float value) -> std::string
{
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << value;
return oss.str();
};
print_msg.clear();
print_msg["Title"] = "过衡单";
print_msg["DeviceID"] = std::to_string(info.DeviceID());
print_msg["DeviceName"] = info.DeviceName();
print_msg["TitleCorp"] = info.EnterpriseName();
print_msg["CoalNumber"] = info.BillNumber();
print_msg["CarNumber"] = info.CarNumber();
print_msg["Supplier"] = info.Supplier();
print_msg["GoodsType"] = info.GoodsType();
print_msg["GrossWeight"] = formatFloat(info.GrossWeight());
print_msg["GrossTime"] = info.GrossTime();
print_msg["GrossOperater"] = info.GrossOperater();
print_msg["TareWeight"] = formatFloat(info.Tare());
print_msg["TareTime"] = info.TareTime();
print_msg["TareOperater"] = info.TareOperater();
print_msg["SuttleWeight"] = formatFloat(info.Suttle());
print_msg["duduction1"] = formatFloat(info.duduction1());
print_msg["duduction2"] = formatFloat(info.duduction2());
print_msg["duduction3"] = formatFloat(info.duduction3());
print_msg["INCoalYard"] = info.INCoalYard();
print_msg["OUTCoalYard"] = info.OutCoalYard();
print_msg["BatchNumber"] = info.BatchNumber();
print_msg["Warning"] = info.Warning();
print_receipt(print_msg);
} }
else if (!DdsMsgData::WeightInfoError_queue_.empty()) else if (!DdsMsgData::WeightInfoError_queue_.empty())
{ {

@ -53,6 +53,7 @@ public:
bool bar_ctrl(const std::map<std::string, std::string>& ctrl); bool bar_ctrl(const std::map<std::string, std::string>& ctrl);
bool light_ctrl(const std::map<std::string, std::string>& ctrl); bool light_ctrl(const std::map<std::string, std::string>& ctrl);
bool print_receipt(const std::map<std::string, std::string>& msg);
private: private:
@ -74,6 +75,9 @@ private:
eprosima::fastdds::dds::Topic* summary_topic_; eprosima::fastdds::dds::Topic* summary_topic_;
eprosima::fastdds::dds::DataWriter* summary_writer_; eprosima::fastdds::dds::DataWriter* summary_writer_;
eprosima::fastdds::dds::TypeSupport summary_type_; eprosima::fastdds::dds::TypeSupport summary_type_;
eprosima::fastdds::dds::Topic* print_topic_;
eprosima::fastdds::dds::DataWriter* print_writer_;
eprosima::fastdds::dds::TypeSupport print_type_;
std::condition_variable cv_; std::condition_variable cv_;
int32_t matched_; int32_t matched_;
std::mutex mutex_; std::mutex mutex_;

@ -8,12 +8,12 @@
MsgHandler::MsgHandler() : fd(-1) MsgHandler::MsgHandler() : fd(-1)
{ {
this->iomap.omap["FrontBarUp"] = {1, 1}; this->iomap.omap["FrontBarUp"] = {2, 0};
this->iomap.omap["FrontBarDown"] = {2, 1}; this->iomap.omap["FrontBarDown"] = {1, 0};
this->iomap.omap["BackBarUp"] = {3, 1}; this->iomap.omap["BackBarUp"] = {3, 0};
this->iomap.omap["BackBarDown"] = {4, 1}; this->iomap.omap["BackBarDown"] = {4, 0};
this->iomap.omap["FrontLED"] = {5, 1}; this->iomap.omap["FrontLED"] = {5, 0};
this->iomap.omap["BackLED"] = {6, 1}; this->iomap.omap["BackLED"] = {6, 0};
this->iomap.imap["FrontResistance"] = {5, 1}; this->iomap.imap["FrontResistance"] = {5, 1};
this->iomap.imap["BackResistance"] = {6, 1}; this->iomap.imap["BackResistance"] = {6, 1};
@ -143,7 +143,7 @@ void MsgHandler::HandleDdsMsg(const std::map<std::string, std::string>& msg)
bool MsgHandler::CtrlBar(const std::vector<std::string>& port) bool MsgHandler::CtrlBar(const std::vector<std::string>& port)
{ {
std::vector<uint8_t> data; std::vector<uint8_t> data;
data.resize(8, 1); data.resize(8, 0);
data.insert(data.begin(), {this->device_id, 0x51, 1}); data.insert(data.begin(), {this->device_id, 0x51, 1});
for (const auto &port : this->iomap.omap) for (const auto &port : this->iomap.omap)
{ {
@ -155,20 +155,20 @@ bool MsgHandler::CtrlBar(const std::vector<std::string>& port)
auto port = this->iomap.omap.find(p); auto port = this->iomap.omap.find(p);
if (port != this->iomap.omap.end()) if (port != this->iomap.omap.end())
{ {
port->second.second = 0; port->second.second = 1;
data[port->second.first + 2] = port->second.second; data[port->second.first + 2] = port->second.second;
} }
} }
SendDeviceMsg(data); SendDeviceMsg(data);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(2));
for (const std::string& p : port) for (const std::string& p : port)
{ {
auto port = this->iomap.omap.find(p); auto port = this->iomap.omap.find(p);
if (port != this->iomap.omap.end()) if (port != this->iomap.omap.end())
{ {
port->second.second = 1; port->second.second = 0;
data[port->second.first + 2] = port->second.second; data[port->second.first + 2] = port->second.second;
} }
} }
@ -181,7 +181,7 @@ bool MsgHandler::CtrlBar(const std::vector<std::string>& port)
bool MsgHandler::CtrlLight(const std::map<std::string, uint8_t>& port) bool MsgHandler::CtrlLight(const std::map<std::string, uint8_t>& port)
{ {
std::vector<uint8_t> data; std::vector<uint8_t> data;
data.resize(8, 1); data.resize(8, 0);
data.insert(data.begin(), {this->device_id, 0x51, 1}); data.insert(data.begin(), {this->device_id, 0x51, 1});
for (const auto &port : this->iomap.omap) for (const auto &port : this->iomap.omap)
{ {

File diff suppressed because it is too large Load Diff

@ -45,21 +45,8 @@ private:
void Page_End(std::vector<uint8_t>& Data); void Page_End(std::vector<uint8_t>& Data);
void GtstrLength(int iVal, char *p); void GtstrLength(int iVal, char *p);
void Print1_1(const std::map<std::string, std::string>& msg);
void Print1_2(const std::map<std::string, std::string>& msg);
void Print2_2(const std::map<std::string, std::string>& msg);
void Print3_2(const std::map<std::string, std::string>& msg);
void Print4_2(const std::map<std::string, std::string>& msg);
void Print5_2(const std::map<std::string, std::string>& msg);
void Print6_2(const std::map<std::string, std::string>& msg);
void Print1_3(const std::map<std::string, std::string>& msg); void Print1_3(const std::map<std::string, std::string>& msg);
void Print2_3(const std::map<std::string, std::string>& msg);
void Print4_3(const std::map<std::string, std::string>& msg);
void Print5_3(const std::map<std::string, std::string>& msg);
void Print6_3(const std::map<std::string, std::string>& msg);
void Print8_3(const std::map<std::string, std::string>& msg);
}; };
#endif #endif

@ -76,7 +76,7 @@ SubscriberApp::SubscriberApp(
// Create the topic // Create the topic
TopicQos topic_qos = TOPIC_QOS_DEFAULT; TopicQos topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos); participant_->get_default_topic_qos(topic_qos);
topic_ = participant_->create_topic("PrintReqTopic", type_.get_type_name(), topic_qos); topic_ = participant_->create_topic("PrintReq", type_.get_type_name(), topic_qos);
if (topic_ == nullptr) if (topic_ == nullptr)
{ {
throw std::runtime_error("InternalSystem::PrintReq Topic initialization failed"); throw std::runtime_error("InternalSystem::PrintReq Topic initialization failed");
@ -86,7 +86,7 @@ SubscriberApp::SubscriberApp(
DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT; DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT;
subscriber_->get_default_datareader_qos(reader_qos); subscriber_->get_default_datareader_qos(reader_qos);
reader_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS; reader_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
reader_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS; reader_qos.durability().kind = DurabilityQosPolicyKind::VOLATILE_DURABILITY_QOS;
reader_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS; reader_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
reader_ = subscriber_->create_datareader(topic_, reader_qos, this, StatusMask::all()); reader_ = subscriber_->create_datareader(topic_, reader_qos, this, StatusMask::all());
if (reader_ == nullptr) if (reader_ == nullptr)
@ -133,7 +133,7 @@ void SubscriberApp::on_data_available(
std::string topic_name = reader->get_topicdescription()->get_name(); std::string topic_name = reader->get_topicdescription()->get_name();
std::cout << topic_name << std::endl; std::cout << topic_name << std::endl;
if (topic_name == "PrintReqTopic") if (topic_name == "PrintReq")
{ {
InternalSystem::PrintReq sample_; InternalSystem::PrintReq sample_;
while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info))) while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info)))

@ -78,7 +78,7 @@ int main(int argc, char** argv)
const char* interface = "serial"; const char* interface = "serial";
const char* port = "ttyS1"; const char* port = "ttyS1";
const char* baudrate = "9600"; const char* baudrate = "9600";
const char* device = "Toledo"; const char* device = "NP80A";
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {

@ -155,7 +155,7 @@ void PublisherApp::run(std::shared_ptr<MsgHandler> handler)
config.min_weight_threshold = handler->empty_weight; // 300kg开始检测 config.min_weight_threshold = handler->empty_weight; // 300kg开始检测
config.empty_car_threshold = handler->empty_weight; // 默认300kg为空车 config.empty_car_threshold = handler->empty_weight; // 默认300kg为空车
config.fast_drop_threshold = 1000.0f; // 快速下降1吨认为车辆离开 config.fast_drop_threshold = 1000.0f; // 快速下降1吨认为车辆离开
config.required_stable_count = 6; // 6次稳定即可 config.required_stable_count = 20; // 20次稳定即可
config.min_jitter_count = 2; // 至少2次非增长 config.min_jitter_count = 2; // 至少2次非增长
config.timeout_extra = 3; // 超时额外次数 config.timeout_extra = 3; // 超时额外次数
config.window_size = 8; // 窗口大小8 config.window_size = 8; // 窗口大小8

Loading…
Cancel
Save