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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/**
* @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