XBot: An Autonomous Mobile Robot [Part 1]
By Dimitris Xanthopoulos
1. Abstract
This design is an autonomous mobile robot which was designed and built in order to perform various navigation algorithms. The design consisted of two main sections: (1) Electronic analysis of the various robot sensors and (2) Programming techniques used to interface the sensors with the robot’s microcontroller.
2. Introduction:
The robot was a small two wheeled mobile platform which was controlled by a micro-controller. The robot could “sense” its surroundings with the aid of various electronic sensors while mechanical actuators were used to move it around. The robot had no specific behaviour; its behaviour was determined by the program which was loaded to the microcontroller. In that way, it could be used as a general robotics experimental platform.
3. Mechanical Part
3.1 The Platform

The platform of the robot (Figure 1) was constructed using hardened aluminium foil. The platform had the geometry of a circle (of diameter 22.5 cm) and the drive wheels and the tail caster were sitting on the perimeter of this circle. Hence the robot could rotate in its own space. The wheel characteristics are shown in Table 1:
|
Table 1: Wheel Characteristics |
|
|
Diameter and Width of tyres |
9.5 cm x 3.5 cm |
|
Circumference of tyres |
2PI(diameter/2) = 29.845cm |
|
Distance between wheels (centre-to-centre) |
19.5cm |
|
A small non-driven tail wheel caster was used to balance the robot. |
|
3.2 The Servo Motors
Two (modified for continuous rotation) servo motors were used to manoeuvre the robot in a dual-differential drive configuration (Figure 2).

A third (unmodified) servo motor mounted on the front of the platform was used to rotate (+/- 90°) the ultrasonic ranging system.

Each servo had a control line wire where a pulse was expected every 20ms. The width of this pulse determined the direction and the speed of the servo shaft as shown in Figure 3:
- A pulse width of 1.5ms made the servo shaft stay still (Figure 3a neutral position).
- A pulse width between 1.25 and 1.5ms made the servo rotate anti-clockwise.
When pulse width à 1.25ms, speed à maximum (Figure 3b).
When pulse width à 1.5ms, speed à minimum.
- A pulse width between 1.5 and 1.75ms made the servo rotate clockwise.
When pulse width à 1.5ms, speed à minimum
When pulse width à 1.75ms, speed à maximum (Figure 3c).
4. Electronic Part
4.1 Microcontroller
A PIC16F84 microcontroller was used to control the robot. This microcontroller was chosen due to its low cost, small dimensions and ease of programming. The microcontroller had a Reduced Instruction Set Computer (RISC) architecture, with 35 instructions (14 bit wide) and 8-bit wide data path. With a 4 MHz crystal used as a clock, the time taken for each instruction cycle was 1μS which was sufficient even for the most speed demanding tasks. The microcontroller had 13 general purpose Input/Output pins (RA0:RA4 and RB0:RB7). The connections between the microcontroller’s pins and the robot’s sensors are shown on Table 2.
|
Table 2: microcontroller-sensors Connections |
|
|
RB0 |
Left Infa-Red Sensor |
|
RB1 |
Centre-Left Infa-Red Sensor |
|
RB2 |
Centre-Right Infa-Red Sensor |
|
RB3 |
Right Infa-Red Sensor |
|
RB4 |
Left Wheel Encoder |
|
RB5 |
Right Wheel Encoder |
|
RB6 |
Left side of the “Line Sensor” |
|
RB7 |
Right side of the “Line Sensor” |
|
RA0 |
Left Wheel Servo Motor |
|
RA1 |
Right Wheel Servo Motor |
|
RA2 |
Ultrasonic Ranger Servo Motor |
|
RA3 |
Sonar Receiver |
|
RA4 |
Sonar Transmitter |
4.2 Power Supply

A 12Volt, 1.3Ah sealed lead-acid battery was used to power the robot. Most of the robot’s sensors though needed a 5 Volt power supply. Hence the voltage regulator shown on Figure 4 was used. The operation of this circuit was based on IC1 (LM7805) which was a dissipative regulator (i.e. excessive electrical energy was converted to heat ó inefficient regulator). The voltage regulator could deliver a maximum current of 1.5 Amps which was sufficient for the robot’s circuitry. Capacitors C1, C2 and C3 were used to eliminate any noise.
4.3 Line Sensor
The purpose of the Line Sensor was to enable the robot to follow a black (or white) line drawn on white (or black) background.
4.3.1 Theory of operation

The line sensor (Figure 5) operation was based on two Cadmium-Sulphide (CdS) photo-resistors (R2 – R4). The values of these photo-resistors depended on the incident to them light. When the light was dim, their values increased and when the light was bright their values decreased.
(1)
and similarly
(2)
(V=5volts)
From equations (1) and (2) the signals (
and
) which were fed to the microcontroller pins (RB6 and RB7) depended on the values of
and
(i.e. depended on how much light was incident to them).

When the robot was centred with the black line (Figure 6a), the light emitted from the LED was not reflected back to any of the two photo-resistors:
High resistance
HIGH
When the robot had bore left (Figure 6b), the light emitted from the LED was reflected back only to the left (
) photo-resistor and hence
LOW
When the robot had bore right (Figure 6c), the light emitted from the LED was reflected back only to the right (
) photo-resistor and hence
LOW
4.3.2 Line Following Flow Chart
The flow chart of the robot's line following behaviour is shown on Figure 7

The actual source code which was used to implement the line following algorithm was written in Assembly language. You can download the file here: Line following source code
4.4 Infa-Red Sensors

Four commercial Infa-Red (IR) detectors (Figure 8) mounted on the front of the robot (Figure 9) were used for obstacle detection and avoidance.
Each detector had an emitter and a receiver side. The emitter continuously emitted IR light. When an object was in front of the detector and its distance from the detector was less than 15 cm, the IR light bounced off of the object and returned back to the receiver thus making the output of the detector (Vo) go HIGH. When no object was detected the output was LOW

A pull-up resistor was connected between Vcc and Vo as shown in Figure 10 to ensure that Vo was HIGH whenever an obstacle was detected.
4.4.1 Obstacle Avoidance Pseudo Code
With the four IR sensors connected to RB0:RB3 I/O pins the robot was able to navigate avoiding any obstacles in its path. The following Pseudo Code was used:
START:
- IF no obstacles detected THEN move forward
- IF obstacle detected on the left side AND no obstacle detected on the right side THEN turn right
- IF obstacle detected on the right side AND no obstacle detected on the left side THEN turn left.
- IF obstacle detected on the left side AND obstacle detected on the right side THEN move backward a little bit and then randomly turn left or right.
Go to START
The 'C' source code can be downloaded here: Obstacle avoidance source code
4.5 Rotating Ultrasonic Ranging Module
Ultrasonic ranging (Figure 11) entailed transmitting an ultrasonic wave (a sound wave at frequencies above human hearing) and measuring the time that it took for the ultrasonic wave to reflect off of an object and return back to the origin. The distance (d) of the object from the origin was proportional to the reflection time ("time of flight", t):

(3)
where v is the speed of sound:
(4)
T is the temperature in °C. Assuming an operating temperature of 20 °C, the speed of sound is:
![]()
4.5.1 Theory of operation: Transmitter

The transmitter's schematic is shown in Figure 13 (Leang, 1994). The microcontroller (pin RA3) was used to generate three driving pulses at approximately 40 kHz (the nominal receiver frequency). The maximum detectable distance
of the ranging system was about 2 meters. Based on that fact and using equations (3) and (5), the maximum "time of flight",
was calculated:
![]()
Hence the 40 kHz driving pulses had to be repeated every 11.64mS in order to allow for the maximum detectable distance to be obtained.
The microcontroller's pin RA3 could only source a few milliamps. There was the need for a current amplifier to drive the 500U ultrasonic transducer and hence the 2N3904 general purpose NPN amplifier was used.
4.5.2 Theory of operation: Receiver

The receiver (Figure 13) (Leang, 1994) simply "listened" for the return 'echo' after it bounced off of an object. The small 'echo' signal, when detected, was amplified 1000 times using a standard operational amplifier (LM741). The signal was then fed into a tone decoder (LM567) which was set to lock onto a 40 kHz signal.. The output from the tone decoder could be directly fed into the microcontroller. To help minimize false triggering, the output from the tone decoder was fed into a voltage comparator (LM311) which was set to trigger at the appropriate level. The output of the voltage comparator (connected to RA4) was HIGH when no echo was detected and swung LOW when an echo was detected
4.5.3 Ranging System - Microcontroller Interface
The next step was to write code to interface the ultrasonic ranging system with the microcontroller. Assuming a maximum detectable distance of 2m (or 4m round-trip) and restricted by the usage of 8-bit variables (i.e. 256 discrete levels of analysis) the maximum resolution,
, of the ranging system was:
m or 1.56 cm (7)
The time that it took for the ultrasonic wave to travel 1.56 cm was calculated:
or 45.4ìs (8)
The algorithm of the range-finding routine was quite simple:
-
Pin RA3 generates three driving pulses at 40 kHz and right after that.
-
. a 256 times loop is executed by the microcontroller. Each time the loop is repeated a 45.4is delay occurs thus giving a total delay time of 256x45.4=11622.4is or 11.622ms which is approximately the maximum "time of flight"
-
If during the 256 times loop the receiver pin RA4 goes LOW (i.e. the ultrasonic wave is reflected back), the loop stops executing and the variable which holds the number of loop repetitions is returned. This number multiplied by 1.56 cm gives the distance of the object from the sensor.
The routine which yielded the measured distance was written in Assembly language. That was because in Assembly language the manipulation of timing routines is much more accurate than in C.
4.5.4 Ultrasonic Ranging System Rotation
The ultrasonic ranging system was mounted on a servo motor as shown in Figure 14. The servo motor was continuously rotating +/- 90° thus giving 180° coverage.

4.6 Wheel Encoders

A pair of wheel encoders (Figure 15) was used to provide feedback information (i.e. distance travelled, speed of the robot) to the microcontroller.
4.6.1 Theory of operation
The operation of the wheel encoders was based on two photo-reflectors (i.e. a photo emitting diode and a photo-transistor housed in a compact package). When a photo-reflective object was in front of the wheel encoders (in a fairly small distance) the light emitted from the diode was reflected back to the photo-transistor thus making the output of the encoder LOW. When the object in front of the encoders was not photo-reflective (or no object was in front of the encoders) the light emitted was not reflected back and hence the output was HIGH.
Both tyres of the robot were divided into 50 small segments (25 white photo-reflective and 25 black non photo-reflective). Each wheel encoder was mounted on robot's platform as shown in Figure 16.

Thus, as the wheels were rotating, the outputs obtained from the wheel encoders had the shape shown in Figure 17 (a pulse train).

Referring back to Table 1, the circumference of the tyres was 29.845 cm. By dividing the circumference of the tyres by 50 (the number of black and white segments), the minimum "understandable" distance travelled,
was calculated:
cm (9)
In other words, every time a LOW to HIGH or a HIGH to LOW transition occurred to an encoder, the corresponding wheel had 'travelled' 0.6 cm (or had rotated by 360/50=7.2°)
4.6.2 Encoders 'Reading' Routine
The first thing that had to be done was to write a routine which would 'read' the encoders. The Pseudo Code on which this routine was based is shown below:
-
Variables counter_left, counter_left_total, counter_right and counter_right_total are set to zero
-
START
-
Variables old_left and old_right hold the status of left and right wheel encoder respectively when the robot is first powered up.
-
Variables new_left and new_right are assigned with the current status of left and right wheel encoder respectively
-
IF old_left != new_left AND left wheel rotates forward THEN increase the variable counter_left
-
IF old_left != new_left AND left wheel rotates backward THEN decrease the variable counter_left
-
IF old_right != new_right AND left wheel rotates forward THEN increase the variable counter_right
-
IF old_right != new_right AND right wheel rotates backward THEN decrease the variable counter_right
-
IF counter_left = 256 THEN increase counter_left_total
-
IF counter_left = 0 THEN decrease counter_left_total
-
IF counter_right = 256 THEN increase counter_right_total
-
IF counter_right = 0 THEN decrease counter_right_total
-
old_left = new_left
-
old_right = new_right
-
Go to step START
Download the source code here: Encoders Reading Routine
4.6.3 Move Straight Forward Routine
The driving servo motors, even though they were identical, did not have equal speeds for a specific pulse width (see Figure 3). For example, the maximum speed of the left servo motor was approximately 75 RPM while the maximum speed of the right servo motor was approximately 72RPM. As a result the robot slightly born to the right even when it was supposed to move straight forward. This problem was solved using the following Pseudo Code:
- START
- Read Encoders
- IF counter_left_total > counter_right_total THEN rotate right wheel
- IF counter_left_total < counter_right_total THEN rotate right wheel
- IF counter_left_total = counter_right_total THEN rotate both wheels
- Go to START
4.6.4 Turn 90° Routine
Referring back to Table 1, the distance between the drive wheels (centre-to-centre) was 19.5 cm. Assuming that the robot turns by rotating only one of the two wheels, then in order to make 90° turn one of the two wheels had to rotate by:
[Perimeter of the circle of radius 19.5] / 4 =
cm (10)
30.6 cm 'translated' into wheel encoder state transitions was: 30.6/0.6=51 state transitions. The following routine describes how a 90° right turn was implemented:
- START
- Read Encoders
- IF counter_left_total != 51 THEN turn right and go to step 2
- IF counter_left_total = 51 THEN go to END
- END
5. Conclusion
The robot was able to perform most of the navigation algorithms quite satisfactory. Some of the more sophisticated algorithms though could not be implemented and the reason was the microcontroller's limited performance (only 13 I/O pins, lack of advanced mathematical functions).
6. References
- Leang, Kam: Sonar Sensor for the MiniBots (1994) http://www.leang.com/robotics/info/articles/minison/minison.html
7. Bibliography
- Gordon McComb "Robot Builder's Bonanza",2000
- David Benson "PIC'n Techniques, PIC Microcontroller Applications Guide",1999
- S.R. Ruocco "Robot Sensors and Transducers"






