In the last lecture you examined different techniques for feature extraction and, in particular, line detection and fitting. Remember from the lecture that when we speak of a feature, such as a line, we are interested in information in sensor data that is robust to variations in rotation and scale as well as noise. Since all sensors in mobile robots are prone to some degree of noise, we will focus on how to integrate multiple sensors to decrease the influence of noise and refine the accuracy of the robot’s world view. Although sensor fusion techniques will be the subject of much of the remaining course, today we will start simply, using only three of the infra-red distance sensors at a time. Your goal for this lab is to implement line detection to recognize the walls of the Ratslife world, and orient the E-Puck accordingly.

Distance Sensors:

The E-Puck has eight distance sensors labeled “ps0” through “ps7”. They have the following bearings in degrees from the front of the E-Puck: 10^\circ, 45^\circ, 90^\circ, 150^\circ, 210^\circ, 270^\circ, 315^\circ, and 350^\circ, with “ps0” on a bearing of 10^\circ and “ps7” on a bearing of 350^\circ. To create, enable, and use a distance sensor, follow these steps:

//declare as a global variable above the constructor
DistanceSensor myDistSensor;
//initialize in construtor
myDistSensor = getDistanceSensor("ps0”);
//use .getValue() in methods to get the value of the sensor
double someDistance = myDistSensor.getValue()

The following additional information was taken from the DistanceSensor page of the Webots Reference Manual. “This device simulation is performed by detecting the collisions between one or several sensor rays and the bounding objects of Solid nodes in the environment. A lookupTable is used for specifying the desired response curve and noise of the device. This table indicates how the ray intersection distances measured by Webots must be mapped to response values returned by the function getValue(). The first column of the table specifies the input distances, the second column specifies the corresponding desired response values, and the third column indicates the desired noise. The noise on the return value is computed according to a uniform random number distribution whose range is calculated as a percent of the response value. Let us consider an example,

\left(\begin{array}{ccc}  0 & 1000 & 0 \\  0.1 & 1000 & 0.1\\  0.2 & 400 & 0.1\\  0.3 & 50 & 0.1\\  0.37 & 30 & 0\end{array}\right)

The above lookup table means that for a distance of 0 meters, the sensor will return a value of 1000 without noise (0); for a distance of 0.1 meter, the sensor will return 1000 with a noise of up to 10 percent (100); for a distance value of 0.2 meters, the sensor will return 400 plus or minus up to 10 percent (40), etc. Distance values not directly specified in the lookup table will be linearly interpolated. Note that the input values of a lookup table must always be positive and sorted in increasing order. ”

The lookuptable for the E-Puck is:
\left(\begin{array}{ccc}  0 & 4095 & 0.005\\  0.01 & 3474 & 0.037\\  0.02 & 2211 & 0.071\\  0.03 & 306 & 0.125\\  0.04 & 164 & 0.206\\  0.05 & 90 & 0.269\\  0.06 & 56 & 0.438\\  0.07 & 34 & 0.704\end{array}\right)

You will need to use this lookuptable to do linear interpolation to find the actual distance in meters given the distance sensor reading. For example, if we receive a reading of 1000 we know this distance is somewhere between 0.02m and 0.03m. To find the exact distance, create a line between the points (2211, 0.02) and (306, 0.03) and then use your line to find the value at x = 1000, which is around 0.026m.

Getting Points from Distances:

The whole premise of line detection assumes that you have a point cloud derived from your sensors to which you will fit your line. This involves creating a point (x, y) from a distance d given by a distance sensor. This point can be created, relative to the E-Puck, using the distance given, d, and the angle, \theta, of the sensor relative to the E-Puck's axis. For example, assume the robot is positioned at (0, 0) on the coordinate system and has received the distances d_1, d_2, and d_3 from the sensors “ps1”, “ps2”, and “ps3”. The sensor “ps1” is positioned 45^\circ from the front of the robot which means it is also 45^\circ from the horizontal axis of the robot. The sensor “ps2” is 90^\circ from the front of the robot which means it has an angle of 0^\circ relative to the horizontal axis. The third sensor, “ps3”, has an angle of 150^\circ from the front which means it has an angle of -60^\circ with respect to the horizontal axis. We can then use these angles from the horizontal to find the (x, y) point relative to the E-Puck by using sine and cosine. For example, the point given by “ps3” would be (d_3cos(-60^\circ), d_3sin(-60^\circ)).

Detecting the Line:

Although the equations introduced in the lecture are more general (and more involved) for this situation involving only three sensors, we can use a simpler approach to finding the best-fit line. If you have the two outer points, you can fit a line through it, by finding m and b in the equation y = mx + b. You can then use the middle point to determine whether all three points are on the same line. The error between where the middle point actually lies and where the line indicates it should lie is given by the difference between these two values. If the error is too large, the three sensors do not see a line and the measurement should be rejected. Otherwise, the E-Puck can use this line to adjust its orientation. The orientation of the E-Puck relative to the wall that it detected is given arctan(m), where m is the slope of the line. The distance of the E-Puck from the wall is given by \frac{b}{m}sin(\alpha), where m is the slope of the line, b is the y-intercept of the line, and \alpha is the angle of orientation of the E-Puck.


For this lab we are going to venture into the Rats Life part of Webots. When you first start Webots, choose the Rats Life Contest window, this will open the Rats Life demo world. This world has a randomized maze and two E-Pucks along with their controllers. You can edit one of the controllers given or create a new controller to work with. When you first try and alter anything about this world, Webots will warn you that you are trying to overwrite one of their source files and will ask if you want to make a copy of the project outside the installation directory. You should choose YES to this and make a copy in your own folders to use for the remainder of this lecture.


1) Write an algorithm that uses the input from three of the distance sensors to detect the line corresponding to one of the E-Puck's neighboring walls.  Use this line to print the E-Puck's orientation and distance with respect to the detected wall.

2) Some of you noticed in lab #6, path-planning, that the E-Puck slowly accumulated orientation errors as it progressed through the maze. Integrate your algorithm from 1) above into your controller for lab #6, path-planning, to help your E-Puck keep its correct orientation within each square as it moves toward its goal.


One Response to Introduction to Robotics, Lab #7: Line Detection

  1. Nate Lapinski says:

    When calculating alpha in the last step, should we be using atan2() or just atan()?
    The book mentions atan2(), and I get what appear to be better results when using atan2().


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Set your Twitter account name in your settings to use the TwitterBar Section.