06 - Range Finder / Line Follower / Gyro / Touch

Ultrasonic Range Finder

The Ultrasonic Range Finder is a sensor used to detect how far an object is in front of the sensor. It uses sound waves as a method of detection. A use of the range finder is to detect objects within the robot's paths. This allows the robot to be able maneuver around, or interact with the objects. To learn more about the details of the range finder, visit this link.

In RobotC, you can view the Ultrasonic Range Finder values using the Debugger Window for the Sensors. It will be under SONAR(cm). The value given is the distance between the object and sensor in centimeters. If the value is -1, it means the sensor is not detecting anything as the object in front of it is out of its range.

Using the Sensor

The following code moves the robot forward until the sensor value is less than 50 cm. That means that object the sensor detect will be about 50cm in front of it when the robot stops.

#pragma config(StandardModel, "RVW CLAWBOT")

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()

{

//set the motors to 100 as long as the sonar sensor

//value is greater than 50cm or the sensor is not detecting anything within its range

while( SensorValue[sonarSensor] > 50 || SensorValue[sonarSensor] == -1 ) {

motor[leftMotor] = 100;

motor[rightMotor] = 100;

}

//stop

motor[leftMotor] = 0;

motor[rightMotor] = 0;

}

Activity 1:

    1. Write code so that robot move forwards until it detects an object that is 70 cm away.

    2. Write code so that robot move forwards until it detects an object that is 40 cm away.

    3. Write code so that robot move forwards until it detects an object that is 30 cm away. Then it will make a 180 degree turn, and stop.

Activity 2:

    1. Write a function that will move the robot forward until an object detected given the amount distance away from it. Name this function as follows.

    2. void moveForwardUntil(float distance){...}

The Line Follower

The line follower sensor is used to detect light and dark surfaces. This is handy when detecting lines as it will have a dark surface. From this, you can use the sensors to have the robot follow a set of lines. To learn more the about the line follower, you can view this link.

Concepts of Line Tracking

To track a line, we can use 3 line follower sensors in a row like the image below.

When the line is detected in middle sensor, in what direction should the robot move towards?

When the line is detected in left sensor, in what direction should the robot move towards?

When the line is detected in right sensor, in what direction should the robot move towards?

Adjusting the Sensors

The value of the sensors are given as number.

This is the number represent the shade of the surface. It it is greater than the threshold value that detects a dark surface, then you know you are one a line.

int threshold = ?? //Find what the threshold number should be.

if(SensorValue(rightLineFollower) > threshold )

{

//Your code here.

}

Corresponding Code

The code below adjust the movements of the robot given the concepts above completed for Line Tracking.

#pragma config(StandardModel, "RVW CLAWBOT")

task main(){

int threshold = 2000;

while(true)

{

if(SensorValue[leftLineFollower] > threshold){

motor[leftMotor] = 0;

motor [rightMotor] = 40;

}

if(SensorValue[rightLineFollower] > threshold){

motor[leftMotor] = 40;

motor [rightMotor] = 0;

}

if(SensorValue[centerLineFollower] > threshold){

motor[leftMotor] = 40;

motor [rightMotor] = 40;

}

}

}

Questions - How could this simple code be improved? How could it become more accurate and/or precise in it's line tracking? What will happen if two or three sensors are triggered at the same time? Will it stutter because the motors get set to zero and forty at the same time? What better logic/code could we use?

Gyro Sensor

The Gyro Sensor is used measure direction. It is use to help your robot navigate and make turns. To learn more about the Gyro Sensor visit this link.

Unlike encoder the rotation of the robot is measured without the use of the wheels. Therefore, this increases accuracy as factors affecting the wheels are not in play.

Gyro Code

The gyro sensor values are rotation values.

The following code turns right until the gyro sensor value is 900. This is about 90 degrees.

#pragma config(StandardModel, "RVW CLAWBOT")

task main()

{

while( SensorValue[gyro] < 900 )

{

motor[leftMotor] = 30;

motor[rightMotor] = -30;

}

}

Notice a few things

    1. I didn't set the sensor to zero before turning, so it starts at whatever direction I am currently pointing.

    2. I also don't use the absolute value, if I turn in the negative direction this code will keep turning forever and the gyro becomes more and more negative and is hence always less than 900.

    3. I don't have any correction at the end. The loop will break once the robot has turned more than 90.0 degrees, but in the end it will usually be pointing about a half of a degree off due to slippage and time delay.

Question - How could this code be improved?

Activity 1

Write the code for the robot to do the follwoing...

    • turn 90 degrees to the left.

    • turn 180 degrees to the right.

    • turn 360 degrees to the left.

*Make sure to set the gyro sensor value to zero before each turn.

Activity 2

    • Create a function called void turnRight(int degrees) that takes in the degree as an integer, and will make the robot turn right the amount the degrees passed in. Test it with 30, 45, and 90 degrees.

    • Create a function called void turnLeft(int degrees) that takes in the degree as an integer, and will make the robot turn left the amount the degrees passed in. Test it with 30, 45, and 90 degrees.

Bumper (Touch) Sensor

The bumper sensor is a physical touch sensor on the front of the robot. It will detect when the switch physically bumps into an object.

There are two versions of this sensor (shown below). The older version has a metal arm sticking out but was prone to breaking, so the newer "button" style was created.

Both of these sensors are a simple digital switch that reads "1" if it is in contact with something or "0" if it is not in contact with something.

Example code

This code will slowly drive the clawbot forward until it touches an object, then it will stop.

#pragma config(StandardModel, "RVW CLAWBOT")

task main()

{

//This moves the arm out of the way

motor[armMotor] = 127;

wait1Msec(500);

motor[armMotor] = 0;

// This loop will run until the robot touches an object

while(SensorValue(touchSensor) == 0)

{

motor[rightMotor] = 40;

motor[leftMotor] = 40;

}

//Once the touch sensor touches an object, the loop stops


//Now turn the motors off

motor[rightMotor] = 0;

motor[leftMotor] = 0;

}

Note: The touch sensor is only located in one specific spot on the front of the robot. If any other part of the robot is touching something, the sensor will not detect it. It will only detect if that one small part of the robot (the sensor button itself) is in contact with something. An image below shows the location of the touch sensor on the virtual robot.