Skip to content

Commit

Permalink
Make sure TPLContext always has a sim object and protect member vars
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoffland committed Mar 26, 2019
1 parent 884c92c commit 5c23b8b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/tplang.cpp
Expand Up @@ -61,7 +61,7 @@ class TPLangApp : public CAMotics::CommandLineApp {
build(pipeline);

if (!simJSON.empty())
ctx.sim = JSON::Reader::parse(StringInputSource(simJSON));
ctx.setSim(JSON::Reader::parse(StringInputSource(simJSON)));

CommandLineApp::run();
}
Expand Down
65 changes: 33 additions & 32 deletions src/tplang/GCodeModule.cpp
Expand Up @@ -135,26 +135,27 @@ void GCodeModule::gcodeCB(const js::Value &args, js::Sink &sink) {
SmartFunctor<TPLContext> popPath(&ctx, &TPLContext::popPath);

// Push XYZ matrix so GCode coordinate rotations work correctly
TransformStack &stack = ctx.machine.getTransforms().get(MachineEnum::XYZ);
TransformStack &stack =
ctx.getMachine().getTransforms().get(MachineEnum::XYZ);
stack.push();
SmartFunctor<TransformStack> pop(&stack, &TransformStack::pop);

ControllerImpl controller(ctx.machine);
ControllerImpl controller(ctx.getMachine());
Interpreter(controller).read(path);
}


void GCodeModule::rapidCB(const js::Value &args, js::Sink &sink) {
Axes position = ctx.machine.getPosition();
Axes position = ctx.getMachine().getPosition();
int axes = parseAxes(args, position, args.getBoolean("incremental"));
ctx.machine.move(position, axes, true);
ctx.getMachine().move(position, axes, true);
}


void GCodeModule::cutCB(const js::Value &args, js::Sink &sink) {
Axes position = ctx.machine.getPosition();
Axes position = ctx.getMachine().getPosition();
int axes = parseAxes(args, position, args.getBoolean("incremental"));
ctx.machine.move(position, axes, false);
ctx.getMachine().move(position, axes, false);
}


Expand All @@ -163,45 +164,45 @@ void GCodeModule::arcCB(const js::Value &args, js::Sink &sink) {
offset(args.getNumber("x"), args.getNumber("y"), args.getNumber("z"));
double angle = args.has("angle") ? args.getNumber("angle") : (M_PI * 2);
plane_t plane = args.has("plane") ? (plane_t)args.getInteger("plane") : XY;
Vector3D start = ctx.machine.getPosition().getXYZ();
Vector3D start = ctx.getMachine().getPosition().getXYZ();

// Handle incremental=false
if (!args.getBoolean("incremental")) offset -= start;

Arc arc(start, offset, angle, plane);

ctx.machine.arc(offset, arc.getTarget(), angle, plane);
ctx.getMachine().arc(offset, arc.getTarget(), angle, plane);
}


void GCodeModule::probeCB(const js::Value &args, js::Sink &sink) {
ctx.machine.seek((MachineEnum::port_t)args.getInteger("port"),
ctx.getMachine().seek((MachineEnum::port_t)args.getInteger("port"),
args.getBoolean("active"), args.getBoolean("error"));

Axes position = ctx.machine.getPosition();
Axes position = ctx.getMachine().getPosition();
int axes = parseAxes(args, position);
ctx.machine.move(position, axes, false);
ctx.getMachine().move(position, axes, false);
}


void GCodeModule::dwellCB(const js::Value &args, js::Sink &sink) {
ctx.machine.dwell(args.getNumber("seconds"));
ctx.getMachine().dwell(args.getNumber("seconds"));
}


void GCodeModule::feedCB(const js::Value &args, js::Sink &sink) {
// Return feed info if no arguments were given
if (!args.has("rate")) {
sink.beginList();
sink.append(ctx.machine.getFeed());
sink.append(ctx.machine.getFeedMode());
sink.append(ctx.getMachine().getFeed());
sink.append(ctx.getMachine().getFeedMode());
sink.endList();

return;
}

// Otherwise set feed
ctx.machine.setFeed(args.getNumber("rate"));
ctx.getMachine().setFeed(args.getNumber("rate"));

if (args.has("mode")) {
feed_mode_t mode = (feed_mode_t)args.getInteger("mode");
Expand All @@ -212,10 +213,10 @@ void GCodeModule::feedCB(const js::Value &args, js::Sink &sink) {
"FEED_UNITS_PER_REV");
}

ctx.machine.setFeedMode(mode);
ctx.getMachine().setFeedMode(mode);
}

sink.write(ctx.machine.getFeed());
sink.write(ctx.getMachine().getFeed());
}


Expand All @@ -225,8 +226,8 @@ void GCodeModule::speedCB(const js::Value &args, js::Sink &sink) {
double max = 0;

sink.beginList();
sink.append(ctx.machine.getSpeed());
sink.append(ctx.machine.getSpinMode(&max));
sink.append(ctx.getMachine().getSpeed());
sink.append(ctx.getMachine().getSpinMode(&max));
if (max) sink.append(max);
sink.endList();

Expand All @@ -237,13 +238,13 @@ void GCodeModule::speedCB(const js::Value &args, js::Sink &sink) {
if (args.has("surface")) {
double max = 0;
if (args.has("max")) max = args.getNumber("max");
ctx.machine.setSpinMode(CONSTANT_SURFACE_SPEED, max);
ctx.getMachine().setSpinMode(CONSTANT_SURFACE_SPEED, max);

} else ctx.machine.setSpinMode(REVOLUTIONS_PER_MINUTE);
} else ctx.getMachine().setSpinMode(REVOLUTIONS_PER_MINUTE);

ctx.machine.setSpeed(args.getNumber("rate"));
ctx.getMachine().setSpeed(args.getNumber("rate"));

sink.write(ctx.machine.getSpeed());
sink.write(ctx.getMachine().getSpeed());
}


Expand All @@ -252,16 +253,16 @@ void GCodeModule::toolCB(const js::Value &args, js::Sink &sink) {

if (args.has("number")) {
number = args.getInteger("number");
ctx.machine.changeTool(number);
ctx.getMachine().changeTool(number);

} else number = ctx.machine.get(TOOL_NUMBER, Units::NO_UNITS);
} else number = ctx.getMachine().get(TOOL_NUMBER, Units::NO_UNITS);

if (number < 0) return;

if (!ctx.sim->hasDict("tools")) return;
if (!ctx.getSim().hasDict("tools")) return;

ToolTable tools;
tools.read(ctx.sim->getDict("tools"));
tools.read(ctx.getSim().getDict("tools"));

if (!tools.has(number)) return;

Expand Down Expand Up @@ -302,12 +303,12 @@ void GCodeModule::unitsCB(const js::Value &args, js::Sink &sink) {

void GCodeModule::pauseCB(const js::Value &args, js::Sink &sink) {
bool optional = args.getBoolean("optional");
ctx.machine.pause(optional ? PAUSE_OPTIONAL : PAUSE_PROGRAM);
ctx.getMachine().pause(optional ? PAUSE_OPTIONAL : PAUSE_PROGRAM);
}


void GCodeModule::positionCB(const js::Value &args, js::Sink &sink) {
Axes axes = ctx.machine.getPosition();
Axes axes = ctx.getMachine().getPosition();

sink.beginDict();

Expand All @@ -320,19 +321,19 @@ void GCodeModule::positionCB(const js::Value &args, js::Sink &sink) {

void GCodeModule::commentCB(const js::Value &args, js::Sink &sink) {
for (unsigned i = 0; i < args.length(); i++)
ctx.machine.comment(args.getString(i)); // TODO Call JSON.stringify()
ctx.getMachine().comment(args.getString(i)); // TODO Call JSON.stringify()
}


void GCodeModule::messageCB(const js::Value &args, js::Sink &sink) {
for (unsigned i = 0; i < args.length(); i++)
ctx.machine.message(args.getString(i)); // TODO Call JSON.stringify()
ctx.getMachine().message(args.getString(i)); // TODO Call JSON.stringify()
}


void GCodeModule::workpieceCB(const js::Value &args, js::Sink &sink) {
Rectangle3D workpiece;
workpiece.read(*ctx.sim->get("workpiece")->get("bounds"));
workpiece.read(*ctx.getSim().get("workpiece")->get("bounds"));

if (getUnitAdapter().getUnits() == Units::IMPERIAL)
workpiece = workpiece / 25.4;
Expand Down
4 changes: 2 additions & 2 deletions src/tplang/Interpreter.cpp
Expand Up @@ -26,7 +26,7 @@ using namespace std;


void Interpreter::read(const InputSource &source) {
ctx.machine.start();
ctx.getMachine().start();
ctx.eval(source);
ctx.machine.end();
ctx.getMachine().end();
}
10 changes: 5 additions & 5 deletions src/tplang/MatrixModule.cpp
Expand Up @@ -50,7 +50,7 @@ void MatrixModule::define(js::Sink &exports) {


GCode::TransformStack &MatrixModule::getTransformStack(const js::Value &args) {
return ctx.machine.getTransforms().get(parseAxes(args));
return ctx.getMachine().getTransforms().get(parseAxes(args));
}


Expand Down Expand Up @@ -114,7 +114,7 @@ MatrixModule::axes_t MatrixModule::parseAxes(const js::Value &args) {


void MatrixModule::getXYZ(const js::Value &args, js::Sink &sink) {
Vector3D v = ctx.machine.getPosition(XYZ);
Vector3D v = ctx.getMachine().getPosition(XYZ);

sink.beginList();
sink.append(v.x());
Expand All @@ -125,15 +125,15 @@ void MatrixModule::getXYZ(const js::Value &args, js::Sink &sink) {


void MatrixModule::getX(const js::Value &args, js::Sink &sink) {
sink.write(ctx.machine.getPosition(XYZ).x());
sink.write(ctx.getMachine().getPosition(XYZ).x());
}


void MatrixModule::getY(const js::Value &args, js::Sink &sink) {
sink.write(ctx.machine.getPosition(XYZ).y());
sink.write(ctx.getMachine().getPosition(XYZ).y());
}


void MatrixModule::getZ(const js::Value &args, js::Sink &sink) {
sink.write(ctx.machine.getPosition(XYZ).z());
sink.write(ctx.getMachine().getPosition(XYZ).z());
}
3 changes: 2 additions & 1 deletion src/tplang/TPLContext.cpp
Expand Up @@ -22,6 +22,7 @@

#include <cbang/Exception.h>
#include <cbang/os/SystemUtilities.h>
#include <cbang/json/Dict.h>

using namespace std;
using namespace cb;
Expand All @@ -31,7 +32,7 @@ using namespace tplang;
TPLContext::TPLContext(const SmartPointer<ostream> &stream,
GCode::MachineInterface &machine, const string &jsImpl) :
js::Javascript(jsImpl, stream), gcodeMod(*this), matrixMod(*this),
dxfMod(*this), stlMod(*this), machine(machine) {
dxfMod(*this), stlMod(*this), machine(machine), sim(new JSON::Dict) {

// Add modules
define(gcodeMod);
Expand Down
6 changes: 5 additions & 1 deletion src/tplang/TPLContext.h
Expand Up @@ -42,14 +42,18 @@ namespace tplang {
DXFModule dxfMod;
STLModule stlMod;

public:
GCode::MachineInterface &machine;
cb::JSON::ValuePtr sim;

public:
TPLContext(const cb::SmartPointer<std::ostream> &stream,
GCode::MachineInterface &machine,
const std::string &jsImpl = std::string());

GCode::MachineInterface &getMachine() {return machine;}
void setSim(const cb::JSON::ValuePtr &sim) {this->sim = sim;}
const cb::JSON::Value &getSim() const {return *sim;}

template <typename T>
T &find() {
GCode::MachineAdapter *adapter =
Expand Down

0 comments on commit 5c23b8b

Please sign in to comment.