sensorium
Version:
makeblock mainboards protocol api
94 lines (85 loc) • 3.98 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<link type="text/css" rel="stylesheet" href="api/styles/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="statics/topbar.css">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>Sensorium - Get Started</title>
</head>
<body>
<header class="topbar">
<ul class="topnav pull-right clearfix">
<li><a href="./">Get Started</a></li>
<li><a href="./code/">Code</a></li>
<li><a href="./api/">API</a></li>
<li><a href="./changelog.html">ChangeLog</a></li>
</ul>
</header>
<div class="container mt80">
<h1 id="sensorium">Sensorium</h1>
<p>Sensorium is a library for Makeblock's rj25 mainboards. It includes <code>mCore</code>, <code>Orion</code>, <code>Auriga</code> and <code>Megapi</code>, <code>MegaPiPro</code>, and <code>Arduino</code> is also supported!
You can use it in browser and node enviroment.</p>
<h1 id="install">Install</h1>
<pre><code>npm install sensorium --save
</code></pre><h1 id="usage">Usage</h1>
<h2 id="browser">Browser</h2>
<p>Include the source <code>/browser/sensorium.js</code> in the root directory of project.
Actually you also need install <a href="https://www.npmjs.com/package/sensorium-server">sensorium-server</a> as a proxy server.</p>
<pre><code>npm install sensorium-server -g
</code></pre><p>Make sure your mainboards connecting to the your computer, and run <code>sensorium-server</code> with following command in your teminal:</p>
<pre><code>ss
</code></pre><p>Then create a HTML page in the root directory, <code>index.html</code> for example, and type in the following script:</p>
<pre><code><script src="./browser/sensorium.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script>
//firstly initialize
var sensorium = new Sensorium();
var mcore = sensorium.createMcore();
//secondly set transport through `socket.io` like this:
var socket = io.connect('http://localhost:8800');
socket.on('sensor2web', function (data) {
sensorium.doReceived(JSON.parse(data));
});
sensorium.setSender(function(buf) {
socket.emit('web2sensor', { buf: JSON.stringify(buf), sensor: 'None' });
});
</script>
<script>
// set speed and run the motor.
mcore.DcMotor(1).speed(200).run();
// read ultrasonic sensor's value
mcore.Ultrasonic(3).getData((val) => console.log(val));
</script>
</code></pre><h2 id="nodejs">Nodejs</h2>
<p>Firstly, install the dependencies:</p>
<pre><code>npm install sensorium --save-dev
npm install serialport --save-dev
</code></pre><p>Touch a js file just like <code>test.js</code>, type in codes as follows:</p>
<pre><code>var Sensorium = require("sensorium");
var SerialPort = require('serialport');
var sensorium = new Sensorium();
// Visit [serialport](https://www.npmjs.com/package/serialport) for detail usage
var serialPort = new SerialPort('/dev/ttyUSB0', { baudRate:115200 });
serialPort.on('open', function() {
sensorium.setSender(function(buf) {
serialPort.write(buf);
});
serialPort.on('data', function(buff) {
sensorium.doReceived(buff);
});
});
// optional mainboards are 'Mcore', 'Auriga', 'MegaPi', 'Orion', 'MegaPiPro', 'Arduino'
var auriga = sensorium.createAuriga();
// run DcMotor
auriga.DcMotor(1).speed(200).run();
// read ultrasonic sensor's value
auriga.Ultrasonic(3).getData().then(val => console.log(val));
</code></pre><p>Make sure your mainboards connecting to the your computer, and run srcipt with node:</p>
<pre><code>node test.js
</code></pre>
</div>
</body>
</html>