Giter VIP home page Giter VIP logo

mirobot-js's Introduction

mirobot with JavaScript

This uses WebSockets in the browser to connect to a mirobot on your local WiFi.

First download this repository as a ZIP and extract it into a folder in your Documents or Desktop.

Approach A: Chrome App

The simplest way to use mirobot locally is with the Chrome app:

https://chrome.google.com/webstore/detail/mirobot/bjkfmkklaabeoomedlpgfplgndpiijea

First turn the mirobot on and connect to its WiFi, then in the Mirobot apps connect to the following IP:

local.mirobot.io

Approach B: Running apps.mirobot.io in the Browser

If for some reason you don't want to use the Chrome App, you can still locally run the apps.mirobot.io site:

First turn the mirobot on and connect to its WiFi.

Then cd or open the apps.mirobot.io/ folder in your Terminal:

cd apps.mirobot.io

Now you need to start a local server, you can do this with Python like so:

python -m SimpleHTTPServer

Or in Node.js + npm if you have that setup:

# first install serve globally (just do this once)
npm install serve -g

# then you can always serve this folder
serve .

Now you can use the apps as usual, but connect to the following IP within the apps:

local.mirobot.io

Approach C: Using Websockets

Let's say you want something really advanced, like connecting Face Tracking or some other cool JavaScript-based app to your Mirobot.

For this you can use my simple websockets/ interface:

  1. Turn the mirobot on.
  2. Connect your computer to its WiFi.
  3. Open up websockets/move.html HTML page within the repository folder. Use Chrome or FireFox to open it.
  4. The bot should start moving! Open the DevTools console so you can see the messages sent and received.

Code Example

Here's an example script that draws a rectangle:

const bot = mirobot();

// Send a 'stop' to kill any previously running commands
bot.stop();

// draw a square
bot.pendown();
bot.forward(250);
bot.left(90);
bot.forward(250);
bot.left(90);
bot.forward(250);
bot.left(90);
bot.forward(250);
bot.penup();

// beep for 250 milliseconds
bot.beep(250);

The code goes inside an HTML page, like this:

<html>
  <body>
    <p>hello world</p>

    <!-- Include the mirobot.js library -->
    <script src='mirobot.js'></script>

    <!-- Now you can run your code -->
    <script>
      .. YOUR MIROBOT CODE HERE ..
    </script>
  </body>
</html>

The mirobot() function also takes an ip address if you want to connect to something other than local.mirobot.io, e.g. if you want to use the internet to drive your robot.

const bot = mirobot('192.168.2.10');
...

Advanced Example

e.g. Hooking up with webcam or microphone you might want to use bot.idle, bot.reset(), and others.

const bot = mirobot();

// handle colliders
bot.enableCollisionListener();
bot.collision(state => {
  console.log(state); // left, right or both
  bot.beep(250); // beep on collision
});

// A 1-second event loop for the robot
setInterval(() => {
  bot.reset(); // clear command queue
  bot.stop(); // stop movement
  bot.forward(Math.random() * 250); // move randomly
}, 1000);

// A 24 frames per second loop for sensors
// e.g. microphone or webcam input
setInterval(() => {
  if (bot.idle) { // if bot is idling
    const sensorData = /* get sensor data */;
    // move based on sensor data...
    bot.left(sensorData);
  }
}, 1000 / 24);

mirobot-js's People

Contributors

mattdesl avatar

Stargazers

Georgiy Chipunov avatar Vilson Vieira avatar AliceLeon avatar

Watchers

James Cloos avatar  avatar Georgiy Chipunov avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.