SlideShare a Scribd company logo
POLITECNICO DI TORINO
Department of CONTROL AND COMPUTER
ENGINEERING (DAUIN)
Master of Science in Mechatronics Engineering
Master Degree Thesis
Laser Sensor RS485 Interface Design and
Tire DOT Code Scanner Software
Development
Supervisor
Prof. Paolo PRINETTO
Candidate
Iman AMIRTAHERI
Internship Tutors
Dr. Giulio GAMBARDELLA
Dr. Francesca BALESTRA
July 2016
Abstract
Tire industries have an annual total sale of approximately $32.1 billion. This number
itself shows how big is this market. Costumers usually buy car tires directly from auto
repair centers. So a tool that helps auto repairmen to sell their tires more effectively is
extremely valuable. Tire Profiles Italy company has designed two different solutions for
this purpose. Both of these devices detect the necessity of tire replacement by means
of scanning tire thread depth. Another important item that should be paid attention
to, is the expiry date of tire. This date is usually calculated based on production date.
According to the department of transportation regulations, tire producers are obliged to
emboss or engrave the production week and year in a structured code on the tire sidewall.
This code is also called as DOT code. So Tire Profiles Italy company introduced a new
project to design a device for DOT code scanning. There were several options about how
this device should scan the code technically. Among possible choices, the solution with 3D
laser sensor was selected. In this approach the laser sensor measures distances between all
points of a line and laser sensor itself. By moving sensor in a trajectory perpendicular to
its laser beam axis, it can scan the whole portion of tire. This thesis is mainly focused on
design and build a communication interface for this laser sensor in order to provide the
infrastructure for PC and laser sensor to communicate. At the next step, thanks to this
communication channel, a software for DOT code scanning is developed which performs
acquisitions and stores the corresponding data in a host PC. As a complementary study,
image pre-processing techniques are implemented in MATLAB and Qt plus Open-CV
framework.
ii
Contents
Abstract ii
List of Tables v
List of Figures vi
1 Introduction 1
1.1 General Tire Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Tire Specification Code . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 Department of Transportation (DOT) Safety Code . . . . . . . . . . 2
1.1.3 Uniform Tire Quality Grading (UTQG) Code . . . . . . . . . . . . . 2
1.2 Tire Replacement Symptoms . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Tire Treads Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.2 Tire Expiry Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 DOT Code Scanning 7
2.1 Camera Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Laser Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Laser Sensor 11
3.1 Functional Criterion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3.1 Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3.2 Height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3.3 Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Communication Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.5 Time Flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.6 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4 Communication Interface 21
4.1 Micro-Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.1.1 General Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.1.2 Pin Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.1.3 Universal Synchronous Asynchronous Receiver Transmitter (USART) 23
iii
4.2 RS485 Transceiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.3 USB to Serial Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.4 Hardware Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.5 Software Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.6 PC-Sensor Communication Test . . . . . . . . . . . . . . . . . . . . . . . . . 30
5 Acquisition Software 33
5.1 LaserSensor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.2 MainWindow Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6 Image Pre-Processing 43
6.1 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.2 Qt and Open-CV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7 Conclusion 63
Bibliography 65
Appendices 66
A Micro-controller C Code 67
B Laser Sensor Header Code 72
C Laser Sensor C++ Code 75
D Profile Processer MATLAB Code 83
E Profile Processor Header Code 88
F Profile Processor C++ Code 90
iv
List of Tables
1.1 Tire DOT code information content . . . . . . . . . . . . . . . . . . . . . . 2
2.1 Laser Scanning Technical Analysis . . . . . . . . . . . . . . . . . . . . . . . 9
3.1 Laser Sensor Functional Criterion . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Laser Sensor Profile Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 Laser Sensor Timing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 Acquisition Timing Calculations . . . . . . . . . . . . . . . . . . . . . . . . 18
3.5 Relevant Laser Sensor Commands . . . . . . . . . . . . . . . . . . . . . . . 20
4.1 USART Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 USB to Serial Module Connections . . . . . . . . . . . . . . . . . . . . . . . 26
4.3 Micro-controller ATMega16 Connections . . . . . . . . . . . . . . . . . . . . 26
4.4 Max485 Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.5 Laser Sensor Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.6 UBRRL Values for available baud rates . . . . . . . . . . . . . . . . . . . . 28
v
List of Figures
1.1 Tire Specification Code Sample: This code contains tire technical information. 1
1.2 DOT Code Sample: All safety information is gathered inside this universal
code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 UTQG Code Sample: This code contains tire grades in three different subjects. 3
1.4 Tire Tread Depth: Tire producers recommend to replace tires with less than
10% life time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Groove Glove Scanner: Handheld device to measure tire tread depth and
identify car’s license plate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 TreadSpec (PRT Scanner): Tread depth scanner which has to be installed
on the ground. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 Fixed Illumination: Naive approach to illuminate tire surface and capture
DOT code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Variable Illumination: Camera captures every time that illumination changes. 8
2.3 Laser Sensor: Creates a distance profile line by line. . . . . . . . . . . . . . 9
3.1 Sensor Installation Methods: a) Standard Installation b) Angled Installation 12
3.2 Sensor Distance Measurements: Average, Maximum and Minimum . . . . . 13
3.3 Sensor Height Measurements: Average, Maximum, Minimum and Delta . . 13
3.4 Laser Sensor Coordinates: It creates a matrix of x and z elements for every
acquisition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.5 RS485 Connection Diagram: This protocol can connect 15 sensors to a
master. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.6 RS485 Pin Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.7 Laser Sensor Timing (Time Flows) . . . . . . . . . . . . . . . . . . . . . . . 17
4.1 General Connection Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 ATMega16 Pin Configurations . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.3 MAX485 Pin Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.4 USB to Serial Converter Module . . . . . . . . . . . . . . . . . . . . . . . . 25
4.5 Communication Interface Connection Scheme . . . . . . . . . . . . . . . . . 25
4.6 TeraTerm Terminal Emulator Serial Settings . . . . . . . . . . . . . . . . . 30
4.7 PC-Sensor Communication Test . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.8 RS485 Communication Interface . . . . . . . . . . . . . . . . . . . . . . . . 31
5.1 DOT Code Recognition Software (DCR) v1 . . . . . . . . . . . . . . . . . . 33
5.2 Signal Slots Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1 MATLAB Loading Raw Data File . . . . . . . . . . . . . . . . . . . . . . . 45
vi
6.2 MATLAB 3D Surface Representation . . . . . . . . . . . . . . . . . . . . . . 46
6.3 Leveled Surface Representation . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.4 MATLAB 3D Model Abrupt Changes . . . . . . . . . . . . . . . . . . . . . 48
6.5 MATLAB 3D Model Added Neighbors . . . . . . . . . . . . . . . . . . . . . 49
6.6 MATLAB 3D Model Removed Min Value . . . . . . . . . . . . . . . . . . . 50
6.7 Segmented Black & White Image . . . . . . . . . . . . . . . . . . . . . . . . 52
6.8 Segmented Grayscale Image . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
6.9 Contrast Stretched Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.10 Negative Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.11 Open-CV Loaded File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
6.12 Open-CV Orientation Corrected . . . . . . . . . . . . . . . . . . . . . . . . 59
6.13 Open-CV Colors Inverted . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.14 Open-CV Edges Sharpened . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
vii
Chapter 1
Introduction
This thesis report documents general information about vehicle tire codes, specifically tire
DOT code and its importance. Then different approaches for DOT code scanning are
analyzed and one of them is chosen. In the rest of this report, the selected approach is
documented, a communication interface is designed and built, a C++ software is developed
for data acquisition and at the end, some image pre-processing methods are performed.
1.1 General Tire Codes
In general, there are three unique codes embossed or engraved on the tire sidewalls.
1.1.1 Tire Specification Code
This code contains information about tire type,tire width, aspect ratio between its height
and width,construction type, wheel diameter, load index and speed rating. Figure 1.1
shows an example of this code on tire sidewall. Note that this code in a real tire is colored
black but in this image for better legibility it is highlighted with white color. [1]
Figure 1.1. Tire Specification Code Sample: This code contains tire technical information.
1
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
1.1.2 Department of Transportation (DOT) Safety Code
Federal laws have obliged tire producer companies to determine the safety related infor-
mation of their tires on its sidewall. This code is named department of transportation
safety code and from now on it is referred to as DOT code. Figure 1.2 shows a sample of
this code.[1]
Figure 1.2. DOT Code Sample: All safety information is gathered inside this universal code.
DOT codes may consist of 8 upto 13 characters. Depending on the coding that manufac-
turer uses, the embedded information varies but in any case some important data has to
be documented. Some of these information fields are gathered in table 1.1.
Table 1.1. Tire DOT code information content
Characters
Index
Example
Characters
Definition
1-3 DOT DOT code identifier
4-5 B9 Tire’s Manufacturer and plant code
6-7 YR Tire size
8-11 UJNX Optional characters usually to determine brand and
other characteristics
12-13 50 Week of the year the tire was produced
14-15 08 Year the tire was produced (starts from year 2000)
1.1.3 Uniform Tire Quality Grading (UTQG) Code
This code was proposed by the national highway traffic safety administration to grade
tires based on their threadwear, traction and temperature. Figure 1.3 depicts a sample of
2
1 – Introduction
this code.[1]
Figure 1.3. UTQG Code Sample: This code contains tire grades in three different subjects.
These grading subjects focus on quality figure of merits as described below.
• Threadwear
This grade determines how much this tire will last with comparison to the other
tires of this producer. Baseline grade is 100, and theoretically a tire with grade of
200 should last twice as long as tire with baseline grade.
• Traction
Traction grade is based on the tire ability to stop on a wet road. There is a standard
road which is used to perform this test on tires. Acceptable grades from high to low
are AA,A,B and C. Tires with traction grade of less than C are not qualified to be
used for road travels.
• Temperature
One of the most important items in tires world is the ability of tire to dissipate heat.
There is a standard controlled indoor test which benchmarks tires heat dissipation
and grade them starting from A for the best ones. Tires with the grade of D and
below are considered unacceptable.
1.2 Tire Replacement Symptoms
Car tires are not designed to last forever and the they have to be changed on a regular
basis. There are two important items on tire checklist which recommend the car owner to
replace tires when at least one of them is checked.
1.2.1 Tire Treads Depth
Tire treads are designed to maintain sufficient interaction between tire and road surface.
After a period of time, tires begin to wear out and lose their treads depth. Most of tire
3
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
producers recommend costumers to change tires as soon as the tire treads depth fall less
than 2/32-inch (1.6mm). Figure 1.4 shows corresponding life time for each level of tread’s
depth. [2]
Figure 1.4. Tire Tread Depth: Tire producers recommend to replace tires
with less than 10% life time.
From longtime ago tread depth measurement was performed by means of length measure-
ment devices such as calipers. Even many of auto repairmen use a small coin to measure
depth. Although this kind of approach is not wrong and use the same principle but it is
not accurate enough because it is necessary to measure and check precisely not only for a
single spot of tire but also for all of it’s surface. So the accurate procedure is very time
consuming for an auto repairman to follow.
Tire Profiles Italy Srl. company has designed two different solutions to measure tread’s
depth.
• Groove Glove Scanner
This handheld device is designed to measure tire depth and also identify car license
plate by means of laser triangulation and camera OCR respectively. Tire surface
is scanned by moving the device over it. Scanned data gets transferred to a cloud
server and required processes are performed inside there. Figure 1.5 shows a Groove
Glove Scanner device.[4]
• TreadSpec
TreadSpec also known as PRT Scanner is a device which requires a solid structure to
mount on the ground surface. This solution is usually used on the entry point of auto
service centers and allows the driver to drive over it. In many cases people pay service
centers a visit just to change oil or diagnose an error, with this scanner installed in
the entrance, service centers generate a full report about tire tread depth and its
alignment. Hence, this device can boost their sale effectively. Figure 1.6 shows two
4
1 – Introduction
Figure 1.5. Groove Glove Scanner: Handheld device to measure tire tread depth
and identify car’s license plate.
pictures of this device combined. [3]
Figure 1.6. TreadSpec (PRT Scanner): Tread depth scanner which has to be
installed on the ground.
1.2.2 Tire Expiry Date
Despite from level of tire thread depth, it is crucial to replace the tire on a regular basis.
Most of tire producers guarantee their product for five years from the week that it was
produced.[5] So another interesting area for auto service centers is to have a device like
Groove Glove Scanner which determines the tire age. As mentioned in 1.1 on page 2, the
DOT code printed on the tire sidewall has the valuable information which indicates the
production week and year.
5
6
Chapter 2
DOT Code Scanning
So far, the reasoning behind necessity to design and make a device to scan DOT code
has been documented. From now on, two different approaches are analyzed and then in
following chapters, preferred approach is designed from hardware and software point of
views.
In order to have a complete vision of whole design process, primarily design requirements
are gathered in this way.
• Have reasonable dimensions for a handheld device.
• Have touch screen for user interaction.
• Connect to cloud for storing raw scanned data and fetch processed data after com-
pletely processed.
• Be capable of scanning DOT code completely, independent from the characters
count. Note that DOT code may consists of 8 upto 13 characters including the
DOT identifier initials.
According to this general design requirements many approaches came into mind. The
most important matter that affects the whole design process is that with which technology
this device has to perform scanning process. Two different solutions were proposed.
2.1 Camera Scanning
This idea makes use of a camera to take a photo from the portion of sidewall which DOT
code is located. The device should be designed such a way that it touches the tire surface,
so ambient light could not affect the photo quality. Then captured photos will be sent to
cloud server and get processed there. Figure 2.1 depicts working principle of fixed illumi-
nation method. A prototype of this approach has been already built in the company but
it has a critical problem.
7
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Figure 2.1. Fixed Illumination: Naive approach to illuminate tire surface and
capture DOT code.
This prototype masks the ambient light thanks to its method of usage which is touching
tire surface. Instead, it has many infrared diodes to illuminate the DOT code when it
captures the photo. Since these illumination lamps are turned on and off simultaneously
and the camera axis is perpendicular to tire surface, it is not possible to distinguish
characters since tire codes are embossed or raised on the body of tire and it is a black on
black pattern.
In order to overcome this problem variable illumination technique (structured lighting)
has proposed[14]. Since the DOT code is only detectable due to its altitude difference,
variable illumination technique can eliminate background and just maintain the raised
code. This technique turns on then lights one by one and captures a photo each time,
because of altitude difference in tire surface each time the resulting shadow will be different.
Then a sophisticated algorithm gathers all of these photos and combine them together
based on the shadows. At the end, the obtained final photo shows only DOT code and
eliminates its background. Figure 2.2 shows a simplified version of this approach. Usually
there are at least 8 lights to illuminate tire surface and they are placed on a circular base.
This method is also called as structured lighting.
Figure 2.2. Variable Illumination: Camera captures every time that illumination changes.
8
2 – DOT Code Scanning
2.2 Laser Scanning
Another possible approach is to use laser sensor to measure the distance between a number
of points belonging to a line. By movement of laser sensor, it is possible to gather these
distances line by line and then generate a 3D model of the scanned tire surface. In this way
there is no need to take care about illumination because the laser sensor measures directly
the distance so there is no need to distinguish DOT code height from scanned image.
Figure 2.3 depicts the new configuration in the same framework of previous approaches.
Figure 2.3. Laser Sensor: Creates a distance profile line by line.
In order to make a comparison between new approach and the previous one, table 2.1 is
created with pros and cons of this new approach. By considering this comparison table,
Table 2.1. Laser Scanning Technical Analysis
Pros Cons
Independent from illumination Scans whole DOT code gradually
Works better in dusty environment Need to move over tire with constant speed
More accurate scanning Sensitive to sensor tilt or rotation
Less data for transmitting to cloud Slower scanning
the best approach is to use camera scanning with variable illumination but because the
engineering part of Tire Profiles Italy company has already defined the project with laser
sensor, this thesis focused on laser scanning. Following chapters are dedicated to introduce
the laser sensor and its features, design an interface for communication with it, design an
user interface to connect to the sensor and perform acquisition, design a protocol to save
measurement data, and try to perform some simple image processing techniques to improve
the scanned data.
9
10
Chapter 3
Laser Sensor
So far, the desired design approach is selected. In this chapter, the laser sensor is in-
troduced, its features are documented and an appropriate communication interface is de-
signed. Sensor selection is another decision which was imposed by the Tire Profiles Italy
company. For administrative and economical reasons, this sensor was already selected and
purchased for this project.
3.1 Functional Criterion
The selected sensor is produced by Baumer company and its from MESAX multi -distance-
measuring sensors family. It measures distances and heights of objects and was specially
developed for easy handling. Also it makes use of a red beam to help sensor alignment.
Table 3.1 denotes functioning criteria for the sensor OM70B-15LB-11125351.[6]
Table 3.1. Laser Sensor Functional Criterion
Function Valid Criteria
Start of measuring range 100mm
End of measuring range 150mm
Measuring field width left 36mm
Measuring field width right 36mm
3.2 Installation
According to sensor’s datasheet provided by Baumer company [6], it can be installed in
two different methods.
• Standard Installation
In this configuration sensor is mounted at a right angle with respect to the surface.
11
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
This method of installation is recommended since there is no need to estimate per-
spective effect. Figure 3.1 (a) shows a typical standard installation. This is very
important that the object is located in the functional range (10cm ∼ 15cm).
• Angled Installation
When space condition does not allow to install sensor in standard configuration it
is possible to tilt sensor upto 30° from the perpendicular reference line. In this case
also, object has to lie within the functional criteria. Note that in this configuration
distance function of sensor does not work and only height function is accessible.
Figure 3.1. Sensor Installation Methods: a) Standard Installation b) Angled Installation
For the purpose of DOT code scanning it is the best to use standard installation since
always there is enough space for auto repairman to hold the scanner device perpendicular
to the tire sidewall surface. Final design should consider a method to satisfy function
range of 10 upto 15 cm of device for all scanning process. There are two different solution
to ensure this device always lie in the correct range.
• Warn auto repairman when the device is approaching to its functional range limits.
For example two LEDs or a single buzzer may be considered as the method of
notification.
• Device housing can be designed such a way that when it touches tire sidewall there is
a distance of 12.5cm between laser sensor and DOT code. In this way auto repairman
should ensure that there is a contact between device and tire during the scan process.
Since this decision is not a matter of current thesis, it is left for other engineers to decide.
3.3 Functions
This sensor supports three different functions.
12
3 – Laser Sensor
3.3.1 Distance
To perform distance measurement it is required to send corresponding command and
choose the desired option. Sensor basically measures the distance between itself and all
points lying in the scanned line. Then it finds the minimum, maximum and also the
average of these measured distances. According to the sent command it responds with one
of these three values. Figure 3.2 depicts these three different measurements on a sample
surface.
Figure 3.2. Sensor Distance Measurements: Average, Maximum and Minimum
3.3.2 Height
Similar to the Distance function, Height function can be selected by sending appropriate
command and attaching desired measurement type. Same as the previous case, sensor first
measures all points to sensor distances and then perform selected operation. Specifically for
Height function all measurements are taken with respect to a hypothetically defined plane
called reference. This plane must be taught to sensor through a certain procedure. Height
command can return minimum, maximum or average distance between line points and
reference plane. There is also another option called delta height which returns maximum
difference between points altitude. Figure 3.3 depicts the purpose of each described option.
Figure 3.3. Sensor Height Measurements: Average, Maximum, Minimum and Delta
13
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
3.3.3 Profile
As described in the two previous functions, this laser sensor always measures the distance
between itself and all points in the scanned line. These measurements are stored in a
matrix format. Figure 3.4 depicts these coordinates on a sample object and table 3.2
denotes a blank profile matrix.
Figure 3.4. Laser Sensor Coordinates: It creates a matrix of x and z elements
for every acquisition.
Table 3.2. Laser Sensor Profile Matrix
x(1/100mm) z(1/100mm)
This function is only accessible when device is in diagnose mode. Since for a 4cm field
of view, it is necessary to transmit about 400 (x,z) pairs, this mode is dramatically slower
than previous modes.
Although this mode of function is slower than the others, but this is the only way to
capture all measurements and build the tire altitude profile. So this mode of operation
is selected and since it only works in diagnose mode, almost all other functions are not
accessible at the same time.
3.4 Communication Protocol
This sensor uses RS485 protocol for communication. RS485 protocol is designed to trans-
mit data in differential mode. In 1962, single-ended data transmission was introduced.
A few years later differential data transmission introduced to overcome single-ended data
transmission losses in noisy environments. RS485 can work in half duplex or full duplex
modes. Half duplex is referred to a connection with possibility of sending and receiving but
14
3 – Laser Sensor
not at the same time. Full duplex mode defines a similar connection but with capability
to send and receive simultaneously. This protocol requires two wires for half duplex mode
and four wires for full duplex transmission. According to Baumer sensors datasheet, fig-
ure 3.5 shows required configuration in order to connect upto fifteen sensors to a master.[7]
Figure 3.5. RS485 Connection Diagram: This protocol can connect 15 sensors to a master.
The most important features of this topology are listed in this way.
• Master is the only member of this configuration which can initiate a request. Sensors
are slaves, so they do no transmit anything unless master asks for it.
• This configuration can support a baud rate of 115kbit/s at max.
• Maximum possible length of cable is 10m.
• RS485 cables must be shielded.
• There are two failsafe resistors called RB to define the resistance level when no
transmitter is active.
Since there is only one sensor to work with, first topology is selected for communication.
This sensor came with a default shielded cable. Figure 3.6 denotes corresponding pin
diagram for this cable.
The suggested voltage supply for this sensor is 24v. Also it is recommended to connect
15
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Figure 3.6. RS485 Pin Diagram
all other not connected pins to ground to help noise cancellation.
16
3 – Laser Sensor
3.5 Time Flows
RS485 commands must be sent through a certain structure so the sensor can interpret
and response appropriately. Before listing the possible commands, it is better to dedicate
a few sentences about communication speed. It is preferred to describe this matter in
current section rather than previous one because it is actually possible to modify the
communication speed via commands.
This laser sensor can work with three different baud rates.[7]
1. 38400 bits/s
2. 57600 bits/s
3. 115200 bits/s
The default configuration for baud rate is 57600 bits/sec. It means that every time that
sensor is powered on, it automatically interprets incoming requests with baud rate of
57600. Also it is possible to change this value with corresponding command and save it
to configuration flash.
Since the application goal is to make acquisition with the highest possible rate, the sensor
timing to response the request is very crucial. Figure 3.7 denotes various timing variables
and table 3.3 determines their values.
Figure 3.7. Laser Sensor Timing (Time Flows)
Since the application must use the sensor in diagnose mode, the maximum time re-
quired by sensor to process the request and prepare appropriate response can be derived
as follows.
(tsmax)noerror = tanswerdiagmode = 200ms
(tsmax)error = tbreak = 500ms
To make a rough calculation about order of acquisition rate, lets assume that field of
view attribute is set to 4cm. In other words, laser sensor is scanning only line with width
of 4cm per each acquisition iteration. According to table 3.2 on page 14, laser sensor
returns a pair of x and z elements for each 0.5mm of field of view.
Total number of pairs = 4cm
0.5mm = 80
17
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Table 3.3. Laser Sensor Timing Values
t_answer Time from reception of the last character of request to
sending the first character of the answer.
X 2.5ms
t_answer_
diagmode
Time from reception of last character of request to sending
the first character of the answer if the sensor is in
diagnosing mode.
X 200ms
t_idle Time from reception of last character of answer to sending
the first character of the next request
0.1msX
t_break If a request or an answer is not completed during t_break,
the message is rejected.
X 500ms
Number of characters per each (x,z) pair = 4 ∗ 2 = 8
Total number of characters to send for each acquisition = 80 ∗ 8 = 640
Total number of bits to send = 640 ∗ 8 = 5120
Time required to transfer bits with the highest baud rate 5120
115200 = 44ms
By combining information from this calculation and previous one, table 3.4 denotes timing
values for an acquisition with field of view equal to 4cm.
Table 3.4. Acquisition Timing Calculations
Timing Reference Period Frequency
Fastest acquisition 44.5ms 22.47Hz
Slowest acquisition (without error) 244.5ms 4.09Hz
Timeout Error 500ms X
To have a rough calculation, it is assumed that laser sensor requires 100 ms (half of
maximum value) to perform measurement and prepare appropriate data. Acquisition pe-
riod = 100 + 44.4 + 0.1 = 144.5ms
Final acquisition rate = 1
144.5m = 6.92Hz
With this calculation its quite clear that this acquisition rate is low, for better under-
standing this matter here is an example. Lets assume it is required to make acquisition
every time that red line moves 1mm (half precision with respect to x axis). According to
the calculated acquisition rate, it is possible to derive,
Movement speed of laser sensor = MovementDistance
MovementTime = 1mm
144.5m = 6.92mm/sec
In other words, if sensor is moved with speed of 7mm/sec (less than 1cm/sec), the acquired
data precision in y axis will be half of x axis precision. Now it is more observable that
18
3 – Laser Sensor
this laser sensor is not suitable for the real action, but since this sensor has been already
purchased, the prototype designing was based on this sensor.
3.6 Commands
In previous sections, all important features of laser sensor and its communication rules
were analyzed. In this section, relevant commands are listed and described. Every single
command has to have a predefined structure in order to be decode-able for laser sensor.[7]
Command structure consists of following ordered items:
• Start identifier: A simple colon character ":" is used to acknowledge beginning of a
command.
• Device address: Two digits have the responsibility to determine target device ID.
All sensors have address equal to 01 at power on instant. In order to have more than
one sensor in a network, sensors ID must be configured one by one such a way that
there will be no conflict among them.
• Payload: This part of command can have different number of characters depending
on the command format. Payload itself consists of:
– Type: It can be "W" or "R" to determine the command is to write or read a
content respectively.
– Index: This item has three characters and form a number. Every single com-
mand which is supported by laser sensor has an ID called index. To perform a
desired operation, command must specify the corresponding index. Note that
it is possible for a single index to support both write and read operations.
– Separator: A semicolon character ";" is used before and after of each payload
element to clarify elements from each other.
– Payload element: If the desired command requires a payload element (data) to
perform operation, desired data should be placed in payload element. Note that,
it is possible to have many payload elements separated by semicolon separator.
• CHECKSUM: This code is generated by CRC-16 algorithm in order to avoid cor-
rupted command execution. CHECKSUM code is generated by the master and will
be controlled by slave upon message receipt. This code can be overridden by using
"****" code instead.
• Termination characters: A combination of carriage return "r" and line feed "n"
characters is used to determine the command’s end. The same characters are used
to represent a new line in Microsoft Windows operating systems.
Most of laser sensors in the category of "MESAX multi-distance measuring" have a
built in display and user input. This specific sensor does not have this feature because
there is no intention to interact with the laser sensor directly. With this introduction, all
19
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
of these sensors are locked in the sense that it is not possible to communicate with them
via RS485 protocol unless they receive a specific command to unlock themselves.
Command to unlock RS485 communication port: ":01W010;0;E9C3rn"
Note that this command should be sent by baud rate equal to 57600 bits/sec.
Table 3.5 has summarized relevant laser sensor commands.
Table 3.5. Relevant Laser Sensor Commands
Command
Name
In-
dex
Read-
/Write
Description
Baud Rate 006 RW Selects baud rate. (0:38400 1:57600 2:115200)
Diagnose
Mode
050 RW Activates/deactivates diagnose mode.
(0:Deactivate 1:Activate)
Profile 052 R Returns (x,z) pairs
20
Chapter 4
Communication Interface
So far, laser sensor characteristics and its features have been analyzed. In order to perform
the communication with this device, first it is necessary to define what is the final device
which requires to communicate with laser sensor. Since data acquisition should be made
by a computer, the final device is computer. But according to the design requirements
listed on page 7, this communication should be wireless. So its a wise choice to add a
micro-controller between this connection to regulate the connection and also to create the
infrastructure for further wireless module addition. Note that adding wireless module was
not a part of this thesis and it is left for further improvements. Figure 4.1 depicts the
general connection diagram considered so far. More detailes will be added during this
chapter.
Figure 4.1. General Connection Scheme
4.1 Micro-Controller
There were different choices for micro-controller device. From a simple Arduino board
till more complicated boards like Raspberry pi series, all of them could satisfy device
requirements but with a glance to the mass production requirements, it was more rational
to use a micro-controller integrated circuit instead of working with a board. One of the
most famous and easy to use family of micro-controllers are AVR produced by Atmal
company. To choose between micro-controllers residing in this family, there were a few
21
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
points to consider:
• Output levels of micro-controller must use TTL logic, because RS485 uses TTL logic
for each wire otherwise there is a need for level shifter.
• It must have dedicated ports for UART/USART communication.
• It is recommended to have SPI port for further wireless module or gyroscope addition.
• In order to work with the highest possible baud rate, micro-controller has to be able
to work with 11.0592MHz clock. This clock assures that UART/USART communi-
cation error will be less than 0.0%.
According to these requirements, ATMega16 micro-controller was the selected choice
among AVR micro-controllers. In the rest of this chapter, technical relevant characteristics
of this device are documented.[9]
4.1.1 General Overview
This little and cheap micro-controller has many features, here are the most important
ones.
• Operating Voltage: 2.7V - 5.5V
• Speed Grade: 0 - 8 MHz
• I/O and Packages
– 32 Programmable I/O Lines
– 40-pin DIP, 44-lead TQFP and 44-pad QFN/MLF
• Peripheral features
– Programmable Serial USART
– Master/Slave SPI Serial Interface
– Byte-Oriented Two-wire Serial Interface
– Four Timer/Counters
– Four PWM Channels
• 32*8 General Purpose Working Registers
• Non-volatile Memory Segments
– 16Kbytes of fIn-System Self-programmable Flash program memory
– 512 Bytes EEPROM
– 1 Kbyte Internal SRAM
The micro-controller used for this project was packaged in DIP format because in this way
it is much easier to use it on bread board.
22
4 – Communication Interface
4.1.2 Pin Configurations
Figure 4.2 denotes pin configurations for ATMega16 micro-controller. Some of pins have
dual functions and they can be selected by software. For example pin #14 is the IO #0
of port D in normal condition. It can be configured to function as serial receiver also.
Figure 4.2. ATMega16 Pin Configurations
4.1.3 Universal Synchronous Asynchronous Receiver Transmitter (US-
ART)
In order to manage the RS485 connection between PC and the laser sensor, micro-
controller has to work with its USART block. According to the pin configurations on
figure 4.2, table 4.1 denotes all USART related pins.[9]
Table 4.1. USART Pins
Pin# Normal
Function
USART
Function
Description
14 PD0 RXD USART receiver input.
15 PD1 TXD USART transmitter output.
11 GND GND Should be connected to the PC and laser sensor
grounds to share the same voltage levels.
23
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
4.2 RS485 Transceiver
Using micro-controller as the medium does not mean that USART output can be con-
nected to RS485 wires. As previously mentioned RS485 can use two or four wires for
communication in half duplex or full duplex mode respectively. This laser sensor supports
only the half duplex mode, That’s why its designers have considered only two wires for
communication in RS485 shielded cable. In order to plug USART output to RS485 con-
nection a transceiver is required. One of the most common solutions for this problem is
MAX485 integrated circuit.[10]
Figure 4.3 shows MAX485 transceiver pin configuration. As it is possible to observe in
the figure, this IC is composed of two tri-state buffers. RE and DE has the responsibility
to control these buffers. RS485 uses two wires to transmit one bit in differential format.
For example in order to represent a "0" logic A has "0" value and B has "1" value. The dif-
ference between these two levels which for this case is -5V, determines that the sender has
sent "0". Its exactly vice versa when sender is transmitting "1". So two control signals, RE
and DE select that master is going to send a command or receive a response respectively.
During command sending DE is set to "1" and RE is set to "0". It is opposite when laser
sensor is transmitting response. DI connects to TXD of micro-controller and RO should
be connected to PC. More information will be provided during this chapter.
Figure 4.3. MAX485 Pin Configuration
4.3 USB to Serial Converter
A few years ago, it was very common for personal computers and laptops to have port
for serial connection. Nowadays it is completely rare to have this kind of port for normal
computers and there are only USB ports that can do the same thing. In order to send
and receive in a serial connection, operating system needs to open a COM port. USB to
Serial converters create a virtual COM port so PC can threat with it like a normal one.
At the other side there is a small circuit to convert USB data signals (D+ and D−) to Rx
and Tx signals. Figure 4.4 depicts a USB to Serial converter module. It has a USB port
24
4 – Communication Interface
so a USB cable can connect it to PC. There are also five pins which three of them deliver
GND, 3.3V and 5V and the two remaining form the serial connection wires (Rx and Tx).
This is not a complicated module, most of USB to Serial converters make use of PL2303
chip for data conversions.[11]
Figure 4.4. USB to Serial Converter Module
4.4 Hardware Connections
So far, required elements have been introduced. In this section, connections between these
elements are described. Figure 4.5 depicts all required connections between previously
described elements.
Figure 4.5. Communication Interface Connection Scheme
In this scheme, PC which is the final device to send commands to laser sensor and
receive response from it, is connected to USB to Serial converter via a simple USB cable. So
in the PC, by installing appropriate driver, it is possible to open a COM port and transmit
serial data. Commands sent by PC are delivered to micro-controller. Micro-controller has
the responsibility to control RS485 connection. This control means that, if there is a
command to be delivered to laser sensor, micro-controller prepares the line by setting DE
to "1" and RE to "0". In this way line is ready to accept value from micro-controller. While
this situation exists, the command is sent to laser sensor. Micro-controller will change the
line situation immediately after sending is finished. In this way the command is sent and
line is ready to receive answer from laser sensor. After computation time required by
25
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
laser sensor, response will be sent back and since the line is tuned to deliver data from
laser sensor to PC, the response propagates through MAX485 and directly reaches USB to
serial and then PC. According to this scheme, tables 4.2,table 4.3,4.4 and 4.5 summarized
are required connections for USB to Serial module, micro-controller ATMega16, MAX485
transceiver and laser sensor respectively.
Table 4.2. USB to Serial Module Connections
Pin# Pin Name Connects to
1 5v Not Connected
2 GND Circuit’s GND
3 3.3v Not Connected
4 NC Not Connected
5 Tx ATMega16 Pin #14 RXD
6 Rx Max485 Pin #1 RO
Table 4.3. Micro-controller ATMega16 Connections
Pin# Pin Name Connects to
10 VCC Circuit’s 5V
11 GND Circuit’s GND
12 XTAL1 Crystal 11.0592MHz Pin #1
13 XTAL2 Crystal 11.0592MHz Pin #2
14 RXD USB to Serial Pin #5 Tx
15 TXD MAX485 Pin #4 DI
20 PD6 MAX485 Pin #2 RE and Pin #3 DE
31 GND Circuit’s GND
Other Pins Not Connected
26
4 – Communication Interface
Table 4.4. Max485 Connections
Pin# Pin Name Connects to
1 RO USB to Serial Pin #6 Rx
2 RE ATMega16 Pin #20 PD6
3 DE ATMega16 Pin #20 PD6
4 DI ATMega16 Pin #15 TXD
5 GND Circuit’s GND
6 A Laser Sensor Pin #6 PINK
7 B Laser Sensor Pin #1 WHITE
8 VCC Circuit’s 5V
Table 4.5. Laser Sensor Connections
Pin# Pin Name Connects to
1 WHITE MAX485 Pin #7 B
2 BROWN Circuit’s 24V
6 PINK MAX485 Pin #6 A
7 BLUE Circuit’s GND
Other Pins Circuit’s GND
4.5 Software Design
So far, hardware design part of communication interface has been completed. Now it is the
time to go into details and create a software for micro-controller to act as a medium and
regulate the connection. This kind of micro-controller is produced by Atmel company.
Atmel suggests an IDE called Atmel studio to program and also simulate this micro-
controller. This can be done by programming using Assembly or C language. Since
using assembly language is very time consuming, C language was preferred. There is also
another IDE called Code Vision AVR which is produced by a third party company called
HP InfoTech. Since this IDE can produce a lot of code automatically by means of selecting
required resources in a graphical user interface, it was the selected IDE to work with.
As previously mentioned, all initialization is made automatically according to pro-
grammer inputs. Here are some important portions of code to initialize various variables.
Listing 4.1 denotes variable initialization for PORT A of micro-controller. In this case;
all pins of PORT A are selected for input and they are working in tri-state mode. It is
possible to select inputs with pulled-up mode to avoid floating signals, but since there is
no need to work with PORT A in current configuration the default configuration remains
untouched. Other ports are initialized in the same way.
1 DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) |
(0<<DDA1) | (0<<DDA0) ;
27
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) |
(0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0) ;
Listing 4.1. Port Initialization
Next item to initialize is USART interface. Listing 4.2 contains all required codes to
initialize USART with following details.
• Baud rate: 57600 bits/sec
• Databits: 8
• Startbit: 1
• Stopbit: 1
• Parity: Even
• Sender/Receiver Interrupt: No
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<
U2X) | (0<<MPCM) ;
2 UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<
UCSZ2) | (0<<RXB8) | (0<<TXB8) ;
UCSRC=(1<<URSEL) | (0<<UMSEL) | (1<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<
UCSZ1) | (1<<UCSZ0) | (0<<UCPOL) ;
4 UBRRH=0x00 ;
UBRRL=0x0B ;
Listing 4.2. USART Initialization
Variables like UCSRA are micro-controller control registers and variables like TXC
are pre-defined masks in order to use in such cases for appropriately modifying control
registers. Control register UBRRL, selects the transmission baud rate based on system
clock frequency. Table 4.6 denotes corresponding values and their transmission error
percentage for three available laser sensor commands.
Table 4.6. UBRRL Values for available baud rates
Bitrate (bits/sec) UBRRL (hex) Error Percentage
38400 0x011 0.0%
57600 0x00B 0.0%
115200 0x005 0.0%
Now that the micro-controller is all set, it is necessary to discuss about its duty.
As it has been said earlier, micro-controller should define line’s situation by constantly
monitoring the PC output. If the output has the character ":", micro-controller sets its
pin #6 of port D to "1" and then puts the same character on its serial output which is
connected to laser sensor through MAX485. Also a local protocol is considered that PC
should send a special character with ascii code of 0x0B to micro-controller to acknowledge
28
4 – Communication Interface
command termination. So each command starts with ":" and ends with 0x0B character.
After receiving ":" character, micro-controller outputs the received data till termination
character (0x0B) receipt. At this point, micro-controller toggles line situation by setting
port D pin #6 to "0". In this way, after enough processing time, laser sensor sends the
corresponding response back to the PC without micro-controller interference. Listing 4.3
contains above explained code. Since micro-controller should constantly run this program,
this code portion is placed in an always true loop.
1 while (1)
{
3 ch=getchar () ;
i f ( ch==’ : ’ )
5 {
PORTD.6=1;
7 putchar ( ch ) ;
}
9 e l s e i f ( ch==0x0B)
{
11 f o r ( i =0; i <100; i++);
PORTD.6=0;
13 }
e l s e
15 putchar ( ch ) ;
}
Listing 4.3. Main Program
As previously mentioned, laser sensor can operate with three different baud rates and
the default one is not the fastest one. Although it is possible to change the baud rate and
store it in the configuration flash of laser sensor, for the sake of maintenance and plug and
play integrity, the micro-controller code is designed to change baud rate to the fastest one
upon system power up. With this option, PC program always send required commands
with baud rate equal to 115200 bits/sec and micro-controller has the duty to change laser
sensor and also its own baud rate at first place.
PORTD.6=1;
2 cr=0x0D ;
cn=0x0A ;
4 delay_ms (2000) ;
p r i n t f ( " :01W010 ; 0 ; ∗∗∗∗%c%c " , cr , cn ) ;
6 delay_ms (100) ;
p r i n t f ( " :01W006 ; 2 ; ∗∗∗∗%c%c " , cr , cn ) ;
8 delay_ms (100) ;
UBRRL=0x05 ;
Listing 4.4. Baud Rate Adjust
In this code portion, first, communication line gets ready for command transfer, then
after a delay of 2 seconds, micro-controller unlocks the RS485 lock, changes laser sensor
baud rate and then changes its own baud rate to 115200 bits/sec. Delays are considered
due to laser sensor power up latency and MAX485 propagation time.
29
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
4.6 PC-Sensor Communication Test
At this point, communication interface setup is complete. By providing required voltage
sources (5V and 24V) to the circuit, laser sensor turns on and emits a red beam, then
micro-controller configures laser sensor to use baud rate equal to 115200 bits/sec. Next
step is to connect USB cable to PC and circuit. Windows will automatically install the
appropriate driver. Now everything is ready and it is possible to communicate with laser
sensor. In order to just test the connection a terminal emulator was necessary. There are
many free solutions like TeraTerm and Putty. Figure 4.6 shows TeraTerm’s serial settings
dialogue filled with communication interface configuration data.
Figure 4.6. TeraTerm Terminal Emulator Serial Settings
By hitting OK, COM port opens and now everything is ready to send a command
to laser sensor and wait for response without having concerns about RS485 protocol or
anything else. Figure shows a sample command and its corresponding response communi-
cated between PC and Laser Sensor. In this communication, PC sent a request to unlock
RS485 lock.
Figure 4.7. PC-Sensor Communication Test
Figure 4.8 shows the final version of RS485 communication interface functioning. On
30
4 – Communication Interface
the left side, there is the power supply which has 5V and 24V voltage sources. On the
right side there are microcontroller, MAX485 and USB to serial converter.
Figure 4.8. RS485 Communication Interface
31
32
Chapter 5
Acquisition Software
In previous chapter, a communication interface was designed and built to make PC-Sensor
communications possible. A sample command sent by means of TeraTerm terminal emula-
tor and corresponding responses received. In this chapter, the goal is to design a software
to automate this process. So the software should be able to initiate a connection to the
laser sensor, send requests, receive responses, display them in a graphical way and store
them in a file for further usage.
In order to have possibility to compile the program for different operating systems and
also for accessing to simpler features, Qt IDE has been used for software development.
Qt IDE is developed and supported by Nokia company. Nowadays many programmers
and developers use Qt for software development. Figure 5.1 depicts the first version of
acquisition program designed. During rest of this chapter different program sections will
be analyzed.[12]
Figure 5.1. DOT Code Recognition Software (DCR) v1
Qt IDE supports signals and slots methodology for objects communication. During
33
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
software design process, it was decided to create a class for laser sensor communication,
so this class could be imported to any other applications for development. In this case,
the object created of this type can use signals and slots for communication or it can be
used in an ordinary way. To proceed with thesis report, two different classes are analyzed
independently. One class called "LaserSensor" is dedicated to sensor communications and
another class called "MainWindow" is for user interface.
5.1 LaserSensor Class
This class is dedicated to handle sensor communication request. With this topology, main
application does not need to worry about RS485 command structure, command index and
so on. Class definition is made through "lasersensor.h" and "lasersensor.cpp" files. Listing
5.1 summarize all declared slots for this class. Thanks to this kind of approach, a signal
emitted by user click on a button can be connected to one of these slots, so whenever that
button is clicked, the laser sensor object will execute desired command.
1 private s l o t s :
void FindAvailablePorts () ;
3 void InitConnection ( QString PORTName) ;
void CheckConnection () ;
5 void ReadApplicationError () ;
void ReadVendorInfo () ;
7 void ReadDeviceInfo () ;
void ReadBusAddress () ;
9 void WriteBusAddress ( int BusAddressValue ) ;
void ReadBaudRate () ;
11 void WriteBaudRate ( int S e l e c t ) ;
void ReadRS485Lock () ;
13 void WriteRS485Lock ( bool Switch ) ;
void ReadTouchButtonLock () ;
15 void WriteTouchButtonLock ( bool Switch ) ;
void ReadMeasurementType () ;
17 void WriteMeasurementType ( int S e l e c t ) ;
void ReadMeasurementValue () ;
19 void ReadAllMeasurementValues () ;
void ReadFieldOfView () ;
21 void WriteFieldOfView ( int LimitLeft , int LimitRight ) ;
void WriteFiledOfViewToMax () ;
23 void ReadObjectType () ;
void WriteObjectType ( bool Switch ) ;
25 void ReadPrecision () ;
void WritePrecision ( int S e l e c t ) ;
27 void ReadLaserOffDataHold () ;
void WriteLaserOffDataHold ( bool Switch ) ;
29 void ReadFlexMountEnable () ;
void WriteFlexMountEnable ( bool Switch ) ;
31 void ReadSetFlexMount () ;
void WriteSetFlexMount ( f l o a t Angle , f l o a t Distance ) ;
33 void WriteTeachFlexMountCommand( f l o a t ReferenceThickness ) ;
void ReadDiagnoseMode () ;
35 void WriteDiagnoseMode ( bool Switch ) ;
34
5 – Acquisition Software
void ReadProfile () ;
37 void WriteStoreConfigurationCommand ( int Config ) ;
void WriteResetToFactorySettingsCommand ( int Command) ;
39 void CloseConnection () ;
void SampleNoChanged ( QString SampleNumber) ;
Listing 5.1. LaserSensor Class Private Slots
Despite from these available slots, a LaserSensor object can emit signals. Signals can
be used in order to send the appropriate response back to the original program. Figure
5.2 depicts an example of this approach. Listing 5.2 lists all declared signals for this class.
Figure 5.2. Signal Slots Example
s i g n a l s :
2 void AvailablePorts ( int TotalNumber , QVector<QString> Ports ) ;
void ConnectionStatus ( bool Status ) ;
4 void ApplicationError ( int ApplicationError ) ;
void VendorID ( int Vendor_ID) ;
6 void VendorName( QString Vendor_Name) ;
void DeviceID ( int Device_ID ) ;
8 void VariantID ( int Variant_ID ) ;
void SensorType ( QString Sensor_Type ) ;
10 void SerialNumber ( QString Serial_Number ) ;
void BusAddress ( int BusAddressValue ) ;
12 void BaudRate ( int BuadRateValue ) ;
void RS485Lock ( bool Switch ) ;
14 void TouchButtonLock ( bool Switch ) ;
void MeasurementType ( QString Type) ;
16 void MeasurementValue ( f l o a t MeasurementValue , QString Quality ) ;
void Average ( QString average ) ;
18 void Max ( QString max) ;
void Min ( QString min) ;
20 void Delta ( QString delta ) ;
void StandardDeviation ( QString standarddeviation ) ;
22 void Quality ( QString quality ) ;
void FieldOfView ( int LimitLeft , int LimitRight ) ;
24 void ObjectType ( QString ObjectType ) ;
void Precision ( QString Precision ) ;
26 void LaserOffDataHold ( bool Switch ) ;
void FlexMountEnable ( bool Switch ) ;
28 void SetFlexMount ( f l o a t Angle , f l o a t Distance ) ;
35
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
void DiagnoseMode ( bool Switch ) ;
30 void P r o f i l e ( int Xpos [ 3 0 0 ] , int Zpos [ 3 0 0 ] ) ;
void SensorTimedOut ( bool Switch ) ;
32 void ProfileReady () ;
Listing 5.2. LaserSensor Class Signals
So with these slots and signals, all requests are sent through slots and all responses
are emitted via signals. Now lets go into the details and analyze the most important slots
functions.
• Laser Sensor Constructor:
This function is called whenever a new object of class LaserSensor is created. This
initializes serial communication and sets its baud rate to 115200 bits/sec. This serial
connection uses a Qt library called "QtSerial.h". After serial port initialization,
it initialize some other variables that will be covered later. Listing 5.3 denotes
LaserSensor constructor definition.
LaserSensor : : LaserSensor ()
2 {
qRegisterMetaType<QVector<QString> >(" QVector<QString>" ) ;
4 s e r i a l = new QSerialPort ( t h i s ) ;
s e r i a l −>setBaudRate ( QSerialPort : : Baud115200 ) ;
6 s e r i a l −>setDataBits ( QSerialPort : : Data8 ) ;
s e r i a l −>setParity ( QSerialPort : : EvenParity ) ;
8 s e r i a l −>setStopBits ( QSerialPort : : OneStop ) ;
s e r i a l −>setFlowControl ( QSerialPort : : NoFlowControl ) ;
10 DeviceAddress =1;
Yindex=0;
12 f i l e c r e a t e d=f a l s e ;
SampleNo=" 1 " ;
14 }
Listing 5.3. LaserSensor Constructor
The third line of code has the duty to register a meta type in order to provide
possibility to move an object of this class to a new thread. DeviceAddress variable
is always one since there is only one sensor in RS485 network. Yindex is the index
of scanned lines and it is set to zero at first. After each successful acquisition it will
be automatically incremented. filecreated flag is used to check if the profile text file
is created or not. In read profile mode, laser sensor class should store acquisition
data in a text file. SampleNo variable is defined to identify different tire samples. It
is just there for further result comparisons.
• FindAvailablePorts
This function scans for all COM ports, then removes those ones that are not available.
A COM port is not available when another program is using it and it is flagged as
busy. Then it emits a signal containing total number of available ports and their
names. Listing 5.4 denotes corresponding codes.
void LaserSensor : : FindAvailablePorts ()
36
5 – Acquisition Software
2 {
int TotalNumber=0;
4 QVector<QString> Ports (0) ;
foreach ( const QSerialPortInfo &info , QSerialPortInfo : :
availablePorts () )
6 {
i f ( ! i n f o . isBusy () )
8 {
Ports . append ( i n f o . portName () ) ;
10 TotalNumber++;
}
12 }
emit AvailablePorts ( TotalNumber , Ports ) ;
14 }
Listing 5.4. FindAvailablePorts: Returns all ready to connect COM ports
• SendCommand
All slots use a function to send a command to laser sensor. SendCommand receives
a string as input, then adds other necessary elements to its begin and end according
to the RS485 command structure. Listing 5.5 contains corresponding codes.
void LaserSensor : : SendCommand ( QString Command)
2 {
QChar cr=0x0D ;
4 QChar cn=0x0A ;
QChar stopchar=0x0B ;
6 QString ToSend=" : " ;
ToSend . append ( QString ( "%1" ) . arg ( DeviceAddress , 2 , 10 , QChar( ’ 0 ’ ) ) ) ;
8 ToSend . append (Command) ;
ToSend . append ( " ; " ) ;
10 ToSend . append ( " ∗∗∗∗ " ) ;
ToSend . append ( cr ) ;
12 ToSend . append ( cn ) ;
ToSend . append ( stopchar ) ;
14 s e r i a l −>write (ToSend . toLatin1 () ) ;
}
Listing 5.5. SendCommand: Prepares command and sends it.
In this function start, carriage return, line feed and stop char has been defined. Stop
char is the conventional character 0x0B that informs micro-controller to change the
RS485 line mode. After command preparation, it will be sent byte by byte to serial
port thanks to the toLatin1 method of QString class.
• ReceiveResponse
This function is intended to be called by each slot after SendCommand execution.
In this way, the function will be blocked until a response is received or a time-out
occurs. Listing 5.6 gathers all necessary codes for this purpose. This function returns
true in case of response receipt and false in case of time-out.
37
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
1 bool LaserSensor : : ReceiveResponse ()
{
3 QChar cn=0x0A ;
Response . c l e a r () ;
5 bool CompletelyReceived=f a l s e ;
while ( ! CompletelyReceived )
7 {
i f ( s e r i a l −>waitForReadyRead (500) )
9 {
Response . append ( s e r i a l −>readAll () ) ;
11 qDebug () << Response ;
i f ( Response . contains ( cn ) ) CompletelyReceived=true ;
13 }
e l s e
15 {
return f a l s e ;
17 }
}
19 qDebug () << Response ;
Payloads=Response . s p l i t ( " ; " ) ;
21 Payloads . removeFirst () ;
return true ;
23 }
Listing 5.6. ReceiveResponse: Verifies response receipt.
This code makes use of serial library time-out ability. If no new byte is received
within 500msec, if statement will be false and function returns false, acknowledging
that the request has been timed-out so the main function should request it again.
If a byte is received, it will be added to Response variable and cleared from serial
buffer. The while loop remains true, until the line feed character receipt. After
successful command receipt, function splits it to payloads. Usually sensor responses
have payloads to return measurement values.
• WriteDiagnoseMode
As previously mentioned about working with profile command of laser sensor, this
command is only available when laser sensor is in diagnose mode. So the WriteDi-
agnoseMode has the duty to enter laser sensor to this mode. Listing 5.7 denotes
corresponding functions.
1 void LaserSensor : : WriteDiagnoseMode ( bool Switch )
{
3 SendCommand( "W030; −20;20 " ) ;
ReceiveResponse () ;
5 SendCommand( "R030" ) ;
ReceiveResponse () ;
7 SendCommand( "W050;1 " ) ;
i f ( ReceiveResponse () )
9 {
emit DiagnoseMode ( true ) ;
11 }
38
5 – Acquisition Software
}
Listing 5.7. WriteDiagnoseMode: Activates diagnose mode.
Although this function is designed to just activate the diagnose mode, it also sets the
field of view range. In third line, laser sensor is asked to set its measuring range from
-20mm to 20mm with respect to the center of red beam. Then the same attribute
requested to read for verification. Finally at line 7, program sends diagnose mode
activation command and if the response is received, it emits a signal to MainWindow
which yields to a check in a checkbox.
• ReadProfile
Finally, its time to analyze the main function that collects acquisition data. This
function creates two data files and store the same content into both of them. The
reason of creating two identical files is that, for image pre-processing part, it was
necessary to address a constant file name in MATLAB. In order to prevent from
overwriting the previously made acquisition, another file with date, time and also
sample number is created each time. According to listing 5.8, this function performs
a profile acquisition once and emits a ProfileReady signal when its done. This is the
duty of MainWindow to call this function in a loop to scan multiple lines.
void LaserSensor : : ReadProfile ()
2 {
i f ( ! f i l e c r e a t e d )
4 {
QDateTime currentdateandtime = QDateTime : : currentDateTime () ;
6 ProfileData . setFileName ( " ProfileData_Sample "+SampleNo+"_"+
currentdateandtime . toString ( " yyyy−MM−dd_hh−mm−ss " )+" . txt " ) ;
ProfileData . open ( QIODevice : : WriteOnly ) ;
8 ProfileData1 . setFileName ( " ProfileData . txt " ) ;
ProfileData1 . open ( QIODevice : : WriteOnly ) ;
10 ProfileStream . setDevice(&ProfileData ) ;
ProfileStream << "YtX t Z" <<endl <<endl ;
12 ProfileStream1 . setDevice(&ProfileData1 ) ;
ProfileStream1 << "YtX t Z" <<endl <<endl ;
14 f i l e c r e a t e d=true ;
}
16 SendCommand( "R052" ) ;
i f ( ReceiveResponse () )
18 {
QStringList XZMeasurements ;
20 XZMeasurements=Payloads [ 1 ] . s p l i t ( " " ) ;
int TotalMeasurements=XZMeasurements [ 0 ] . toInt () ;
22 XZMeasurements . removeFirst () ;
Yindex++;
24 f o r ( int i =0; i<TotalMeasurements /2; i++)
{
26 ProfileStream << Yindex <<"  t " << XZMeasurements [2 ∗ i ] <<"  t
" <<XZMeasurements [2 ∗ i +1] <<endl ;
ProfileStream1 << Yindex <<"  t " << XZMeasurements [2 ∗ i ] <<" 
t " <<XZMeasurements [2 ∗ i +1] <<endl ;
39
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
28 }
30 }
emit ProfileReady () ;
32 }
Listing 5.8. ReadProfile: Makes profile acquisition once.
This function primarily checks for file existence, if it is not created, it creates two files
as described above. Then it prints column headers into these two files. After that
a command with read profile index will be sent to laser sensor and upon successful
receipt, it splits (x,z) pairs and prints them along corresponding y index into both
files. Listing 5.9 shows 10 lines of an acquisition data file with 10534 lines.
Y X Z
2
1 0 3889
4 1 50 3897
1 100 3911
6 1 150 3955
1 200 3966
8 1 250 3953
1 300 3971
10 1 350 4006
Listing 5.9. Read Profile Sample
5.2 MainWindow Class
In previous section, the LaserSensor class was introduced. In this section MainWindow
class is going to be analyzed. This class also is defined by means of "MainWindow.h" and
"MainWindow.cpp" files. MainWindow class is already filled by the information coming
from user interface design and other necessary initialization. There are not too many
modifications required for this class since most of the work is done in LaserSensor class.
By the way it is necessary to define a new object of class LaserSensor and a new thread
to move the object into it. Listing 5.10 denotes corresponding codes.
LaserSensor ∗ Sensor = new LaserSensor () ;
2 QThread∗ SensorThread = new QThread ;
Sensor−>moveToThread( SensorThread ) ;
Listing 5.10. Thread Creation for LaserSensor Object
Note that the thread should be started before program execution, but this will happen
after signal and slots connections definition. So next part should define the signal and
slots connection. This process is like wiring building blocks. Listing 5.11 lists all of these
connections.
1 //Find Available Ports
40
5 – Acquisition Software
connect ( ui−>Refresh_Ports ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT(
FindAvailablePorts () ) ) ;
3 connect ( Sensor ,SIGNAL( AvailablePorts ( int , QVector<QString >)) , this ,SLOT(
ShowAvailablePorts ( int , QVector<QString >)) ) ;
5 // I n i t i a l i z e Connection
connect ( this ,SIGNAL( InitConnection ( QString ) ) , Sensor ,SLOT( InitConnection
( QString ) ) ) ;
7 connect ( Sensor ,SIGNAL( ConnectionStatus ( bool ) ) , ui−>Serial_Status ,SLOT(
setChecked ( bool ) ) ) ;
9 //Vendor Information
connect ( ui−>GetVendorInfo ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadVendorInfo ()
) ) ;
11 connect ( Sensor ,SIGNAL( VendorID ( int ) ) , ui−>VendorID_Label ,SLOT(setNum( int
) ) ) ;
connect ( Sensor ,SIGNAL(VendorName( QString ) ) , ui−>VendorName_Label ,SLOT(
setText ( QString ) ) ) ;
13
// Device Information
15 connect ( ui−>GetDeviceInfo ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadDeviceInfo ()
) ) ;
connect ( Sensor ,SIGNAL( DeviceID ( int ) ) , ui−>DeviceID_Label ,SLOT(setNum( int )
) ) ;
17 connect ( Sensor ,SIGNAL( VariantID ( int ) ) , ui−>VariantID_Label ,SLOT(setNum(
int ) ) ) ;
connect ( Sensor ,SIGNAL( SensorType ( QString ) ) , ui−>SensorType_Label ,SLOT(
setText ( QString ) ) ) ;
19 connect ( Sensor ,SIGNAL( SerialNumber ( QString ) ) , ui−>SerialNumber_Label ,SLOT
( setText ( QString ) ) ) ;
21 //Read All Measurement Values
connect ( ui−>ReadAllMeasurementValues ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT(
ReadAllMeasurementValues () ) ) ;
23 connect ( Sensor ,SIGNAL( Average ( QString ) ) , ui−>Average_Label ,SLOT( setText (
QString ) ) ) ;
connect ( Sensor ,SIGNAL(Min( QString ) ) , ui−>Min_Label ,SLOT( setText ( QString ) )
) ;
25 connect ( Sensor ,SIGNAL(Max( QString ) ) , ui−>Max_Label ,SLOT( setText ( QString ) )
) ;
connect ( Sensor ,SIGNAL( Delta ( QString ) ) , ui−>Delta_Label ,SLOT( setText (
QString ) ) ) ;
27 connect ( Sensor ,SIGNAL( StandardDeviation ( QString ) ) , ui−>
StandardDeviation_Label ,SLOT( setText ( QString ) ) ) ;
connect ( Sensor ,SIGNAL( Quality ( QString ) ) , ui−>Quality_Label ,SLOT( setText (
QString ) ) ) ;
29
//Read P e r i o d i c a l l y
31 connect ( ui−>ReadPeriodically ,SIGNAL( c l i c k e d () ) , this ,SLOT(
ReadPeriodically () ) ) ;
33 // Enable Diagnose Mode
connect ( ui−>EnableDiagMode ,SIGNAL( c l i c k e d ( bool ) ) , Sensor ,SLOT(
WriteDiagnoseMode ( bool ) ) ) ;
41
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
35 connect ( Sensor ,SIGNAL( DiagnoseMode ( bool ) ) , ui−>DiagModeStatus ,SLOT(
setChecked ( bool ) ) ) ;
37 //Read P r o f i l e P e r i o d i c a l l y
connect ( ui−>ReadProfile ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadProfile () ) ) ;
39 connect ( Sensor ,SIGNAL( ProfileReady () ) , Sensor ,SLOT( ReadProfile () ) ) ;
connect ( ui−>spinBox ,SIGNAL( valueChanged ( QString ) ) , Sensor ,SLOT(
SampleNoChanged ( QString ) ) ) ;
41
// Close Connection
43 connect ( ui−>STOP,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( CloseConnection () ) ) ;
45 SensorThread−>s t a r t () ;
Listing 5.11. Signal-Slot Connections
These connections, connect user interface elements signals such as clicked() to a slot
in sensor object. Then it connects a relevant signal of sensor to a slot of user interface
element. For example a click on Connect button in user interface, will cause a request
to laser sensor device to remove its RS485 lock and upon successful operation, a check
will appear in user interface communication status checkbox. This concept is depicted on
figure 5.2 on page 35. After providing all connections, thread is started on line 45.
42
Chapter 6
Image Pre-Processing
In previous chapter, a Qt program developed to make use of communication interface
to perform acquisition and store received data in a file. In this chapter, two different
approaches are used to perform image pre-processing techniques. First approach is done
by MATLAB and second approach is designed with Qt C++ and help of OpenCV image
processing library.
6.1 MATLAB
In many similar cases, when the goal is to perform image processing techniques and have
OCR program, the starting point is to use MATLAB software because it offers many easy
to use functions which are already developed. In this section a .m program is analyzed
step by step to determine how does it work.
Step 1: Listing 6.1 represents a portion of code dedicated to open the data file, fill three
x,y and z vectors based on imported data and draw a 3D object to represent scanned
tire portion.
1 %Read measured data from data f i l e
filename = ’E: Education University MS4 th Semester  Thesis  Versions Qt
v1 .6 build−DCR−Desktop_Qt_5_6_0_MSVC2015_64bit−Debug P r o f i l e s 
ProfileData . tpd ’ ;
3 d e l i m i t e r I n = ’  t ’ ;
headerlinesIn = 1;
5 P r o f i l e = importdata ( filename , delimiterIn , headerlinesIn ) ;
7 %Store x , y , z combination on x , y and z vectors
x=P r o f i l e . data ( : , 2 ) ;
9 y=P r o f i l e . data ( : , 1 ) ;
z=P r o f i l e . data ( : , 3 ) ;
11
plot3 (x , y , z , ’.− ’ )
13
t r i = delaunay (x , y) ;
15 plot (x , y , ’ . ’ )
43
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
17 [ r , c ] = s i z e ( t r i ) ;
disp ( r )
19
h = t r i s u r f ( t r i , x , y , z ) ;
21 axis vis3d
axis o f f
23 l = l i g h t ( ’ Position ’ ,[ −50 −15 29 ] ) ;
set ( gca , ’ CameraPosition ’ ,[208 −50 7687]) ;
25 l i g h t i n g phong
shading interp
27 colorbar EastOutside
Listing 6.1. Data file loading
As previously mentioned, in order to run this program again and again for different
newly scanned samples, file name points to "ProfileData.tpd". The ".tpd" extension
has been chosen to the courtesy of Tire Profiles company and stands for Tire Pro-
files Data. According to the previous convention about filling data file, it has one
row of column headers and values are separated by means of tabs. The code for
representing x,y and z vectors in 3D format, uses point by point drawing and then
changing the color based on corresponding z value. Figure 6.1 shows output of these
commands, As it is possible to observe the DOT code on tire is "DOT CN3R PY42".
On the right side of this figure, there is a bar showing corresponding color for
different distances. These distances are actually the distances between each point
and laser sensor. For example on right side of tire representation, color dedicated
to characters are less than background color in the sense that the corresponding
distance value is less than the same value for background. Which means the DOT
characters are closer to the laser sensor rather than the background.
Step 2: For further improvements on achieved results it is a good choice to store x,y
and z vectors inside a matrix called P such a way that x and y values are row and
column indices and z values are stored as matrix elements values. Listing 6.2 lists
all required MATLAB commands to transform x,y,z vectors to P matrix.
1 %Loading Measured data to matrix P
Yindex=0;
3 Xindex=0;
yvalue=−1;
5 f o r i =1: length (x)
i f (y( i )==yvalue )
7 Xindex=Xindex+1;
e l s e
9 Yindex=Yindex+1;
Xindex=1;
11 yvalue=y( i ) ;
end
13 P( Yindex , Xindex )=z ( i ) ;
end
15
44
6 – Image Pre-Processing
Figure 6.1. MATLAB Loading Raw Data File
% Removing zero elements from P r o f i l e matrix
17 f o r x=1: s i z e (P, 1 )
f o r y=1: s i z e (P, 2 )
19 i f (P(x , y)==0)
P(x , y)=P(x , y−1) ;
21 end
end
23 end
figure , s u r f (P) , t i t l e ( ’ 3D Surface Representation ’ )
Listing 6.2. Store Loaded Data into P Matrix
In this portion of code, one loop sweeps all x,y,z elements and fill P matrix accord-
ingly. Note that the value stored in x vector is not needed and only the sequence of
data is important. After filling P matrix based on x,y,z vectors, it is possible to have
zero elements in P because at time of acquisition sensor couldn’t scan for example
400 points of a few lines. In such cases that are usually a result of functional criterion
limits trespassing, it is necessary to fill those zero elements with a neighbor non zero
element. When P matrix is completely built up and does not have zero elements
inside, surf function can make a better 3D surface representation like in figure 6.2.
45
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Figure 6.2. MATLAB 3D Surface Representation
Step 3: As it is observable from 3D image, the acquisition is not perfect, in the sense that
sensor did not maintain a constant vertical distance to tire during scanning process.
To reduce effect of this matter, listing 6.3 lists a few simple commands to level the
3D model from one side.
f o r x=1: s i z e (P, 1 )
2 P(x , : )=P(x , : ) −(P(x , 1 )−P( 1 : 1 ) ) ;
end
4 P=f l i p l r (P) ;
figure , s u r f (P) , t i t l e ( ’ 3D Surface Representation − Leveled ’ )
Listing 6.3. Leveled Model
In this code portion, all points of every single horizontal line are modified such a
way that the starting point of all lines have the same value. Figure 6.3 represents
the result of leveling process.
Step 4: Now that the 3D model is leveled, it is the time to scan for abrupt changes in the
model. The only way to recognize and the main reason behind using laser sensor
instead of camera is to find DOT code based on altitude changes instead of trying
to recognize it from a black on black pattern. Listing 6.4 summarizes all required
commands to find abrupt changes.
1 %% Finding maximum two consecutive points d i f f e r e n c e
46
6 – Image Pre-Processing
Figure 6.3. Leveled Surface Representation
f o r y=1: s i z e (P, 2 )
3 MaxDeltaVector (y) =0;
f o r x=2: s i z e (P, 1 )
5 i f ( abs (P(x , y)−P(x−1,y) )>MaxDeltaVector (y) )
MaxDeltaVector (y)=abs (P(x , y)−P(x−1,y) ) ;
7 end
end
9 end
MaxDelta=max( MaxDeltaVector ) ;
11
% Finding abrupt z change between two consectuive points ( r i g h t to l e f t
)
13 f o r y=1: s i z e (P, 2 )
P1(1 , y)=min(P( : ) ) ;
15 P1(2 , y)=min(P( : ) ) ;
f o r x=3: s i z e (P, 1 )
17 ExpectedValue=2∗P(x−1,y)−P(x−2,y) ;
i f ( abs (P(x , y)−ExpectedValue ) <0.3∗MaxDelta )
19 P1(x , y)=min(P( : ) ) ;
e l s e
21 P1(x , y)=P(x , y) ;
end
23 end
end
25
% Finding abrupt z change between two consectuive points ( l e f t to r i g h t
)
27 f o r y=1: s i z e (P, 2 )
47
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
P1( end , y)=min(P( : ) ) ;
29 P1( end −1,y)=min(P( : ) ) ;
f o r x=s i z e (P, 1 ) −2:−1:1
31 ExpectedValue=2∗P(x+1,y)−P(x+2,y) ;
i f ( abs (P(x , y)−ExpectedValue ) >0.3∗MaxDelta && P1(x , y)==0)
33 P1(x , y)=P(x , y) ;
end
35 end
end
37 figure , s u r f (P1) , t i t l e ( ’ Abrupt a l t i t u d e changes in 3D model ’ )
Listing 6.4. Procedure to Find Abrupt Altitude Changes
This step works by finding the most variation between two consecutive points. This
value gives an order of expected variation. Its expected to have the most variations
in altitude where ever there is an edge of DOT code. At the rest of code, MATLAB
will try to find points that have a absolute consecutive change more than 0.3 times
of maximum abrupt changes. These points will be considered as potential edges.
This control process is executed twice, one from right to left and one from left to
right. All potentially important points are gathered in P1 matrix. Figre 6.4 shows
abrupt changes in 3D model, all other points have been changed to have altitude
equal to zero.
Figure 6.4. MATLAB 3D Model Abrupt Changes
Step 5: As its observable from previous step result, the algorithm has found only the
edges as it was intended to do. In order to have characters also included in the
segmented area, listing 6.5 contains the appropriate code to add a point to selected
48
6 – Image Pre-Processing
points if there is an already selected point in its neighborhood with size of three
points.
1 % Adding neighbor points to c o l l e c t e d data
NeighborSize =3;
3 P2=P1 ;
f o r y=1+NeighborSize : s i z e (P, 2 )−NeighborSize
5 f o r x=1+NeighborSize : s i z e (P, 1 )−NeighborSize
f o r i=−NeighborSize : NeighborSize
7 f o r j=−NeighborSize : NeighborSize
i f (P1(x+i , y+j )==P(x+i , y+j ) )
9 P2(x , y)=P(x , y) ;
end
11 end
end
13 end
end
15 figure , s u r f (P2) , t i t l e ( ’Added neighbours ’ )
Listing 6.5. Adding Neighbors to Selected Points
This code uses four nested loops to check if a point has to be added to selected points
or not. In order to prevent from selecting all points, a new P2 matrix is filled based
on P1 matrix data.
Figure 6.5. MATLAB 3D Model Added Neighbors
Step 6: For image segmentation it is better to have the 3D model with miinmum value
equal to zero. In this simple step minimum value of matrix is decremented from all
49
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
points. Note that the minimum value is referred to the minimum value of all points
without considering points with zero value. Listing 6.6 lists these simple commands.
1 % Remove min value ( except 0)
P2=P2−min(P2 ( : ) ) ;
3 minvalue=i n f ;
f o r y=1: s i z e (P, 2 )
5 f o r x=1: s i z e (P, 1 )
i f (P2(x , y)~=0 && P2(x , y)<minvalue )
7 minvalue=P2(x , y) ;
end
9 end
end
11 f o r y=1: s i z e (P, 2 )
f o r x=1: s i z e (P, 1 )
13 i f (P2(x , y)~=0)
P2(x , y)=P2(x , y)−minvalue ;
15 end
end
17 end
figure , s u r f (P2) , t i t l e ( ’Min Value Removed ’ )
19 P=P2 ;
Listing 6.6. Removing Minimum Value (Except 0)
Figure 6.6 depicts the new 3D model which has zero minimum value. Comparison
may be made by considering the z axis numbers.
Figure 6.6. MATLAB 3D Model Removed Min Value
50
6 – Image Pre-Processing
Step 7: Now that the 3D manipulation is made effectively by exploiting distance mea-
surement feature, it is possible to convert this 3D model to a normal 2D image to
perform some simple image pre-processing techniques. Listing 6.7 denotes required
commands to perform this important step.
1 % Image Segmentation
P=P ’ ;
3 I = mat2gray (P) ;
mask = f a l s e ( s i z e ( I ) ) ;
5
f o r x=1: s i z e ( I , 1 )
7 f o r y=1: s i z e ( I , 2 )
i f (P(x , y) >=0.9∗max(P( : ) ) )
9 mask(x , y)=true ;
end
11 end
end
13
W = graydiffweight ( I , mask , ’ GrayDifferenceCutoff ’ , 25) ;
15 thresh = 0 . 0 1 ;
[BW, D] = imsegfmm (W, mask , thresh ) ;
17 f i g u r e
imshow (BW)
19 t i t l e ( ’ Segmented BW Image ’ )
Listing 6.7. Image Black & White Segmentation
This step primarily uses a "mat2gray" function to just scale each matrix element value
to have a number between 0 and 255. "mask" variable is used to define a mask which
represents potential seeds of image segmentation. This binary matrix is filled with
ones for points with highest values (above 90%) and zeros for all remaining points.
"graydiffweight" uses the grayscale image and provided mask to calculate a weight
for pixels according to the difference between their value and masked point values.
At the end, "imgsegfmm" function performs segmentation based on the weighted
values, provided mask and a threshold which determines the border of black and
white. Figure 6.7 shows the result of this process.
Step 8: It seems that the code has selected the correct region that contains the DOT
code but with current black and white image it is not possible to perform OCR for
sure. So in this step, segmentation mask created by previous step is used to filter
original grayscale image obtained by beginning of previous step.
1 f o r x=1: s i z e ( I , 1 )
f o r y=1: s i z e ( I , 2 )
3 i f (BW(x , y)==f a l s e )
P(x , y) =0;
5 end
end
7 end
I = mat2gray (P) ; figure , imshow ( I ) , t i t l e ( ’ Segmented Grayscale Image ’ )
51
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Figure 6.7. Segmented Black & White Image
Listing 6.8. Image Grayscale Segmentation
This code uses BW image created by previous step to filter original P matrix. Figure
6.8 reveals grayscale segmented image which is quite promising.
Figure 6.8. Segmented Grayscale Image
Step 9: This image can be improved by means of contrast stretching. Listing 6.9 denotes
required commands one after the other.
%Contrast Stretching
2 P=P−min(P(P>0)) ;
P(P<0)=0;
4 P=P. ∗255/max(P( : ) ) ;
I = mat2gray (P) ; figure , imshow ( I ) , t i t l e ( ’ Contrast Stretched Image ’ ) ;
52
6 – Image Pre-Processing
Listing 6.9. Contrast Stretching
This code guarantees to have minimum and maximum grayscale values by removing
minimum value and then stretch the gray value spectrum to 255. Figure 6.9 shows
improved constrast stretched image.
Figure 6.9. Contrast Stretched Image
Step 10: Since most of OCR techniques works best when the input image has black text
on white background, 6.10 denotes the simple method to find the negative image.
1 %Negate Image
P=255−P;
3 I = mat2gray (P) ; figure , imshow ( I ) , t i t l e ( ’ Negative Image ’ ) ;
Listing 6.10. Negative Image
Finally, figure 6.10 shows the result of 10 consecutive steps for image pre-processing.
Figure 6.10. Negative Image
53
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Although this image pre-processing is not perfect but it offers a good approach to make the
3D model clearer for image processing and specifically OCR. Anyway the thesis goal was
mainly to achieve a stable working condition for laser sensor communication and scanner
software acquisition.
6.2 Qt and Open-CV
In previous section, a 10 step pre-image processing algorithm introduced to use with MAT-
LAB program. Since the final program should be developed and deployed with Qt IDE,
although this was not a part of this thesis subject, author tried to implement a few ba-
sic pre-processing algorithms into the scanner Qt C++ software. In this way the field is
widely open for another engineers to expand and make use of it.
Open-CV is an open source computer vision library that supports various operating sys-
tems and programming languages. This flexibility is encouraging programmers to use this
library for their image processing applications. Qt and Open-CV can make a powerful
image processing software since both of them can deploy programs for different operating
systems.[13]
The new Qt software developed along with Open-CV libraries has one more class named
"ProfileProcessor". This class also is defined by files "profileprocessor.h" and "profilepro-
cessor.cpp". Listing 6.11 denotes public and private functions declared by this class.
1 public :
P r o f i l e P r o c e s s o r () ;
3 void Process ( QString ProfileDataLocation , QString
ConfigurationFileLocation ) ;
private :
5 void LoadProfile ( QString FileLocation ) ;
void ShowImage () ;
7 void CorrectOrientation () ;
void InvertColor () ;
9 void SharpenEdges () ;
Listing 6.11. ProfileProcessor Functions
In this case there is no advantage to use slots over class functions because the processed
data by the profile processor object should be handled differently than just connecting
it to a user interface element slot. But for signals part, listing 6.12 shows two signals
declared by this class.
1 s i g n a l s :
void SendImage (QPixmap Image ) ;
3 void SendCurrentOperation ( QString Command) ;
Listing 6.12. ProfileProcessor Signals
SendImage and SendCurrentOperation signals, send an image and a descriptive string
respectively. This design is intended in order to make possible to have a good interface in
MainWindow object. Further information will be available later.
Listing F.1 describes the definition of process function of this class.
54
6 – Image Pre-Processing
1 void P r o f i l e P r o c e s s o r : : Process ( QString ProfileDataLocation , QString
ConfigurationFileLocation )
{
3 QFile Configuration ;
Configuration . setFileName ( ConfigurationFileLocation ) ;
5 Configuration . open ( QIODevice : : ReadOnly ) ;
QTextStream ConfigurationStream ;
7 ConfigurationStream . setDevice(&Configuration ) ;
while ( ! ConfigurationStream . atEnd () )
9 {
QString Action=ConfigurationStream . readLine () ;
11 i f ( Action . contains ( " Load F i l e " ) )
{
13 LoadProfile ( ProfileDataLocation ) ;
}
15 e l s e i f ( Action . contains ( " Correct Orientation " ) )
{
17 CorrectOrientation () ;
}
19 e l s e i f ( Action . contains ( " Invert Color " ) )
{
21 InvertColor () ;
}
23 e l s e i f ( Action . contains ( " Sharpen Edges " ) )
{
25 SharpenEdges () ;
}
27 }
29 Configuration . c l o s e () ;
ProfileMat . r e l e a s e () ;
31 ProfileData . c l o s e () ;
33 }
Listing 6.13. ProfileProcessor Process Function
This portion of code, defines that this function requires two arguments as inputs. One
is the location address of a profile data file and the other is the location address of a
configuration file. Profile data file is the same file that has been used to store acquisition
data while configuration file is a new input file which dictates to processor how to process
files. This new file may seems to make the system unnecessarily complex but this option is
intended to make it simpler. Sometimes when a utility software similar to this one is sold,
it happens that the system is no longer be able to perform its job. In most cases when
a technical support gets dispatched and checks the system, it comes up that the system
needs to reconfigure to adopt to the new situation. A change in situation may be caused
by even different illumination. So in this cases it is much easier to change a configuration
file instead of whole program. By the way, this function reads contents of configuration
file and decides what is the next action to take. After all actions are finished, it closes two
text files.
Now lets analyze four actions that have been already implemented.
• LoadProfile
55
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Listing 6.14 lists the required functions to load a profile data file.
1 void P r o f i l e P r o c e s s o r : : LoadProfile ( QString FileLocation )
{
3 ProfileData . setFileName ( FileLocation ) ;
ProfileData . open ( QIODevice : : ReadOnly ) ;
5 ProfileStream . setDevice(&ProfileData ) ;
int x , y , z , yold =0, xindex=−1,yindex=−1;
7 int xindexmax=0,yindexmax=0,zmax=0,zmin=std : : numeric_limits<int >::
max() ;
QString ReadLine ;
9 QList <QString> SplittedData ;
11 //Scan f i l e in order to find the image dimensions
ProfileStream . readLine () ; // F i r s t Line
13 ProfileStream . readLine () ; //Second Line
while ( ! ProfileStream . atEnd () )
15 {
ReadLine=ProfileStream . readLine () ;
17 SplittedData=ReadLine . s p l i t ( "  t " ) ;
y=SplittedData . at (0) . toInt () ;
19 x=SplittedData . at (1) . toInt () ;
z=SplittedData . at (2) . toInt () ;
21 i f (y!= yold )
{
23 yindex++;
xindex=−1;
25 yold=y ;
}
27 xindex++;
i f ( xindex>xindexmax ) xindexmax=xindex ;
29 i f (y>yindexmax ) yindexmax=y ;
i f ( z>zmax) zmax=z ;
31 i f ( z<zmin ) zmin=z ;
}
33
// Create Mat object based on image s i z e
35 ProfileMat= Mat : : zeros ( yindexmax+1,xindexmax+1,CV_8UC1) ;
37 //Read f i l e again to f i l l the Mat object accordingly
ProfileStream . seek (0) ;
39 ProfileStream . readLine () ; // F i r s t Line
ProfileStream . readLine () ; //Second Line
41 xindex=−1;
yindex=−1;
43 while ( ! ProfileStream . atEnd () )
{
45 ReadLine=ProfileStream . readLine () ;
SplittedData=ReadLine . s p l i t ( "  t " ) ;
47 y=SplittedData . at (0) . toInt () ;
x=SplittedData . at (1) . toInt () ;
49 z=SplittedData . at (2) . toInt () ;
i f (y!= yold )
51 {
yindex++;
56
6 – Image Pre-Processing
53 xindex=−1;
yold=y ;
55 }
xindex++;
57 int ScaledValue =((z−zmin ) ∗ 254/(zmax−zmin ) ) +1;
ProfileMat . at<uchar >(yindex , xindex )= ScaledValue ;
59 }
//Check i f i t has zero values
61 f o r ( yindex =1; yindex<ProfileMat . rows ; yindex++)
f o r ( xindex =0; xindex<ProfileMat . c o l s ; xindex++)
63 i f ( ProfileMat . at<uchar >(yindex , xindex )==0)
{
65 ProfileMat . at<uchar >(yindex , xindex )= ProfileMat . at<
uchar >(yindex −1, xindex ) ;
}
67 f o r ( yindex=ProfileMat . rows −2; yindex >=0;yindex −−)
f o r ( xindex=ProfileMat . cols −1; xindex >=0;xindex −−)
69 i f ( ProfileMat . at<uchar >(yindex , xindex )==0)
{
71 ProfileMat . at<uchar >(yindex , xindex )= ProfileMat . at<
uchar >(yindex +1, xindex ) ;
}
73 emit SendCurrentOperation ( " P r o f i l e Data Loaded " ) ;
ShowImage () ;
75 }
Listing 6.14. ProfileProcessor Load Profile
The LoadProfile function primarily opens the data file and reads it line by line.
Two first lines are not important since they are dedicated to column headers. Then
it reads following lines and use the same method of MATLAB program to find
dimensions of P matrix. Open-CV uses a variable of type mat to store and work
with images. This variable dynamically allocates memory and release it when it is
not needed anymore. Copying mat objects by using a=b syntax does not copy the
data but it only copies the pointer. There is a specific function to copy also the
content of mat objects. in the first loop the function scans the file just to find the
appropriate dimensions of final mat object. After that at line 35, it creates a mat
object which is a matrix of appropriate dimensions and defines that the value of
this matrix elements is a single scalar and can be represented by 8 bits. This is the
method that Open-CV deals with grayscale images. Now that the matrix is created
and filled with zeros, its the time to restart reading data file and fill the mat object
accordingly. Same as MATLAB code it is possible to have zero elements inside the
matrix because sensor does not return same number of points for every acquisition,
so it is necessary to fill the zero elements with a neighbor’s value. Figure 6.11 depicts
the output of this stage.
• According to obtained picture, it is necessary to rotate the image whenever its x
dimension is less than y dimension. Listing 6.15 lists the required command to
check and rotate image mat.
57
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
Figure 6.11. Open-CV Loaded File
1 void P r o f i l e P r o c e s s o r : : CorrectOrientation ()
{
3 //Check i f i t i s necessary to rotate the image
i f ( ProfileMat . cols <ProfileMat . rows )
5 transpose ( ProfileMat , ProfileMat ) ;
emit SendCurrentOperation ( " Orientation Corrected " ) ;
7 ShowImage () ;
}
Listing 6.15. ProfileProcessor Correct Orientation
This function and all others send the title of operation and also the image to "Main-
Window" by means of two signals. Figure 6.12 depicts the image with corrected
orientation.
• Same as MATLAB’s procedure, next step is to invert colors. Listing 6.16 shows the
usage of "bitwise_not" command that is defined in Open-CV library.
void P r o f i l e P r o c e s s o r : : InvertColor ()
2 {
bitwise_not ( ProfileMat , ProfileMat ) ;
4 emit SendCurrentOperation ( " Colors Inverted " ) ;
ShowImage () ;
6 }
Listing 6.16. ProfileProcessor Invert Colors
Figure 6.13 shows the result of color inversion.
58
6 – Image Pre-Processing
Figure 6.12. Open-CV Orientation Corrected
Figure 6.13. Open-CV Colors Inverted
• Sharpen edges action is a bit different from what was done in MATLAB procedure.
Listing 6.17 shows the steps of this action.
void P r o f i l e P r o c e s s o r : : SharpenEdges ()
2 {
4 GaussianBlur ( ProfileMat , ProfileMatAux , Size (0 , 0) , 3) ;
addWeighted ( ProfileMat , 1.5 , ProfileMatAux , −0.5 , 0 , ProfileMatAux )
;
59
Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software
6 ProfileMat=ProfileMatAux ;
emit SendCurrentOperation ( " Edges Sharpened " ) ;
8 ShowImage () ;
}
Listing 6.17. ProfileProcessor Sharpen Edges
This function finds a new image by applying Gaussian blur technique. Then this
blurred new image is deducted from original one with a half weight. The resulting
image is depicted on figure 6.14.
Figure 6.14. Open-CV Edges Sharpened
• As previously mentioned all of these functions use a function to send the image
result. Listing 6.18 denotes the required commands to transform a mat object to a
QPixmap object so that Qt will be able to show it as a background image of a label.
1 void P r o f i l e P r o c e s s o r : : ShowImage ()
{
3 QThread : : msleep (200) ;
s t a t i c QVector<QRgb> sColorTable ;
5
// only create our c o l o r table once
7 i f ( sColorTable . isEmpty () )
{
9 f o r ( int i = 0; i < 256; ++i )
sColorTable . push_back ( qRgb( i , i , i ) ) ;
11 }
13 QImage ImageQImage ( ProfileMat . data , ProfileMat . cols , ProfileMat .
rows , ProfileMat . step , QImage : : Format_Indexed8 ) ;
ImageQImage . setColorTable ( sColorTable ) ;
60
6 – Image Pre-Processing
15
QPixmap ImageQPixmap = QPixmap : : fromImage ( ImageQImage ) ;
17 emit SendImage (ImageQPixmap) ;
}
Listing 6.18. ProfileProcessor ShowImage
ShowImage function creates a color table only once to map a single color value to
three values colors. This action is necessary since QImage objects needs to store
color data in RGB format. When the QImage object is created, it can be converted
to QPixmap object and sent to MainWindow.
These actions were the implemented ones in this thesis. Since the main focus of it
was on design and build of RS485 interface, other image processing actions were left for
future studies. The most important point is that this software created an infrastructure
for further completions which is easy to understand and interact.
61
62
Chapter 7
Conclusion
This thesis analyzed the importance of DOT code and created a device to make possible
the communication between a laser sensor and PC. It also developed different computer
programs to acquire and process data. In order to have the final device to be sold there
are still following works to be done.
• A gyroscope should be added to this device to compensate sudden movements of
device otherwise the scanned data could be scrambled.
• A cloud server should be procured and the corresponding program should be up-
loaded on it.
• A wireless module should be added to system such a way that it interacts with
micro-controller and MAX485 on behalf of USB to Serial converter.
• Further actions should be added to Qt plus Open-CV application. The substrate for
this addition has been already done. So this work will be fast.
• A PC application should be developed to just receive the final answer of cloud which
is a DOT code and print it with its corresponding data on a paper. This application
needs socket programming.
Although this approach can be modified to have better and more accurate data, the
variable illumination (structured lighting) method will be much more accurate since in
this approach there is no need to move the device over tire and it can be done by hovering
the device on DOT code for less than a second.
63
64
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report
Thesis Report

More Related Content

What's hot

Thesis
ThesisThesis
Thesis
truongnthytr
 
Actron CP9690 User Manual
Actron CP9690 User ManualActron CP9690 User Manual
Actron CP9690 User Manual
Tim Miller
 
wronski_ugthesis[1]
wronski_ugthesis[1]wronski_ugthesis[1]
wronski_ugthesis[1]
Michal Wronski
 
Discovering st32
Discovering st32Discovering st32
Discovering st32
pesccia
 
Tinyos programming
Tinyos programmingTinyos programming
Tinyos programming
ssuserf04f61
 
Team Omni L2 Requirements Revised
Team Omni L2 Requirements RevisedTeam Omni L2 Requirements Revised
Team Omni L2 Requirements Revised
Andrew Daws
 
Badripatro dissertation 09307903
Badripatro dissertation 09307903Badripatro dissertation 09307903
Badripatro dissertation 09307903
patrobadri
 
Actron CP9695 User Manual
Actron CP9695 User ManualActron CP9695 User Manual
Actron CP9695 User Manual
Tim Miller
 
Actron CP9680 User Manual
Actron CP9680 User ManualActron CP9680 User Manual
Actron CP9680 User Manual
Tim Miller
 
Maxime Javaux - Automated spike analysis
Maxime Javaux - Automated spike analysisMaxime Javaux - Automated spike analysis
Maxime Javaux - Automated spike analysis
Maxime Javaux
 
Stm32 f103c8t6
Stm32 f103c8t6Stm32 f103c8t6
Stm32 f103c8t6
whatry1995
 
En.cd00161566
En.cd00161566En.cd00161566
En.cd00161566
Mukesh Palsaniya
 
Design of an arm based microcontroller circuit board
Design of an arm based microcontroller circuit boardDesign of an arm based microcontroller circuit board
Design of an arm based microcontroller circuit board
tuanngoc253
 
En.dm00024550 stm8 s003f3p6
En.dm00024550   stm8 s003f3p6En.dm00024550   stm8 s003f3p6
En.dm00024550 stm8 s003f3p6
betodias29
 
report
reportreport
Smart otdr JDSU
Smart otdr JDSUSmart otdr JDSU
Smart otdr JDSU
WELLINGTON MARTINS
 
Promass80 flow meter
Promass80 flow meterPromass80 flow meter
Promass80 flow meter
Sajid Mahmood
 
Manual programação stm 32 f4
Manual programação stm 32 f4Manual programação stm 32 f4
Manual programação stm 32 f4
Ricardo Albani
 
Jdsu mts 2000_manual
Jdsu mts 2000_manualJdsu mts 2000_manual
Jdsu mts 2000_manual
WELLINGTON MARTINS
 
573916%20 obd1150%20mnl en%20rev%20b_0auto
573916%20 obd1150%20mnl en%20rev%20b_0auto573916%20 obd1150%20mnl en%20rev%20b_0auto
573916%20 obd1150%20mnl en%20rev%20b_0auto
ssuser058892
 

What's hot (20)

Thesis
ThesisThesis
Thesis
 
Actron CP9690 User Manual
Actron CP9690 User ManualActron CP9690 User Manual
Actron CP9690 User Manual
 
wronski_ugthesis[1]
wronski_ugthesis[1]wronski_ugthesis[1]
wronski_ugthesis[1]
 
Discovering st32
Discovering st32Discovering st32
Discovering st32
 
Tinyos programming
Tinyos programmingTinyos programming
Tinyos programming
 
Team Omni L2 Requirements Revised
Team Omni L2 Requirements RevisedTeam Omni L2 Requirements Revised
Team Omni L2 Requirements Revised
 
Badripatro dissertation 09307903
Badripatro dissertation 09307903Badripatro dissertation 09307903
Badripatro dissertation 09307903
 
Actron CP9695 User Manual
Actron CP9695 User ManualActron CP9695 User Manual
Actron CP9695 User Manual
 
Actron CP9680 User Manual
Actron CP9680 User ManualActron CP9680 User Manual
Actron CP9680 User Manual
 
Maxime Javaux - Automated spike analysis
Maxime Javaux - Automated spike analysisMaxime Javaux - Automated spike analysis
Maxime Javaux - Automated spike analysis
 
Stm32 f103c8t6
Stm32 f103c8t6Stm32 f103c8t6
Stm32 f103c8t6
 
En.cd00161566
En.cd00161566En.cd00161566
En.cd00161566
 
Design of an arm based microcontroller circuit board
Design of an arm based microcontroller circuit boardDesign of an arm based microcontroller circuit board
Design of an arm based microcontroller circuit board
 
En.dm00024550 stm8 s003f3p6
En.dm00024550   stm8 s003f3p6En.dm00024550   stm8 s003f3p6
En.dm00024550 stm8 s003f3p6
 
report
reportreport
report
 
Smart otdr JDSU
Smart otdr JDSUSmart otdr JDSU
Smart otdr JDSU
 
Promass80 flow meter
Promass80 flow meterPromass80 flow meter
Promass80 flow meter
 
Manual programação stm 32 f4
Manual programação stm 32 f4Manual programação stm 32 f4
Manual programação stm 32 f4
 
Jdsu mts 2000_manual
Jdsu mts 2000_manualJdsu mts 2000_manual
Jdsu mts 2000_manual
 
573916%20 obd1150%20mnl en%20rev%20b_0auto
573916%20 obd1150%20mnl en%20rev%20b_0auto573916%20 obd1150%20mnl en%20rev%20b_0auto
573916%20 obd1150%20mnl en%20rev%20b_0auto
 

Similar to Thesis Report

Vehicle to Vehicle Communication using Bluetooth and GPS.
Vehicle to Vehicle Communication using Bluetooth and GPS.Vehicle to Vehicle Communication using Bluetooth and GPS.
Vehicle to Vehicle Communication using Bluetooth and GPS.
Mayur Wadekar
 
Accident reporting system using mems
Accident reporting system using memsAccident reporting system using mems
Accident reporting system using mems
Rohit Sinha
 
2D ROBOTIC PLOTTER
2D ROBOTIC PLOTTER2D ROBOTIC PLOTTER
2D ROBOTIC PLOTTER
ANANDKRISHNANVSSS
 
T401
T401T401
report
reportreport
BE Project Final Report on IVRS
BE Project Final Report on IVRSBE Project Final Report on IVRS
BE Project Final Report on IVRS
Abhishek Nadkarni
 
Dm00046982
Dm00046982Dm00046982
Dm00046982
Heber Solis
 
Data over dab
Data over dabData over dab
Data over dab
Digris AG
 
Emf2192 ib _ethercat aif module__v3-1__en
Emf2192 ib _ethercat aif module__v3-1__enEmf2192 ib _ethercat aif module__v3-1__en
Emf2192 ib _ethercat aif module__v3-1__en
Charles Santos
 
Modbus protocol reference guide
Modbus protocol reference guideModbus protocol reference guide
Modbus protocol reference guide
Panggih Supraja
 
Bx310x Product Specification
Bx310x Product SpecificationBx310x Product Specification
Bx310x Product Specification
Frederic Petit
 
Project Report Distance measurement system
Project Report Distance measurement systemProject Report Distance measurement system
Project Report Distance measurement system
kurkute1994
 
Manual
ManualManual
TFG_Cristobal_Cuevas_Garcia_2018.pdf
TFG_Cristobal_Cuevas_Garcia_2018.pdfTFG_Cristobal_Cuevas_Garcia_2018.pdf
TFG_Cristobal_Cuevas_Garcia_2018.pdf
Gerard Labernia
 
trex_astf.pdf
trex_astf.pdftrex_astf.pdf
trex_astf.pdf
ssuserf52607
 
Sdr
SdrSdr
Sdr
kujung
 
Distributed Mobile Graphics
Distributed Mobile GraphicsDistributed Mobile Graphics
Distributed Mobile Graphics
Jiri Danihelka
 
Milan_thesis.pdf
Milan_thesis.pdfMilan_thesis.pdf
Milan_thesis.pdf
kanaka vardhini
 
49529487 abis-interface
49529487 abis-interface49529487 abis-interface
49529487 abis-interface
Terra Sacrifice
 
thesis_SaurabhPanda
thesis_SaurabhPandathesis_SaurabhPanda
thesis_SaurabhPanda
Saurabh Panda
 

Similar to Thesis Report (20)

Vehicle to Vehicle Communication using Bluetooth and GPS.
Vehicle to Vehicle Communication using Bluetooth and GPS.Vehicle to Vehicle Communication using Bluetooth and GPS.
Vehicle to Vehicle Communication using Bluetooth and GPS.
 
Accident reporting system using mems
Accident reporting system using memsAccident reporting system using mems
Accident reporting system using mems
 
2D ROBOTIC PLOTTER
2D ROBOTIC PLOTTER2D ROBOTIC PLOTTER
2D ROBOTIC PLOTTER
 
T401
T401T401
T401
 
report
reportreport
report
 
BE Project Final Report on IVRS
BE Project Final Report on IVRSBE Project Final Report on IVRS
BE Project Final Report on IVRS
 
Dm00046982
Dm00046982Dm00046982
Dm00046982
 
Data over dab
Data over dabData over dab
Data over dab
 
Emf2192 ib _ethercat aif module__v3-1__en
Emf2192 ib _ethercat aif module__v3-1__enEmf2192 ib _ethercat aif module__v3-1__en
Emf2192 ib _ethercat aif module__v3-1__en
 
Modbus protocol reference guide
Modbus protocol reference guideModbus protocol reference guide
Modbus protocol reference guide
 
Bx310x Product Specification
Bx310x Product SpecificationBx310x Product Specification
Bx310x Product Specification
 
Project Report Distance measurement system
Project Report Distance measurement systemProject Report Distance measurement system
Project Report Distance measurement system
 
Manual
ManualManual
Manual
 
TFG_Cristobal_Cuevas_Garcia_2018.pdf
TFG_Cristobal_Cuevas_Garcia_2018.pdfTFG_Cristobal_Cuevas_Garcia_2018.pdf
TFG_Cristobal_Cuevas_Garcia_2018.pdf
 
trex_astf.pdf
trex_astf.pdftrex_astf.pdf
trex_astf.pdf
 
Sdr
SdrSdr
Sdr
 
Distributed Mobile Graphics
Distributed Mobile GraphicsDistributed Mobile Graphics
Distributed Mobile Graphics
 
Milan_thesis.pdf
Milan_thesis.pdfMilan_thesis.pdf
Milan_thesis.pdf
 
49529487 abis-interface
49529487 abis-interface49529487 abis-interface
49529487 abis-interface
 
thesis_SaurabhPanda
thesis_SaurabhPandathesis_SaurabhPanda
thesis_SaurabhPanda
 

Thesis Report

  • 1. POLITECNICO DI TORINO Department of CONTROL AND COMPUTER ENGINEERING (DAUIN) Master of Science in Mechatronics Engineering Master Degree Thesis Laser Sensor RS485 Interface Design and Tire DOT Code Scanner Software Development Supervisor Prof. Paolo PRINETTO Candidate Iman AMIRTAHERI Internship Tutors Dr. Giulio GAMBARDELLA Dr. Francesca BALESTRA July 2016
  • 2. Abstract Tire industries have an annual total sale of approximately $32.1 billion. This number itself shows how big is this market. Costumers usually buy car tires directly from auto repair centers. So a tool that helps auto repairmen to sell their tires more effectively is extremely valuable. Tire Profiles Italy company has designed two different solutions for this purpose. Both of these devices detect the necessity of tire replacement by means of scanning tire thread depth. Another important item that should be paid attention to, is the expiry date of tire. This date is usually calculated based on production date. According to the department of transportation regulations, tire producers are obliged to emboss or engrave the production week and year in a structured code on the tire sidewall. This code is also called as DOT code. So Tire Profiles Italy company introduced a new project to design a device for DOT code scanning. There were several options about how this device should scan the code technically. Among possible choices, the solution with 3D laser sensor was selected. In this approach the laser sensor measures distances between all points of a line and laser sensor itself. By moving sensor in a trajectory perpendicular to its laser beam axis, it can scan the whole portion of tire. This thesis is mainly focused on design and build a communication interface for this laser sensor in order to provide the infrastructure for PC and laser sensor to communicate. At the next step, thanks to this communication channel, a software for DOT code scanning is developed which performs acquisitions and stores the corresponding data in a host PC. As a complementary study, image pre-processing techniques are implemented in MATLAB and Qt plus Open-CV framework. ii
  • 3. Contents Abstract ii List of Tables v List of Figures vi 1 Introduction 1 1.1 General Tire Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.1 Tire Specification Code . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1.2 Department of Transportation (DOT) Safety Code . . . . . . . . . . 2 1.1.3 Uniform Tire Quality Grading (UTQG) Code . . . . . . . . . . . . . 2 1.2 Tire Replacement Symptoms . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2.1 Tire Treads Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2.2 Tire Expiry Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 DOT Code Scanning 7 2.1 Camera Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 Laser Scanning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3 Laser Sensor 11 3.1 Functional Criterion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.3.1 Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3.2 Height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3.3 Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.4 Communication Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.5 Time Flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.6 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 4 Communication Interface 21 4.1 Micro-Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4.1.1 General Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 4.1.2 Pin Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.1.3 Universal Synchronous Asynchronous Receiver Transmitter (USART) 23 iii
  • 4. 4.2 RS485 Transceiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 4.3 USB to Serial Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 4.4 Hardware Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 4.5 Software Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.6 PC-Sensor Communication Test . . . . . . . . . . . . . . . . . . . . . . . . . 30 5 Acquisition Software 33 5.1 LaserSensor Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 5.2 MainWindow Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 6 Image Pre-Processing 43 6.1 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6.2 Qt and Open-CV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 7 Conclusion 63 Bibliography 65 Appendices 66 A Micro-controller C Code 67 B Laser Sensor Header Code 72 C Laser Sensor C++ Code 75 D Profile Processer MATLAB Code 83 E Profile Processor Header Code 88 F Profile Processor C++ Code 90 iv
  • 5. List of Tables 1.1 Tire DOT code information content . . . . . . . . . . . . . . . . . . . . . . 2 2.1 Laser Scanning Technical Analysis . . . . . . . . . . . . . . . . . . . . . . . 9 3.1 Laser Sensor Functional Criterion . . . . . . . . . . . . . . . . . . . . . . . . 11 3.2 Laser Sensor Profile Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.3 Laser Sensor Timing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.4 Acquisition Timing Calculations . . . . . . . . . . . . . . . . . . . . . . . . 18 3.5 Relevant Laser Sensor Commands . . . . . . . . . . . . . . . . . . . . . . . 20 4.1 USART Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.2 USB to Serial Module Connections . . . . . . . . . . . . . . . . . . . . . . . 26 4.3 Micro-controller ATMega16 Connections . . . . . . . . . . . . . . . . . . . . 26 4.4 Max485 Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.5 Laser Sensor Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.6 UBRRL Values for available baud rates . . . . . . . . . . . . . . . . . . . . 28 v
  • 6. List of Figures 1.1 Tire Specification Code Sample: This code contains tire technical information. 1 1.2 DOT Code Sample: All safety information is gathered inside this universal code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 UTQG Code Sample: This code contains tire grades in three different subjects. 3 1.4 Tire Tread Depth: Tire producers recommend to replace tires with less than 10% life time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 Groove Glove Scanner: Handheld device to measure tire tread depth and identify car’s license plate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 TreadSpec (PRT Scanner): Tread depth scanner which has to be installed on the ground. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.1 Fixed Illumination: Naive approach to illuminate tire surface and capture DOT code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2 Variable Illumination: Camera captures every time that illumination changes. 8 2.3 Laser Sensor: Creates a distance profile line by line. . . . . . . . . . . . . . 9 3.1 Sensor Installation Methods: a) Standard Installation b) Angled Installation 12 3.2 Sensor Distance Measurements: Average, Maximum and Minimum . . . . . 13 3.3 Sensor Height Measurements: Average, Maximum, Minimum and Delta . . 13 3.4 Laser Sensor Coordinates: It creates a matrix of x and z elements for every acquisition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.5 RS485 Connection Diagram: This protocol can connect 15 sensors to a master. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.6 RS485 Pin Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.7 Laser Sensor Timing (Time Flows) . . . . . . . . . . . . . . . . . . . . . . . 17 4.1 General Connection Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4.2 ATMega16 Pin Configurations . . . . . . . . . . . . . . . . . . . . . . . . . 23 4.3 MAX485 Pin Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 4.4 USB to Serial Converter Module . . . . . . . . . . . . . . . . . . . . . . . . 25 4.5 Communication Interface Connection Scheme . . . . . . . . . . . . . . . . . 25 4.6 TeraTerm Terminal Emulator Serial Settings . . . . . . . . . . . . . . . . . 30 4.7 PC-Sensor Communication Test . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.8 RS485 Communication Interface . . . . . . . . . . . . . . . . . . . . . . . . 31 5.1 DOT Code Recognition Software (DCR) v1 . . . . . . . . . . . . . . . . . . 33 5.2 Signal Slots Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 6.1 MATLAB Loading Raw Data File . . . . . . . . . . . . . . . . . . . . . . . 45 vi
  • 7. 6.2 MATLAB 3D Surface Representation . . . . . . . . . . . . . . . . . . . . . . 46 6.3 Leveled Surface Representation . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.4 MATLAB 3D Model Abrupt Changes . . . . . . . . . . . . . . . . . . . . . 48 6.5 MATLAB 3D Model Added Neighbors . . . . . . . . . . . . . . . . . . . . . 49 6.6 MATLAB 3D Model Removed Min Value . . . . . . . . . . . . . . . . . . . 50 6.7 Segmented Black & White Image . . . . . . . . . . . . . . . . . . . . . . . . 52 6.8 Segmented Grayscale Image . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.9 Contrast Stretched Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.10 Negative Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.11 Open-CV Loaded File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 6.12 Open-CV Orientation Corrected . . . . . . . . . . . . . . . . . . . . . . . . 59 6.13 Open-CV Colors Inverted . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 6.14 Open-CV Edges Sharpened . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 vii
  • 8.
  • 9. Chapter 1 Introduction This thesis report documents general information about vehicle tire codes, specifically tire DOT code and its importance. Then different approaches for DOT code scanning are analyzed and one of them is chosen. In the rest of this report, the selected approach is documented, a communication interface is designed and built, a C++ software is developed for data acquisition and at the end, some image pre-processing methods are performed. 1.1 General Tire Codes In general, there are three unique codes embossed or engraved on the tire sidewalls. 1.1.1 Tire Specification Code This code contains information about tire type,tire width, aspect ratio between its height and width,construction type, wheel diameter, load index and speed rating. Figure 1.1 shows an example of this code on tire sidewall. Note that this code in a real tire is colored black but in this image for better legibility it is highlighted with white color. [1] Figure 1.1. Tire Specification Code Sample: This code contains tire technical information. 1
  • 10. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 1.1.2 Department of Transportation (DOT) Safety Code Federal laws have obliged tire producer companies to determine the safety related infor- mation of their tires on its sidewall. This code is named department of transportation safety code and from now on it is referred to as DOT code. Figure 1.2 shows a sample of this code.[1] Figure 1.2. DOT Code Sample: All safety information is gathered inside this universal code. DOT codes may consist of 8 upto 13 characters. Depending on the coding that manufac- turer uses, the embedded information varies but in any case some important data has to be documented. Some of these information fields are gathered in table 1.1. Table 1.1. Tire DOT code information content Characters Index Example Characters Definition 1-3 DOT DOT code identifier 4-5 B9 Tire’s Manufacturer and plant code 6-7 YR Tire size 8-11 UJNX Optional characters usually to determine brand and other characteristics 12-13 50 Week of the year the tire was produced 14-15 08 Year the tire was produced (starts from year 2000) 1.1.3 Uniform Tire Quality Grading (UTQG) Code This code was proposed by the national highway traffic safety administration to grade tires based on their threadwear, traction and temperature. Figure 1.3 depicts a sample of 2
  • 11. 1 – Introduction this code.[1] Figure 1.3. UTQG Code Sample: This code contains tire grades in three different subjects. These grading subjects focus on quality figure of merits as described below. • Threadwear This grade determines how much this tire will last with comparison to the other tires of this producer. Baseline grade is 100, and theoretically a tire with grade of 200 should last twice as long as tire with baseline grade. • Traction Traction grade is based on the tire ability to stop on a wet road. There is a standard road which is used to perform this test on tires. Acceptable grades from high to low are AA,A,B and C. Tires with traction grade of less than C are not qualified to be used for road travels. • Temperature One of the most important items in tires world is the ability of tire to dissipate heat. There is a standard controlled indoor test which benchmarks tires heat dissipation and grade them starting from A for the best ones. Tires with the grade of D and below are considered unacceptable. 1.2 Tire Replacement Symptoms Car tires are not designed to last forever and the they have to be changed on a regular basis. There are two important items on tire checklist which recommend the car owner to replace tires when at least one of them is checked. 1.2.1 Tire Treads Depth Tire treads are designed to maintain sufficient interaction between tire and road surface. After a period of time, tires begin to wear out and lose their treads depth. Most of tire 3
  • 12. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software producers recommend costumers to change tires as soon as the tire treads depth fall less than 2/32-inch (1.6mm). Figure 1.4 shows corresponding life time for each level of tread’s depth. [2] Figure 1.4. Tire Tread Depth: Tire producers recommend to replace tires with less than 10% life time. From longtime ago tread depth measurement was performed by means of length measure- ment devices such as calipers. Even many of auto repairmen use a small coin to measure depth. Although this kind of approach is not wrong and use the same principle but it is not accurate enough because it is necessary to measure and check precisely not only for a single spot of tire but also for all of it’s surface. So the accurate procedure is very time consuming for an auto repairman to follow. Tire Profiles Italy Srl. company has designed two different solutions to measure tread’s depth. • Groove Glove Scanner This handheld device is designed to measure tire depth and also identify car license plate by means of laser triangulation and camera OCR respectively. Tire surface is scanned by moving the device over it. Scanned data gets transferred to a cloud server and required processes are performed inside there. Figure 1.5 shows a Groove Glove Scanner device.[4] • TreadSpec TreadSpec also known as PRT Scanner is a device which requires a solid structure to mount on the ground surface. This solution is usually used on the entry point of auto service centers and allows the driver to drive over it. In many cases people pay service centers a visit just to change oil or diagnose an error, with this scanner installed in the entrance, service centers generate a full report about tire tread depth and its alignment. Hence, this device can boost their sale effectively. Figure 1.6 shows two 4
  • 13. 1 – Introduction Figure 1.5. Groove Glove Scanner: Handheld device to measure tire tread depth and identify car’s license plate. pictures of this device combined. [3] Figure 1.6. TreadSpec (PRT Scanner): Tread depth scanner which has to be installed on the ground. 1.2.2 Tire Expiry Date Despite from level of tire thread depth, it is crucial to replace the tire on a regular basis. Most of tire producers guarantee their product for five years from the week that it was produced.[5] So another interesting area for auto service centers is to have a device like Groove Glove Scanner which determines the tire age. As mentioned in 1.1 on page 2, the DOT code printed on the tire sidewall has the valuable information which indicates the production week and year. 5
  • 14. 6
  • 15. Chapter 2 DOT Code Scanning So far, the reasoning behind necessity to design and make a device to scan DOT code has been documented. From now on, two different approaches are analyzed and then in following chapters, preferred approach is designed from hardware and software point of views. In order to have a complete vision of whole design process, primarily design requirements are gathered in this way. • Have reasonable dimensions for a handheld device. • Have touch screen for user interaction. • Connect to cloud for storing raw scanned data and fetch processed data after com- pletely processed. • Be capable of scanning DOT code completely, independent from the characters count. Note that DOT code may consists of 8 upto 13 characters including the DOT identifier initials. According to this general design requirements many approaches came into mind. The most important matter that affects the whole design process is that with which technology this device has to perform scanning process. Two different solutions were proposed. 2.1 Camera Scanning This idea makes use of a camera to take a photo from the portion of sidewall which DOT code is located. The device should be designed such a way that it touches the tire surface, so ambient light could not affect the photo quality. Then captured photos will be sent to cloud server and get processed there. Figure 2.1 depicts working principle of fixed illumi- nation method. A prototype of this approach has been already built in the company but it has a critical problem. 7
  • 16. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Figure 2.1. Fixed Illumination: Naive approach to illuminate tire surface and capture DOT code. This prototype masks the ambient light thanks to its method of usage which is touching tire surface. Instead, it has many infrared diodes to illuminate the DOT code when it captures the photo. Since these illumination lamps are turned on and off simultaneously and the camera axis is perpendicular to tire surface, it is not possible to distinguish characters since tire codes are embossed or raised on the body of tire and it is a black on black pattern. In order to overcome this problem variable illumination technique (structured lighting) has proposed[14]. Since the DOT code is only detectable due to its altitude difference, variable illumination technique can eliminate background and just maintain the raised code. This technique turns on then lights one by one and captures a photo each time, because of altitude difference in tire surface each time the resulting shadow will be different. Then a sophisticated algorithm gathers all of these photos and combine them together based on the shadows. At the end, the obtained final photo shows only DOT code and eliminates its background. Figure 2.2 shows a simplified version of this approach. Usually there are at least 8 lights to illuminate tire surface and they are placed on a circular base. This method is also called as structured lighting. Figure 2.2. Variable Illumination: Camera captures every time that illumination changes. 8
  • 17. 2 – DOT Code Scanning 2.2 Laser Scanning Another possible approach is to use laser sensor to measure the distance between a number of points belonging to a line. By movement of laser sensor, it is possible to gather these distances line by line and then generate a 3D model of the scanned tire surface. In this way there is no need to take care about illumination because the laser sensor measures directly the distance so there is no need to distinguish DOT code height from scanned image. Figure 2.3 depicts the new configuration in the same framework of previous approaches. Figure 2.3. Laser Sensor: Creates a distance profile line by line. In order to make a comparison between new approach and the previous one, table 2.1 is created with pros and cons of this new approach. By considering this comparison table, Table 2.1. Laser Scanning Technical Analysis Pros Cons Independent from illumination Scans whole DOT code gradually Works better in dusty environment Need to move over tire with constant speed More accurate scanning Sensitive to sensor tilt or rotation Less data for transmitting to cloud Slower scanning the best approach is to use camera scanning with variable illumination but because the engineering part of Tire Profiles Italy company has already defined the project with laser sensor, this thesis focused on laser scanning. Following chapters are dedicated to introduce the laser sensor and its features, design an interface for communication with it, design an user interface to connect to the sensor and perform acquisition, design a protocol to save measurement data, and try to perform some simple image processing techniques to improve the scanned data. 9
  • 18. 10
  • 19. Chapter 3 Laser Sensor So far, the desired design approach is selected. In this chapter, the laser sensor is in- troduced, its features are documented and an appropriate communication interface is de- signed. Sensor selection is another decision which was imposed by the Tire Profiles Italy company. For administrative and economical reasons, this sensor was already selected and purchased for this project. 3.1 Functional Criterion The selected sensor is produced by Baumer company and its from MESAX multi -distance- measuring sensors family. It measures distances and heights of objects and was specially developed for easy handling. Also it makes use of a red beam to help sensor alignment. Table 3.1 denotes functioning criteria for the sensor OM70B-15LB-11125351.[6] Table 3.1. Laser Sensor Functional Criterion Function Valid Criteria Start of measuring range 100mm End of measuring range 150mm Measuring field width left 36mm Measuring field width right 36mm 3.2 Installation According to sensor’s datasheet provided by Baumer company [6], it can be installed in two different methods. • Standard Installation In this configuration sensor is mounted at a right angle with respect to the surface. 11
  • 20. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software This method of installation is recommended since there is no need to estimate per- spective effect. Figure 3.1 (a) shows a typical standard installation. This is very important that the object is located in the functional range (10cm ∼ 15cm). • Angled Installation When space condition does not allow to install sensor in standard configuration it is possible to tilt sensor upto 30° from the perpendicular reference line. In this case also, object has to lie within the functional criteria. Note that in this configuration distance function of sensor does not work and only height function is accessible. Figure 3.1. Sensor Installation Methods: a) Standard Installation b) Angled Installation For the purpose of DOT code scanning it is the best to use standard installation since always there is enough space for auto repairman to hold the scanner device perpendicular to the tire sidewall surface. Final design should consider a method to satisfy function range of 10 upto 15 cm of device for all scanning process. There are two different solution to ensure this device always lie in the correct range. • Warn auto repairman when the device is approaching to its functional range limits. For example two LEDs or a single buzzer may be considered as the method of notification. • Device housing can be designed such a way that when it touches tire sidewall there is a distance of 12.5cm between laser sensor and DOT code. In this way auto repairman should ensure that there is a contact between device and tire during the scan process. Since this decision is not a matter of current thesis, it is left for other engineers to decide. 3.3 Functions This sensor supports three different functions. 12
  • 21. 3 – Laser Sensor 3.3.1 Distance To perform distance measurement it is required to send corresponding command and choose the desired option. Sensor basically measures the distance between itself and all points lying in the scanned line. Then it finds the minimum, maximum and also the average of these measured distances. According to the sent command it responds with one of these three values. Figure 3.2 depicts these three different measurements on a sample surface. Figure 3.2. Sensor Distance Measurements: Average, Maximum and Minimum 3.3.2 Height Similar to the Distance function, Height function can be selected by sending appropriate command and attaching desired measurement type. Same as the previous case, sensor first measures all points to sensor distances and then perform selected operation. Specifically for Height function all measurements are taken with respect to a hypothetically defined plane called reference. This plane must be taught to sensor through a certain procedure. Height command can return minimum, maximum or average distance between line points and reference plane. There is also another option called delta height which returns maximum difference between points altitude. Figure 3.3 depicts the purpose of each described option. Figure 3.3. Sensor Height Measurements: Average, Maximum, Minimum and Delta 13
  • 22. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 3.3.3 Profile As described in the two previous functions, this laser sensor always measures the distance between itself and all points in the scanned line. These measurements are stored in a matrix format. Figure 3.4 depicts these coordinates on a sample object and table 3.2 denotes a blank profile matrix. Figure 3.4. Laser Sensor Coordinates: It creates a matrix of x and z elements for every acquisition. Table 3.2. Laser Sensor Profile Matrix x(1/100mm) z(1/100mm) This function is only accessible when device is in diagnose mode. Since for a 4cm field of view, it is necessary to transmit about 400 (x,z) pairs, this mode is dramatically slower than previous modes. Although this mode of function is slower than the others, but this is the only way to capture all measurements and build the tire altitude profile. So this mode of operation is selected and since it only works in diagnose mode, almost all other functions are not accessible at the same time. 3.4 Communication Protocol This sensor uses RS485 protocol for communication. RS485 protocol is designed to trans- mit data in differential mode. In 1962, single-ended data transmission was introduced. A few years later differential data transmission introduced to overcome single-ended data transmission losses in noisy environments. RS485 can work in half duplex or full duplex modes. Half duplex is referred to a connection with possibility of sending and receiving but 14
  • 23. 3 – Laser Sensor not at the same time. Full duplex mode defines a similar connection but with capability to send and receive simultaneously. This protocol requires two wires for half duplex mode and four wires for full duplex transmission. According to Baumer sensors datasheet, fig- ure 3.5 shows required configuration in order to connect upto fifteen sensors to a master.[7] Figure 3.5. RS485 Connection Diagram: This protocol can connect 15 sensors to a master. The most important features of this topology are listed in this way. • Master is the only member of this configuration which can initiate a request. Sensors are slaves, so they do no transmit anything unless master asks for it. • This configuration can support a baud rate of 115kbit/s at max. • Maximum possible length of cable is 10m. • RS485 cables must be shielded. • There are two failsafe resistors called RB to define the resistance level when no transmitter is active. Since there is only one sensor to work with, first topology is selected for communication. This sensor came with a default shielded cable. Figure 3.6 denotes corresponding pin diagram for this cable. The suggested voltage supply for this sensor is 24v. Also it is recommended to connect 15
  • 24. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Figure 3.6. RS485 Pin Diagram all other not connected pins to ground to help noise cancellation. 16
  • 25. 3 – Laser Sensor 3.5 Time Flows RS485 commands must be sent through a certain structure so the sensor can interpret and response appropriately. Before listing the possible commands, it is better to dedicate a few sentences about communication speed. It is preferred to describe this matter in current section rather than previous one because it is actually possible to modify the communication speed via commands. This laser sensor can work with three different baud rates.[7] 1. 38400 bits/s 2. 57600 bits/s 3. 115200 bits/s The default configuration for baud rate is 57600 bits/sec. It means that every time that sensor is powered on, it automatically interprets incoming requests with baud rate of 57600. Also it is possible to change this value with corresponding command and save it to configuration flash. Since the application goal is to make acquisition with the highest possible rate, the sensor timing to response the request is very crucial. Figure 3.7 denotes various timing variables and table 3.3 determines their values. Figure 3.7. Laser Sensor Timing (Time Flows) Since the application must use the sensor in diagnose mode, the maximum time re- quired by sensor to process the request and prepare appropriate response can be derived as follows. (tsmax)noerror = tanswerdiagmode = 200ms (tsmax)error = tbreak = 500ms To make a rough calculation about order of acquisition rate, lets assume that field of view attribute is set to 4cm. In other words, laser sensor is scanning only line with width of 4cm per each acquisition iteration. According to table 3.2 on page 14, laser sensor returns a pair of x and z elements for each 0.5mm of field of view. Total number of pairs = 4cm 0.5mm = 80 17
  • 26. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Table 3.3. Laser Sensor Timing Values t_answer Time from reception of the last character of request to sending the first character of the answer. X 2.5ms t_answer_ diagmode Time from reception of last character of request to sending the first character of the answer if the sensor is in diagnosing mode. X 200ms t_idle Time from reception of last character of answer to sending the first character of the next request 0.1msX t_break If a request or an answer is not completed during t_break, the message is rejected. X 500ms Number of characters per each (x,z) pair = 4 ∗ 2 = 8 Total number of characters to send for each acquisition = 80 ∗ 8 = 640 Total number of bits to send = 640 ∗ 8 = 5120 Time required to transfer bits with the highest baud rate 5120 115200 = 44ms By combining information from this calculation and previous one, table 3.4 denotes timing values for an acquisition with field of view equal to 4cm. Table 3.4. Acquisition Timing Calculations Timing Reference Period Frequency Fastest acquisition 44.5ms 22.47Hz Slowest acquisition (without error) 244.5ms 4.09Hz Timeout Error 500ms X To have a rough calculation, it is assumed that laser sensor requires 100 ms (half of maximum value) to perform measurement and prepare appropriate data. Acquisition pe- riod = 100 + 44.4 + 0.1 = 144.5ms Final acquisition rate = 1 144.5m = 6.92Hz With this calculation its quite clear that this acquisition rate is low, for better under- standing this matter here is an example. Lets assume it is required to make acquisition every time that red line moves 1mm (half precision with respect to x axis). According to the calculated acquisition rate, it is possible to derive, Movement speed of laser sensor = MovementDistance MovementTime = 1mm 144.5m = 6.92mm/sec In other words, if sensor is moved with speed of 7mm/sec (less than 1cm/sec), the acquired data precision in y axis will be half of x axis precision. Now it is more observable that 18
  • 27. 3 – Laser Sensor this laser sensor is not suitable for the real action, but since this sensor has been already purchased, the prototype designing was based on this sensor. 3.6 Commands In previous sections, all important features of laser sensor and its communication rules were analyzed. In this section, relevant commands are listed and described. Every single command has to have a predefined structure in order to be decode-able for laser sensor.[7] Command structure consists of following ordered items: • Start identifier: A simple colon character ":" is used to acknowledge beginning of a command. • Device address: Two digits have the responsibility to determine target device ID. All sensors have address equal to 01 at power on instant. In order to have more than one sensor in a network, sensors ID must be configured one by one such a way that there will be no conflict among them. • Payload: This part of command can have different number of characters depending on the command format. Payload itself consists of: – Type: It can be "W" or "R" to determine the command is to write or read a content respectively. – Index: This item has three characters and form a number. Every single com- mand which is supported by laser sensor has an ID called index. To perform a desired operation, command must specify the corresponding index. Note that it is possible for a single index to support both write and read operations. – Separator: A semicolon character ";" is used before and after of each payload element to clarify elements from each other. – Payload element: If the desired command requires a payload element (data) to perform operation, desired data should be placed in payload element. Note that, it is possible to have many payload elements separated by semicolon separator. • CHECKSUM: This code is generated by CRC-16 algorithm in order to avoid cor- rupted command execution. CHECKSUM code is generated by the master and will be controlled by slave upon message receipt. This code can be overridden by using "****" code instead. • Termination characters: A combination of carriage return "r" and line feed "n" characters is used to determine the command’s end. The same characters are used to represent a new line in Microsoft Windows operating systems. Most of laser sensors in the category of "MESAX multi-distance measuring" have a built in display and user input. This specific sensor does not have this feature because there is no intention to interact with the laser sensor directly. With this introduction, all 19
  • 28. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software of these sensors are locked in the sense that it is not possible to communicate with them via RS485 protocol unless they receive a specific command to unlock themselves. Command to unlock RS485 communication port: ":01W010;0;E9C3rn" Note that this command should be sent by baud rate equal to 57600 bits/sec. Table 3.5 has summarized relevant laser sensor commands. Table 3.5. Relevant Laser Sensor Commands Command Name In- dex Read- /Write Description Baud Rate 006 RW Selects baud rate. (0:38400 1:57600 2:115200) Diagnose Mode 050 RW Activates/deactivates diagnose mode. (0:Deactivate 1:Activate) Profile 052 R Returns (x,z) pairs 20
  • 29. Chapter 4 Communication Interface So far, laser sensor characteristics and its features have been analyzed. In order to perform the communication with this device, first it is necessary to define what is the final device which requires to communicate with laser sensor. Since data acquisition should be made by a computer, the final device is computer. But according to the design requirements listed on page 7, this communication should be wireless. So its a wise choice to add a micro-controller between this connection to regulate the connection and also to create the infrastructure for further wireless module addition. Note that adding wireless module was not a part of this thesis and it is left for further improvements. Figure 4.1 depicts the general connection diagram considered so far. More detailes will be added during this chapter. Figure 4.1. General Connection Scheme 4.1 Micro-Controller There were different choices for micro-controller device. From a simple Arduino board till more complicated boards like Raspberry pi series, all of them could satisfy device requirements but with a glance to the mass production requirements, it was more rational to use a micro-controller integrated circuit instead of working with a board. One of the most famous and easy to use family of micro-controllers are AVR produced by Atmal company. To choose between micro-controllers residing in this family, there were a few 21
  • 30. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software points to consider: • Output levels of micro-controller must use TTL logic, because RS485 uses TTL logic for each wire otherwise there is a need for level shifter. • It must have dedicated ports for UART/USART communication. • It is recommended to have SPI port for further wireless module or gyroscope addition. • In order to work with the highest possible baud rate, micro-controller has to be able to work with 11.0592MHz clock. This clock assures that UART/USART communi- cation error will be less than 0.0%. According to these requirements, ATMega16 micro-controller was the selected choice among AVR micro-controllers. In the rest of this chapter, technical relevant characteristics of this device are documented.[9] 4.1.1 General Overview This little and cheap micro-controller has many features, here are the most important ones. • Operating Voltage: 2.7V - 5.5V • Speed Grade: 0 - 8 MHz • I/O and Packages – 32 Programmable I/O Lines – 40-pin DIP, 44-lead TQFP and 44-pad QFN/MLF • Peripheral features – Programmable Serial USART – Master/Slave SPI Serial Interface – Byte-Oriented Two-wire Serial Interface – Four Timer/Counters – Four PWM Channels • 32*8 General Purpose Working Registers • Non-volatile Memory Segments – 16Kbytes of fIn-System Self-programmable Flash program memory – 512 Bytes EEPROM – 1 Kbyte Internal SRAM The micro-controller used for this project was packaged in DIP format because in this way it is much easier to use it on bread board. 22
  • 31. 4 – Communication Interface 4.1.2 Pin Configurations Figure 4.2 denotes pin configurations for ATMega16 micro-controller. Some of pins have dual functions and they can be selected by software. For example pin #14 is the IO #0 of port D in normal condition. It can be configured to function as serial receiver also. Figure 4.2. ATMega16 Pin Configurations 4.1.3 Universal Synchronous Asynchronous Receiver Transmitter (US- ART) In order to manage the RS485 connection between PC and the laser sensor, micro- controller has to work with its USART block. According to the pin configurations on figure 4.2, table 4.1 denotes all USART related pins.[9] Table 4.1. USART Pins Pin# Normal Function USART Function Description 14 PD0 RXD USART receiver input. 15 PD1 TXD USART transmitter output. 11 GND GND Should be connected to the PC and laser sensor grounds to share the same voltage levels. 23
  • 32. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 4.2 RS485 Transceiver Using micro-controller as the medium does not mean that USART output can be con- nected to RS485 wires. As previously mentioned RS485 can use two or four wires for communication in half duplex or full duplex mode respectively. This laser sensor supports only the half duplex mode, That’s why its designers have considered only two wires for communication in RS485 shielded cable. In order to plug USART output to RS485 con- nection a transceiver is required. One of the most common solutions for this problem is MAX485 integrated circuit.[10] Figure 4.3 shows MAX485 transceiver pin configuration. As it is possible to observe in the figure, this IC is composed of two tri-state buffers. RE and DE has the responsibility to control these buffers. RS485 uses two wires to transmit one bit in differential format. For example in order to represent a "0" logic A has "0" value and B has "1" value. The dif- ference between these two levels which for this case is -5V, determines that the sender has sent "0". Its exactly vice versa when sender is transmitting "1". So two control signals, RE and DE select that master is going to send a command or receive a response respectively. During command sending DE is set to "1" and RE is set to "0". It is opposite when laser sensor is transmitting response. DI connects to TXD of micro-controller and RO should be connected to PC. More information will be provided during this chapter. Figure 4.3. MAX485 Pin Configuration 4.3 USB to Serial Converter A few years ago, it was very common for personal computers and laptops to have port for serial connection. Nowadays it is completely rare to have this kind of port for normal computers and there are only USB ports that can do the same thing. In order to send and receive in a serial connection, operating system needs to open a COM port. USB to Serial converters create a virtual COM port so PC can threat with it like a normal one. At the other side there is a small circuit to convert USB data signals (D+ and D−) to Rx and Tx signals. Figure 4.4 depicts a USB to Serial converter module. It has a USB port 24
  • 33. 4 – Communication Interface so a USB cable can connect it to PC. There are also five pins which three of them deliver GND, 3.3V and 5V and the two remaining form the serial connection wires (Rx and Tx). This is not a complicated module, most of USB to Serial converters make use of PL2303 chip for data conversions.[11] Figure 4.4. USB to Serial Converter Module 4.4 Hardware Connections So far, required elements have been introduced. In this section, connections between these elements are described. Figure 4.5 depicts all required connections between previously described elements. Figure 4.5. Communication Interface Connection Scheme In this scheme, PC which is the final device to send commands to laser sensor and receive response from it, is connected to USB to Serial converter via a simple USB cable. So in the PC, by installing appropriate driver, it is possible to open a COM port and transmit serial data. Commands sent by PC are delivered to micro-controller. Micro-controller has the responsibility to control RS485 connection. This control means that, if there is a command to be delivered to laser sensor, micro-controller prepares the line by setting DE to "1" and RE to "0". In this way line is ready to accept value from micro-controller. While this situation exists, the command is sent to laser sensor. Micro-controller will change the line situation immediately after sending is finished. In this way the command is sent and line is ready to receive answer from laser sensor. After computation time required by 25
  • 34. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software laser sensor, response will be sent back and since the line is tuned to deliver data from laser sensor to PC, the response propagates through MAX485 and directly reaches USB to serial and then PC. According to this scheme, tables 4.2,table 4.3,4.4 and 4.5 summarized are required connections for USB to Serial module, micro-controller ATMega16, MAX485 transceiver and laser sensor respectively. Table 4.2. USB to Serial Module Connections Pin# Pin Name Connects to 1 5v Not Connected 2 GND Circuit’s GND 3 3.3v Not Connected 4 NC Not Connected 5 Tx ATMega16 Pin #14 RXD 6 Rx Max485 Pin #1 RO Table 4.3. Micro-controller ATMega16 Connections Pin# Pin Name Connects to 10 VCC Circuit’s 5V 11 GND Circuit’s GND 12 XTAL1 Crystal 11.0592MHz Pin #1 13 XTAL2 Crystal 11.0592MHz Pin #2 14 RXD USB to Serial Pin #5 Tx 15 TXD MAX485 Pin #4 DI 20 PD6 MAX485 Pin #2 RE and Pin #3 DE 31 GND Circuit’s GND Other Pins Not Connected 26
  • 35. 4 – Communication Interface Table 4.4. Max485 Connections Pin# Pin Name Connects to 1 RO USB to Serial Pin #6 Rx 2 RE ATMega16 Pin #20 PD6 3 DE ATMega16 Pin #20 PD6 4 DI ATMega16 Pin #15 TXD 5 GND Circuit’s GND 6 A Laser Sensor Pin #6 PINK 7 B Laser Sensor Pin #1 WHITE 8 VCC Circuit’s 5V Table 4.5. Laser Sensor Connections Pin# Pin Name Connects to 1 WHITE MAX485 Pin #7 B 2 BROWN Circuit’s 24V 6 PINK MAX485 Pin #6 A 7 BLUE Circuit’s GND Other Pins Circuit’s GND 4.5 Software Design So far, hardware design part of communication interface has been completed. Now it is the time to go into details and create a software for micro-controller to act as a medium and regulate the connection. This kind of micro-controller is produced by Atmel company. Atmel suggests an IDE called Atmel studio to program and also simulate this micro- controller. This can be done by programming using Assembly or C language. Since using assembly language is very time consuming, C language was preferred. There is also another IDE called Code Vision AVR which is produced by a third party company called HP InfoTech. Since this IDE can produce a lot of code automatically by means of selecting required resources in a graphical user interface, it was the selected IDE to work with. As previously mentioned, all initialization is made automatically according to pro- grammer inputs. Here are some important portions of code to initialize various variables. Listing 4.1 denotes variable initialization for PORT A of micro-controller. In this case; all pins of PORT A are selected for input and they are working in tri-state mode. It is possible to select inputs with pulled-up mode to avoid floating signals, but since there is no need to work with PORT A in current configuration the default configuration remains untouched. Other ports are initialized in the same way. 1 DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0) ; 27
  • 36. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0) ; Listing 4.1. Port Initialization Next item to initialize is USART interface. Listing 4.2 contains all required codes to initialize USART with following details. • Baud rate: 57600 bits/sec • Databits: 8 • Startbit: 1 • Stopbit: 1 • Parity: Even • Sender/Receiver Interrupt: No UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<< U2X) | (0<<MPCM) ; 2 UCSRB=(0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<< UCSZ2) | (0<<RXB8) | (0<<TXB8) ; UCSRC=(1<<URSEL) | (0<<UMSEL) | (1<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<< UCSZ1) | (1<<UCSZ0) | (0<<UCPOL) ; 4 UBRRH=0x00 ; UBRRL=0x0B ; Listing 4.2. USART Initialization Variables like UCSRA are micro-controller control registers and variables like TXC are pre-defined masks in order to use in such cases for appropriately modifying control registers. Control register UBRRL, selects the transmission baud rate based on system clock frequency. Table 4.6 denotes corresponding values and their transmission error percentage for three available laser sensor commands. Table 4.6. UBRRL Values for available baud rates Bitrate (bits/sec) UBRRL (hex) Error Percentage 38400 0x011 0.0% 57600 0x00B 0.0% 115200 0x005 0.0% Now that the micro-controller is all set, it is necessary to discuss about its duty. As it has been said earlier, micro-controller should define line’s situation by constantly monitoring the PC output. If the output has the character ":", micro-controller sets its pin #6 of port D to "1" and then puts the same character on its serial output which is connected to laser sensor through MAX485. Also a local protocol is considered that PC should send a special character with ascii code of 0x0B to micro-controller to acknowledge 28
  • 37. 4 – Communication Interface command termination. So each command starts with ":" and ends with 0x0B character. After receiving ":" character, micro-controller outputs the received data till termination character (0x0B) receipt. At this point, micro-controller toggles line situation by setting port D pin #6 to "0". In this way, after enough processing time, laser sensor sends the corresponding response back to the PC without micro-controller interference. Listing 4.3 contains above explained code. Since micro-controller should constantly run this program, this code portion is placed in an always true loop. 1 while (1) { 3 ch=getchar () ; i f ( ch==’ : ’ ) 5 { PORTD.6=1; 7 putchar ( ch ) ; } 9 e l s e i f ( ch==0x0B) { 11 f o r ( i =0; i <100; i++); PORTD.6=0; 13 } e l s e 15 putchar ( ch ) ; } Listing 4.3. Main Program As previously mentioned, laser sensor can operate with three different baud rates and the default one is not the fastest one. Although it is possible to change the baud rate and store it in the configuration flash of laser sensor, for the sake of maintenance and plug and play integrity, the micro-controller code is designed to change baud rate to the fastest one upon system power up. With this option, PC program always send required commands with baud rate equal to 115200 bits/sec and micro-controller has the duty to change laser sensor and also its own baud rate at first place. PORTD.6=1; 2 cr=0x0D ; cn=0x0A ; 4 delay_ms (2000) ; p r i n t f ( " :01W010 ; 0 ; ∗∗∗∗%c%c " , cr , cn ) ; 6 delay_ms (100) ; p r i n t f ( " :01W006 ; 2 ; ∗∗∗∗%c%c " , cr , cn ) ; 8 delay_ms (100) ; UBRRL=0x05 ; Listing 4.4. Baud Rate Adjust In this code portion, first, communication line gets ready for command transfer, then after a delay of 2 seconds, micro-controller unlocks the RS485 lock, changes laser sensor baud rate and then changes its own baud rate to 115200 bits/sec. Delays are considered due to laser sensor power up latency and MAX485 propagation time. 29
  • 38. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 4.6 PC-Sensor Communication Test At this point, communication interface setup is complete. By providing required voltage sources (5V and 24V) to the circuit, laser sensor turns on and emits a red beam, then micro-controller configures laser sensor to use baud rate equal to 115200 bits/sec. Next step is to connect USB cable to PC and circuit. Windows will automatically install the appropriate driver. Now everything is ready and it is possible to communicate with laser sensor. In order to just test the connection a terminal emulator was necessary. There are many free solutions like TeraTerm and Putty. Figure 4.6 shows TeraTerm’s serial settings dialogue filled with communication interface configuration data. Figure 4.6. TeraTerm Terminal Emulator Serial Settings By hitting OK, COM port opens and now everything is ready to send a command to laser sensor and wait for response without having concerns about RS485 protocol or anything else. Figure shows a sample command and its corresponding response communi- cated between PC and Laser Sensor. In this communication, PC sent a request to unlock RS485 lock. Figure 4.7. PC-Sensor Communication Test Figure 4.8 shows the final version of RS485 communication interface functioning. On 30
  • 39. 4 – Communication Interface the left side, there is the power supply which has 5V and 24V voltage sources. On the right side there are microcontroller, MAX485 and USB to serial converter. Figure 4.8. RS485 Communication Interface 31
  • 40. 32
  • 41. Chapter 5 Acquisition Software In previous chapter, a communication interface was designed and built to make PC-Sensor communications possible. A sample command sent by means of TeraTerm terminal emula- tor and corresponding responses received. In this chapter, the goal is to design a software to automate this process. So the software should be able to initiate a connection to the laser sensor, send requests, receive responses, display them in a graphical way and store them in a file for further usage. In order to have possibility to compile the program for different operating systems and also for accessing to simpler features, Qt IDE has been used for software development. Qt IDE is developed and supported by Nokia company. Nowadays many programmers and developers use Qt for software development. Figure 5.1 depicts the first version of acquisition program designed. During rest of this chapter different program sections will be analyzed.[12] Figure 5.1. DOT Code Recognition Software (DCR) v1 Qt IDE supports signals and slots methodology for objects communication. During 33
  • 42. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software software design process, it was decided to create a class for laser sensor communication, so this class could be imported to any other applications for development. In this case, the object created of this type can use signals and slots for communication or it can be used in an ordinary way. To proceed with thesis report, two different classes are analyzed independently. One class called "LaserSensor" is dedicated to sensor communications and another class called "MainWindow" is for user interface. 5.1 LaserSensor Class This class is dedicated to handle sensor communication request. With this topology, main application does not need to worry about RS485 command structure, command index and so on. Class definition is made through "lasersensor.h" and "lasersensor.cpp" files. Listing 5.1 summarize all declared slots for this class. Thanks to this kind of approach, a signal emitted by user click on a button can be connected to one of these slots, so whenever that button is clicked, the laser sensor object will execute desired command. 1 private s l o t s : void FindAvailablePorts () ; 3 void InitConnection ( QString PORTName) ; void CheckConnection () ; 5 void ReadApplicationError () ; void ReadVendorInfo () ; 7 void ReadDeviceInfo () ; void ReadBusAddress () ; 9 void WriteBusAddress ( int BusAddressValue ) ; void ReadBaudRate () ; 11 void WriteBaudRate ( int S e l e c t ) ; void ReadRS485Lock () ; 13 void WriteRS485Lock ( bool Switch ) ; void ReadTouchButtonLock () ; 15 void WriteTouchButtonLock ( bool Switch ) ; void ReadMeasurementType () ; 17 void WriteMeasurementType ( int S e l e c t ) ; void ReadMeasurementValue () ; 19 void ReadAllMeasurementValues () ; void ReadFieldOfView () ; 21 void WriteFieldOfView ( int LimitLeft , int LimitRight ) ; void WriteFiledOfViewToMax () ; 23 void ReadObjectType () ; void WriteObjectType ( bool Switch ) ; 25 void ReadPrecision () ; void WritePrecision ( int S e l e c t ) ; 27 void ReadLaserOffDataHold () ; void WriteLaserOffDataHold ( bool Switch ) ; 29 void ReadFlexMountEnable () ; void WriteFlexMountEnable ( bool Switch ) ; 31 void ReadSetFlexMount () ; void WriteSetFlexMount ( f l o a t Angle , f l o a t Distance ) ; 33 void WriteTeachFlexMountCommand( f l o a t ReferenceThickness ) ; void ReadDiagnoseMode () ; 35 void WriteDiagnoseMode ( bool Switch ) ; 34
  • 43. 5 – Acquisition Software void ReadProfile () ; 37 void WriteStoreConfigurationCommand ( int Config ) ; void WriteResetToFactorySettingsCommand ( int Command) ; 39 void CloseConnection () ; void SampleNoChanged ( QString SampleNumber) ; Listing 5.1. LaserSensor Class Private Slots Despite from these available slots, a LaserSensor object can emit signals. Signals can be used in order to send the appropriate response back to the original program. Figure 5.2 depicts an example of this approach. Listing 5.2 lists all declared signals for this class. Figure 5.2. Signal Slots Example s i g n a l s : 2 void AvailablePorts ( int TotalNumber , QVector<QString> Ports ) ; void ConnectionStatus ( bool Status ) ; 4 void ApplicationError ( int ApplicationError ) ; void VendorID ( int Vendor_ID) ; 6 void VendorName( QString Vendor_Name) ; void DeviceID ( int Device_ID ) ; 8 void VariantID ( int Variant_ID ) ; void SensorType ( QString Sensor_Type ) ; 10 void SerialNumber ( QString Serial_Number ) ; void BusAddress ( int BusAddressValue ) ; 12 void BaudRate ( int BuadRateValue ) ; void RS485Lock ( bool Switch ) ; 14 void TouchButtonLock ( bool Switch ) ; void MeasurementType ( QString Type) ; 16 void MeasurementValue ( f l o a t MeasurementValue , QString Quality ) ; void Average ( QString average ) ; 18 void Max ( QString max) ; void Min ( QString min) ; 20 void Delta ( QString delta ) ; void StandardDeviation ( QString standarddeviation ) ; 22 void Quality ( QString quality ) ; void FieldOfView ( int LimitLeft , int LimitRight ) ; 24 void ObjectType ( QString ObjectType ) ; void Precision ( QString Precision ) ; 26 void LaserOffDataHold ( bool Switch ) ; void FlexMountEnable ( bool Switch ) ; 28 void SetFlexMount ( f l o a t Angle , f l o a t Distance ) ; 35
  • 44. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software void DiagnoseMode ( bool Switch ) ; 30 void P r o f i l e ( int Xpos [ 3 0 0 ] , int Zpos [ 3 0 0 ] ) ; void SensorTimedOut ( bool Switch ) ; 32 void ProfileReady () ; Listing 5.2. LaserSensor Class Signals So with these slots and signals, all requests are sent through slots and all responses are emitted via signals. Now lets go into the details and analyze the most important slots functions. • Laser Sensor Constructor: This function is called whenever a new object of class LaserSensor is created. This initializes serial communication and sets its baud rate to 115200 bits/sec. This serial connection uses a Qt library called "QtSerial.h". After serial port initialization, it initialize some other variables that will be covered later. Listing 5.3 denotes LaserSensor constructor definition. LaserSensor : : LaserSensor () 2 { qRegisterMetaType<QVector<QString> >(" QVector<QString>" ) ; 4 s e r i a l = new QSerialPort ( t h i s ) ; s e r i a l −>setBaudRate ( QSerialPort : : Baud115200 ) ; 6 s e r i a l −>setDataBits ( QSerialPort : : Data8 ) ; s e r i a l −>setParity ( QSerialPort : : EvenParity ) ; 8 s e r i a l −>setStopBits ( QSerialPort : : OneStop ) ; s e r i a l −>setFlowControl ( QSerialPort : : NoFlowControl ) ; 10 DeviceAddress =1; Yindex=0; 12 f i l e c r e a t e d=f a l s e ; SampleNo=" 1 " ; 14 } Listing 5.3. LaserSensor Constructor The third line of code has the duty to register a meta type in order to provide possibility to move an object of this class to a new thread. DeviceAddress variable is always one since there is only one sensor in RS485 network. Yindex is the index of scanned lines and it is set to zero at first. After each successful acquisition it will be automatically incremented. filecreated flag is used to check if the profile text file is created or not. In read profile mode, laser sensor class should store acquisition data in a text file. SampleNo variable is defined to identify different tire samples. It is just there for further result comparisons. • FindAvailablePorts This function scans for all COM ports, then removes those ones that are not available. A COM port is not available when another program is using it and it is flagged as busy. Then it emits a signal containing total number of available ports and their names. Listing 5.4 denotes corresponding codes. void LaserSensor : : FindAvailablePorts () 36
  • 45. 5 – Acquisition Software 2 { int TotalNumber=0; 4 QVector<QString> Ports (0) ; foreach ( const QSerialPortInfo &info , QSerialPortInfo : : availablePorts () ) 6 { i f ( ! i n f o . isBusy () ) 8 { Ports . append ( i n f o . portName () ) ; 10 TotalNumber++; } 12 } emit AvailablePorts ( TotalNumber , Ports ) ; 14 } Listing 5.4. FindAvailablePorts: Returns all ready to connect COM ports • SendCommand All slots use a function to send a command to laser sensor. SendCommand receives a string as input, then adds other necessary elements to its begin and end according to the RS485 command structure. Listing 5.5 contains corresponding codes. void LaserSensor : : SendCommand ( QString Command) 2 { QChar cr=0x0D ; 4 QChar cn=0x0A ; QChar stopchar=0x0B ; 6 QString ToSend=" : " ; ToSend . append ( QString ( "%1" ) . arg ( DeviceAddress , 2 , 10 , QChar( ’ 0 ’ ) ) ) ; 8 ToSend . append (Command) ; ToSend . append ( " ; " ) ; 10 ToSend . append ( " ∗∗∗∗ " ) ; ToSend . append ( cr ) ; 12 ToSend . append ( cn ) ; ToSend . append ( stopchar ) ; 14 s e r i a l −>write (ToSend . toLatin1 () ) ; } Listing 5.5. SendCommand: Prepares command and sends it. In this function start, carriage return, line feed and stop char has been defined. Stop char is the conventional character 0x0B that informs micro-controller to change the RS485 line mode. After command preparation, it will be sent byte by byte to serial port thanks to the toLatin1 method of QString class. • ReceiveResponse This function is intended to be called by each slot after SendCommand execution. In this way, the function will be blocked until a response is received or a time-out occurs. Listing 5.6 gathers all necessary codes for this purpose. This function returns true in case of response receipt and false in case of time-out. 37
  • 46. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 1 bool LaserSensor : : ReceiveResponse () { 3 QChar cn=0x0A ; Response . c l e a r () ; 5 bool CompletelyReceived=f a l s e ; while ( ! CompletelyReceived ) 7 { i f ( s e r i a l −>waitForReadyRead (500) ) 9 { Response . append ( s e r i a l −>readAll () ) ; 11 qDebug () << Response ; i f ( Response . contains ( cn ) ) CompletelyReceived=true ; 13 } e l s e 15 { return f a l s e ; 17 } } 19 qDebug () << Response ; Payloads=Response . s p l i t ( " ; " ) ; 21 Payloads . removeFirst () ; return true ; 23 } Listing 5.6. ReceiveResponse: Verifies response receipt. This code makes use of serial library time-out ability. If no new byte is received within 500msec, if statement will be false and function returns false, acknowledging that the request has been timed-out so the main function should request it again. If a byte is received, it will be added to Response variable and cleared from serial buffer. The while loop remains true, until the line feed character receipt. After successful command receipt, function splits it to payloads. Usually sensor responses have payloads to return measurement values. • WriteDiagnoseMode As previously mentioned about working with profile command of laser sensor, this command is only available when laser sensor is in diagnose mode. So the WriteDi- agnoseMode has the duty to enter laser sensor to this mode. Listing 5.7 denotes corresponding functions. 1 void LaserSensor : : WriteDiagnoseMode ( bool Switch ) { 3 SendCommand( "W030; −20;20 " ) ; ReceiveResponse () ; 5 SendCommand( "R030" ) ; ReceiveResponse () ; 7 SendCommand( "W050;1 " ) ; i f ( ReceiveResponse () ) 9 { emit DiagnoseMode ( true ) ; 11 } 38
  • 47. 5 – Acquisition Software } Listing 5.7. WriteDiagnoseMode: Activates diagnose mode. Although this function is designed to just activate the diagnose mode, it also sets the field of view range. In third line, laser sensor is asked to set its measuring range from -20mm to 20mm with respect to the center of red beam. Then the same attribute requested to read for verification. Finally at line 7, program sends diagnose mode activation command and if the response is received, it emits a signal to MainWindow which yields to a check in a checkbox. • ReadProfile Finally, its time to analyze the main function that collects acquisition data. This function creates two data files and store the same content into both of them. The reason of creating two identical files is that, for image pre-processing part, it was necessary to address a constant file name in MATLAB. In order to prevent from overwriting the previously made acquisition, another file with date, time and also sample number is created each time. According to listing 5.8, this function performs a profile acquisition once and emits a ProfileReady signal when its done. This is the duty of MainWindow to call this function in a loop to scan multiple lines. void LaserSensor : : ReadProfile () 2 { i f ( ! f i l e c r e a t e d ) 4 { QDateTime currentdateandtime = QDateTime : : currentDateTime () ; 6 ProfileData . setFileName ( " ProfileData_Sample "+SampleNo+"_"+ currentdateandtime . toString ( " yyyy−MM−dd_hh−mm−ss " )+" . txt " ) ; ProfileData . open ( QIODevice : : WriteOnly ) ; 8 ProfileData1 . setFileName ( " ProfileData . txt " ) ; ProfileData1 . open ( QIODevice : : WriteOnly ) ; 10 ProfileStream . setDevice(&ProfileData ) ; ProfileStream << "YtX t Z" <<endl <<endl ; 12 ProfileStream1 . setDevice(&ProfileData1 ) ; ProfileStream1 << "YtX t Z" <<endl <<endl ; 14 f i l e c r e a t e d=true ; } 16 SendCommand( "R052" ) ; i f ( ReceiveResponse () ) 18 { QStringList XZMeasurements ; 20 XZMeasurements=Payloads [ 1 ] . s p l i t ( " " ) ; int TotalMeasurements=XZMeasurements [ 0 ] . toInt () ; 22 XZMeasurements . removeFirst () ; Yindex++; 24 f o r ( int i =0; i<TotalMeasurements /2; i++) { 26 ProfileStream << Yindex <<" t " << XZMeasurements [2 ∗ i ] <<" t " <<XZMeasurements [2 ∗ i +1] <<endl ; ProfileStream1 << Yindex <<" t " << XZMeasurements [2 ∗ i ] <<" t " <<XZMeasurements [2 ∗ i +1] <<endl ; 39
  • 48. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 28 } 30 } emit ProfileReady () ; 32 } Listing 5.8. ReadProfile: Makes profile acquisition once. This function primarily checks for file existence, if it is not created, it creates two files as described above. Then it prints column headers into these two files. After that a command with read profile index will be sent to laser sensor and upon successful receipt, it splits (x,z) pairs and prints them along corresponding y index into both files. Listing 5.9 shows 10 lines of an acquisition data file with 10534 lines. Y X Z 2 1 0 3889 4 1 50 3897 1 100 3911 6 1 150 3955 1 200 3966 8 1 250 3953 1 300 3971 10 1 350 4006 Listing 5.9. Read Profile Sample 5.2 MainWindow Class In previous section, the LaserSensor class was introduced. In this section MainWindow class is going to be analyzed. This class also is defined by means of "MainWindow.h" and "MainWindow.cpp" files. MainWindow class is already filled by the information coming from user interface design and other necessary initialization. There are not too many modifications required for this class since most of the work is done in LaserSensor class. By the way it is necessary to define a new object of class LaserSensor and a new thread to move the object into it. Listing 5.10 denotes corresponding codes. LaserSensor ∗ Sensor = new LaserSensor () ; 2 QThread∗ SensorThread = new QThread ; Sensor−>moveToThread( SensorThread ) ; Listing 5.10. Thread Creation for LaserSensor Object Note that the thread should be started before program execution, but this will happen after signal and slots connections definition. So next part should define the signal and slots connection. This process is like wiring building blocks. Listing 5.11 lists all of these connections. 1 //Find Available Ports 40
  • 49. 5 – Acquisition Software connect ( ui−>Refresh_Ports ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( FindAvailablePorts () ) ) ; 3 connect ( Sensor ,SIGNAL( AvailablePorts ( int , QVector<QString >)) , this ,SLOT( ShowAvailablePorts ( int , QVector<QString >)) ) ; 5 // I n i t i a l i z e Connection connect ( this ,SIGNAL( InitConnection ( QString ) ) , Sensor ,SLOT( InitConnection ( QString ) ) ) ; 7 connect ( Sensor ,SIGNAL( ConnectionStatus ( bool ) ) , ui−>Serial_Status ,SLOT( setChecked ( bool ) ) ) ; 9 //Vendor Information connect ( ui−>GetVendorInfo ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadVendorInfo () ) ) ; 11 connect ( Sensor ,SIGNAL( VendorID ( int ) ) , ui−>VendorID_Label ,SLOT(setNum( int ) ) ) ; connect ( Sensor ,SIGNAL(VendorName( QString ) ) , ui−>VendorName_Label ,SLOT( setText ( QString ) ) ) ; 13 // Device Information 15 connect ( ui−>GetDeviceInfo ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadDeviceInfo () ) ) ; connect ( Sensor ,SIGNAL( DeviceID ( int ) ) , ui−>DeviceID_Label ,SLOT(setNum( int ) ) ) ; 17 connect ( Sensor ,SIGNAL( VariantID ( int ) ) , ui−>VariantID_Label ,SLOT(setNum( int ) ) ) ; connect ( Sensor ,SIGNAL( SensorType ( QString ) ) , ui−>SensorType_Label ,SLOT( setText ( QString ) ) ) ; 19 connect ( Sensor ,SIGNAL( SerialNumber ( QString ) ) , ui−>SerialNumber_Label ,SLOT ( setText ( QString ) ) ) ; 21 //Read All Measurement Values connect ( ui−>ReadAllMeasurementValues ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadAllMeasurementValues () ) ) ; 23 connect ( Sensor ,SIGNAL( Average ( QString ) ) , ui−>Average_Label ,SLOT( setText ( QString ) ) ) ; connect ( Sensor ,SIGNAL(Min( QString ) ) , ui−>Min_Label ,SLOT( setText ( QString ) ) ) ; 25 connect ( Sensor ,SIGNAL(Max( QString ) ) , ui−>Max_Label ,SLOT( setText ( QString ) ) ) ; connect ( Sensor ,SIGNAL( Delta ( QString ) ) , ui−>Delta_Label ,SLOT( setText ( QString ) ) ) ; 27 connect ( Sensor ,SIGNAL( StandardDeviation ( QString ) ) , ui−> StandardDeviation_Label ,SLOT( setText ( QString ) ) ) ; connect ( Sensor ,SIGNAL( Quality ( QString ) ) , ui−>Quality_Label ,SLOT( setText ( QString ) ) ) ; 29 //Read P e r i o d i c a l l y 31 connect ( ui−>ReadPeriodically ,SIGNAL( c l i c k e d () ) , this ,SLOT( ReadPeriodically () ) ) ; 33 // Enable Diagnose Mode connect ( ui−>EnableDiagMode ,SIGNAL( c l i c k e d ( bool ) ) , Sensor ,SLOT( WriteDiagnoseMode ( bool ) ) ) ; 41
  • 50. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 35 connect ( Sensor ,SIGNAL( DiagnoseMode ( bool ) ) , ui−>DiagModeStatus ,SLOT( setChecked ( bool ) ) ) ; 37 //Read P r o f i l e P e r i o d i c a l l y connect ( ui−>ReadProfile ,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( ReadProfile () ) ) ; 39 connect ( Sensor ,SIGNAL( ProfileReady () ) , Sensor ,SLOT( ReadProfile () ) ) ; connect ( ui−>spinBox ,SIGNAL( valueChanged ( QString ) ) , Sensor ,SLOT( SampleNoChanged ( QString ) ) ) ; 41 // Close Connection 43 connect ( ui−>STOP,SIGNAL( c l i c k e d () ) , Sensor ,SLOT( CloseConnection () ) ) ; 45 SensorThread−>s t a r t () ; Listing 5.11. Signal-Slot Connections These connections, connect user interface elements signals such as clicked() to a slot in sensor object. Then it connects a relevant signal of sensor to a slot of user interface element. For example a click on Connect button in user interface, will cause a request to laser sensor device to remove its RS485 lock and upon successful operation, a check will appear in user interface communication status checkbox. This concept is depicted on figure 5.2 on page 35. After providing all connections, thread is started on line 45. 42
  • 51. Chapter 6 Image Pre-Processing In previous chapter, a Qt program developed to make use of communication interface to perform acquisition and store received data in a file. In this chapter, two different approaches are used to perform image pre-processing techniques. First approach is done by MATLAB and second approach is designed with Qt C++ and help of OpenCV image processing library. 6.1 MATLAB In many similar cases, when the goal is to perform image processing techniques and have OCR program, the starting point is to use MATLAB software because it offers many easy to use functions which are already developed. In this section a .m program is analyzed step by step to determine how does it work. Step 1: Listing 6.1 represents a portion of code dedicated to open the data file, fill three x,y and z vectors based on imported data and draw a 3D object to represent scanned tire portion. 1 %Read measured data from data f i l e filename = ’E: Education University MS4 th Semester Thesis Versions Qt v1 .6 build−DCR−Desktop_Qt_5_6_0_MSVC2015_64bit−Debug P r o f i l e s ProfileData . tpd ’ ; 3 d e l i m i t e r I n = ’ t ’ ; headerlinesIn = 1; 5 P r o f i l e = importdata ( filename , delimiterIn , headerlinesIn ) ; 7 %Store x , y , z combination on x , y and z vectors x=P r o f i l e . data ( : , 2 ) ; 9 y=P r o f i l e . data ( : , 1 ) ; z=P r o f i l e . data ( : , 3 ) ; 11 plot3 (x , y , z , ’.− ’ ) 13 t r i = delaunay (x , y) ; 15 plot (x , y , ’ . ’ ) 43
  • 52. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 17 [ r , c ] = s i z e ( t r i ) ; disp ( r ) 19 h = t r i s u r f ( t r i , x , y , z ) ; 21 axis vis3d axis o f f 23 l = l i g h t ( ’ Position ’ ,[ −50 −15 29 ] ) ; set ( gca , ’ CameraPosition ’ ,[208 −50 7687]) ; 25 l i g h t i n g phong shading interp 27 colorbar EastOutside Listing 6.1. Data file loading As previously mentioned, in order to run this program again and again for different newly scanned samples, file name points to "ProfileData.tpd". The ".tpd" extension has been chosen to the courtesy of Tire Profiles company and stands for Tire Pro- files Data. According to the previous convention about filling data file, it has one row of column headers and values are separated by means of tabs. The code for representing x,y and z vectors in 3D format, uses point by point drawing and then changing the color based on corresponding z value. Figure 6.1 shows output of these commands, As it is possible to observe the DOT code on tire is "DOT CN3R PY42". On the right side of this figure, there is a bar showing corresponding color for different distances. These distances are actually the distances between each point and laser sensor. For example on right side of tire representation, color dedicated to characters are less than background color in the sense that the corresponding distance value is less than the same value for background. Which means the DOT characters are closer to the laser sensor rather than the background. Step 2: For further improvements on achieved results it is a good choice to store x,y and z vectors inside a matrix called P such a way that x and y values are row and column indices and z values are stored as matrix elements values. Listing 6.2 lists all required MATLAB commands to transform x,y,z vectors to P matrix. 1 %Loading Measured data to matrix P Yindex=0; 3 Xindex=0; yvalue=−1; 5 f o r i =1: length (x) i f (y( i )==yvalue ) 7 Xindex=Xindex+1; e l s e 9 Yindex=Yindex+1; Xindex=1; 11 yvalue=y( i ) ; end 13 P( Yindex , Xindex )=z ( i ) ; end 15 44
  • 53. 6 – Image Pre-Processing Figure 6.1. MATLAB Loading Raw Data File % Removing zero elements from P r o f i l e matrix 17 f o r x=1: s i z e (P, 1 ) f o r y=1: s i z e (P, 2 ) 19 i f (P(x , y)==0) P(x , y)=P(x , y−1) ; 21 end end 23 end figure , s u r f (P) , t i t l e ( ’ 3D Surface Representation ’ ) Listing 6.2. Store Loaded Data into P Matrix In this portion of code, one loop sweeps all x,y,z elements and fill P matrix accord- ingly. Note that the value stored in x vector is not needed and only the sequence of data is important. After filling P matrix based on x,y,z vectors, it is possible to have zero elements in P because at time of acquisition sensor couldn’t scan for example 400 points of a few lines. In such cases that are usually a result of functional criterion limits trespassing, it is necessary to fill those zero elements with a neighbor non zero element. When P matrix is completely built up and does not have zero elements inside, surf function can make a better 3D surface representation like in figure 6.2. 45
  • 54. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Figure 6.2. MATLAB 3D Surface Representation Step 3: As it is observable from 3D image, the acquisition is not perfect, in the sense that sensor did not maintain a constant vertical distance to tire during scanning process. To reduce effect of this matter, listing 6.3 lists a few simple commands to level the 3D model from one side. f o r x=1: s i z e (P, 1 ) 2 P(x , : )=P(x , : ) −(P(x , 1 )−P( 1 : 1 ) ) ; end 4 P=f l i p l r (P) ; figure , s u r f (P) , t i t l e ( ’ 3D Surface Representation − Leveled ’ ) Listing 6.3. Leveled Model In this code portion, all points of every single horizontal line are modified such a way that the starting point of all lines have the same value. Figure 6.3 represents the result of leveling process. Step 4: Now that the 3D model is leveled, it is the time to scan for abrupt changes in the model. The only way to recognize and the main reason behind using laser sensor instead of camera is to find DOT code based on altitude changes instead of trying to recognize it from a black on black pattern. Listing 6.4 summarizes all required commands to find abrupt changes. 1 %% Finding maximum two consecutive points d i f f e r e n c e 46
  • 55. 6 – Image Pre-Processing Figure 6.3. Leveled Surface Representation f o r y=1: s i z e (P, 2 ) 3 MaxDeltaVector (y) =0; f o r x=2: s i z e (P, 1 ) 5 i f ( abs (P(x , y)−P(x−1,y) )>MaxDeltaVector (y) ) MaxDeltaVector (y)=abs (P(x , y)−P(x−1,y) ) ; 7 end end 9 end MaxDelta=max( MaxDeltaVector ) ; 11 % Finding abrupt z change between two consectuive points ( r i g h t to l e f t ) 13 f o r y=1: s i z e (P, 2 ) P1(1 , y)=min(P( : ) ) ; 15 P1(2 , y)=min(P( : ) ) ; f o r x=3: s i z e (P, 1 ) 17 ExpectedValue=2∗P(x−1,y)−P(x−2,y) ; i f ( abs (P(x , y)−ExpectedValue ) <0.3∗MaxDelta ) 19 P1(x , y)=min(P( : ) ) ; e l s e 21 P1(x , y)=P(x , y) ; end 23 end end 25 % Finding abrupt z change between two consectuive points ( l e f t to r i g h t ) 27 f o r y=1: s i z e (P, 2 ) 47
  • 56. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software P1( end , y)=min(P( : ) ) ; 29 P1( end −1,y)=min(P( : ) ) ; f o r x=s i z e (P, 1 ) −2:−1:1 31 ExpectedValue=2∗P(x+1,y)−P(x+2,y) ; i f ( abs (P(x , y)−ExpectedValue ) >0.3∗MaxDelta && P1(x , y)==0) 33 P1(x , y)=P(x , y) ; end 35 end end 37 figure , s u r f (P1) , t i t l e ( ’ Abrupt a l t i t u d e changes in 3D model ’ ) Listing 6.4. Procedure to Find Abrupt Altitude Changes This step works by finding the most variation between two consecutive points. This value gives an order of expected variation. Its expected to have the most variations in altitude where ever there is an edge of DOT code. At the rest of code, MATLAB will try to find points that have a absolute consecutive change more than 0.3 times of maximum abrupt changes. These points will be considered as potential edges. This control process is executed twice, one from right to left and one from left to right. All potentially important points are gathered in P1 matrix. Figre 6.4 shows abrupt changes in 3D model, all other points have been changed to have altitude equal to zero. Figure 6.4. MATLAB 3D Model Abrupt Changes Step 5: As its observable from previous step result, the algorithm has found only the edges as it was intended to do. In order to have characters also included in the segmented area, listing 6.5 contains the appropriate code to add a point to selected 48
  • 57. 6 – Image Pre-Processing points if there is an already selected point in its neighborhood with size of three points. 1 % Adding neighbor points to c o l l e c t e d data NeighborSize =3; 3 P2=P1 ; f o r y=1+NeighborSize : s i z e (P, 2 )−NeighborSize 5 f o r x=1+NeighborSize : s i z e (P, 1 )−NeighborSize f o r i=−NeighborSize : NeighborSize 7 f o r j=−NeighborSize : NeighborSize i f (P1(x+i , y+j )==P(x+i , y+j ) ) 9 P2(x , y)=P(x , y) ; end 11 end end 13 end end 15 figure , s u r f (P2) , t i t l e ( ’Added neighbours ’ ) Listing 6.5. Adding Neighbors to Selected Points This code uses four nested loops to check if a point has to be added to selected points or not. In order to prevent from selecting all points, a new P2 matrix is filled based on P1 matrix data. Figure 6.5. MATLAB 3D Model Added Neighbors Step 6: For image segmentation it is better to have the 3D model with miinmum value equal to zero. In this simple step minimum value of matrix is decremented from all 49
  • 58. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software points. Note that the minimum value is referred to the minimum value of all points without considering points with zero value. Listing 6.6 lists these simple commands. 1 % Remove min value ( except 0) P2=P2−min(P2 ( : ) ) ; 3 minvalue=i n f ; f o r y=1: s i z e (P, 2 ) 5 f o r x=1: s i z e (P, 1 ) i f (P2(x , y)~=0 && P2(x , y)<minvalue ) 7 minvalue=P2(x , y) ; end 9 end end 11 f o r y=1: s i z e (P, 2 ) f o r x=1: s i z e (P, 1 ) 13 i f (P2(x , y)~=0) P2(x , y)=P2(x , y)−minvalue ; 15 end end 17 end figure , s u r f (P2) , t i t l e ( ’Min Value Removed ’ ) 19 P=P2 ; Listing 6.6. Removing Minimum Value (Except 0) Figure 6.6 depicts the new 3D model which has zero minimum value. Comparison may be made by considering the z axis numbers. Figure 6.6. MATLAB 3D Model Removed Min Value 50
  • 59. 6 – Image Pre-Processing Step 7: Now that the 3D manipulation is made effectively by exploiting distance mea- surement feature, it is possible to convert this 3D model to a normal 2D image to perform some simple image pre-processing techniques. Listing 6.7 denotes required commands to perform this important step. 1 % Image Segmentation P=P ’ ; 3 I = mat2gray (P) ; mask = f a l s e ( s i z e ( I ) ) ; 5 f o r x=1: s i z e ( I , 1 ) 7 f o r y=1: s i z e ( I , 2 ) i f (P(x , y) >=0.9∗max(P( : ) ) ) 9 mask(x , y)=true ; end 11 end end 13 W = graydiffweight ( I , mask , ’ GrayDifferenceCutoff ’ , 25) ; 15 thresh = 0 . 0 1 ; [BW, D] = imsegfmm (W, mask , thresh ) ; 17 f i g u r e imshow (BW) 19 t i t l e ( ’ Segmented BW Image ’ ) Listing 6.7. Image Black & White Segmentation This step primarily uses a "mat2gray" function to just scale each matrix element value to have a number between 0 and 255. "mask" variable is used to define a mask which represents potential seeds of image segmentation. This binary matrix is filled with ones for points with highest values (above 90%) and zeros for all remaining points. "graydiffweight" uses the grayscale image and provided mask to calculate a weight for pixels according to the difference between their value and masked point values. At the end, "imgsegfmm" function performs segmentation based on the weighted values, provided mask and a threshold which determines the border of black and white. Figure 6.7 shows the result of this process. Step 8: It seems that the code has selected the correct region that contains the DOT code but with current black and white image it is not possible to perform OCR for sure. So in this step, segmentation mask created by previous step is used to filter original grayscale image obtained by beginning of previous step. 1 f o r x=1: s i z e ( I , 1 ) f o r y=1: s i z e ( I , 2 ) 3 i f (BW(x , y)==f a l s e ) P(x , y) =0; 5 end end 7 end I = mat2gray (P) ; figure , imshow ( I ) , t i t l e ( ’ Segmented Grayscale Image ’ ) 51
  • 60. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Figure 6.7. Segmented Black & White Image Listing 6.8. Image Grayscale Segmentation This code uses BW image created by previous step to filter original P matrix. Figure 6.8 reveals grayscale segmented image which is quite promising. Figure 6.8. Segmented Grayscale Image Step 9: This image can be improved by means of contrast stretching. Listing 6.9 denotes required commands one after the other. %Contrast Stretching 2 P=P−min(P(P>0)) ; P(P<0)=0; 4 P=P. ∗255/max(P( : ) ) ; I = mat2gray (P) ; figure , imshow ( I ) , t i t l e ( ’ Contrast Stretched Image ’ ) ; 52
  • 61. 6 – Image Pre-Processing Listing 6.9. Contrast Stretching This code guarantees to have minimum and maximum grayscale values by removing minimum value and then stretch the gray value spectrum to 255. Figure 6.9 shows improved constrast stretched image. Figure 6.9. Contrast Stretched Image Step 10: Since most of OCR techniques works best when the input image has black text on white background, 6.10 denotes the simple method to find the negative image. 1 %Negate Image P=255−P; 3 I = mat2gray (P) ; figure , imshow ( I ) , t i t l e ( ’ Negative Image ’ ) ; Listing 6.10. Negative Image Finally, figure 6.10 shows the result of 10 consecutive steps for image pre-processing. Figure 6.10. Negative Image 53
  • 62. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Although this image pre-processing is not perfect but it offers a good approach to make the 3D model clearer for image processing and specifically OCR. Anyway the thesis goal was mainly to achieve a stable working condition for laser sensor communication and scanner software acquisition. 6.2 Qt and Open-CV In previous section, a 10 step pre-image processing algorithm introduced to use with MAT- LAB program. Since the final program should be developed and deployed with Qt IDE, although this was not a part of this thesis subject, author tried to implement a few ba- sic pre-processing algorithms into the scanner Qt C++ software. In this way the field is widely open for another engineers to expand and make use of it. Open-CV is an open source computer vision library that supports various operating sys- tems and programming languages. This flexibility is encouraging programmers to use this library for their image processing applications. Qt and Open-CV can make a powerful image processing software since both of them can deploy programs for different operating systems.[13] The new Qt software developed along with Open-CV libraries has one more class named "ProfileProcessor". This class also is defined by files "profileprocessor.h" and "profilepro- cessor.cpp". Listing 6.11 denotes public and private functions declared by this class. 1 public : P r o f i l e P r o c e s s o r () ; 3 void Process ( QString ProfileDataLocation , QString ConfigurationFileLocation ) ; private : 5 void LoadProfile ( QString FileLocation ) ; void ShowImage () ; 7 void CorrectOrientation () ; void InvertColor () ; 9 void SharpenEdges () ; Listing 6.11. ProfileProcessor Functions In this case there is no advantage to use slots over class functions because the processed data by the profile processor object should be handled differently than just connecting it to a user interface element slot. But for signals part, listing 6.12 shows two signals declared by this class. 1 s i g n a l s : void SendImage (QPixmap Image ) ; 3 void SendCurrentOperation ( QString Command) ; Listing 6.12. ProfileProcessor Signals SendImage and SendCurrentOperation signals, send an image and a descriptive string respectively. This design is intended in order to make possible to have a good interface in MainWindow object. Further information will be available later. Listing F.1 describes the definition of process function of this class. 54
  • 63. 6 – Image Pre-Processing 1 void P r o f i l e P r o c e s s o r : : Process ( QString ProfileDataLocation , QString ConfigurationFileLocation ) { 3 QFile Configuration ; Configuration . setFileName ( ConfigurationFileLocation ) ; 5 Configuration . open ( QIODevice : : ReadOnly ) ; QTextStream ConfigurationStream ; 7 ConfigurationStream . setDevice(&Configuration ) ; while ( ! ConfigurationStream . atEnd () ) 9 { QString Action=ConfigurationStream . readLine () ; 11 i f ( Action . contains ( " Load F i l e " ) ) { 13 LoadProfile ( ProfileDataLocation ) ; } 15 e l s e i f ( Action . contains ( " Correct Orientation " ) ) { 17 CorrectOrientation () ; } 19 e l s e i f ( Action . contains ( " Invert Color " ) ) { 21 InvertColor () ; } 23 e l s e i f ( Action . contains ( " Sharpen Edges " ) ) { 25 SharpenEdges () ; } 27 } 29 Configuration . c l o s e () ; ProfileMat . r e l e a s e () ; 31 ProfileData . c l o s e () ; 33 } Listing 6.13. ProfileProcessor Process Function This portion of code, defines that this function requires two arguments as inputs. One is the location address of a profile data file and the other is the location address of a configuration file. Profile data file is the same file that has been used to store acquisition data while configuration file is a new input file which dictates to processor how to process files. This new file may seems to make the system unnecessarily complex but this option is intended to make it simpler. Sometimes when a utility software similar to this one is sold, it happens that the system is no longer be able to perform its job. In most cases when a technical support gets dispatched and checks the system, it comes up that the system needs to reconfigure to adopt to the new situation. A change in situation may be caused by even different illumination. So in this cases it is much easier to change a configuration file instead of whole program. By the way, this function reads contents of configuration file and decides what is the next action to take. After all actions are finished, it closes two text files. Now lets analyze four actions that have been already implemented. • LoadProfile 55
  • 64. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Listing 6.14 lists the required functions to load a profile data file. 1 void P r o f i l e P r o c e s s o r : : LoadProfile ( QString FileLocation ) { 3 ProfileData . setFileName ( FileLocation ) ; ProfileData . open ( QIODevice : : ReadOnly ) ; 5 ProfileStream . setDevice(&ProfileData ) ; int x , y , z , yold =0, xindex=−1,yindex=−1; 7 int xindexmax=0,yindexmax=0,zmax=0,zmin=std : : numeric_limits<int >:: max() ; QString ReadLine ; 9 QList <QString> SplittedData ; 11 //Scan f i l e in order to find the image dimensions ProfileStream . readLine () ; // F i r s t Line 13 ProfileStream . readLine () ; //Second Line while ( ! ProfileStream . atEnd () ) 15 { ReadLine=ProfileStream . readLine () ; 17 SplittedData=ReadLine . s p l i t ( " t " ) ; y=SplittedData . at (0) . toInt () ; 19 x=SplittedData . at (1) . toInt () ; z=SplittedData . at (2) . toInt () ; 21 i f (y!= yold ) { 23 yindex++; xindex=−1; 25 yold=y ; } 27 xindex++; i f ( xindex>xindexmax ) xindexmax=xindex ; 29 i f (y>yindexmax ) yindexmax=y ; i f ( z>zmax) zmax=z ; 31 i f ( z<zmin ) zmin=z ; } 33 // Create Mat object based on image s i z e 35 ProfileMat= Mat : : zeros ( yindexmax+1,xindexmax+1,CV_8UC1) ; 37 //Read f i l e again to f i l l the Mat object accordingly ProfileStream . seek (0) ; 39 ProfileStream . readLine () ; // F i r s t Line ProfileStream . readLine () ; //Second Line 41 xindex=−1; yindex=−1; 43 while ( ! ProfileStream . atEnd () ) { 45 ReadLine=ProfileStream . readLine () ; SplittedData=ReadLine . s p l i t ( " t " ) ; 47 y=SplittedData . at (0) . toInt () ; x=SplittedData . at (1) . toInt () ; 49 z=SplittedData . at (2) . toInt () ; i f (y!= yold ) 51 { yindex++; 56
  • 65. 6 – Image Pre-Processing 53 xindex=−1; yold=y ; 55 } xindex++; 57 int ScaledValue =((z−zmin ) ∗ 254/(zmax−zmin ) ) +1; ProfileMat . at<uchar >(yindex , xindex )= ScaledValue ; 59 } //Check i f i t has zero values 61 f o r ( yindex =1; yindex<ProfileMat . rows ; yindex++) f o r ( xindex =0; xindex<ProfileMat . c o l s ; xindex++) 63 i f ( ProfileMat . at<uchar >(yindex , xindex )==0) { 65 ProfileMat . at<uchar >(yindex , xindex )= ProfileMat . at< uchar >(yindex −1, xindex ) ; } 67 f o r ( yindex=ProfileMat . rows −2; yindex >=0;yindex −−) f o r ( xindex=ProfileMat . cols −1; xindex >=0;xindex −−) 69 i f ( ProfileMat . at<uchar >(yindex , xindex )==0) { 71 ProfileMat . at<uchar >(yindex , xindex )= ProfileMat . at< uchar >(yindex +1, xindex ) ; } 73 emit SendCurrentOperation ( " P r o f i l e Data Loaded " ) ; ShowImage () ; 75 } Listing 6.14. ProfileProcessor Load Profile The LoadProfile function primarily opens the data file and reads it line by line. Two first lines are not important since they are dedicated to column headers. Then it reads following lines and use the same method of MATLAB program to find dimensions of P matrix. Open-CV uses a variable of type mat to store and work with images. This variable dynamically allocates memory and release it when it is not needed anymore. Copying mat objects by using a=b syntax does not copy the data but it only copies the pointer. There is a specific function to copy also the content of mat objects. in the first loop the function scans the file just to find the appropriate dimensions of final mat object. After that at line 35, it creates a mat object which is a matrix of appropriate dimensions and defines that the value of this matrix elements is a single scalar and can be represented by 8 bits. This is the method that Open-CV deals with grayscale images. Now that the matrix is created and filled with zeros, its the time to restart reading data file and fill the mat object accordingly. Same as MATLAB code it is possible to have zero elements inside the matrix because sensor does not return same number of points for every acquisition, so it is necessary to fill the zero elements with a neighbor’s value. Figure 6.11 depicts the output of this stage. • According to obtained picture, it is necessary to rotate the image whenever its x dimension is less than y dimension. Listing 6.15 lists the required command to check and rotate image mat. 57
  • 66. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software Figure 6.11. Open-CV Loaded File 1 void P r o f i l e P r o c e s s o r : : CorrectOrientation () { 3 //Check i f i t i s necessary to rotate the image i f ( ProfileMat . cols <ProfileMat . rows ) 5 transpose ( ProfileMat , ProfileMat ) ; emit SendCurrentOperation ( " Orientation Corrected " ) ; 7 ShowImage () ; } Listing 6.15. ProfileProcessor Correct Orientation This function and all others send the title of operation and also the image to "Main- Window" by means of two signals. Figure 6.12 depicts the image with corrected orientation. • Same as MATLAB’s procedure, next step is to invert colors. Listing 6.16 shows the usage of "bitwise_not" command that is defined in Open-CV library. void P r o f i l e P r o c e s s o r : : InvertColor () 2 { bitwise_not ( ProfileMat , ProfileMat ) ; 4 emit SendCurrentOperation ( " Colors Inverted " ) ; ShowImage () ; 6 } Listing 6.16. ProfileProcessor Invert Colors Figure 6.13 shows the result of color inversion. 58
  • 67. 6 – Image Pre-Processing Figure 6.12. Open-CV Orientation Corrected Figure 6.13. Open-CV Colors Inverted • Sharpen edges action is a bit different from what was done in MATLAB procedure. Listing 6.17 shows the steps of this action. void P r o f i l e P r o c e s s o r : : SharpenEdges () 2 { 4 GaussianBlur ( ProfileMat , ProfileMatAux , Size (0 , 0) , 3) ; addWeighted ( ProfileMat , 1.5 , ProfileMatAux , −0.5 , 0 , ProfileMatAux ) ; 59
  • 68. Iman AMIRTAHERI: Design RS485 Interface & DOT Code Scanner Software 6 ProfileMat=ProfileMatAux ; emit SendCurrentOperation ( " Edges Sharpened " ) ; 8 ShowImage () ; } Listing 6.17. ProfileProcessor Sharpen Edges This function finds a new image by applying Gaussian blur technique. Then this blurred new image is deducted from original one with a half weight. The resulting image is depicted on figure 6.14. Figure 6.14. Open-CV Edges Sharpened • As previously mentioned all of these functions use a function to send the image result. Listing 6.18 denotes the required commands to transform a mat object to a QPixmap object so that Qt will be able to show it as a background image of a label. 1 void P r o f i l e P r o c e s s o r : : ShowImage () { 3 QThread : : msleep (200) ; s t a t i c QVector<QRgb> sColorTable ; 5 // only create our c o l o r table once 7 i f ( sColorTable . isEmpty () ) { 9 f o r ( int i = 0; i < 256; ++i ) sColorTable . push_back ( qRgb( i , i , i ) ) ; 11 } 13 QImage ImageQImage ( ProfileMat . data , ProfileMat . cols , ProfileMat . rows , ProfileMat . step , QImage : : Format_Indexed8 ) ; ImageQImage . setColorTable ( sColorTable ) ; 60
  • 69. 6 – Image Pre-Processing 15 QPixmap ImageQPixmap = QPixmap : : fromImage ( ImageQImage ) ; 17 emit SendImage (ImageQPixmap) ; } Listing 6.18. ProfileProcessor ShowImage ShowImage function creates a color table only once to map a single color value to three values colors. This action is necessary since QImage objects needs to store color data in RGB format. When the QImage object is created, it can be converted to QPixmap object and sent to MainWindow. These actions were the implemented ones in this thesis. Since the main focus of it was on design and build of RS485 interface, other image processing actions were left for future studies. The most important point is that this software created an infrastructure for further completions which is easy to understand and interact. 61
  • 70. 62
  • 71. Chapter 7 Conclusion This thesis analyzed the importance of DOT code and created a device to make possible the communication between a laser sensor and PC. It also developed different computer programs to acquire and process data. In order to have the final device to be sold there are still following works to be done. • A gyroscope should be added to this device to compensate sudden movements of device otherwise the scanned data could be scrambled. • A cloud server should be procured and the corresponding program should be up- loaded on it. • A wireless module should be added to system such a way that it interacts with micro-controller and MAX485 on behalf of USB to Serial converter. • Further actions should be added to Qt plus Open-CV application. The substrate for this addition has been already done. So this work will be fast. • A PC application should be developed to just receive the final answer of cloud which is a DOT code and print it with its corresponding data on a paper. This application needs socket programming. Although this approach can be modified to have better and more accurate data, the variable illumination (structured lighting) method will be much more accurate since in this approach there is no need to move the device over tire and it can be done by hovering the device on DOT code for less than a second. 63
  • 72. 64