| | |
| | | } |
| | | |
| | | // 构造函数 |
| | | RecipeManager::RecipeManager() : m_recipeFolder("Recipe") {} |
| | | RecipeManager::RecipeManager() : m_recipeFolder("Recipe"), m_currentRecipeName("") {} |
| | | |
| | | // 设置配方文件夹 |
| | | void RecipeManager::setRecipeFolder(const std::string& folderPath) { |
| | | m_recipeFolder = folderPath; |
| | | } |
| | | |
| | | // 加载配方(如果文件不存在,加载默认数据) |
| | | bool RecipeManager::loadRecipe(const std::string& recipeName) { |
| | | std::string filePath = m_recipeFolder + "/" + recipeName + ".xml"; |
| | | pugi::xml_document doc; |
| | | |
| | | if (!doc.load_file(filePath.c_str())) { |
| | | std::cerr << "Recipe file not found: " << filePath << ". Loading default recipe." << std::endl; |
| | | generateDefaultRecipe(); |
| | | return false; // 文件不存在,但加载了默认数据 |
| | | } |
| | | |
| | | // 加载轴信息 |
| | | bool RecipeManager::loadAxes(pugi::xml_node axesNode) { |
| | | m_axes.clear(); |
| | | |
| | | auto recipe = doc.child("Recipe"); |
| | | for (auto axisNode : recipe.child("Axes").children("Axis")) { |
| | | for (auto axisNode : axesNode.children("Axis")) { |
| | | AxisInfo axisInfo; |
| | | axisInfo.id = axisNode.attribute("id").as_int(); |
| | | axisInfo.number = axisNode.attribute("number").value(); |
| | |
| | | ); |
| | | |
| | | // 加载 PositionRange 值 |
| | | axisInfo.positioningPointCount = axisNode.child("Positions").attribute("positioningPointCount").as_int(); |
| | | for (auto positionNode : axisNode.child("Positions").children("Position")) { |
| | | bool isEnable = positionNode.attribute("isEnable").as_bool(); |
| | | std::string description = positionNode.attribute("description").value(); |
| | | ValueRange positionRange( |
| | | positionNode.attribute("min").as_double(), |
| | |
| | | positionNode.attribute("current").as_double() |
| | | ); |
| | | |
| | | axisInfo.positions.emplace_back(PositionRange(description, positionRange)); |
| | | axisInfo.positions.emplace_back(PositionRange(isEnable, description, positionRange)); |
| | | } |
| | | |
| | | m_axes[axisInfo.id] = axisInfo; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | // 保存轴信息 |
| | | void RecipeManager::saveAxes(pugi::xml_node& axesNode) { |
| | | for (const auto& axisEntry : m_axes) { |
| | | const AxisInfo& axisInfo = axisEntry.second; |
| | | |
| | | auto axisNode = axesNode.append_child("Axis"); |
| | | axisNode.append_attribute("id") = axisInfo.id; |
| | | axisNode.append_attribute("number") = axisInfo.number.c_str(); |
| | | axisNode.append_attribute("description") = axisInfo.description.c_str(); |
| | | axisNode.append_attribute("start_address") = axisInfo.startAddress.c_str(); |
| | | |
| | | // 保存 ValueRange 值 |
| | | auto jog_distance = axisNode.append_child("jog_distance"); |
| | | jog_distance.append_attribute("min") = axisInfo.jogDistance.minValue; |
| | | jog_distance.append_attribute("max") = axisInfo.jogDistance.maxValue; |
| | | jog_distance.append_attribute("current") = axisInfo.jogDistance.currentValue; |
| | | |
| | | auto manual_speed = axisNode.append_child("manual_speed"); |
| | | manual_speed.append_attribute("min") = axisInfo.manualSpeed.minValue; |
| | | manual_speed.append_attribute("max") = axisInfo.manualSpeed.maxValue; |
| | | manual_speed.append_attribute("current") = axisInfo.manualSpeed.currentValue; |
| | | |
| | | auto auto_speed = axisNode.append_child("auto_speed"); |
| | | auto_speed.append_attribute("min") = axisInfo.autoSpeed.minValue; |
| | | auto_speed.append_attribute("max") = axisInfo.autoSpeed.maxValue; |
| | | auto_speed.append_attribute("current") = axisInfo.autoSpeed.currentValue; |
| | | |
| | | auto acceleration_time = axisNode.append_child("acceleration_time"); |
| | | acceleration_time.append_attribute("min") = axisInfo.accelerationTime.minValue; |
| | | acceleration_time.append_attribute("max") = axisInfo.accelerationTime.maxValue; |
| | | acceleration_time.append_attribute("current") = axisInfo.accelerationTime.currentValue; |
| | | |
| | | auto deceleration_time = axisNode.append_child("deceleration_time"); |
| | | deceleration_time.append_attribute("min") = axisInfo.decelerationTime.minValue; |
| | | deceleration_time.append_attribute("max") = axisInfo.decelerationTime.maxValue; |
| | | deceleration_time.append_attribute("current") = axisInfo.decelerationTime.currentValue; |
| | | |
| | | // 保存 PositionRange 值 |
| | | auto positionsNode = axisNode.append_child("Positions"); |
| | | positionsNode.append_attribute("positioningPointCount") = axisInfo.positioningPointCount; |
| | | for (const auto& position : axisInfo.positions) { |
| | | auto positionNode = positionsNode.append_child("Position"); |
| | | positionNode.append_attribute("isEnable") = position.isEnable; |
| | | positionNode.append_attribute("description") = position.description.c_str(); |
| | | positionNode.append_attribute("min") = position.range.minValue; |
| | | positionNode.append_attribute("max") = position.range.maxValue; |
| | | positionNode.append_attribute("current") = position.range.currentValue; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 设置配方文件夹 |
| | | void RecipeManager::setRecipeFolder(const std::string& folderPath) { |
| | | m_recipeFolder = folderPath; |
| | | } |
| | | |
| | | // 获取当前配方名称 |
| | | std::string RecipeManager::getCurrentRecipeName() const { |
| | | return m_currentRecipeName; |
| | | } |
| | | |
| | | // 加载配方(如果文件不存在,加载默认数据) |
| | | bool RecipeManager::loadRecipe(const std::string& recipeName) { |
| | | std::string filePath = m_recipeFolder + "/" + recipeName + ".xml"; |
| | | pugi::xml_document doc; |
| | | |
| | | if (!doc.load_file(filePath.c_str())) { |
| | | std::cerr << "Recipe file not found: " << filePath << ". Loading default recipe." << std::endl; |
| | | return false; // 文件不存在 |
| | | } |
| | | m_currentRecipeName = recipeName; |
| | | |
| | | auto recipeNode = doc.child("Recipe"); |
| | | auto axesNode = recipeNode.child("Axes"); |
| | | loadAxes(axesNode); // 加载轴信息 |
| | | |
| | | return true; |
| | | } |
| | |
| | | } |
| | | |
| | | // 添加配方根节点 |
| | | auto recipe = doc.append_child("Recipe"); |
| | | auto recipeNode = doc.append_child("Recipe"); |
| | | |
| | | // 添加轴列表节点 |
| | | auto axesNode = recipe.append_child("Axes"); |
| | | |
| | | // 遍历所有轴数据并写入 XML |
| | | for (const auto& axisEntry : m_axes) { |
| | | const AxisInfo& axisInfo = axisEntry.second; |
| | | |
| | | auto axisNode = axesNode.append_child("Axis"); |
| | | axisNode.append_attribute("id") = axisInfo.id; |
| | | axisNode.append_attribute("number") = axisInfo.number.c_str(); |
| | | axisNode.append_attribute("description") = axisInfo.description.c_str(); |
| | | axisNode.append_attribute("start_address") = axisInfo.startAddress.c_str(); |
| | | |
| | | // 保存 ValueRange 值 |
| | | axisNode.append_child("jog_distance").append_attribute("min") = axisInfo.jogDistance.minValue; |
| | | axisNode.append_child("jog_distance").append_attribute("max") = axisInfo.jogDistance.maxValue; |
| | | axisNode.append_child("jog_distance").append_attribute("current") = axisInfo.jogDistance.currentValue; |
| | | |
| | | axisNode.append_child("manual_speed").append_attribute("min") = axisInfo.manualSpeed.minValue; |
| | | axisNode.append_child("manual_speed").append_attribute("max") = axisInfo.manualSpeed.maxValue; |
| | | axisNode.append_child("manual_speed").append_attribute("current") = axisInfo.manualSpeed.currentValue; |
| | | |
| | | axisNode.append_child("auto_speed").append_attribute("min") = axisInfo.autoSpeed.minValue; |
| | | axisNode.append_child("auto_speed").append_attribute("max") = axisInfo.autoSpeed.maxValue; |
| | | axisNode.append_child("auto_speed").append_attribute("current") = axisInfo.autoSpeed.currentValue; |
| | | |
| | | axisNode.append_child("acceleration_time").append_attribute("min") = axisInfo.accelerationTime.minValue; |
| | | axisNode.append_child("acceleration_time").append_attribute("max") = axisInfo.accelerationTime.maxValue; |
| | | axisNode.append_child("acceleration_time").append_attribute("current") = axisInfo.accelerationTime.currentValue; |
| | | |
| | | axisNode.append_child("deceleration_time").append_attribute("min") = axisInfo.decelerationTime.minValue; |
| | | axisNode.append_child("deceleration_time").append_attribute("max") = axisInfo.decelerationTime.maxValue; |
| | | axisNode.append_child("deceleration_time").append_attribute("current") = axisInfo.decelerationTime.currentValue; |
| | | |
| | | // 保存 PositionRange 值 |
| | | auto positionsNode = axisNode.append_child("Positions"); |
| | | for (const auto& position : axisInfo.positions) { |
| | | auto positionNode = positionsNode.append_child("Position"); |
| | | positionNode.append_attribute("description") = position.description.c_str(); |
| | | positionNode.append_attribute("min") = position.range.minValue; |
| | | positionNode.append_attribute("max") = position.range.maxValue; |
| | | positionNode.append_attribute("current") = position.range.currentValue; |
| | | } |
| | | } |
| | | // 添加轴信息 |
| | | auto axesNode = recipeNode.append_child("Axes"); |
| | | saveAxes(axesNode); |
| | | |
| | | // 保存 XML 文件 |
| | | return doc.save_file(filePath.c_str()); |
| | |
| | | // 生成默认配方 |
| | | void RecipeManager::generateDefaultRecipe() { |
| | | m_axes.clear(); |
| | | m_currentRecipeName = "Default"; |
| | | |
| | | for (int axisId = 1; axisId <= 12; ++axisId) { |
| | | AxisInfo axisInfo; |
| | | axisInfo.id = axisId; |
| | | axisInfo.positioningPointCount = 25; |
| | | axisInfo.number = "M100-M" + std::to_string(axisId); |
| | | axisInfo.description = "Default_Axis" + std::to_string(axisId); |
| | | axisInfo.startAddress = "ZR" + std::to_string(10000 + (axisId - 1) * 300); |
| | |
| | | axisInfo.decelerationTime = ValueRange(1.0, 10.0, 1.0); |
| | | |
| | | // 添加定位点并设置默认的最小值和最大值 |
| | | for (int posId = 1; posId <= 25; ++posId) { |
| | | double minPos = posId * 5.0; |
| | | double maxPos = posId * 20.0; |
| | | axisInfo.positions.emplace_back(PositionRange("Position " + std::to_string(posId), ValueRange(minPos, maxPos, posId * 10.0))); |
| | | for (int posId = 0; posId < axisInfo.positioningPointCount; ++posId) { |
| | | double minPos = (posId + 1) * 5.0; |
| | | double maxPos = (posId + 1) * 20.0; |
| | | axisInfo.positions.emplace_back(PositionRange(TRUE, "Position " + std::to_string(posId + 1), ValueRange(minPos, maxPos, (posId + 1) * 10.0))); |
| | | } |
| | | |
| | | m_axes[axisId] = axisInfo; |
| | |
| | | } |
| | | |
| | | // 如果没有找到该轴,返回一个默认的无效 AxisInfo |
| | | return AxisInfo{ -1, "", "", "", ValueRange(), ValueRange(), ValueRange(), ValueRange(), ValueRange(), {} }; |
| | | return AxisInfo{ -1, 0, /*0.0, 0.0,*/ "", "", "", ValueRange(), ValueRange(), ValueRange(), ValueRange(), ValueRange(), {} }; |
| | | } |
| | | |
| | | // 更新轴信息 |