• EUR€
  • £GBP
  • $USD
TUTORIALS RoboticsArduino

How to Play With MiniQ 2WD Complete Robot Kit v2.0- Lesson 6 (Obstacle Avoidance)

DFRobot Mar 24 2017 1104

Lesson 6. Obstacle Avoidance

Key Points

Avoiding obstacle sometimes means avoiding danger. We have learned how to make a robot that follows the line, and in this lesson, you’ll tech it to avoid obstacles themselves.
Key Points
1. Learn how to use infrared transmitter and receiver
2. Get to know how to avoid the obstacle
3. Program your robot
4. Equipment needed: Micro USB CableMiniQ 2WD Complete robot Kit v2.0

Upload the Code

Open the folder and find “avoidance”

1) Find the code we use

Upload the code, and then put your robot on the ground, use your hands as obstacle to test the robot.

2) The code

Code Analysis


#define EN1 6//enable the right motor driver port #define IN1 7//direction of the right motor #define EN2 5//enable the left motor driver port #define IN2 12//direction of the left motor #define FORW 0//forward #define BACK 1//back #define IR_IN 17//infrared receiver #define L_IR 13//infrared transmitter in the left side #define R_IR 8//infrared transmitter in the right side int count;//the count of the pulse received //M1_DIR for left motor direction, M1_EN for speed of left motor, so does the right motor

void Motor_Control(int M1_DIR,int M1_EN,int M2_DIR,int M2_EN) { //////////M1//////////////////////// if(M1_DIR==FORW) digitalWrite(IN1,FORW); else digitalWrite(IN1,BACK); if(M1_EN==0) analogWrite(EN1,LOW); else analogWrite(EN1,M1_EN);

///////////M2////////////////////// if(M2_DIR==FORW) digitalWrite(IN2,FORW); else digitalWrite(IN2,BACK); if(M2_EN==0) analogWrite(EN2,LOW); else analogWrite(EN2,M2_EN); } void L_Send40KHZ(void)// left infrared LED send 40kHZ pulse { int i; for(i=0;i<24;i++) { digitalWrite(L_IR,LOW); delayMicroseconds(8); digitalWrite(L_IR,HIGH); delayMicroseconds(8); } } void R_Send40KHZ(void)//right infrared LED send 40kHZ pulse { int i; for(i=0;i<24;i++) { digitalWrite(R_IR,LOW);// delayMicroseconds(8); digitalWrite(R_IR,HIGH); delayMicroseconds(8); } } void pcint0_init(void)//interrupt initialize { PCICR = 0X01;//enable pins in set 0 PCMSK0 = 0X01;//enable pin of number 0 } ISR(PCINT0_vect)//Pin PB0 interrupt function { count++;//count the pulse } void Obstacle_Avoidance(void) { char i; count=0; for(i=0;i<20;i++)//send 20 pulse { L_Send40KHZ(); delayMicroseconds(600); } if(count>20)//if count more than 20, it is obstacle { Motor_Control(BACK,100,BACK,100);//turn back delay(200); Motor_Control(BACK,100,FORW,100);//turn right delay(200); } else { Motor_Control(FORW,100,FORW,100); } count=0; for(i=0;i<20;i++) { R_Send40KHZ(); delayMicroseconds(600); } if(count>20) { Motor_Control(BACK,100,BACK,100); delay(200); Motor_Control(FORW,100,BACK,100); delay(200); } else { Motor_Control(FORW,100,FORW,100);//forward } } void setup() { pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(12,OUTPUT); pinMode(8,OUTPUT); pinMode(10,OUTPUT); pinMode(13,OUTPUT); pinMode(14,OUTPUT); pinMode(16,OUTPUT); pinMode(17,INPUT); pcint0_init(); } void loop() { Obstacle_Avoidance(); }

Principle Analysis

1. Infrared receiver 
The receiver on the robot consist of receiver, amplification and demodulation. The infrared signal will be demodulated after being received, then the receiver will gives a high level voltage or low level voltage. And for the users, we often connect the output pin to the interrupt pin of Arduino thus to get the data.

2. The concept if interrupt
Interrupt is the action the MCU will memorize where the code runs and   jump   to run a special function at the same time when it meets special situation or request, and it will jump back when the MCU finish the function for acting the request or the situation.
Take a simple example, if you were eating, and you hear it was raining, you would go and close the window, and then went back to eat your meal. The rain was  the interrupt signal, you do the action of closing the window. The continue eating.

3. Principle of avoiding obstacle
We  use infrared sensor to  help  us  finish  the task of avoiding obstacle. The   left transmitter will give 40KHz pulse and then the right one does the same work. If the robot can receive the pulse be reflected, the interrupt function will work to count how many time it receives the signal. When the time is more than 20, we can consider that there must be something in front, then we just need to change the route.

3) Obstacle Avoiding Modules

In the flow chart below, we can see that we just need to check that how many times the receiver can receive the signal when the transmitter is working. After finish the simple way to avoid the obstacle, it is very nice to make your own opinion to have fun with your robot like let your robot follow your hand or something else.
And you can also use line tracking sensors and obstacle avoiding sensor together to let your robot can stop when he have a line follow work. And we also put an electronic compass sensor, so you can let your robot runs more straightly

4) Flow chart about the code

Because if we just turn on the infrared LED, the distance we can detect will be very short, that is not enough for using, and now we use pulse about 38KHz, we can detect longer. This is an interesting point that we change the way of transmitting, and it gives different feedback.

Circuit Design

5) Obstacle Avoidance Circuit

See the schematic upon, C23 is used for smoothing, that means ignore the affection by the noise of the circuit. IRR means infrared led in the right and if this pin is given a low voltage, D8 will work. The infrared light reflected will be received by U10, the infrared receiver, and if it receives the right signal, it will change voltage of pin2 to make the MCU run its interrupt function. D17 means digital pin 17  of Arduino.
Now you can do something else with these sensors, just go ahead your idea.

Continue Reading

There is a kind of game named sumo robot which is based on obstacle avoiding strategies,so would you have a try?

6) SumoRobot

Related Category: Robotic > Robot Platform
Last Arduino MiniQ 2WD Tutorial: MiniQ 2WD Complete Robot Kit v2.0- Lesson 5(Make It Glow) 

Next Arduino MiniQ 2WD Tutorial: MiniQ 2WD Complete Robot Kit v2.0- Lesson 7(Encoder)