Skip to content

Commit

Permalink
Merge pull request #45 from GenerateNU/feature/manual-input-to-gcode
Browse files Browse the repository at this point in the history
Converting Manual Input to GCode. NTD: Anvil Positions
  • Loading branch information
muneerlalji authored Mar 28, 2024
2 parents 18542f3 + 026e60e commit 04d3b7c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
26 changes: 19 additions & 7 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ find_package(OpenCASCADE REQUIRED)
include_directories("/usr/include/opencascade/")

add_executable(cad_to_gcode
src/cad_to_gcode.cpp
src/iges_to_topo.cpp
src/step_to_topo.cpp
src/shape_to_bspline.cpp
src/tangent_vectors_geom_bspline.cpp
src/gcode_from_vectors.cpp
# src/cad_to_gcode.cpp
# src/iges_to_topo.cpp
# src/step_to_topo.cpp
# src/shape_to_bspline.cpp
# src/tangent_vectors_geom_bspline.cpp
# src/gcode_from_vectors.cpp
src/manual_input_to_gcode.cpp
)
target_link_libraries(cad_to_gcode ${OpenCASCADE_LIBRARIES})

# add_executable(iges_to_topo src/iges_to_topo.cpp)
# add_executable(step_to_topo src/step_to_topo.cpp)
# add_executable(shape_to_bspline src/shape_to_bspline.cpp)
# add_executable(tangent_vectors_geom_bspline src/tangent_vectors_geom_bspline.cpp)
add_executable(manual_input_to_gcode src/manual_input_to_gcode.cpp)
# target_link_libraries(cad_to_gcode ${OpenCASCADE_LIBRARIES})
# target_link_libraries(iges_to_topo ${OpenCASCADE_LIBRARIES})
# target_link_libraries(shape_to_bspline ${OpenCASCADE_LIBRARIES})
# target_link_libraries(tangent_vectors_geom_bspline ${OpenCASCADE_LIBRARIES})
# target_link_libraries(step_to_topo ${OpenCASCADE_LIBRARIES})
target_link_libraries(cad_to_gcode ${OpenCASCADE_LIBRARIES})
73 changes: 73 additions & 0 deletions backend/src/manual_input_to_gcode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include "manual_input_to_gcode.hpp"

std::string generateAnvilPosition(const int bendRadius) {
return "8.907";
}

void getAbsolutePositionGCode(std::ofstream &outFile){
outFile << "G90" << std::endl;
outFile << "G1 X0 Y0 Z0 F80" << std::endl;
outFile << "G91" << std::endl;
}

// NTD: Figure out + vs - for bend direction
void generateGCode(const std::vector<Bend> &bends, const std::string &outputPath) {
std::ofstream outFile(outputPath);
if (!outFile.is_open()) {
std::cerr << "Error opening output file." << std::endl;
return;
}
std::cout << "Starting G-code generation..." << std::endl;
// Constants
const int anvilFeedRate = 80; // Avil feed rate
const int tubeFeedRate = 150; // Tube feed rate
// Iterate over each bend
for (const auto &bend : bends) {
// Set Anvil for Use
getAbsolutePositionGCode(outFile);
// Push Tube Striaght Through the Machine
outFile << "G1 "
<< "Z" << bend.tubeLengthBeforeBend << " "
<< "F" << tubeFeedRate << std::endl;
// Position the anvil
char direction = bend.bendDirection[0];
char bendSign = bend.bendDirection[1];
outFile << "G1 "
// << std::toupper(static_cast<unsigned char>(bend.bendDirection[0])) << generateAnvilPosition(bend.bendRadius) << " "
<< char(toupper(direction)) << bendSign << generateAnvilPosition(bend.bendRadius) << " "
<< "F" << anvilFeedRate << std::endl;
// Push Tube Bend Through the Machine
outFile << "G1 "
<< "Z" << bend.bendArcLength << " "
<< "F" << tubeFeedRate << std::endl;
// Check if last bend
if (&bend == &bends.back()) {
// If last bend, push tube straight through the machine
getAbsolutePositionGCode(outFile);
outFile << "G1 "
<< "Z" << bend.tubeLengthAfterBend << " "
<< "F" << tubeFeedRate << std::endl;
}
}
outFile.close();
std::cout << "G-code generated successfully at " << outputPath << std::endl;
}


int main(int argc, char *argv[]) {
std::cout << "Program started." << std::endl;
std::string outputPath = "output.gcode";
Bend bendsArray[2] = {
{308.864, "y+", 4.25, 128.016, 333.502},
{333.502, "y-", 4.25, 55.372, 150.876}
};
std::vector<Bend> bends(bendsArray, bendsArray + 2);
// Generate G-code
generateGCode(bends, outputPath);
std::cout << "Program completed." << std::endl;
return 0;
}
19 changes: 19 additions & 0 deletions backend/src/manual_input_to_gcode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef GCODE_FROM_MANUAL_INPUT
#define GCODE_FROM_MANUAL_INPUT

#include <vector>
#include <string>

struct Bend {
double tubeLengthBeforeBend;
std::string bendDirection;
double bendRadius;
double bendArcLength;
double tubeLengthAfterBend;
};

std::string generateAnvilPosition(const int bendRadius);
void getAbsolutePositionGCode(std::ofstream &outFile);
void generateGCode(const std::vector<Bend> &bends, const std::string &outputPath);

#endif

0 comments on commit 04d3b7c

Please sign in to comment.