FPGA based co-design of a speed fuzzy logic controller applied to an autonomous car

Received Jun 4, 2021 Revised Jul 27, 2021 Accepted Aug 13, 2021 This paper invests in FPGA technology to control the speed of an autonomous car using fuzzy logic. For that purpose, we propose a co-design based on a novel fuzzy controller IP. It was developed using the hardware language VHDL and driven by the Zynq processor through an SDK software design written in C. The proposed IP acts according to the ambient temperature and the presence or absence of an obstacle and its distance from the car. The partitioning of the co-design tasks divides them into hardware and software parts. The simulation results of the fuzzy IP and those of the complete co-design implementation on a Xilinx Zynq board showed the effectiveness of the proposed controller to meet the target constraints and generate suitable PWM signals. The proposed hardware architecture based on 6-LUT blocks uses 11 times fewer logic resources than other previous similar designs. Also, it can be easily updated when new constraints on the system are to be considered, which makes it suitable for many related applications. Fuzzy computing was accelerated thanks to the use of digital signal processing blocks that ensure parallel processing. Indeed, a complete execution cycle takes only 7 us.


INTRODUCTION
Nowadays, technological advances in hardware and software have facilitated communication and exchange between several objects (machines, parts, peripherals and people) through networks of smart sensors and actuators. This makes them interactive and part of autonomous systems or/and applications. Among the autonomous systems that are currently emerging are electrical autonomous vehicles. They are massively used in several areas like transport, agriculture, residential, industrial maintenance, production, etc. Indeed, they allow reducing the air pollution rate, and improving the reliability of systems in which they integrate by accelerating the execution of required tasks. They also allow access to inaccessible areas by humans and explore them under severe conditions. In this context, many efforts have been made to improve their functionality, especially in detecting the various obstacles they may encounter, improving the trajectory following [1], [2] and especially their good decision-making [3]. Indeed, individual, and collective safety is a very important factor in the technologically open and so active world of today, especially with the emergence of the concept of the Internet of Things and autonomous vehicles. It requires the development of reliable and accurate applications in the detection of obstacles by autonomous vehicles to minimize the risk of accidents. we have only to modify their values in the algorithm (in the declaration section of signals). Then, the proposed architecture of the fuzzy controller is suitable for many other applications. The paper is structured as follows: Section 2 gives the Simulink model of the speed fuzzy logic controller to fix the fuzzy rules. Section 3 depicts the method used to develop the hardware/software design of the speed fuzzy logic control. The simulation and implementation results are reported in section 4. The last section is devoted to concluding the paper.

MODELLING OF THE FUZZY LOGIC CONTROLLER USNG MATLAB/SIMULINK ENVIRONMENT
The fuzzy logic controller considers the speed error, the surrounding temperature, and the distance from an obstacle through a sonar sensor. The derivative speed error is also considered as an input for the controller in order to have information about the reaction speed of the system. The output signal is the speed expressed in (rpm). The inputs and outputs of the controller, the fuzzy rules and the computation method of the control signal applied to the car are subsequently performed.

Principle of the fuzzy logic control
The fuzzy logic control consists of three major modules: the fuzzifier, the inference, which is driven by the fuzzy rules, and the defuzzifier, as shown in Figure 1. The fuzzifier encodes the crisp measured values of each system parameter (ambient temperature, speed and its derivative errors, and sonar) into a fuzzy term using the given membership functions. The inference module allows identifying the subset of fuzzy rules that can be fired and draw adequate fuzzy consequences [17]. Herein, we have used the Mamdani Min-Max process. Finally, the defuzzifier decodes the fuzzy variable (speed control signal) and computes the corresponding crisp value using the center of gravity method. These modules are described next.

Model of the speed fuzzy controller 2.2.1. Fuzzifier
For all fuzzy input variables, as well as the fuzzy output, we have chosen triangular membership functions, which are illustrated in Figure 2. Furthermore, the linguistic terms and the corresponding universes of the discourse of each input and output variable are summarized in Table 1.
For triangular membership functions, the determination of the truth degree μ of each linguistic term is carried out using (1) or (2) according to having an increasing or decreasing slope, respectively, as shown in  (1)

Fuzzy rules
To thoroughly control the behavior of the car, we have defined 44 rules that mainly consist of: (i) If the ambient temperature is hot, then the speed is very low. (ii) If the sonar is very near or near, then the speed is very low or low, respectively. For a cold or moderate temperature, the rules are given in Table 2. It is worth noting that when the error speed is negative and the derivative one is zero, the speed is low or very low when the obstacle (referred by the sonar signal) is far or very far, respectively. However, when both the error speed and the derivative one is positive, the speed is high or very high according to the distance from the obstacle respectively far or very far.
The use of the 'and' operator means that the truth degree μ of each output linguistic term (verylow, low, moderate, high, veryhigh) takes the lowest truth degree of the input linguistic terms according to the verified one or many rules.

Defuzzifier
The crisp value of the control signal xspeed is determined using the center of gravity method according to (3).

System model
Before carrying out the hardware/software design of the proposed fuzzy controller, we have tested it by applying a car plant [26] according to our car dimensions, shown in Figure 5. It is a discrete transfer function given in (4). The sample time is equal to 0.2 s.
where u  is the control signal that is considered as throttle control and used to increase or decrease the engine driver force [26] and v  is the car speed in cm/s. Figure

HARDWARE/SOFTWARE CO-DESIGN OF THE FUZZY LOGIC CONTROLLER
In this section, we depict a synthesizable hardware and software co-design of the proposed fuzzy controller dedicated to being applied to the autonomous car, which is presented in Figure 5. Herein, the control signal is considered as a speed expressed in rpm, as mentioned earlier. The design generates three PWM signals that will drive the car left and right wheels, as well as the servo motor that controls the front steering. However, the steering control is already achieved [27] and herein used. Figure 5. The autonomous car [27] The partitioning of the application tasks consists of dividing them into a hardware part and a software part. In the hardware part, we developed the hardware architecture of the fuzzy controller IP and used the IPs of the temperature and sonar sensors. However, the software part uses the Zynq processor to accede the registers of sensors to read the distance and ambient temperature values. It also sends them to the fuzzy controller, which computes the car speed according to the control signal and generates the corresponding PWM signals.

Hardware design
The hardware design was developed using VHDL and some specifications were made to efficiently program the fuzzy algorithm.

Specifications of the programming process
To get a synthesizable code, we adopt the hexadecimal format to present the data values. Thus, the signals have the type std_logic_vector where their width is chosen as follows: − 1 byte for signal positive values. − 2 bytes for signal, which can have negative values. − 1 byte for the output signal. Therefore, the truth degrees are between 0 and 255.
In addition, we use the min-max method to compute the signals of the fuzzy output linguistic terms (verylow, low, etc.), as well as the center of gravity to compute the control signal. This latter is used to determine the PWM signal that drives the car and then its corresponding duty cycle. Indeed, each output linguistic term has a specific duty cycle width, as reported in Table 3. The system clock frequency and those of the PWM signal are fixed to 50 MHz and 100 kHz, respectively. Therefore, a counter is used to count up to 500 to reach a PWM period.

Hardware architecture of the fuzzy controller
Here, the objective is to prepare an IP that describes the operation of the fuzzy controller. This was accomplished by writing the fuzzy controller's VHDL code based on the Mamdani method. The entity block of the fuzzy controller, as well as its architecture, are shown in Figure 6. It is worth noting that each block consists of a separate VHDL module, and all of them are interconnected using the 'port map' instruction. The speed error value (signal Errsp) is obtained using a subtractor, which receives the computed speed and includes the target speed as an internal signal. Furthermore, the derivative speed error value (signal Derrsp) is considered as the difference between the current speed error value and the previous one using a finite state machine. It is presented in Figure 7.
where r is the wheel ray and is equal to 3 cm. It is worth noting that the car speed can maximum reach 90 cm/s for a control value of 300 rpm. As shown in Table 3, each PWM generation block generates a different PWM corresponding to one of the output linguistic terms to which the output signal 'PWM' of the fuzzy controller is assigned. For instance, this signal is assigned to PWMl when the speed should be low. Each Duty signal (Dutyvl, Dutyl, Dutym, Dutyh, and Dutyvh) indicates the number of cycles for which the corresponding PWM signal becomes high. It is worth noting that the PWM period corresponds to 500 cycles. We assume that the maximum speed rate corresponds to only 96 % for security matters.
The algorithm developed in the 'Fuzzy controller' block consists of the following successive steps: − Defining the values of the linguistic terms in hexadecimal format.
For example, the first two values of the sonar sensor are defined as follows: Determination of the truth degree of each input linguistic term according to its membership function. For instance, the truth degree 'dvnear' of the linguistic term 'vnear' is determined using the following VHDL code: In this case, the fuzzy output of the controller should be assigned to the linguistic term 'verylow'.

Complete hardware design of the speed fuzzy control
The design of the speed controller was performed using the Xilinx Vivado Design Suite 2018. It includes the IP of the fuzzy controller (myipFLC_0), the Zynq processor, and the sensors, as shown in Figure  8. The fuzzy controller starts operating when the enable signal is high. İt is an asynchronous reset active low (enable <= not rst). The ambient temperature and the distance from the sonar sensor are computed and read by the Zynq processor at the level of its 32-bit data registers through C files. The development of these files was performed in the software section. Then, they are sent to the fuzzy controller through the AXI bus to generate the suitable PWM signal that is determined by the 'Fuzzy controller' block presented in Figure 6. This signal is driven to each of the output PWM signals motor_left_pwm_out, servo_pwm_out, and motor_right_pwm_out, which drive the car movement. Herein, the Vivado design suite assigns a 32-bit range of addresses to registers of each IP. Therefore, the Zynq processor can act on the fuzzy controller and the sensors by accessing their registers through the AXI bus. For the ZedBoard, four slave registers are defined for the fuzzy IP.

Software design
The software design consists of developing the C files. They allow reading the temperature value, as well as, calculating the distance from the obstacle according to the PWM signal generated by the sonar sensor. Indeed, a duty width of 147 us corresponds to 2.54 cm. No obstacle is detected if the distance exceeds 6.5 m. We also added various messages that the vehicle must generate to inform us about the state of its operation and the decisions that were made, as explained in Figure 9.  Figure 9. Software design tasks to send data to the fuzzy controller through the Zynq processor and to get feedback from the car This step is performed using the Xilinx SDK tool after exporting the hardware and the bitstream file (.bit). After that, the building of the SDK project allows the generation of the executable file (.elf). This latter allows applying the software application to the exported hardware after loading the corresponding bitstream into the FPGA circuit to configure it.

Simulation results of the Simulink model
The simulation was carried out for a target speed variation from 30 to 40 cm/s, an ambient temperature equal to 25 °C, and an obstacle distance of 6 m. In this case, the car reaches the target speed, as shown in Figure 10. Figure 10. Car speed behavior when the ambient temperature is moderate, and the obstacle is far When this distance decreases by 1 m and then by 4 m after 30 s, the car speed also decreases from 40 to 27 and 10 cm/s, respectively, as shown in Figure 11(a) and Figure 11(b). In cases where the temperature becomes hot (40 °C), the speed decreases to 8.5 cm/s and no more follow-up of the target speed, as shown in Figure 11(c). The same behavior is reached when, at the beginning, the obstacle distance is 2 m near or very near (1 m) from the car. However, the speed starts by increasing at the beginning seen that the ambient temperature is moderate. Then, the speed was immediately decreased due to the very near distance between the obstacle and the car. This scenario is illustrated in Figure 11

Simulation results of the hardware design
The fuzzy logic algorithm developed in VHDL was tested for several scenarios during 20 us, which correspond to two complete cycles (or control periods). The scenarios prove to achieve the desired behavior. Indeed, Figure 12 illustrates the case of a target speed equal to 40 cm/s, an ambient temperature equal to 25 °C, and an obstacle distance of 6 m from the sonar sensor. Hence, only the moderate truth degree is active, as well as those of the far/veryfar fuzzy variables of the sonar. Consequently, no constraints are imposed on the car, which should accelerate to reach the target speed thanks to the high state of the generated PWM signal.
As shown in Figure 12, the speed control is high. It varies between 110 and 170 rpm according to the values of the input fuzzy variables. Therefore, only the output truth degrees of the fuzzy terms moderate, far, and veryfar are active. Furthermore, the speed average value is about 42 cm/s, which meets with the target speed. Indeed, the error speed and the derivative one is the output truth degrees of the fuzzy terms moderate, far, and veryfar are both low. Nonetheless, Figure 13 illustrates the cases for which the car should brake or stop to avoid the collision with the obstacle. Indeed, the scenario I shows that the control signal decreased to 2 rpm corresponding to the output fuzzy set verylow. It is due to the hot ambient temperature (the truth degree of the hot variable is active) despite that the obstacle is far. Thus, the PWM signal is low, and then the speed decreased considerably, and the car stops. Moreover, scenario II shows that for a moderate temperature, the control signal decreases as long as the obstacle is closer to the sensor. It is also considered low/verylow. Then, the PWM signal is inhibited, and the car speed drops in order to avoid the collision with the obstacle.
As for the results achieved by the fuzzy model developed on MATLAB, the operation of the hardware speed fuzzy controller also satisfies the required specifications.

Implementation results
The implementation of the proposed co-design was carried out on the ZedBoard 7z020-clg484 while choosing the clock frequency 50 MHz. After generating and verifying the schematic design of the hardware design mentioned in Figure 8, we executed the synthesis and implementation processes, which allow placing the hardware architecture in the ZedBoard one, as shown in Figure 14. The Zynq processor I/O (here FIXED_IO) is routed to the multiplexed I/O (MIO)). Figure 14. Implementation design of the speed fuzzy control Moreover, Table 4 summarizes the postimplementation results of the hardware design that fits into the selected SoC's logic, leaving enough resources available for further design applications or improvements. As shown in Table 4, DSP blocks were used to perform the calculation operations of the membership functions, the truth degrees of fuzzy inputs and outputs, the crisp output, and the car speed. They allow reducing the calculation time thanks to parallel processing. An output value is computed at each clock cycle. However, as soon as the sensors' measurements are available, it is necessary to run the application for at least 7 us to complete the execution of the PWM blocks used in the IP of the fuzzy controller. We also note that the proposed design does not require a lot of hardware resources compared to previous similar hardware fuzzy logic designs, such as in [17]. For instance, the utilization of the 6-LUTs is reduced 11 times compared to those used in this reference. This is achieved thanks to the optimization of the fuzzy algorithm and the high performance of the FPGA circuit. The power consumption of the chip is about 1.68 W of which 96 % is consumed by the Zynq processor.
Moreover, the reconfiguration of the FPGA is achieved by generating the bitstream file, which was exported with the hardware design to the SDK environment, where we developed the software design and then built it to get the executable file (.elf). Indeed, Figure 15 and Figure 16 show the messages sent by the car using the UART peripheral according to values of sonar and ambient temperature sensors. These values are sent to the fuzzy controller through the slave registers 1 and 3, respectively, in hexadecimal format. Furthermore, Figure 17 illustrates the ZedBoard configuration when no constraints are imposed on the car (the ambient temperature and the obstacle distance are equal to 25 °C and 6 m, respectively). In fact, the enable switch is enabled, then, the PWM signals are active seen that the car does not encounter constraints. Figure 17. ZedBoard configuration and result obtained

CONCLUSION
In the present paper, we have developed a hardware/software co-design to control the speed of an autonomous car using FPGA technology and a fuzzy logic controller. This latter is performed by considering the variation of the ambient temperature and the distance from a given obstacle. It makes the car brake or stop for hot temperature or a near obstacle. Otherwise, the controller ensures reaching the target speed. The simulation and implementation results showed that these specifications were achieved. In addition, the novel hardware/software solution allowed accelerating the fuzzy computing. It is due to the optimized fuzzy algorithm developed and the availability of DSP blocks on the ZedBoard that perform the parallel processing. Hence, a complete execution cycle takes only 7 us. The proposed solution is also considered flexible thanks to the Zynq processor. This latter can be configured without modifying the hardware architecture. Furthermore, the utilization rate of hardware resources is considered lower than other previous hardware solutions. In future work, we aim to build an embedded Linux solution to detect and recognize a given obstacle that encounters the car.