1. Washington State University
EE3234 Microprocessor Systems
Final Project
Line Following Motor Robot Kit with PIC32
Nathan Wendt
11401887
Date: Dec. 11, 2015
2. 1.0 Objective:
The objective of this project was to use the PIC32 motor robot kit and 4 infrared sensors
to identify and react to a small black path (created with electrical tape). The robot must have
two modes of operation: Tracking mode, and search mode. In tracking mode, the robot is to
continue following the line until the sensors cannot detect the line anymore. Once the sensors
have lost the track, the robot enters search mode. In search mode, the robot is to travel a
discrete distance forward, then reverse, then turns 90* and repeats for each North, East, South,
and West directions. While in search mode, at any time that the sensors detect the line, search
mode must be broken and track mode will be entered. As such our robot should be able to
traverse a simple electrical tape course with 90* corners, T crossroads, gaps, and curves.
1.1 Design Requirements:
1. Assemble the robot to the manufacturer’s specifications.
2. Use PmodHB5 H-bridges to control the DC motors.
3. Use the PmodLS1 and IROS sensors to detect the path
4. The robot must contain 2 modes of operation:
-Search mode: Travels 3 ft north, 3 ft reverse, 90* turn and repeat
for East, South, West
-Track mode: While a sensor has detected a line the robot will
follow the line via a logical mapping of commands to the sensor
inputs.
2.0 Introduction:
This lab uses a high-level programming language and compiler, specifically, C in MPLabX.
We used output compare registers in PWM mode without a fault pin to manage the duty cycle
of the motors. There are a total of 5 OC modules. We will use OC2 and OC3. We use the value
of PR2 to determine the PWM period and the value of OCxRS to determine the duty cycle.
Overall this lab can be broken into two pieces: external hardware peripherals and internal
peripherals. The external hardware used are two DC motors, two PmodHB5’s to control the
output of the DC motors, and the PmodLS1 with the 4 IROS infrared detectors. The internal
peripherals used are Timer1 (used as the basis for the interrupt), Timer2 (used to set OC2 and
OC3 PWM periods), Timer3 is used for the operations of the robot.3.0 Design:
3.1 Hardware
The hardware used is the PIC32, two DC motors controlled by H-bridges
(PmodHB5), and the 4 IROS sensors read by the PmodLS1. The duty cycles of the DC
motors are controlled within the output compare modules while the direction of the
motor is controlled by the H-bridge. A diagram of the H-bridge operation is shown on
the page below:
3. Figure 1: PmodHB5 4 pin operations
The PmodLS1 takes the inputs from the IROS infrared detectors to determine the
path. The IROS emit an infrared signal and assert high if they receive a reflection. As
such, the sensors read 1 over white areas and 0 over the electrical tape. Each of these
inputs is then assigned to a port and then the software of the robot uses conditional
logic to decide on the robot’s operation.
3.2 Software
We used the MPLabX IDE in Assembly to program our pic32. For the most part,
the software of this project was very straight forward. The OC modules were
programmed to operate in PWM mode. OC2 and OC3 each controlled the duty cycle of
one of the motors and were generally set together. The basic motion functions of the
robots were forward, reverse, right, left, and soft right (right and left have the wheels
spinning opposite while soft right had only one wheel spinning). Soft right was needed
to get past T’s and coming across horizontal paths in search mode. Additionally, there
were two functions to control which motion operation was used. These were the search
and track functions. Track used a logical mapping of binary representations of sensor
readings to control whether the robot used forward, right, left, or soft right. In track
mode, the robot simply followed a pattern of forward distance x, reverse distance x, and
turn 90* for each unique direction. The final key piece to the operation of the line-
tracking robot was the timer1 interrupt. Using a timer size of 13500 tics at 10MHz ended
up being the perfect refresh rate for the robot to operate smoothly. The timer1
interrupt contained 4 conditional statements that decided whether the robot was
supposed to start operations, cease operations, operate in track mode, or operate in
search mode.
4. The duty cycle of the motors is controlled by the OC registers in PWM mode. The figure below
shows how the period of the associated timer, and the OCxRS registers are used to control duty
cycles with pulse width modulation:
Figure 2: Duty cycle specified by OCxRS in PWM mode
4.0 Procedure:
The procedure for this lab is straight forward:
- Construct the motor robot kit drone with the IROS sensors
- Devise and write the C source code to control the robot’s operations
- Calibrate the duty cycles and timings
- Stop the robot from traveling down stairs
5.0 Results and Conclusion:
5.1 Results
Most of the issues I had in this lab were not solved by logical proceedings. The first
attempt to create the code for the robot simply would not do anything for the robot. I
eventually decided to create a new project file and copy over the code and it decided to
register. From the point it was basic debugging and determining the proper timings for
everything. The final product of my efforts was a fast acting and accurate line-following robot
that preferred to turn right. The reason I say this is when all sensors were asserted low (such as
when the robot came across a perpendicular track while in search mode or if a T crossroad was
hit in track mode) the robot’s logic told it to soft turn right (on wheel turn).
5.2 Conclusion:
In hindsight, this project would probably take me 20 minutes to complete so I think that
is a testament to lessons learned. My main issues were in understanding the operation of the
timer interrupts. As funny as it may be, I did not realize that the interrupt was triggered when
the PR limit was reached. Once that registered in my brain, the rest of the project was straight
forward. I also found that calling functions from the interrupt caused inefficiencies in the
robots movements and as such I created a run() function that controlled the function calls while
5. the timer1 interrupt simply set the robot’s state (just an enumerated type for track, search, and
stop). My final code came out fairly concise at under 300 lines and there is even more
optimization that could be done to the search operation.