Mantis Rover
  • 🔡Mantis Rover Documentation
  • Supported Devices
  • Assembly Guide
    • 📺Video Tutorials
    • 📰Printed Instructions
      • Wheel Assembly using Improved Coupler
    • 📺3D CAD Model
    • 📺3D Model File Download
    • 📺Packing Guide
    • 🚫General Care/Precautions
    • 🚫Charging your Rover
  • 3D Printing
    • 🐺Designing your Part (TinkerCAD)
    • 🐺MODi Adapter 3D Print
    • ⌚Printing your Part
  • PWM Control
    • 🤖PWM Controls-Scratch
    • 🚗PWM Setup-JavaScript
    • 🚗Man-In-The-Loop Obstacle Avoidance Tutorial-JavaScript
    • 🚕PWM Motor Drive-Python
  • MODI Sensor
    • Product Specification Sheet
    • Quick Start Guide
    • MODi Cart Experiment (Bonus)
    • 🤖MODi Data-Scratch
    • 🤖MODi CSV-Scratch
    • 🚗MODi LIDAR Obstacle Avoidance-JavaScript
    • 🍊MODi LIDAR Obstacle Avoidance-Scratch
  • Motor Hat Module
    • Product Specification Sheet
    • Quick Start Guide
  • 🆘Support
Powered by GitBook
On this page
  • MODi LIDAR Avoidance Tutorial
  • Overview
  • Connect MTH Board
  • Mthobstacle.html Breakdown
  • mthobstacle.js Breakdown
  • man-in-the-loop exercise

Was this helpful?

  1. MODI Sensor

MODi LIDAR Obstacle Avoidance-JavaScript

PreviousMODi CSV-ScratchNextMODi LIDAR Obstacle Avoidance-Scratch

Last updated 1 month ago

Was this helpful?

The user must have access to an internet connected device that runs a modern web browser that supports WebBluetooth. Most modern web browsers support this, with the exception of Safari on MacOS, and Firefox. Google Chrome or Edge are recommended for best results. Mobile browsers are not typically not supported.

MODi LIDAR Avoidance Tutorial

Learn how to command the motors using the Motor Hat provided in your Rover kit, along with the LIDAR sensor on the MODi.

This guide requires no prior programming experience and users should focus more on the concepts being introduced versus getting lost in the minutia of the scripting language.

Overview

In this guide you will learn how to manipulate and control the Rover based on the following basic concepts:

  1. How to generate PWM commands for the ROVER.

  2. Understand how the different PWM settings affect motor drive and direction.

  3. Visualize PWM concepts with an appreciation for generated outputs.

  4. Interpret the sensor command provided by the MODI sensor.

To access the program, simply follow this link

This Rover GUI code demo contains two files:

mthjoystick.html

o This must be in the same folder as the Java script file

o Contains metadata about the extension and how to configure all the Gui items in the browser.

mthjoystick.js

o This module contains the underlying BLE communications.

o We will be iteratively modifying the functions specific to the Joystick buttons above.

Connect MTH Board

Click on the Bluetooth icon and make sure your device is broadcasting. Once broadcasting, click on the pair button to connect to the board.

Once you have a successful connection, the interface will report that a connection was successful.

After the MTH, now you need to connect you modi sensor. Click the Connect MODi button to initiate the process.

once paird, sensor data will start to show.

Now that both sensors are connected, the montoring features can be used in addtion to the joystick commands.

Mthobstacle.html Breakdown

Let's break down the contents of mthjoystick.html file. This code tells your browser how and where to place everything that you see on the screen demonstrated above. You can copy this code directly into a text editor and make sure to name it with the same convention above.

<!doctype html>
<html>

    <head>
        <title>MTH Control Panel</title>

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
        <link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.indigo-pink.min.css">
        <script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>

  
        <style>

body {
  background: #fff;
}

.mip-layout {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
}

.mip-card {
  width: 400px;
}

.mip-pad {
  display: flex;
  align-items: center;
  flex-direction: column;
}

.mip-pad button {
  margin: 1em 0.5em;
}


#logTextArea {
  width: 950%;
  height: 150px;
  overflow: auto;
}

        </style>
    </head>

    <body>

        <div class="mip-layout mdl-layout mdl-js-layout">

            <div class="mdl-card mdl-shadow--2dp mip-card">
                <div class="mdl-card__title">
                    <h3 class="mdl-card__title-text">Mantis MTH</h3>
                </div>
                <div class="mdl-card__supporting-text">
                    Please connect and control
						
    
                    </button>
                </div>
                <div class="mdl-card__actions mdl-card--border mip-pad">

                    <button onclick="moveForward()" class="mdl-button mdl-js-button mdl-button--fab mdl-button--raised mdl-js-ripple-effect">
                        <i class="material-icons">arrow_upward</i>
                    </button>

                    <div>
                        <button onclick="turnLeft()" class="mdl-button mdl-js-button mdl-button--fab mdl-button--raised mdl-js-ripple-effect">
                            <i class="material-icons">arrow_back</i>
                        </button>

                        <button onclick="connect()" class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored">
                            <i class="material-icons">bluetooth_searching</i>
                        </button>

                        <button onclick="turnRight()" class="mdl-button mdl-js-button mdl-button--fab mdl-button--raised mdl-js-ripple-effect">
                            <i class="material-icons">arrow_forward</i>
                        </button>
                    </div>

                    <button onclick="moveBackward()" class="mdl-button mdl-js-button mdl-button--fab mdl-button--raised mdl-js-ripple-effect">
                        <i class="material-icons">arrow_downward</i>
                    </button>
				

                </div>
                <div class="mdl-card__actions mdl-card--border">
                    <button onclick="stop()">
                        Stop
                    </button>
                    <BR><BR>
                    <button onclick="onStartButtonClick()">
                        Connect MODI
                    </button>
                    <button onclick="onStopButtonClick()">
                        Stop MODI
                    </button>

                    <BR><BR>
                    <button onclick="EnableMonitoring(1)">
                        Start Monitoring 
                    </button>
                    <button onclick="EnableMonitoring(0)">
                        Stop Monitoring 
                    </button>
                    
                </div>
                <textarea id="logTextArea" readonly></textarea>
            </div>
           
        </div>
        

        <script src="mthobstacle.js"></script>
      
    </body>

</html>

For starters you can see where the buttons are called out here starting on line 1, five buttons in total.

                  <button onclick="stop()">
                        Stop
                    </button>
                    <BR><BR>
                    <button onclick="onStartButtonClick()">
                        Connect MODI
                    </button>
                    <button onclick="onStopButtonClick()">
                        Stop MODI
                    </button>

                    <BR><BR>
                    <button onclick="EnableMonitoring(1)">
                        Start Monitoring 
                    </button>
                    <button onclick="EnableMonitoring(0)">
                        Stop Monitoring 
                    </button>

mthobstacle.js Breakdown

The man-in-the-loop experiment calls for manipulating the drive strength of these buttons and observing the results as you maneuver around the obstacle course that you made. It is important when you learn to code that you appreciate certain aspects of the code while focusing your efforts on the specific regions of concern. All other sections in the mthjoystick.js java script file maintain very low level details of the BLE layer that is beyond the scope of this exercise. Before proceeding further go ahead and copy this code into a text editor in its entirety and save it with the exact naming convention above.

'use strict';
var mDev=null;
var isMTHConnected=false;
var isMonitoring=0;
var interval=null;
const originalLog = console.log;

var myCharacteristic,gattServer;
const logTextArea = document.getElementById("logTextArea");

var AccelX, AccelY, AccelZ, GyroX, GyroY, GyroZ, Distance;

function connect() {
    console.log('Scanning for MTH Device...');
    if (!navigator.bluetooth) {
        alert('Web Bluetooth is disabled! Please go to chrome://flags and enable it.');
        return;
    }


    navigator.bluetooth.requestDevice({
        filters: [{ namePrefix: "MTH" }],
        optionalServices: ['0000ffd0-0000-1000-8000-00805f9b34fb']
    })
        .then(device => {
           let deviceprefix=device.name.split("_")[0];
            mDev = new MantisDevice(deviceprefix.toLowerCase());
            // Connect to the GATT server and get the services
            device.gatt.connect().then(server => {
                // Get the Service
              server.getPrimaryService(mDev.PrimaryService)
                    .then(service => {
                        mDev.primaryServiceObj = service;
                        console.log("got service");

                        if (mDev.InitializationChar != null) {
                            service.getCharacteristic(mDev.InitializationChar)
                                .then(initChar => {
                                    console.log("got initialize characteristic");
                                    mDev.initializationCharObj = initChar;
                                })
                        }
                        if (mDev.MailboxChar != null) {
                            service.getCharacteristic(mDev.MailboxChar)
                                .then(mailboxChar => {
                                    console.log("got mailbox characteristic");
                                    mDev.mailboxCharObj = mailboxChar;
                                })
                        }

                        if (mDev.CommandChar != null) {
                            service.getCharacteristic(mDev.CommandChar)
                                .then(commandChar => {
                                    console.log("got command characteristic");
                                    mDev.commandCharObj = commandChar;
                                    isMTHConnected=true;
                                    console.log("Connected!");
                                })
                        }
                       

                    })
                    .catch(error => {
                        console.error("Failed to get services: " + error.message);
                    });
            })
                .catch(error => {
                    console.error("Error requesting device: " + error.message)
                })
        })
 
}

function writeBytesUUID(characteristic, bytes) {
    characteristic.writeValue(bytes)
        .catch(error => {
            console.error("writeBytesUUID Error writing characteristic: " + error.message);
        })
}

var conversions = (function () {

    // define private constant variables

    var TWO_TO_FIFTHTEENTH = 32768;
    var GRAV_CONST = 9.80665;
    var DEG2RAD = 0.0174532925;                     // pi/180
 
    var code2accel = function code2accel (code) {
        return (code / TWO_TO_FIFTHTEENTH) * 2 * GRAV_CONST;
    };
    var code2radsec = function code2radsec (code) {
        return (code / TWO_TO_FIFTHTEENTH) * 250 * DEG2RAD;
    };
   
    var modi_code2distance = function modi_code2distance (distADCLSByte,distADCMSByte){
 

        var distCorrected, dist;
        var modiDistanceLSB=33.3 / 65536.0;
        var m_modi=1.0426;
        var b_modi=-3.5514;
        
        var distCode = (distADCLSByte * 256) + distADCMSByte;     // Concatenate LSByte and MSByte of distance ADC code

        distCode &= ~0x0007;											 // Throw away (clear) Bits 2-0;

        dist = distCode * modiDistanceLSB * 100.0;                      // Compute distance (in cm)
        distCorrected = (dist * m_modi) + b_modi;     

        if (distCorrected < 0 || distCorrected >1000 ) { distCorrected = 0; }{                   // Truncate out of range values
            return distCorrected;										    // return corrected distance in cm
        }
    };
    // public methods/attributes
    return {
        code2accel             : code2accel,
        code2radsec            : code2radsec,
        modi_code2distance 	   : modi_code2distance
    };
})();

function onStartButtonClick() {

  //console.log('Requesting Bluetooth Device for Modi...');
  
    navigator.bluetooth.requestDevice(
        {
            filters: [{ namePrefix: "MDI" },{ namePrefix: "MODI" }],  
            optionalServices: [0xffD0]        
        })
        .then(device => {
            //console.log('> Found ' + device.name);
           // console.log('Connecting to GATT Server...');
            return device.gatt.connect();
        })
        .then(server => {
            gattServer = server;
           // console.log('Getting Service 0xffb0 - Command control...');
            return server.getPrimaryService(0xffD0);
        })
        .then(service => {
           // console.log('Getting Characteristic 0xffb5 - Command...');
            return service.getCharacteristic(0xffb5);
        })
  .then(characteristic => {
    myCharacteristic = characteristic;
    return myCharacteristic.startNotifications().then(_ => {
      console.log('> Notifications started');
      myCharacteristic.addEventListener('characteristicvaluechanged',
          handleNotificationsModi);
	  sendCommand(1,0,100,0x03,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,239,190);					 
     
     
    });
  })
  .catch(error => {
    console.log('Argh! ' + error);
  });
  
  
}

function sendCommand(...bytes) {
    var buffer = new ArrayBuffer(bytes.length);
    var view = new Uint8Array(buffer);
    for (let i = 0; i < bytes.length; i++) {
        view[i] = bytes[i];
    }
    return myCharacteristic.writeValue(buffer)
        .catch(err => console.log('Error when writing command! ', err));
}
function onStopButtonClick() {
  if (myCharacteristic) {
    myCharacteristic.stopNotifications()
    .then(_ => {
      console.log('> Notifications stopped');
      myCharacteristic.removeEventListener('characteristicvaluechanged',
          handleNotificationsModi);
    })
    .catch(error => {
      console.log('Argh! ' + error);
    });
  }
}
	
function handleNotificationsModi(event) {
    let value = event.target.value;
        var a= new Array(20);
              for (var i = 0; i < 20; i++)
                      a[i] = value.getUint8(i);
  
      AccelX = conversions.code2accel(toSignedInt16(bitConverter(a, 0, 1)));
      AccelY = conversions.code2accel(toSignedInt16(bitConverter(a, 2, 3)));
      AccelZ = conversions.code2accel(toSignedInt16(bitConverter(a, 4, 5)));
                
      GyroX = conversions.code2radsec(toSignedInt16(bitConverter(a, 6, 7)));
      GyroY = conversions.code2radsec(toSignedInt16(bitConverter(a, 8, 9)));
      GyroZ= conversions.code2radsec(toSignedInt16(bitConverter(a, 10, 11)));
  
      Distance=conversions.modi_code2distance(a[13],a[12]);
  
if (isMonitoring==0){//
     console.log('D: ' + Distance );
     //console.log('d: ' + Distance + ' Ax: ' + AccelX+ ' AY: ' + AccelY + ' Z ' + AccelZ+ ' GX ' + GyroX + ' GY ' + GyroY+ ' GZ ' + GyroZ );

}       
  }

  function MantisDevice (devName1) {
     
   // console.log("DeviceName: " + devName1.split("_")[0]);
    switch (devName1) {
      case "modi":
          this.PrimaryService =     '0000ffd0-0000-1000-8000-00805f9b34fb';
          this.NotificationChar =   '0000ffb5-0000-1000-8000-00805f9b34fb';
          this.InitializationChar = '0000ffb1-0000-1000-8000-00805f9b34fb';
          break;
      case "mdi":
          this.PrimaryService =     '0000ffd0-0000-1000-8000-00805f9b34fb';
          this.NotificationChar =   '0000ffb5-0000-1000-8000-00805f9b34fb';
          this.InitializationChar = '0000ffb1-0000-1000-8000-00805f9b34fb';
          break;
      case "mth":
        this.PrimaryService =     '0000ffd0-0000-1000-8000-00805f9b34fb';
        this.NotificationChar =   '0000ffd4-0000-1000-8000-00805f9b34fb';
        this.MailboxChar = '0000ffd1-0000-1000-8000-00805f9b34fb';
        this.CommandChar = '0000ffd2-0000-1000-8000-00805f9b34fb';
      break;
      case "sct":
        this.PrimaryService =     '0000ffd0-0000-1000-8000-00805f9b34fb';
        this.NotificationChar =   '0000ffd4-0000-1000-8000-00805f9b34fb';
        this.MailboxChar = '0000ffd1-0000-1000-8000-00805f9b34fb';
        this.CommandChar = '0000ffd2-0000-1000-8000-00805f9b34fb';
      break;
      //default:
   //       console.log("Dev Init: Unknown device, doing nothing: " + devName1);
  }
  
    this.primaryServiceObj =     null;
    this.notificationCharObj =   null;
    this.initializationCharObj = null;  
    this.mailboxCharObj = null;  
    this.commandCharObj = null; 
  
  }

  var bitConverter = function bitConverter (arr, start, end) {
    var populate = function populate () {
        var data = [];
        for (var i = start; i <= end; i++) {
            (function (val) {
                data.push(arr[val]);
            })(i);
        }
        return data;
    };
    var combine = function combine (data, index) {
        if (index >= 0) {
            var tmp = data[index] << (index << 3);
            return tmp | combine(data, --index);
        } else {
            return 0;
        }
    };
    if (start >= 0 && start < arr.length && end > 0 && end < arr.length) {
        return combine(populate(), arr.length - 1);
    } else {
        throw Error('Invalid parameters to bitConverter: start: ' + start + ' end: ' + end + ' arr.length: ' + arr.length);
    }
};
/**
 * A function to treat a 16 bit integer as two's complement signed
 *
 * @param val A 16 bit integer
 * @returns {int} The parameter converted to its two's complement signed value
 */
var toSignedInt16 = function toSignedInt16 (val) {
    return (new Int16Array([val]))[0];
};

var toUInt16=function toUInt16(val) {
    return (new Uint16Array([val]))[0];
};

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

// Override console.log to append logs to the text area
console.log = function() {
  // Convert all arguments to a single string
  const message = Array.from(arguments).map(arg => String(arg)).join(" ");

  // Append the log message to the text area
  logTextArea.value += message + "\n";
  logTextArea.scrollTop = logTextArea.scrollHeight;
  // Call the original console.log
  originalLog.apply(console, arguments);
};

function movecmd(payload_cmd, payload_data) {
    writeBytesUUID(mDev.mailboxCharObj, payload_data);

    setTimeout(function () {
        self.writeBytesUUID(mDev.commandCharObj, payload_cmd);
    //    console.log("Executed CMD after 300 MS delay");
    }, 300);//timeout
 
}

function EnableMonitoring(state) { 
    isMonitoring=state;
    if (isMonitoring){
        checkDistanceValue();
    }
    else{
        clearInterval(interval);
    }
   
    console.log("monitor state: "+ state)
    
 }

 function checkDistanceValue() {
    var theshold=60;
    interval = setInterval(() => {
    // Perform your distance value check here
    console.log("Check Distance: " + Distance );
    
    if (isMonitoring==1 && isMTHConnected==true){//only do this if monitoring was started
      if (Distance < theshold){ 
          setTimeout(function () {  //slight Delay to prevent spamming the robot       
              stop();
          }, 200)      
      }
      else{
          setTimeout(function () {  //slight Delay to prevent spamming the robot       
              moveForward();
          }, 200)    
      }
  }
    
 
  }, 350); // Interval in milliseconds
}
  
function moveBackward() {
  
     //both PWM's set to same direction.
     let MTR1_PWM=-40;
     let MTR2_PWM=-40;
     

     let  payload_data  = new Uint8Array(2);
     payload_data[0] = MTR1_PWM;
     payload_data[1] = MTR2_PWM;
     
     
     let  payload_cmd  = new Uint8Array(2);
     payload_cmd [0] = 0x05;//controls both motors
     payload_cmd [1] = 0x00;
     
     movecmd(payload_cmd,payload_data);
 
     console.log("Move Backward")
 
 }

function moveForward() {
    //both PWM's set to to the same negative direciton.
    let MTR1_PWM=40;
    let MTR2_PWM=40;
    

    let  payload_data  = new Uint8Array(2);
    payload_data[0] = MTR1_PWM;
    payload_data[1] = MTR2_PWM;
    
    
    let  payload_cmd  = new Uint8Array(2);
    payload_cmd [0] = 0x05;//controls both motors
    payload_cmd [1] = 0x00;
    
    movecmd(payload_cmd,payload_data);

    console.log("Move Forward")

}

function turnLeft() {
   //both PWM's set to opposited directions.
   let MTR1_PWM=40;
   let MTR2_PWM=-40;
   

   let  payload_data  = new Uint8Array(2);
   payload_data[0] = MTR1_PWM;
   payload_data[1] = MTR2_PWM;
   
   
   let  payload_cmd  = new Uint8Array(2);
   payload_cmd [0] = 0x05;//controls both motors
   payload_cmd [1] = 0x00;
   
   movecmd(payload_cmd,payload_data);

   console.log("Turn Left")

}

function turnRight() {
    
   //both PWM's set to opposite directions.
    let MTR1_PWM=-40;
    let MTR2_PWM=40;


    let  payload_data  = new Uint8Array(2);
    payload_data[0] = MTR1_PWM;
    payload_data[1] = MTR2_PWM;
    
    
    let  payload_cmd  = new Uint8Array(2);
    payload_cmd [0] = 0x05;//controls both motors
    payload_cmd [1] = 0x00;
    
    movecmd(payload_cmd,payload_data);

    console.log("Turn Right")
}

function stop() {
  //both PWM's set to 0 to stop.
  let MTR1_PWM=0;
  let MTR2_PWM=0;
  

  let  payload_data  = new Uint8Array(2);
  payload_data[0] = MTR1_PWM;
  payload_data[1] = MTR2_PWM;
  
  
  let  payload_cmd  = new Uint8Array(2);
  payload_cmd [0] = 0x05;//controls both motors
  payload_cmd [1] = 0x00;
 
  movecmd(payload_cmd,payload_data);
  console.log("Stop")

}

man-in-the-loop exercise

This example builds upon the previus PWMRemote Example.

In the ongoing exercise, there's a monitoring function named EnableMonitoring. This function sets motor commands according to the distance value acquired from the MODI sensor. Activation or deactivation of this function is accomplished by clicking either the Start or Stop Monitoring buttons.

function EnableMonitoring(state) { 
    isMonitoring=state;
    if (isMonitoring){
        checkDistanceValue();
    }
    else{
        clearInterval(interval);
    }
   
    console.log("monitor state: "+ state)
    
 }

When Monitoring, the code enables a callback that executes the checkDistanceValue() funtion once every 200ms. When in that function, the code will issue the a stop command if the distance value is below a threshold set on line 2. Otherwise, the code will issue the moveForward() command.

 function checkDistanceValue() {
    var theshold=60;
    interval = setInterval(() => {
    // Perform your distance value check here
    console.log("Check Distance: " + Distance );
    
    if (isMonitoring==1 && isMTHConnected==true){//only do this if monitoring was started
      if (Distance < theshold){ 
          setTimeout(function () {  //slight Delay to prevent spamming the robot       
              stop();
          }, 200)      
      }
      else{
          setTimeout(function () {  //slight Delay to prevent spamming the robot       
              moveForward();
          }, 200)    
      }
  }
    
 
  }, 350); // Interval in milliseconds
}

In this experiment, adjust the threshold value and observe the performance of your robot. Additionally, modify the moveForward() command to achieve the desired PWM values for your Rover. Operate the rover in a space with obstacles, and iteratively fine-tune your settings until it can successfully maneuver without collisions.

🚗
https://mantiscode.azurewebsites.net/webble/mthobstacle.html
🚗Man-In-The-Loop Obstacle Avoidance Tutorial-JavaScript