3d-printer-controller 中文文档教程

发布于 5年前 浏览 26 项目主页 更新于 3年前

3D printer controller

此模块可帮助您从 JavaScript(和 TypeScript)控制您的 3d 打印机。 该模块使用承诺,因此您可以等待电机移动完成。

Features

  • Send custom GCode to the printer
  • Move the nozzle to an absolute position
  • Move the nozzle with a relative position
  • Set the speed of movement in mm/s
  • Get current position, get target position
  • Auto home the nozzle on the specified axes.

Get started

  1. Add the module to your dependencies: shell script npm i 3d-printer-controller --save
  2. Create a printer instance in your code and call its .init() function:
   import {Printer} from "3d-printer-controller";

   (async () => {
       const myPrinter = new Printer("COM5", 115200, {x: 220, y: 220, z: 300});
       await myPrinter.init();
   })();

请注意,您应该提供正确大小的打印区域,以防您不希望打印机损坏。 如果要使用 await,则必须在异步函数中编写代码。

API

Cosntructor

new Printer(port: string, baudRate: number, maxPrintSize: Vector3D);
    // Vector3D is an object like {x: number, y: number, z: number}

示例:

const myPrinter = new Printer("COM5", 115200, {x: 220, y: 220, z: 300});

Initialise the 3D printer

await myPrinter.init();

您必须在使用打印机之前调用此方法。

Auto home

await myPrinter.autoHome(["X", "Y", "Z"]);

这会在 X、Y 和 Z 轴上自动归位喷嘴。

有一个特殊的功能可以自动将喷嘴定位在 X 和 Y 轴上。

await myPrinter.autoHomeXY();

Send GCode

您可以使用此函数发送 GCode 字符串或 GCode 字符串数组:

// string:
await myPrinter.sendGCode("G28");

// array:
await myPrinter.sendGCode([
    "G91",
    "G0 F4800",
    "G0 X10 Y40"
]);

Set the speed of the movement

await myPrinter.setSpeed(80);

以毫米/秒为单位设置移动速度。

Go to a specified position

您可以使喷嘴以毫米为单位到达指定位置。 您可以直接指定矢量或矢量的 X、Y(可选)、Z(可选)分量:

await myPrinter.goTo({x: 30, y: 60, z: 1}); // The nozzle goes to (30; 60; 1)
await myPrinter.goTo(50, 90, 2); // The nozzle goes to (50; 90; 2).
await myPrinter.goTo(30, 50); // The z value was not specified, so it goes remains the same as in the previous movement. The new position will be (30; 50; 2).

Get current position

const currentPos = await myPrinter.getCurrentPosition();
console.log(currentPos); // {x: number, y: number, z: number}

返回喷嘴的当前位置。 该值在喷嘴移动时发生变化。 一旦喷嘴完成其移动,这将等于目标位置。

Get target position

const targetPos = await myPrinter.getTargetPosition();
console.log(targetPos); // {x: number, y: number, z: number}

返回喷嘴的目标位置。

Get current position and target position at once

const posData = await myPrinter.getPositionData();
const currentPos = posData.currentPosition;
const targetPos = posData.targetPosition;

这种方法只是为了节省时间,当我们需要将两个位置都放在一个地方时(例如等待喷嘴完成运动时)。

3D printer controller

This module helps you control your 3d printer from JavaScript (& TypeScript). The module uses promises, so you can wait for the motor moves to be completed.

Features

  • Send custom GCode to the printer
  • Move the nozzle to an absolute position
  • Move the nozzle with a relative position
  • Set the speed of movement in mm/s
  • Get current position, get target position
  • Auto home the nozzle on the specified axes.

Get started

  1. Add the module to your dependencies: shell script npm i 3d-printer-controller --save
  2. Create a printer instance in your code and call its .init() function:
   import {Printer} from "3d-printer-controller";

   (async () => {
       const myPrinter = new Printer("COM5", 115200, {x: 220, y: 220, z: 300});
       await myPrinter.init();
   })();

Note, that you should provide the correct size of the printing area in case you don't want your printer be broken. You have to write your code inside an async function if you want to use await.

API

Cosntructor

new Printer(port: string, baudRate: number, maxPrintSize: Vector3D);
    // Vector3D is an object like {x: number, y: number, z: number}

Example:

const myPrinter = new Printer("COM5", 115200, {x: 220, y: 220, z: 300});

Initialise the 3D printer

await myPrinter.init();

You must invoke this method before using the printer.

Auto home

await myPrinter.autoHome(["X", "Y", "Z"]);

This auto homes the nozzle on the X, Y, and Z axes.

There is a particular function which auto homes the nozzle on the X and Y axes.

await myPrinter.autoHomeXY();

Send GCode

You can send a GCode string, or an array of GCode strings with this function:

// string:
await myPrinter.sendGCode("G28");

// array:
await myPrinter.sendGCode([
    "G91",
    "G0 F4800",
    "G0 X10 Y40"
]);

Set the speed of the movement

await myPrinter.setSpeed(80);

Sets the speed of the movement in millimeters / second.

Go to a specified position

You can make the nozzle go to specified position in millimeters. You can specify a vector or the X, Y(optional), Z(optional) components of the vector directly:

await myPrinter.goTo({x: 30, y: 60, z: 1}); // The nozzle goes to (30; 60; 1)
await myPrinter.goTo(50, 90, 2); // The nozzle goes to (50; 90; 2).
await myPrinter.goTo(30, 50); // The z value was not specified, so it goes remains the same as in the previous movement. The new position will be (30; 50; 2).

Get current position

const currentPos = await myPrinter.getCurrentPosition();
console.log(currentPos); // {x: number, y: number, z: number}

Returns the current position of the nozzle. This value is changing while the nozzle moves. This will become equal to the target position as soon as the nozzle finishes its movement.

Get target position

const targetPos = await myPrinter.getTargetPosition();
console.log(targetPos); // {x: number, y: number, z: number}

Returns the target position of the nozzle.

Get current position and target position at once

const posData = await myPrinter.getPositionData();
const currentPos = posData.currentPosition;
const targetPos = posData.targetPosition;

This method is only for saving time when we need all the two positions in one place (for example when waiting for the nozzle to finish its movement).

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文