=Paper= {{Paper |id=Vol-2694/paper2 |storemode=property |title=Fuzzy system as a method of controlling LEGO Linefollower vehicle using C# programming language |pdfUrl=https://ceur-ws.org/Vol-2694/p2.pdf |volume=Vol-2694 |authors=Krzysztof Grzesica,Jakub Wadas |dblpUrl=https://dblp.org/rec/conf/system/GrzesicaW20 }} ==Fuzzy system as a method of controlling LEGO Linefollower vehicle using C# programming language== https://ceur-ws.org/Vol-2694/p2.pdf
Fuzzy system as a method of controlling LEGO
Linefollower vehicle using C# programming language
Krzysztof Grzesicaa , Jakub Wadasa
a Faculty of Applied Mathematics, Silesian University of Technology, Kaszubska 23, 44-100 Gliwice



                                          Abstract
                                          In this article we present our implementation of intelligent system developed for a LEGO robot. We have built a robot which
                                          is using sensors to trace the line and follow it. The model of tracing is based on fuzzy rules, which have been done to follow
                                          the shape of a black line on the white background. The system was implemented using C# language for LEGO Mindstorm
                                          elements. Results of our experiments show that the robot is able to follow various shapes of black lines.

                                          Keywords
                                          LEGO robot, Line detection, Fuzzy rules


1. Introduction                                                                                                    lect such libraries and optimize them for robotic con-
                                                                                                                   structions. Also some approaches are based on frame-
In many fields of industry and production various ro-                                                              works, as proposed in [5] that frameworks are also
botics are applied to help in situations where the work                                                            possible for other constructions based on arduino el-
may be dangerous for humans or there is required high                                                              ements, which provide similar possibilities of robotic
precision at work. To start the research in such field                                                             constructions. All these what we want to create de-
we have selected LEGO mindstorm elements. This rob-                                                                pends on our ability to develop the construction and
ot model is easy to develop just by using simple bricks                                                            than to implement the control software. An interest-
in many shapes from the set. The set also provides                                                                 ing discussion was given by [6, 7].
a programmable electronics with a variety of sensors.                                                                 The other aspect of robotic models is to select a pro-
On the other hand such approach is widely presented                                                                per decision support model. In many IoT constructions
in other literature, what gave us an inspiration to de-                                                            we can find various approaches. In [8] was proposed
velop our idea.                                                                                                    how to combine neural networks with rules in a form
   In [1] a model of LEGO blocks was used to work                                                                  of soft set table. While in [9] the fuzzy rule system
with human gestures, where sensors were used to read                                                               was implemented with neural networks to work in a
gestures and therefore take actions in mindstorm ele-                                                              smart house environment. It is also very popular to
ments. In [2] was presented how to use such LEGO                                                                   develop fuzzy rules working on images, as discussed
models to help in first contact with machine intelli-                                                              in [10]. Mechanical vehicle constructions also very
gence. Authors describe good practices when using                                                                  often benefit from artificial intelligence, both at con-
LEGO Mindstorm as a platform for programming ar-                                                                   struction and simulation level, as proposed in [11, 12,
tificial intelligence systems. As presented in [3] these                                                           13, 14].
models also support creative thinking, especially when                                                                Our approach is using C# language to implement
we develop new algorithms that must be programmed                                                                  control system based on fuzzy rules to first detect and
in a special way accepted by the LEGO Mindstorm                                                                    than follow the line. The LEGO Mindstorm robot is
platform. There are various approaches to use pro-                                                                 using two servo-motors as accelerators of wheels con-
gramming languages in developing such robots.                                                                      trolled by our developed artificial intelligence to fol-
   However the most necessary is tu use an optimal                                                                 low the line. our experiment shows that our construc-
library, which will support all necessary function that                                                            tion is well defined and can follow even the complex
provide optimal connection and configuration between                                                               lines on the board.
elements of the robot. In [4] was discussed how to se-

SYSTEM 2020: Symposium for Young Scientists in Technology,                                                         2. Model
Engineering and Mathematics, Online, May 20 2020
" krzygrz684@student.polsl.pl (K. Grzesica);                                                                       In this section we will describe how we have developed
jakuwad985@student.polsl.pl (J. Wadas)                                                                             the model both from programming side and thinking

                                    ยฉ 2020 Copyright for this paper by its authors. Use permitted under Creative
                                                                                                                   model.
                                    Commons License Attribution 4.0 International (CC BY 4.0).
 CEUR
 Workshop
 Proceedings
               http://ceur-ws.org
               ISSN 1613-0073       CEUR Workshop Proceedings (CEUR-WS.org)
Figure 1: Vehicle construction



2.1. Project assumptions
The aim of this project was to build an artificial in-
telligence system based on fuzzy logic, which would
steer the vehicle used in LEGO Line-follower compe-
tition. Based on readings, the system must decide how
to change the speed of each engine, so that:
                                                               Figure 2: The bottom of the vehicle
    โ€ข The vehicle does not go off the road (A moment
      when a black line is not between vehicleโ€™s wheels
      is considered going off the road)                                                    100
                                                                                 ๐‘ƒ๐‘… =                              (2)
    โ€ข The vehicle goes as fast as possible                                            ๐‘Š ๐ถ๐‘… โˆ’ ๐ต๐ถ๐‘…
                                                               Where ๐‘Š ๐ถ๐ฟ and ๐ต๐ถ๐ฟ are accordingly, maximum and
  The robot is connected to computer via Bluetooth.            minimum reading value of left sensor. ๐‘Š ๐ถ๐‘… and ๐ต๐ถ๐‘…
The robot sends raw readings and data to computer              are maximum and minimum reading values of right
where all necessary calculations are being performed.          sensor. These values are obtained during setup con-
Then, based on calculation results appropriate com-            figuration. Reading output is determined using the fol-
mands from the computer are sent to robot.                     lowing formula:

2.2. Robot Construction                                              ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐ฟ = |(๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘–๐‘›๐‘”๐ฟ๐‘’๐‘“ ๐‘ก โˆ’ ๐ต๐ถ๐ฟ) โˆ— ๐‘ƒ๐ฟ โˆ’ 100|     (3)

To accomplish that task we have built a vehicle using               ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘… = |(๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘–๐‘›๐‘”๐‘…๐‘–๐‘”โ„Ž๐‘ก โˆ’ ๐ต๐ถ๐‘…) โˆ— ๐‘ƒ๐‘… โˆ’ 100|      (4)
LEGO parts. The robot is based on intelligent EV3
                                                               Where ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘–๐‘›๐‘”๐ฟ๐‘’๐‘“ ๐‘ก and ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘–๐‘›๐‘”๐‘…๐‘–๐‘”โ„Ž๐‘ก are respectively
brick, it is equipped with two color sensors, which
                                                               raw vuales returned by left and right color sensor.
measure the level of reflected light they emit. The ve-
                                                               ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐ฟ and ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘… take values from 0 to 100, where 0
hicle is driven by two independent engines which di-
                                                               means total white, 100 - pure black.
rectly spin the wheels. The actual look of vehicle can
                                                                  After that, the resultant reading value meaning the
be seen in Fig. 1 and Fig. 2.
                                                               inclination of the road is calculated accordingly to the
                                                               following formula:
3. Mathematical Model                                                                    ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘… โˆ’ ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐ฟ
                                                                             ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘–๐‘›๐‘” =                             (5)
3.1. Normalization                                                                            10

Firstly, we must normalize the readings from color sen- If both color sensors went off the road the variable
sors to negate the hardware differences. To do that we ๐‘Ÿ๐‘’๐‘Ž๐‘‘๐‘–๐‘›๐‘” i set to 10 or -10 depending on the side of road
must first calculate parameters ๐‘ƒ๐ฟ and ๐‘ƒ๐‘… accordingly the sensors are located.
as:
                            100
                   ๐‘ƒ๐ฟ =                              (1)
                        ๐‘Š ๐ถ๐ฟ โˆ’ ๐ต๐ถ๐ฟ


                                                          10
Figure 3: Reading membership functions                           Figure 5: Application window before configuration




                                                                 Figure 6: Application window after configuration
Figure 4: Speed membership functions


                                                                 Table 1
3.2. Fuzzyfication                                               Speed base of rules
To determine grade of membership Bell shaped and
Linear functions are used. The formulas are as follows:            Left/Right    rear       slow      medium          fast
                {                                                     rear       rear       slow       slow           slow
                        1
                   1+| ๐‘ฅโˆ’๐‘
                                ,๐‘ฆ โฉฝ๐‘ฅ โฉฝ๐‘ง                              slow       slow       slow      medium         medium
                                                    (6)
                            2๐‘
         ๐ต๐‘’๐‘™๐‘™ =         ๐‘Ž |
                                                                    medium       slow      medium     medium          fast
                   0            ,๐‘ฅ <๐‘ฆโˆจ๐‘ฅ >๐‘ง
                                                                       fast      slow      medium      fast           fast
                           {
                               1 ,๐‘ฅ <0
               ๐ฟ๐‘–๐‘›๐‘’๐‘Ž๐‘Ÿ =                             (7)
                               0 ,๐‘ฅ โฉพ0
Where ๐‘Ž, ๐‘ are parameters. Variables ๐‘ฆ and ๐‘ง are end-               The results are being grouped by the rules they cor-
points of interval. Variable ๐‘ stands for center of func-        respond with and for each of rule we choose the biggest
tion. In our project ๐‘ = 2.5 and ๐‘Ž = 2.5 in all but              value. Lets assume that the biggest value for rule slow
one membership function. The membership function                 is ๐‘ข๐‘ค , for medium - ๐‘ข๐‘š and for fast - ๐‘ข๐‘“ . The final
Straight uses ๐‘Ž = 1.5. The reading membership func-              resultant speed is obtained by using center of gravity
tions can be seen in Fig.3, while speed membership               method defined as:
functions in Fig. 4.                                                                   ๐‘ข๐‘ค โˆ— ๐‘๐‘  + ๐‘ข๐‘š โˆ— ๐‘๐‘š + ๐‘ข๐‘“ โˆ— ๐‘๐‘“
   Then, the resultant vehicle speed composed from                        ๐‘ ๐‘๐‘’๐‘’๐‘‘ =                                         (8)
                                                                                              ๐‘ข๐‘ค + ๐‘ข๐‘š + ๐‘ข๐‘“
separate engines speeds is being calculated. In order to
determine it we calculate each engineโ€™s grade of mem-            where ๐‘๐‘  = 10, ๐‘๐‘š = 50 and ๐‘๐‘“ = 90 are coefficients of
bership for all of speed membership functions and ac-            speed rules.
cordingly to fuzzy rules base shown in Tab. 1 we con-
duct necessary calculations.



                                                            11
Table 2
Inclination base of rules

      Speed/Reading HL EL S ER HR
           fast           HL HL S HR HR
         medium           HL    EL S ER             HR
          slow            HL    EL S ER             HR
        HL - Hard Left, EL - Easy Left, S - Straight,
             ER - Easy Right, HR - Hard Right
                                                                       Figure 7: Reading method


3.3. Deffuzyfication                                                                                     ๐‘–๐‘›๐‘
                                                                                         ๐‘…๐‘–๐‘”โ„Ž๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ =                        (19)
The next step is to calculate the inclination, which con-                                                 4
sists of resultant speed and resultant reading values.                 where ๐‘–๐‘›๐‘ is the inclination value calculated before.
In order to determine it we calculate resultant speed                  In case of Easy Left and Easy Right if the ๐‘–๐‘›๐‘ value is
grade of membership for all of speed membership func-                  lesser than -50 or greater than 50, then it is set to 50.
tions and resultant reading grade of membership for
all of reading membership functions. Then, accord-
ingly to fuzzy rules base shown in Tab. 2 we conduct                   4. Implementation
necessary calculations. The results are being grouped
by the rules they correspond with and for each of rule                 Let us now present the software we have done.
we choose the biggest value. Lets assume that the big-
gest value for rule HL is ๐‘ข1 , for EL - ๐‘ข2 , for S - ๐‘ข3 , for ER       4.1. Application
- ๐‘ข4 and for HR - ๐‘ข5 . The final inclination is obtained
                                                                       Based on the mathematical model described above, we
by using center of gravity method defined as:
                                                                       have created a robot control application. The applica-
                                                                       tion has a very simple graphical user interface, which
  ๐‘–๐‘›๐‘ =
         ๐‘ข1 โˆ— ๐‘1 + ๐‘ข2 โˆ— ๐‘2 + ๐‘ข3 โˆ— ๐‘3 + ๐‘ข4 โˆ— ๐‘4 + ๐‘ข5 โˆ— ๐‘5               makes it user-friendly. In Fig. 5 and Fig. 6 application
                     ๐‘ข1 + ๐‘ข2 + ๐‘ข3 + ๐‘ข4 + ๐‘ข5                            window can be seen.
                                                        (9)
   where ๐‘1 = โˆ’100, ๐‘2 = โˆ’50, ๐‘3 = 0, ๐‘4 = 50, ๐‘5 = 100
are coefficients of inclination rules accordingly for HL,              4.2. Program code
EL, S, ER and HR.                                                      The program is written in ๐ถ#. In addition to the stan-
   Finally, the rule with the greatest inclination grade               dard .๐‘ ๐ธ๐‘‡ libraries, the program uses the ๐ฟ๐‘’๐‘”๐‘œ.๐ธ๐‘ฃ3
of membership value is chosen. Depending on it, the                    library for communication between the computer and
engines speeds are calculated and set. For Hard Left                   the robot. The program has been divided into classes
(10) and (11), for Easy Left (12) and (13), for Straight               and appropriate methods.
(14) and (15), for Easy Right (16) and (17) and for Hard
Right (18) and (19).                                            Reading method This method is responsible for the
                                  ๐‘–๐‘›๐‘                           normalization of color sensor data. This method also
                     ๐ฟ๐‘’๐‘“ ๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ =                          (10) serve as a precaution against loosing the route by a
                                   4
                                                                robot. Method code can be seen in Fig. 7.
                    ๐‘…๐‘–๐‘”โ„Ž๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = ๐‘–๐‘›๐‘                       (11)
                  ๐ฟ๐‘’๐‘“ ๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = ๐‘–๐‘›๐‘ โˆ— 1.5                   (12) HowTheRouteRuns method This method is a frag-
                   ๐‘…๐‘–๐‘”โ„Ž๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = ๐‘–๐‘›๐‘ โˆ— 2                    (13) ment of the fuzzy system. It combines the route incli-
                                                                nation with the current vehicle speed and decides how
                     ๐ฟ๐‘’๐‘“ ๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = 100                      (14) to react based on that data. A piece of the method code
                    ๐‘…๐‘–๐‘”โ„Ž๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = 100                       (15) is in Fig. 8.
                   ๐ฟ๐‘’๐‘“ ๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = ๐‘–๐‘›๐‘ โˆ— 2                    (16)
                                                                Robot class This is the class which object repre-
                  ๐‘…๐‘–๐‘”โ„Ž๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = ๐‘–๐‘›๐‘ โˆ— 1.5                   (17) sents vehicle instances in the program. The most im-
                                                                portant class fields and properties representing the state
                     ๐ฟ๐‘’๐‘“ ๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ = ๐‘–๐‘›๐‘                      (18)



                                                                  12
                                                              Figure 11: ChooseTurn method




Figure 8: HowTheRouteRuns method




                                                              Figure 12: TurnHard method


Figure 9: Robot class




Figure 10: Go method



of the object can be seen in Fig. 9. ๐ต๐‘Ÿ๐‘–๐‘๐‘˜ class ob-
ject, which belongs to the ๐ฟ๐‘’๐‘”๐‘œ.๐ธ๐‘ฃ3 library represents Figure 13: Test route
the LEGO EV3 brick. ๐ถ๐‘œ๐‘›๐‘›๐‘’๐‘๐‘ก๐‘’๐‘‘ property represents
the state of connection between the program and the
robot. Fields ๐‘™๐‘’๐‘“ ๐‘ก๐‘†๐‘’๐‘›๐‘ ๐‘œ๐‘Ÿ and ๐‘Ÿ๐‘–๐‘”โ„Ž๐‘ก๐‘†๐‘’๐‘›๐‘ ๐‘œ๐‘Ÿ store values
                                                       The method is responsible for choosing the direction
obtained from color sensors. Fields _๐‘™๐‘’๐‘“ ๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ and
                                                       in which the robot should go. The code for this method
_๐‘Ÿ๐‘–๐‘”โ„Ž๐‘ก๐‘€๐‘œ๐‘ก๐‘œ๐‘Ÿ store current motors speeds.
                                                       is shown in Fig. 11.

Go method This method belongs to the ๐‘…๐‘œ๐‘๐‘œ๐‘ก class.
                                                              TurnHard method This method also belongs to the
The method is responsible for the robotโ€™s movement.
                                                              ๐‘…๐‘œ๐‘๐‘œ๐‘ก class. It is responsible for updating the variables
It contains loop in which the fuzzy system makes cal-
                                                              representing the current motors power. The full code
culations, decides about the next move and finally ma-
                                                              of the method can be seen in Fig. 12. The ๐‘…๐‘œ๐‘๐‘œ๐‘ก class
kes that move by setting the power of motors. The full
                                                              also has ๐‘‡๐‘ข๐‘Ÿ๐‘›๐ธ๐‘Ž๐‘ ๐‘ฆ and ๐บ๐‘œ๐‘†๐‘ก๐‘Ÿ๐‘Ž๐‘–๐‘”โ„Ž๐‘ก methods whose
code of the method can be found in Fig. 10.
                                                              operations are analogous to the ๐‘‡๐‘ข๐‘Ÿ๐‘›๐ป ๐‘Ž๐‘Ÿ๐‘‘ method.
ChooseTurn method This method belongs to the
๐‘…๐‘œ๐‘๐‘œ๐‘ก class. It is called in the loop described above.



                                                         13
Figure 14: A part of a test ride



5. Tests                                                               Interactive Learning Environments 27 (2019)
                                                                       293โ€“306.
In order to test how the vehicle performs we created a             [3] N. L. Fanchamps, L. Slangen, P. Hennissen,
test route (Fig. 13). The route is a 19-millimeter-wide                M. Specht, The influence of sra programming
black line on a white background. It is quite compli-                  on algorithmic thinking and self-efficacy using
cated due to a lot of 90โ—ฆ angle turns, it does not contain             lego robotics in two types of instruction, Inter-
crossroads though. The vehicle runs through the route                  national Journal of Technology and Design Edu-
precisely but with little speed. It is worth mentioning                cation (2019) 1โ€“20.
that the system is incredibly sensitive to any changes             [4] A. Spanรฒ, A. Cortesi, Legodroid: A type-driven
in lightning and the non-uniformity of it. A part of a                 library for android and lego mindstorms interop-
test ride can be seen in Fig. 14.                                      erability, Sensors 20 (2020) 1926.
                                                                   [5] J. Vega, J. M. Caรฑas, Pybokids: An innova-
                                                                       tive python-based educational framework using
6. Conclusion                                                          real and simulated arduino robots, Electronics 8
Because of poor functioning of some of ๐ฟ๐‘’๐‘”๐‘œ.๐ธ๐‘‰ 3 li-                   (2019) 899.
brary functions that our project was based on, the re-             [6] J. Wang, X. Du, H. Wang, Research & imple-
sults were not as good as we had imagined. Commands                    mentation of multitasking lego robots, in: 2019
must have been initialized for a specified time amount                 IEEE 4th International Conference on Advanced
which led to delays and unstable movement of vehicle.                  Robotics and Mechatronics (ICARM), IEEE, 2019,
What is more, in order to work properly the program                    pp. 655โ€“659.
had to use function ๐ท๐‘’๐‘™๐‘Ž๐‘ฆ(), which stops the robot                 [7] R. Giuliano, G. Cardarilli, C. Cesarini, L. Di Nun-
suddenly for few milliseconds.                                         zio, F. Fallucchi, R. Fazzolari, F. Mazzenga, M. Re,
   Considering that ๐ฟ๐‘’๐‘”๐‘œ.๐ธ๐‘‰ 3 library has not been up-                 A. Vizzarri, Indoor localization system based on
dated for 7 years and itโ€™s author officially abandoned                 bluetooth low energy for museum applications,
the project, rewriting the whole project to ๐‘…๐‘œ๐‘๐‘œ๐‘ก๐ถ lan-                Electronics (Switzerland) 9 (2020) 1โ€“20.
guage seems to be the best way to improve programโ€™s                [8] M. Woลบniak, D. Poล‚ap, Soft trees with neural
performance, allowing the program to run on EV3 brick                  components as image-processing technique for
itself. Apart from solving problems mentioned above,                   archeological excavations, Personal and Ubiqui-
that approach would also terminate problems connect-                   tous Computing (2020) 1โ€“13.
ed with Bluetooth connection latency.                              [9] M. Woลบniak, D. Poล‚ap, Intelligent home systems
                                                                       for ubiquitous user support by using neural net-
                                                                       works and rule based approach, IEEE Transac-
References                                                             tions on Industrial Informatics (2019).
                                                                  [10] G. Capizzi, G. Lo Sciuto, C. Napoli, D. Polap,
 [1] L. I. Kovรกcs, Gesture-driven lego robots, Acta                    M. Woลบniak, Small lung nodules detection based
     Universitatis Sapientiae, Informatica 11 (2019)                   on fuzzy-logic and probabilistic neural network
     80โ€“94.                                                            with bio-inspired reinforcement learning, IEEE
 [2] ร. Martรญnez-Tenor, A. Cruz-Martรญn, J.-A.                          Transactions on Fuzzy Systems 6 (2020).
     Fernรกndez-Madrigal, Teaching machine learning                [11] J. T. Starczewski, P. Goetzen, C. Napoli, Triangu-
     in robotics interactively: the case of rein-                      lar fuzzy-rough set based fuzzification of fuzzy
     forcement learning with legoยฎ mindstorms,



                                                             14
     rule-based systems, Journal of Artificial Intel-
     ligence and Soft Computing Research 10 (2020)
     271โ€“285.
[12] M. Woลบniak, D. Poล‚ap, Hybrid neuro-heuristic
     methodology for simulation and control of dy-
     namic systems over time interval, Neural Net-
     works 93 (2017) 45โ€“56.
[13] G. Capizzi, F. Bonanno, C. Napoli, Hybrid neural
     networks architectures for soc and voltage pre-
     diction of new generation batteries storage, 2011,
     pp. 341โ€“344.
[14] F. Bonanno, G. Capizzi, C. Napoli, Some remarks
     on the application of rnn and prnn for the charge-
     discharge simulation of advanced lithium-ions
     battery energy storage, 2012, pp. 941โ€“945.




                                                          15