How I Hacked The mBot Ranger Kit For Autonomous Driving Capabilities

Two weeks ago, I wrote a review of the mBot Ranger kit. It was for the first time when I’ve been playing with a Makeblock kit, and I was impressed. Quality components, a lot of room for further extensions, etc. You can read more about my experience with the kit in the mBot Ranger review. In this post, I’ll continue my experience with the mBot Ranger, and I shared with you how I hacked the mBot Ranger kit to work autonomously using the ultrasonic sensor.

After 30 minutes of working, I assembled all the hardware components of the mBot kit into a nice robot tank. I was very happy, at least until I get into the software side. The kit can be programmed via PC or using an application. Very soon I found that I have some problems with the documentation, and the software version of the smartphone was not up to date because the kit hadn’t been released at that moment. Regarding the tablet version of the software, I don’t own a tablet. (Yes, I don’t want a tablet!)

Luckily, I knew that the brain of the robot is a Me Auriga brick based on Arduino Mega 2560. Being an Arduino fan, I know what to do to move forward.

The plan was to search in the libraries and find the pins to control the robot motors using the Arduino sketch. After a few minutes of research, I found the pins that I needed to control the two DC motors. Working with the ultrasonic sensor was much easier. I use the Makeblock library to read the distance from it.

The idea was to program the robot tank to detect an obstacle, and when the sensor detects one or more obstacles, the robot turns right until the sensor no longer detects any obstacle.

Here is the Arduino sketch and the result of my work with the mBot Ranger kit.

/**
 * @file         mBot Ranger Autonomous Mode.ino
 * @author       IntoRobotics.com
 * @version      V1.1
 * @date         2016/05/13
 * @description  this file is sample code for the mBot Ranger kit
 */
 
#include "MeAuriga.h"
#include <SoftwareSerial.h>

MeUltrasonicSensor ultraSensor(PORT_7); // Ultrasonic module

int distanceLimit=50;    // Maximum distance we want to ping for (in centimeters).
int maxPwm=255;
int halfPwm=200;
int turnPwm=150;
int d=500;
int frontDistance;


//Motor Left
const int pwmMotor1 = 11;
const int inMotor1_1 = 49;
const int inMotor1_2 = 48;

//Motor Right
const int pwmMotor2 = 10;
const int inMotor2_1 = 47;
const int inMotor2_2 = 46;

void setup()
{
  Serial.begin(9600);
 
  pinMode(pwmMotor1,OUTPUT);    //We have to set PWM pin as output
  pinMode(inMotor1_1,OUTPUT);  //Logic pins are also set as output
  pinMode(inMotor1_2,OUTPUT);
  pinMode(pwmMotor2,OUTPUT);    //We have to set PWM pin as output
  pinMode(inMotor2_1,OUTPUT);  //Logic pins are also set as output
  pinMode(inMotor2_2,OUTPUT);
 
}

void loop()
{
   GetFrontDistance();  //Get the ultrasonic distance in cm
 
   if(frontDistance>=distanceLimit){
      FullSpeedMode();  //The robot is in full speed mode
    }

   else {  
      ReduceSpeed();   //Reduce the speed of the robot
      Stop();          //Stop the robot
      do {  
          TurnRight();
          delay(200);  // Wait for sensor to stabilize
          GetFrontDistance();  //Get the ultrasonic distance in cm  
          } while(frontDistance<distanceLimit);
      }
}

 int GetFrontDistance(){
   frontDistance=ultraSensor.distanceCm();
   Serial.print("Front distance (in cm) is: "); //For debugging
   Serial.println(frontDistance);
   return frontDistance;
   }

 void FullSpeedMode(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,maxPwm);
    digitalWrite(inMotor2_1, LOW);
    digitalWrite(inMotor2_2, HIGH);
    analogWrite(pwmMotor2,maxPwm);
    Serial.println("Full speed mode"); //For debugging
   }

  void ReduceSpeed(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,halfPwm);//Set speed via PWM
    digitalWrite(inMotor2_1, LOW);
    digitalWrite(inMotor2_2, HIGH);
    analogWrite(pwmMotor2,halfPwm);//Set speed via PWM
    Serial.println("Reduce the speed"); //For debugging
    delay(d);
    }

  void Stop(){
    analogWrite(pwmMotor1, 0);
    analogWrite(pwmMotor2, 0);
    Serial.println("Stop");
    delay(d);
   }
 
   void TurnRight(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,turnPwm);//Set speed via PWM
    digitalWrite(inMotor2_1, HIGH);
    digitalWrite(inMotor2_2, LOW);
    analogWrite(pwmMotor2,turnPwm);//Set speed via PWM
    Serial.println("Turn the robot right"); //For debugging
    }

Makeblock mBot Ranger Kit Autonomous Mode

13 comments » Write a comment

  1. Hi!

    I have read this blog of yours and I am extremely impressed. I am trying to do something similar with mBot Ranger. Could you please share with me how did you figure out the pins to control the robot motors using the Arduino sketch?

    Kind Regards.

      • Hi!

        Thank you for your prompt reply. I think I may not have been very clear with my question. Following is attached a snippet of the code:
        //Motor Left
        const int pwmMotor1 = 11;
        const int inMotor1_1 = 49;
        const int inMotor1_2 = 48;

        //Motor Right
        const int pwmMotor2 = 10;
        const int inMotor2_1 = 47;
        const int inMotor2_2 = 46;

        In this snippet, you are initialising the pins. My question is: how did you figure out which pins the motor connects to on the MeAuriga board?

        I hope I am making more sense now. Thank you once again.

        • Hi,

          Well, now I understood the question 🙂
          I looked into the libraries. There you can find many more details about the pins, etc.

          I already write that in the article: “The plan was to search in the libraries and find the pins to control the robot motors using the Arduino sketch.”

  2. Hi!

    I ran this code and I am having the strangest problem. I thought to ask you since you have quite some experience working with mBot Ranger and arduino. When I uploaded this code to the Auriga board and the bot was still thethered to the computer, it was working brillaintly. However, when I took the cable off, as soon as the ultrasonic sensor sensed the wall, the motors would move a little and then stop and continue to do this. Turning the mBot off and turning it on again made no difference. I changed the batteries as well but the problem persisted. Could you tell me what I am doing wrong?

    Kind Regards

  3. Hello,

    I’m trying to simply make my motors move on the mBot ranger. I’ve successfully read stuff from sensors, by reading the firmware. However I’m stuck with the motors: I’ve tried many things – including your example above – but alas 🙁 nothing moves.

    The code executes fine, no errors. I think the pin numbers might not be the right for my board. How exactly did you find the pins for your board ? When i look at the firmware code for mBot, I see things like SLOT_1 and SLOT_2, not hard-coded numbers.
    Any idea on how to make sure i got the pins right ?

  4. Hi,

    Can you tell me more exactly for what do I look for in the libraries? Where should i look to find those pins? (I’m new to arduino and everything related to it and it’s such a mess on google regarding mBots (Ranger)).

    First I tried to program the robot through Arduino IDE, using the libraries from MakeBlock. But somehow i can’t manage to start the motors using motor.run(). I tried all kinds of adressing to the motors (MeDCMotor name(9) or MeDCMotor name(Slot_1) or MeDCMotor name(M1), etc.) and nothing works. I really think there’s something related to the adressing to the motors, because my code compiles perfectly.But YOUR method worked, it’s the only code that worked on my robot.

    Also, I tried different examples from internet that also uses those libraries and neither these are working. It’s pretty exhausting to look on google for answers, because 80% of the results are related to mBlock applications, not Arduino IDE.

    Multumesc!

    • Hi Emanuel,
      “Can you tell me more exactly for what do I look for in the libraries? Where should i look to find those pins?” – First of all, you have to know what type of board you’re using – Auriga, MegaPi,…?

      Depending on what you want to control from Arduino IDE(sensors, motors), you have to find the proper pins in files like these: https://github.com/Makeblock-official/Makeblock-Libraries/tree/master/makeblock/src
      In general, the pin numbers are at the beginning of the files.

      “I really think there’s something related to the adressing to the motors, because my code compiles perfectly.But YOUR method worked, it’s the only code that worked on my robot.” – most probably you don’t control the right pins.

      Cu placere 🙂

      • Hi again! This gonna be a longer message, but if you are not in the mood, you can ignore it, I will totally understand. I didn’t even expected to answer my first one, since it’s a 2 years old post.

        Soo, yeah, I’ve got an Auriga board. Your code is working. I also found the same pins in the /meauriga.h, at the bottom, but if I had to search for the other pins (that are not specified clearly like in meauriga.h), I think I would be lost.

        I looked through the written codes in library, I didn’t yet figured why it’s not working using their library and functions,moreover the board type doesn’t really matter, cause every type of board uses the same MeDCmotor file. Maybe I’ll try calling meDCmotor with another constructor that let’s me give pins as parameters, but I saw that they have only 1 pin as a direction in the parameter list, and as far as I know, I need 2 pins for each motor. The best decision would be to stick to your method.

        The thing is that we have a project for school, and I chose mbot Ranger. As a first task, our teacher said to make a control loop for the robot that works like this: I give him a set value speed, if the robot starts to go uphill or downhill, it must keep the same speed.

        In my mind, the encoder and motor were 2 parts separated, so my method was something like this: I set a speed to the motor, if the speed read from encoder is lower than the one set to motor then it means that the robot is climbing and I need to increase the speed. (Something like I put 5 volts on motor, it goes 5 m/s, but if I continue to power it with 5 volts, and the speed decreases, then it must be because it’s climbing or other impediments).

        Anyway. I figured out that I can’t really do that. I don’t think I can even set and read from the encoder at the same time, since I use and set the pins as outputs. I also noticed that in the libraries, the getSpeed() function, it doesn’t actually read from the hardware, but it reads from my previously set value).

        Should I use the built-in gyroscope? I’m afraid that I’ll get through the same problems as with motors and maybe I won’t be able to find and configure pins properly. I’ll give it a shot as soon as I get to use the robot again (there are only a few for use purposes inside the college building).

        Multumesc din nou.Promit ca e ultimul strigat de ajutor, haha! O zi faina!

        • Hi,
          Well, you want to control the robot with PWM and encoder. I didn’t try in this way and to be honest, I didn’t know how to do it. Maybe you have to try contact the manufacturer to give you access to the code.
          PS: daca te pot ajuta, cu mare drag oricand!
          Regards,
          Dragos

Leave a Reply to Josh Cancel reply

Required fields are marked *.