| | |
| | | } |
| | | |
| | | // 构造函数 |
| | | RecipeManager::RecipeManager() : m_recipeFolder("Recipe") {} |
| | | RecipeManager::RecipeManager() : m_recipeFolder("Recipe"), m_currentRecipeName("") {} |
| | | |
| | | // 加载轴信息 |
| | | bool RecipeManager::loadAxes(pugi::xml_node axesNode) { |
| | | m_axes.clear(); |
| | | for (auto axisNode : axesNode.children("Axis")) { |
| | | AxisInfo axisInfo; |
| | | axisInfo.id = axisNode.attribute("id").as_int(); |
| | | axisInfo.number = axisNode.attribute("number").value(); |
| | | axisInfo.description = axisNode.attribute("description").value(); |
| | | axisInfo.startAddress = axisNode.attribute("start_address").value(); |
| | | |
| | | // 加载 ValueRange 值 |
| | | axisInfo.jogDistance = ValueRange( |
| | | axisNode.child("jog_distance").attribute("min").as_double(), |
| | | axisNode.child("jog_distance").attribute("max").as_double(), |
| | | axisNode.child("jog_distance").attribute("current").as_double() |
| | | ); |
| | | axisInfo.manualSpeed = ValueRange( |
| | | axisNode.child("manual_speed").attribute("min").as_double(), |
| | | axisNode.child("manual_speed").attribute("max").as_double(), |
| | | axisNode.child("manual_speed").attribute("current").as_double() |
| | | ); |
| | | axisInfo.autoSpeed = ValueRange( |
| | | axisNode.child("auto_speed").attribute("min").as_double(), |
| | | axisNode.child("auto_speed").attribute("max").as_double(), |
| | | axisNode.child("auto_speed").attribute("current").as_double() |
| | | ); |
| | | axisInfo.accelerationTime = ValueRange( |
| | | axisNode.child("acceleration_time").attribute("min").as_double(), |
| | | axisNode.child("acceleration_time").attribute("max").as_double(), |
| | | axisNode.child("acceleration_time").attribute("current").as_double() |
| | | ); |
| | | axisInfo.decelerationTime = ValueRange( |
| | | axisNode.child("deceleration_time").attribute("min").as_double(), |
| | | axisNode.child("deceleration_time").attribute("max").as_double(), |
| | | axisNode.child("deceleration_time").attribute("current").as_double() |
| | | ); |
| | | |
| | | // 加载 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("max").as_double(), |
| | | positionNode.attribute("current").as_double() |
| | | ); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | // 加载配方(如果文件不存在,加载默认数据) |
| | |
| | | |
| | | if (!doc.load_file(filePath.c_str())) { |
| | | std::cerr << "Recipe file not found: " << filePath << ". Loading default recipe." << std::endl; |
| | | generateDefaultRecipe(); |
| | | return false; // 文件不存在,但加载了默认数据 |
| | | return false; // 文件不存在 |
| | | } |
| | | m_currentRecipeName = recipeName; |
| | | |
| | | m_axes.clear(); |
| | | |
| | | auto recipe = doc.child("Recipe"); |
| | | for (auto axisNode : recipe.child("Axes").children("Axis")) { |
| | | AxisInfo axisInfo; |
| | | axisInfo.id = axisNode.attribute("id").as_int(); |
| | | axisInfo.number = axisNode.attribute("number").value(); |
| | | axisInfo.description = axisNode.attribute("description").value(); |
| | | axisInfo.startAddress = axisNode.attribute("start_address").value(); |
| | | axisInfo.jogDistance = axisNode.attribute("jog_distance").as_double(); |
| | | axisInfo.manualSpeed = axisNode.attribute("manual_speed").as_double(); |
| | | axisInfo.autoSpeed = axisNode.attribute("auto_speed").as_double(); |
| | | axisInfo.accelerationTime = axisNode.attribute("acceleration_time").as_double(); |
| | | axisInfo.decelerationTime = axisNode.attribute("deceleration_time").as_double(); |
| | | |
| | | for (auto positionNode : axisNode.child("Positions").children("Position")) { |
| | | std::string description = positionNode.attribute("description").value(); |
| | | double positionValue = positionNode.attribute("value").as_double(); |
| | | axisInfo.positions.emplace_back(description, positionValue); |
| | | } |
| | | |
| | | m_axes[axisInfo.id] = axisInfo; |
| | | } |
| | | 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(); |
| | | axisNode.append_attribute("jog_distance") = axisInfo.jogDistance; |
| | | axisNode.append_attribute("manual_speed") = axisInfo.manualSpeed; |
| | | axisNode.append_attribute("auto_speed") = axisInfo.autoSpeed; |
| | | axisNode.append_attribute("acceleration_time") = axisInfo.accelerationTime; |
| | | axisNode.append_attribute("deceleration_time") = axisInfo.decelerationTime; |
| | | |
| | | // 添加定位点列表 |
| | | auto positionsNode = axisNode.append_child("Positions"); |
| | | for (const auto& position : axisInfo.positions) { |
| | | auto positionNode = positionsNode.append_child("Position"); |
| | | positionNode.append_attribute("description") = position.first.c_str(); |
| | | positionNode.append_attribute("value") = position.second; |
| | | } |
| | | } |
| | | // 添加轴信息 |
| | | 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.number = "M100-M" + std::to_string(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.jogDistance = 0.5; |
| | | axisInfo.manualSpeed = 10.0; |
| | | axisInfo.autoSpeed = 15.0; |
| | | axisInfo.accelerationTime = 0.2; |
| | | axisInfo.decelerationTime = 0.3; |
| | | |
| | | for (int posId = 1; posId <= 25; ++posId) { |
| | | axisInfo.positions.emplace_back("Position " + std::to_string(posId), posId * 10.0); |
| | | // 设置默认的 ValueRange |
| | | axisInfo.jogDistance = ValueRange(1.0, 10.0, 1.0); |
| | | axisInfo.manualSpeed = ValueRange(1.0, 100.0, 10.0); |
| | | axisInfo.autoSpeed = ValueRange(5.0, 200.0, 50.0); |
| | | axisInfo.accelerationTime = ValueRange(1.0, 10.0, 1.0); |
| | | axisInfo.decelerationTime = ValueRange(1.0, 10.0, 1.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 RecipeManager::getAxis(int axisId) const { |
| | | auto it = m_axes.find(axisId); |
| | | if (it != m_axes.end()) { |
| | | return it->second; |
| | | return it->second; // 如果找到了轴,返回其详细信息 |
| | | } |
| | | |
| | | // 返回一个无效的 AxisInfo |
| | | return AxisInfo{ -1, "", "", "", 0.0, 0.0, 0.0, 0.0, 0.0, {}}; |
| | | // 如果没有找到该轴,返回一个默认的无效 AxisInfo |
| | | return AxisInfo{ -1, 0, /*0.0, 0.0,*/ "", "", "", ValueRange(), ValueRange(), ValueRange(), ValueRange(), ValueRange(), {} }; |
| | | } |
| | | |
| | | // 更新轴信息 |
| | |
| | | } |
| | | |
| | | // 获取指定页的定位点 |
| | | std::vector<std::pair<std::string, double>> RecipeManager::getPositions(int axisId, int pageNumber, int pageSize) const { |
| | | std::vector<std::pair<std::string, double>> result; |
| | | std::vector<PositionRange> RecipeManager::getPositions(int axisId, int pageNumber, int pageSize) const { |
| | | std::vector<PositionRange> result; |
| | | |
| | | // 检查轴是否存在 |
| | | auto it = m_axes.find(axisId); |
| | |
| | | // 遍历定位点,按分页提取数据 |
| | | int index = 0; |
| | | for (const auto& pos : positions) { |
| | | const std::string& description = pos.first; // 键:描述 |
| | | double value = pos.second; // 值:位置值 |
| | | const PositionRange& position = pos; // 定位点包含描述、位置、最小值、最大值和当前值 |
| | | |
| | | if (index >= startIndex && index < endIndex) { |
| | | result.emplace_back(description, value); |
| | | result.push_back(position); // 添加完整的 PositionRange 对象 |
| | | } |
| | | |
| | | ++index; |
| | |
| | | |
| | | return result; |
| | | } |
| | | |
| | | // 获取指定轴的定位点 |
| | | PositionRange RecipeManager::getPositionByIndex(int axisId, int pageNumber, int pageSize, int currentIndex) const { |
| | | // 检查轴是否存在 |
| | | auto it = m_axes.find(axisId); |
| | | if (it == m_axes.end()) { |
| | | return PositionRange(); // 轴不存在,返回默认构造的 PositionRange(无效的定位点) |
| | | } |
| | | |
| | | // 获取指定轴的所有定位点 |
| | | const auto& positions = it->second.positions; |
| | | |
| | | // 确定分页范围 |
| | | int startIndex = (pageNumber - 1) * pageSize; |
| | | int endIndex = startIndex + pageSize; |
| | | |
| | | // 如果 currentIndex 超过了当前页的范围,返回无效的 PositionRange |
| | | if (currentIndex < 0 || currentIndex >= pageSize || currentIndex + startIndex >= positions.size()) { |
| | | return PositionRange(); // 返回无效的定位点 |
| | | } |
| | | |
| | | // 返回指定定位点(考虑分页) |
| | | return positions[startIndex + currentIndex]; |
| | | } |