Using the Sharp IR Sensor to Navigate a Simple Maze

From ROBOTC API Guide
Jump to: navigation, search
ArduinoArduino Tutorials and Guided ProjectsLego + Arduino, Mobile Robotics Platform → Tutorials/Arduino Projects/Mobile Robotics/Lego/Using the Sharp IR Sensor to Navigate A Simple Maze

Contents

The robot easily navigates with the Sharp IR sensor


The Maze, Again

The last time you navigated the simple maze (discounting the light sensor maze, which was different), you used an ultrasonic sensor to detect and avoid the walls. Using the Sharp IR sensor is not all that different: You command the robot to go forward until it detects a wall, then stop, then turn the appropriate direction, then repeat until the robot reaches its goal.

Here is a diagram of the maze and the path we are going to take:

The maze

Programming

Configuration

Once again, we need to tell ROBOTC how the robot is configured:

  • DFRobot Motor Shield
  • Motor_5 as a reversed Internal H.Bridge named "rightServo"
  • Motor_6 as a reversed Internal H.Bridge named "leftServo"
  • Analog pin 2 as an analog input named "sharpIR"


#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, anlg2,  sharpIR,        sensorAnalog)
#pragma config(Motor,  motor_5,         rightServo,    tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4)
#pragma config(Motor,  motor_6,         leftServo,     tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

Functions

To make our lives easier, we can take a number of functions from previous programs, such as the functions to turn, and the function to stop.

void TurnRight(int time)
{
  motor[leftServo] = 100;
  motor[rightServo] = -100;
  wait1Msec(time);
}
 
void TurnLeft(int time)
{
  motor[leftServo] = -100;
  motor[rightServo] = 100;
  wait1Msec(time);
}
 
void Stop()
{
  motor[leftServo] = 0;
  motor[rightServo] = 0;
}
 
void DriveBackward()
{
  motor[leftServo] = -100;
  motor[rightServo] = -100;
  wait1Msec(400);
}

But we still need one more function, this one a modified version of our old ultrasonic function, "DriveForwardUntilWall"

void DriveForwardUntilWall()
{
  motor[leftServo] = 100;
  motor[rightServo] = 100;
  while(SensorValue[sharpIR] < 300) 
  {
  }
  DriveBackward();
  Stop();
}

This function tells the robot to move forward until the sensor detects an object, then back up. You'll then have to code a turn command.

The Program

So when we put it all together in one program, the code comes out looking something like this:

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(PluginCircuitBoard, typeShieldDFRobotMotor)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, anlg2,  sharpIR,        sensorAnalog)
#pragma config(Motor,  motor_5,         rightServo,    tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl5, dgtl4)
#pragma config(Motor,  motor_6,         leftServo,     tmotorInternalHBridgeSinglePWM, openLoop, reversed, IOPins, dgtl6, dgtl7)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
void TurnRight(int time)
{
  motor[leftServo] = 100;
  motor[rightServo] = -100;
  wait1Msec(time);
}
 
void TurnLeft(int time)
{
  motor[leftServo] = -100;
  motor[rightServo] = 100;
  wait1Msec(time);
}
 
void Stop()
{
  motor[leftServo] = 0;
  motor[rightServo] = 0;
}
 
void DriveBackward()
{
  motor[leftServo] = -100;
  motor[rightServo] = -100;
  wait1Msec(400);
}
 
void DriveForwardUntilWall()
{
  motor[leftServo] = 100;
  motor[rightServo] = 100;
  while(SensorValue[sharpIR] < 300)
  {
  }
  DriveBackward();
  Stop();
}
 
task main ()
{
    //wait 1 second before starting.
  wait1Msec(1000);
  DriveForwardUntilWall();
  TurnLeft(500);
  Stop();
  DriveForwardUntilWall();
  TurnRight(900);
  Stop();
  DriveForwardUntilWall();
  TurnRight(800);
  Stop();
  DriveForwardUntilWall();
  Stop();
  //A last little drive forward to put us into the goal
  motor[leftServo]=100;
  motor[rightServo]=100;
  wait1Msec(1000);
 
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox