JAVAScript Tutorial

Last revision 2026/01/29

This JavaScript tutorial offers a detailed guide to setting up Node.js environments on Windows, MacOS, and Linux. It includes installation instructions, commands for initializing development programs, and sample codes for controlling robotic joints using JavaScript.

JavaScript

1. Preparations before development

windows 32-bit downloading address

windows 64-site downloading address

macOs downloading address

1) Windows Node environment building

Step 1: After downloading is completed, double-click the downloaded installation package to start installing Node.js. Click the Next button

Step 2: Check the option in the lower left red box, and click Next

Step 3: Customize the installation directory

Step 4: Click the Next button (default)

Step 5: Click Install

Step 6: Click Finish to complete the installation

Step 7: Open and run with win+r, input cmd to a command indicator

Step 8: Input node -v to get the node version; when the version is displayed, its means that the installation is completed successfully.

2)MacOs node environment building

Step 1: After downloading is completed, double-click the downloaded installation package to start installing Node.js. Click the Next button, click Continue

Step 2: Click Continue again

Step 3: Click Agree to go to the next step

Step 4: Click Customize, choose the installation address, or click Install to continue the installation and input your password to install

Step 5: When successful installation is prompted, click Close to exit The installation process

Right-click on the desktop and select a punching terminal, enter the terminal and input node -v. If the node version number is displayed, it means that installation is done successfully

3)Linux node environment building

Step 1: The official Node website has changed the linux downloading version to a compiled version, so we can directly download and unzip it for use:

# wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz    // download
# tar xf  node-v10.9.0-linux-x64.tar.xz       // unzip
# cd node-v10.9.0-linux-x64/                  // enter the unzipping directory
# ./bin/node -v                               // execute node command, and check version
v10.9.0

Step 2: The bin directory of the unzipped file contains commands such as node, npm, etc. We can use the ln command to set up a soft connection:

ln -s /usr/software/nodejs/bin/npm   /usr/local/bin/ 
ln -s /usr/software/nodejs/bin/node   /usr/local/bin/

(4) Installing Node.js using Ubuntu source code
Step 1: In the following part, we will introduce the installation of Node.js in Ubuntu Linux using source code. For other Linux systems, such as Centos, perform the installation steps below. Get Node.js source code from Github.

$ sudo git clone https://github.com/nodejs/node.git
Cloning into 'node'...

Step 2: Modify the permission of the directory.

$ sudo chmod -R 755 node

Step 3: Create a compiling file using ./configure according to the following code:

$ cd node
$ sudo ./configure
$ sudo make
$ sudo make install

Step 4: Check the node version

$ node --version
v0.10.25

2.Preparations for development

(1) Downloading a project file

Open the command prompt

git clone https://github.com/elephantrobotics/jsmycobot.git

(2) Initializing a development program

Note: Open the command prompt in the downloaded project file.

<!-- initialize the program, then install and run all plug-ins required therefor -->
npm i

(3) Initializing the program

<!-- Import the plug-in installed in step 1-1 -->
const mycobot = require("mycobot")

<!-- initialize the program,mycobot.connect(serial port number, serial baud rate) -->
const obj = mycobot.connect("COM15",115200)

<!-- write in the first command, power up the robot arm and keep its current posture -->
obj.write(mycobot.powerOn())

3.Using Case

IO Control

<!-- Initialize Program -->
const mycobot = require('mycobot')

const obj = mycobot.connect('COM15',115200)

<!-- Set ATOM indicator color, mycobot.setColor(redValue, greenValue, blueVaule)-->
<!-- Note: the three parameters should be within 0-255 -->
obj.write(mycobot.setColor(125,11,9))

<!-- Take the current angle and coordinates of the robot arm as the starting point -->
obj.write(mycobot.setGripperInit())

Single Joint Control

<!-- Initialize Program -->
const mycobot = require('mycobot')

const obj = mycobot.connect('COM15',115200)

<!-- Set angle of single-joint robot arm, mycobot.sendAngle(Robot arm ID, angle, speed)-->
<!-- Note: pay attention to the number of joints when adjusting angles. For 4-joints, input 1-4; for 6 joints, input 1-6, otherwise, error may occur -->
<!-- For 4-axis/6-axis robot arm angle setup, refer to chart 1-3-->
obj.write(mycobot.sendAngle(1,110,10))

<!-- Set coordinate of single-joint robot arm, mycobot.sendCoord(Robot arm ID, coordinate, speed)-->
obj.write(mycobot.sendCoord(1,20,10))

Multiple Joints Control

Note: Fill the corresponding parameters according to the number of joints when multiple joints are operated.

<!-- Initialize -->
const mycobot = require('mycobot')

const obj = mycobot.connect('COM15',115200)

<!-- Set angles of multi-joints robot arm, mycobot.sendAngles([joint 1 , joint 2, joint 3, joint 4, joint 5, joint 6], speed) -->
obj.write(mycobot.sendAngles([-110,23,-22,110],20))

<!-- Set coordinates of multi-joints robot arm, mycobot.sendCoords([joint 1 , joint 2, joint 3, joint 4, joint 5, joint 6], speed) -->
obj.write(mycobot.sendCoords([22.5,12,-22,45],20))
Joint ID Range
1 -170 to 170
2 -170 to 170
3 -170 to 170
4 -170 to 170
5 -170 to 170
6 Not limited

Gripper Control

<!-- Initialize Program -->
const mycobot = require('mycobot')

const obj = mycobot.connect('COM15',115200)

<!-- Set gripper state, mycobot.setGripperState(On/Off, speed)-->
<!-- Note: 0 for on, 1 for off. Speed range: 0-100 -->
obj.write(mycobot.setGripperState(0,10))

<!-- Set gripper angle, mycobot.setGripperValue(angle, speed)-->
<!-- Note: angle and speed values are both between 0-100 -->
obj.write(mycobot.setGripperValue(80,20)))

Sample Code 1

<!-- Initialize Program -->
const mycobot = require('mycobot')
const obj = mycobot.connect('COM15',115200)

<!-- If the device connected is a 4-axis robot arm -->
const name = "myPallizer"
<!-- If the device connected is a 6-axis robot arm -->
if(name == "myCobot"){
    <!-- Set angles of six joints -->
    obj.write(mycobot.sendAngles([-1.49,115,-153.45,30,-33.42,137.9],80))
    <!-- And judge if it reaches the coordinate -->
    if(obj.write(mycobot.isInPosition([-1.49,115,-153.45,30,-33.42,137.9],0)) == 1){
        <!-- Robot arm continues to move -->
        obj.write(mycobot.programResume())
        <!-- Wait 0.5 second -->
        settimeout(() =>{
            <!-- Robot arm stops -->
            obj.write(mycobot.programPause())
        },500)
    }
}else{
    <!-- Set angles of four joints -->
    obj.write(mycobot.sendAngles([-1.49,45,-23,30],80))
    <!-- Set ATOM indicator color -->
    obj.write(mycobot.setColor(0,0,50))
    <!-- Wait 0.7 second -->
    settimeout(() =>{
        <!-- Set angles of four joints again -->
        obj.write(mycobot.sendAngles([-1.49,60,11,30],80))
        <!-- Set ATOM indicator color -->
        obj.write(mycobot.setColor(0,50,0))
    },700)

}

Sample Code 2

<!-- Initialize Program -->
const mycobot = require('mycobot')
const obj = mycobot.connect('COM15',115200)

<!-- Read all joint angles -->
const botData = obj.write(mycobot.getAngles())
<!-- Output all joint angles -->
console.log(botdata)
<!-- Set all joint angles -->
obj.write(mycobot.sendAngles([0,0,0,0],50))
<!-- Print if the robot arm reaches the position currently -->
console.log(obj.write(mycobot.isInPosition([0,0,0,0,0,0],0)) == 1)

<!-- Wait 3 seconds -->
settimeout(() =>{
    <!-- Set the first joint angle -->
    obj.write(mycobot.sendAngle(1,90,50))
},3000)

<!-- Wait 2 seconds -->
settimeout(() =>{
    <!-- Set the second joint angle to 50 degrees -->
    obj.write(mycobot.sendAngle(2,50,50))
},2000)
<!-- Wait 1.5 seconds -->
settimeout(() =>{
    <!-- Set the third joint angle to 50 degrees -->
    obj.write(mycobot.sendAngle(3,-50,50))
},1500)
<!-- Wait 1.5 seconds -->
settimeout(() =>{
    <!-- Set all joint angles -->
    obj.write(mycobot.sendAngles([88.68, -138.51, 155.65, -128.05, -9.93, -15.29],50))
},1500)
<!-- Wait 2.5 seconds -->
settimeout(() =>{
    <!-- Set robot arm to free mode -->
    obj.write(mycobot.releaseAllServos())
},2500)

Was this article helpful?

ON THIS PAGE

TOP