-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added new getting started guide for node js #357
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "really" sounds mildly unprofessional in this context. Can we use another word there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's also limited to an equivalently old Node.js version, which should also be mentioned. |
||
{: .alert .alert-warning} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make this page more approachable to have a one-sentence explanation of what Node.js is and what it's used for. You can format it as a "lead" paragraph if you'd like. |
||
**Before you start, make sure that you have configured a network connection to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you link to the tutorials that we have for these tasks? |
||
your ev3dev device and have opened an SSH connection to it.** | ||
|
||
## Installing ev3dev-lang | ||
To install the library run: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without an explicit mention that this should be run via SSH, some people will try running this on their PC 😆 |
||
|
||
```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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
## 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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Period after sentence |
||
|
||
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! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although I've often formatted the name as "node" or "Node," they officially use "Node.js"; I think we should align with that wherever possible. |
||
|
||
## Motors | ||
Plug a ev3 large motor into port A, then open `test.js` and write: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. People might need to be reminded how to open files again. Maybe you can just link to the cheat sheet everywhere that you mention nano 😆 Your choice. |
||
|
||
{% include /style/icon.html type="warning" %} | ||
This snippet of code will not work for any other motors! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention what other motors the user might have (so they know how to tell them apart) |
||
{: .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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to not tell people that this is even an option for running for a length of time, given that there is a |
||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "And" at the beginning of the sentence |
||
|
||
```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`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep in mind that
What you do exactly depends on what you'd like to communicate there; I'll leave that up to you. |
||
|
||
## Sensors | ||
Plug a ev3 color sensor into port 1, then edit `test.js` again appending this to the bottom: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
```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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think people will need to set the mode for? Properties like |
||
|
||
```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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
## 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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "So" |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
should be
|
||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run
->runs