From 5011bb7e358d19b79280fab01f188a434c1b5367 Mon Sep 17 00:00:00 2001 From: Liam Brennan Date: Sat, 25 Mar 2017 10:07:21 +1100 Subject: [PATCH 1/4] Added new getting started guide for node js --- .../tutorials/getting-started-with-node-js.md | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 docs/tutorials/getting-started-with-node-js.md diff --git a/docs/tutorials/getting-started-with-node-js.md b/docs/tutorials/getting-started-with-node-js.md new file mode 100644 index 000000000..881178210 --- /dev/null +++ b/docs/tutorials/getting-started-with-node-js.md @@ -0,0 +1,100 @@ +--- +title: Getting Started with Node JS +group: software-languages +author: "@liam-b" +--- + +{% include /style/icon.html type="warning" %} +Note that Node JS run really slowly on the EV3 brick and is limited to NPM version 0.10.29 which can be a problem when trying to install some libraries. +{: .alert .alert-warning} + +**Before you start, make sure that you have configured a network connection to +your ev3dev device and have opened an SSH connection to it.** + +## Installing ev3dev-lang +To install the library run: + +```shell +npm install ev3dev-lang +``` + +Now whenever you want to use this in code, just write: + +```javascript +var ev3dev = require('ev3dev-lang'); +``` + +For a bit more info on this visit the ev3dev-lang [github page](https://github.com/wasabifan/ev3dev-lang-js). + +## Nano +For this tutorial, we will be using nano. For more info on how to use nano check out the [nano cheat sheet](/docs/tutorials/nano-cheat-sheet). + +## Hello world +Navigate to a project directory (e.g `~/src/js`) and make a new file with `touch test.js` + +Open it with `nano test.js` and write: +```javascript +var test = 'hello world!'; +console.log(test); +``` + +Running `node test.js`should print `hello world!` to the console. If this worked you are ready to start coding with node! + +## Motors +Plug a ev3 large motor into port A, then open `test.js` and write: + +{% include /style/icon.html type="warning" %} +This snippet of code will not work for any other motors! +{: .alert .alert-warning} + +```javascript +var ev3dev = require('ev3dev-lang'); + +var motor = new ev3dev.LargeMotor('outA'); // create new motor plugged into port A called 'motor' + +motor.runForever(200); // run motor at speed 200 + +setTimeout(function () { + motor.stop(); // stop the motor after a second +}, 1000) +``` + +If you get any errors when you try to run this, make sure your motor is plugged in correctly. + +For other motors such as the `MeduimMotor`, check the [default supported motors](http://wasabifan.github.io/ev3dev-lang-js/modules/_motors_.html) list. + +And from reading the [documentation page](http://wasabifan.github.io/ev3dev-lang-js) you should be able to get a good idea of what methods you can use with the motor (things like `runForever()` and `runForTime()`). Then when you use it in code, it should look like: + +```javascript +motor.doSomething(speed, otherArgument) // where doSomething can be replaced by any valid method. +``` + +You can also **reset** and **read** the position of a motor using `motor.reset()` and `motor.position`. + +## Sensors +Plug a ev3 color sensor into port 1, then edit `test.js` again appending this to the bottom: + +```javascript +var sensor = new ev3dev.ColorSensor('in1'); // create new color sensor called 'sensor'. in1 refers to port 1 + +setInterval(function () { + console.log(sensor.reflectedLightIntensity); // log the reflected light percent of the sensor every 300 milliseconds +}, 300) +``` + +For other sensors, check the [default supported sensors](http://wasabifan.github.io/ev3dev-lang-js/modules/_sensors_.html) list. + +To change a sensors mode you'll need to check what modes it has by looking at the [ev3dev sensor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html) and use: + +```javascript +sensor.mode = 'NEW-MODE'; // where 'NEW-MODE' can be replaced by any valid mode +``` + +And then using `sensor.someValueAcessor` will return the sensor value, where `someValueAcessor` can be replaced by any valid accessor (also found on the [default supported sensors](http://wasabifan.github.io/ev3dev-lang-js/modules/_sensors_.html) list by clicking on the sensor you are using) + +## Next steps +So that's the basic overview of how to use node with ev3dev! For info on all supported sensors, motors and other documentation visit the [github page](https://github.com/wasabifan/ev3dev-lang-js) and the [documentation page](http://wasabifan.github.io/ev3dev-lang-js). + +The documentation page looks a bit confusing, but what you really need to worry about are the names of the classes and the **methods** / **accessors**. (eg. the large motor has **methods** such as: `runForever()`, `runForTime()` etc and the color sensor has **accessors** such as: `reflectedLightIntensity`, `ambientLightIntensity` etc) + +The [sensor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html) and the [motor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/motor_data.html) are both really useful pages aswell. From 3036fbe5df203c03f2adea9f54be72518d332d18 Mon Sep 17 00:00:00 2001 From: Liam Brennan Date: Sat, 25 Mar 2017 14:14:18 +1100 Subject: [PATCH 2/4] added 'getting started guides' for languages and tooling category --- _data/tutorial-groups.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_data/tutorial-groups.yml b/_data/tutorial-groups.yml index 07ba55388..ffe5a044f 100644 --- a/_data/tutorial-groups.yml +++ b/_data/tutorial-groups.yml @@ -9,7 +9,7 @@ description: "Communicating using special protocols between multiple devices on a network." - id: software-languages title: Programming Languages and Tooling - description: "Configuring a development environment, installing new languages, and using advanced language tools." + description: "Configuring a development environment, installing new languages, using advanced language tools and getting started guides for languages." - id: software-editors title: "Text Editors" description: "Tips on using text editors which ship with ev3dev out-of-the-box." @@ -36,4 +36,4 @@ description: "Configuring, updating and modifying an ev3dev installation." - id: website title: This Website - description: "Using this website and contributing content." \ No newline at end of file + description: "Using this website and contributing content." From 90e988b032d09136e29d820dd7ddc597647a27d5 Mon Sep 17 00:00:00 2001 From: Wasabi Fan Date: Sun, 26 Mar 2017 23:08:29 -0700 Subject: [PATCH 3/4] Update getting-started-with-node-js.md --- .../tutorials/getting-started-with-node-js.md | 92 +++++++++---------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/docs/tutorials/getting-started-with-node-js.md b/docs/tutorials/getting-started-with-node-js.md index 881178210..4de963c4f 100644 --- a/docs/tutorials/getting-started-with-node-js.md +++ b/docs/tutorials/getting-started-with-node-js.md @@ -5,74 +5,70 @@ author: "@liam-b" --- {% include /style/icon.html type="warning" %} -Note that Node JS run really slowly on the EV3 brick and is limited to NPM version 0.10.29 which can be a problem when trying to install some libraries. +Note that Node.js runs frustratingly slowly on the EV3 brick and is limited to an old version of both Node.js and npm. You may want to consider using Python or another ev3dev-supported language instead. {: .alert .alert-warning} -**Before you start, make sure that you have configured a network connection to -your ev3dev device and have opened an SSH connection to it.** +

+Node.js is a framework that allows you to run JavaScript as a local application. It is designed to make building servers and other asynchronous apps as easy as possible. In this tutorial, we'll show you how to get up-and-running with Node.js on ev3dev. +

-## Installing ev3dev-lang -To install the library run: +**Before you start, make sure that you have [configured a network connection](/docs/networking/) on +your ev3dev device and have [opened an SSH connection](/docs/tutorials/connecting-to-ev3dev-with-ssh/) to it.** + +## Installing ev3dev-lang (and quick-start) +To install the library which allows you to use ev3dev features, run the following command from your SSH connection: ```shell npm install ev3dev-lang ``` -Now whenever you want to use this in code, just write: - +In all JavaScript files that you write from here forward, include the following line to import the module: ```javascript var ev3dev = require('ev3dev-lang'); ``` -For a bit more info on this visit the ev3dev-lang [github page](https://github.com/wasabifan/ev3dev-lang-js). +For a bit more info on this visit the ev3dev-lang-js [GitHub page](https://github.com/wasabifan/ev3dev-lang-js). -## Nano -For this tutorial, we will be using nano. For more info on how to use nano check out the [nano cheat sheet](/docs/tutorials/nano-cheat-sheet). +## Exploring Node.js and ev3dev -## Hello world -Navigate to a project directory (e.g `~/src/js`) and make a new file with `touch test.js` +### Picking an editor: Nano -Open it with `nano test.js` and write: -```javascript -var test = 'hello world!'; -console.log(test); -``` +For this tutorial, we will be using a text editor called `nano`. For more info on how to use nano, check out the [nano cheat sheet](/docs/tutorials/nano-cheat-sheet). Feel free to use a different one if you know what you're doing. -Running `node test.js`should print `hello world!` to the console. If this worked you are ready to start coding with node! +### Hello world +To start, navigate to a project directory (e.g `~/src/js`) that we can store our files in: -## Motors -Plug a ev3 large motor into port A, then open `test.js` and write: +```shell +mkdir -p ~/src/js && cd ~/src/js +``` -{% include /style/icon.html type="warning" %} -This snippet of code will not work for any other motors! -{: .alert .alert-warning} +Create a JavaScript file and open it with `nano test.js`. In the opened editor, add the following code: ```javascript -var ev3dev = require('ev3dev-lang'); - -var motor = new ev3dev.LargeMotor('outA'); // create new motor plugged into port A called 'motor' - -motor.runForever(200); // run motor at speed 200 - -setTimeout(function () { - motor.stop(); // stop the motor after a second -}, 1000) +console.log('hello world!'); ``` -If you get any errors when you try to run this, make sure your motor is plugged in correctly. +If you're using `nano`, refer to the [nano cheat sheet](/docs/tutorials/nano-cheat-sheet/) to learn how to save the file. -For other motors such as the `MeduimMotor`, check the [default supported motors](http://wasabifan.github.io/ev3dev-lang-js/modules/_motors_.html) list. +Running `node test.js`should print `hello world!` to the console. If this worked, you are ready to start coding with node! -And from reading the [documentation page](http://wasabifan.github.io/ev3dev-lang-js) you should be able to get a good idea of what methods you can use with the motor (things like `runForever()` and `runForTime()`). Then when you use it in code, it should look like: +### Motors +Let's try running a motor. Plug an EV3 motor into port A, then open `test.js` again and write: ```javascript -motor.doSomething(speed, otherArgument) // where doSomething can be replaced by any valid method. +var ev3dev = require('ev3dev-lang'); + +var motor = new ev3dev.Motor('outA'); // create new motor plugged into port A called 'motor' + +motor.runForTime(3000, 500); // run the motor for 3 seconds at 500 degrees/second ``` -You can also **reset** and **read** the position of a motor using `motor.reset()` and `motor.position`. +Save the file and run it. Again, you can run the file with `node test.js`. If you get any errors when you try to run this, make sure that your motor is plugged in correctly. -## Sensors -Plug a ev3 color sensor into port 1, then edit `test.js` again appending this to the bottom: +You can refer to the [motor documentation page](http://wasabifan.github.io/ev3dev-lang-js/classes/_motors_.motor.html) to learn about other things you can do with motors (such as using the `runForever()` and `runToRelativePosition()` methods). + +### Sensors +Plug an EV3 color sensor into port 1, then edit `test.js` again appending this to the bottom: ```javascript var sensor = new ev3dev.ColorSensor('in1'); // create new color sensor called 'sensor'. in1 refers to port 1 @@ -82,19 +78,15 @@ setInterval(function () { }, 300) ``` -For other sensors, check the [default supported sensors](http://wasabifan.github.io/ev3dev-lang-js/modules/_sensors_.html) list. - -To change a sensors mode you'll need to check what modes it has by looking at the [ev3dev sensor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html) and use: +If you run your file now, you should see it continuously log the brightness that the sensor is detecting to the console. To stop the code, press Ctrl+C. -```javascript -sensor.mode = 'NEW-MODE'; // where 'NEW-MODE' can be replaced by any valid mode -``` +For other sensors, check out the [sensors documentation page](http://wasabifan.github.io/ev3dev-lang-js/modules/_sensors_.html). Every sensor has a set of "properties" that can be read; you should refer to the documentation site to see which properties can be used. -And then using `sensor.someValueAcessor` will return the sensor value, where `someValueAcessor` can be replaced by any valid accessor (also found on the [default supported sensors](http://wasabifan.github.io/ev3dev-lang-js/modules/_sensors_.html) list by clicking on the sensor you are using) +### Next steps +You've now learned the basics of using Node.js with ev3dev! For info on all supported sensors, motors and other documentation visit the [GitHub page](https://github.com/wasabifan/ev3dev-lang-js) and the [documentation page](http://wasabifan.github.io/ev3dev-lang-js). -## Next steps -So that's the basic overview of how to use node with ev3dev! For info on all supported sensors, motors and other documentation visit the [github page](https://github.com/wasabifan/ev3dev-lang-js) and the [documentation page](http://wasabifan.github.io/ev3dev-lang-js). +The documentation website has a list of "modules" along the side; click on each to visit the detail page for that module. When looking at the page for a specific class, pay attention to the **methods** and **accessors** (e.g., the large motor has **methods** such as `runForever()` and `runForTime()`, and the color sensor has **accessors** such as `reflectedLightIntensity`and `ambientLightIntensity`). -The documentation page looks a bit confusing, but what you really need to worry about are the names of the classes and the **methods** / **accessors**. (eg. the large motor has **methods** such as: `runForever()`, `runForTime()` etc and the color sensor has **accessors** such as: `reflectedLightIntensity`, `ambientLightIntensity` etc) +The [sensor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html) and the [motor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/motor_data.html) are the best place to look to see what you can do with ev3dev. -The [sensor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html) and the [motor list](http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/motor_data.html) are both really useful pages aswell. +Happy coding! From 4f19dbbe929304bda2fef0f6a95e34451fef7012 Mon Sep 17 00:00:00 2001 From: Liam Brennan Date: Thu, 30 Mar 2017 15:37:42 +1100 Subject: [PATCH 4/4] added node and npm versions --- docs/tutorials/getting-started-with-node-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/getting-started-with-node-js.md b/docs/tutorials/getting-started-with-node-js.md index 4de963c4f..5b09d4a4d 100644 --- a/docs/tutorials/getting-started-with-node-js.md +++ b/docs/tutorials/getting-started-with-node-js.md @@ -5,7 +5,7 @@ author: "@liam-b" --- {% include /style/icon.html type="warning" %} -Note that Node.js runs frustratingly slowly on the EV3 brick and is limited to an old version of both Node.js and npm. You may want to consider using Python or another ev3dev-supported language instead. +Note that Node.js runs frustratingly slowly on the EV3 brick and is limited to version v0.10.29 for Node.js and version 1.4.21 for npm. You may want to consider using Python or another ev3dev-supported language instead. {: .alert .alert-warning}