A CONTROLLER FOR BRUSHLESS DIRECT CURRENT ELECTRIC MOTORS PART 2: SOFTWARE

A universal controller for brushless direct current (BLDC) motors was designed in the presented article. The system is controlled from the user console where operating parameters are set by the user. Signals are transmitted by cables to microcontrollers which control and monitor electric motors. Microprocessors communicate via a data bus. The controller contains the user console module and the motor control module. The user console module generates commands, and motors are controlled and monitored by the control module. Motor control modules operate independently, and each brushless motor has a dedicated control module. Brushless motors can be controlled in bipolar or unipolar mode. The control method is selected by the operator. The user console and motor controllers communicate via the I2C bus.


Introduction
The article consists of two parts. The first part describes the electronic and electrical design of the motor controller, and the second part presents the applied software. The described controller for brushless motors has been designed and patented by Z. Syroka and K. krajewSki (patent No. P431380, filing date: 4 October 2019).
Industrial machines, vehicles, power tools and household appliances have motor drive elements that power mechanical components. The popularity of electric cars has been increasing in the 21 st century. Batteries and motors are the key components of electric vehicles. Batteries are positioned at the bottom of the chassis to lower the vehicle's center of gravity. The motor is located in the front or at the back of the vehicle (Tesla Model S). The motors in electric cars are powered by direct (DC) or alternating current (AC). Brushless motors are one type of DC motors. Rapid advances in electrical and power engineering have introduced novel components to brushless motors, including thyristors, diodes and MOSFET transistors. Electronic subassemblies can turn power supply on and off hundreds or even thousands of times within one second. In electric bicycles, a brushless motor is placed inside the wheel hub. Power is transferred to the wheel, which reduces the physical effort associated with pedaling by 50%.
This article discusses a controller for brushless DC motors that are mounted inside the wheels of land vehicles. The motor is controlled by the user via the user console. The user can change motor speed, driving direction and deploy the electric braking system. The controller controls the steering system, the drivetrain and the braking system. Brushless DC motors can be controlled in unipolar or bipolar mode.

Microcontroller software
The controller contains three microcontrollers. Each microcontroller requires dedicated software for performing its functions. The described controller features Atmega328 microcontrollers which were programmed in the Arduino environment. The Arduino tool resembles a simple text editor, and it is divided into three main sections: toolbar, message area, and code editor. The toolbar at the top features command buttons. The code editor is where the sketches (programs) are written. The message area displays information about the status of the current sketch and possible errors.
The programs that control brushless motors have similar codes. The code for the master microcontroller is related to the user panel, and it sends commands to the microcontrollers that drive brushless motors.

Code for the master microcontroller
The master microcontroller receives and processes data from the user panel and sends commands to slave microcontrollers that drive brushless motors. The master microcontroller controls motor speed and enables the vehicle to move in different directions. The user operates push-buttons and a potentiometer on the user panel to send information about driving speed and direction to the master microcontroller. The data received by the master microcontroller is processed by algorithms and transmitted by the I²C bus to slave microcontrollers.
Microcontroller pins are identified, libraries are included and variables are added in the first step of the program (Fig. 1). Command #define gives a name to a constant value. For example, value 4 is preceded by the name right because the SW1 button is connected to the pin corresponding to this value. Command #include <Wire.h> includes an external library in the code. The Wire library enables devices to communicate via the I²C bus. Command int currentSpeed previousSpeed adds two values that store information about motor speed. The setup function is described in the next step of the program (Fig. 2).

Zenon Syroka
The void setup function is called when the microcontroller is powered up. The function code with the instructions to be executed is placed inside braces. Command pinMode (forward, INPUT_PULLUP) informs the microcontroller that the pin connected to the forward drive button is an input pin that receives data. Successive commands carry the respective information for the remaining microcontroller pins. Command Wire.begin connects the master microcontroller to the I²C bus. Command ControlMode calls a function which describes the motor control mode selected by the user. The control mode is selected by the operator with the use of left or right turn switch. The left turn switch activates the unipolar mode, and the right turn switch activates the bipolar mode. Function codes are presented in Figure 3. The while command is a loop that checks the expression inside the parentheses. When the expression is true, the function inside the braces below the command is called. The absence of code inside braces denotes a void loop that will stop the program. The program waits until the expression inside the parentheses becomes false, in this case -until the right or left turn button is For example, when the left turn button is pressed, the second if statement becomes true and information is transmitted to slave microcontrollers. Command Wire.beginTransmission (7) initiates communication with device 7 connected to the I²C bus. Command Wire.write("bipolar") sends data to an external device. In this case, the transmitted information is the word bipolar which denotes the selected motor control mode. Command Wire.endTransmission() ends the data transfer. Each command is repeated twice because data are sent to two microcontrollers -7 and 8. The while loop after the Communication command makes the program wait until the user releases the button.
The code defines the direction of motor rotation. The if statement is called when the user presses the forward drive button (Fig. 4). The Communication function is called to send information to external microcontrollers. In the parentheses, 7 is the address of the receiving device, and 1 is the direction of motor rotation. The code for reverse drive (Fig. 5) and braking (Fig. 6) is similar. The code for reverse drive and braking differs only in the data transmitted by the bus and the functions that are called by different buttons on the user console.
The microcontroller controls motor speed through the potentiometer on the user console. The code checks the value at the analog input connected to the potentiometer (Fig. 7). In the next step, the values are converted by the map function to the range of 3-255. The if statement checks potentiometer value. If potentiometer value has changed, the respective function is called to transmit information about motor speed to slave microcontrollers.
If the user wants to turn the vehicle and presses the left turn button, the motor speed value that is transmitted to the right motor controller is halved. As a result, the left motor turns at half the speed of the right motor, which causes the vehicle to turn in a given direction. The left turn code is presented in Figure 8, and the right turn code is presented in Figure 9.  The information sent by the master microcontroller to slave microcontrollers is composed of values in the range of 0 to 255 and ASCII characters. Each value and character carries specific information: 1 -forward drive, 2 -reverse drive, 3-255 -motor speed, 'h' -engage brake, 'q' -release brake, 'u' -unipolar motor control, 'b' -bipolar motor control.

Code for slave microcontrollers
The code for slave microcontrollers that drive motors contains functions responsible for: -switching transistors in a desired commutation sequence at the corresponding pulse width modulation (PWM) values, -defining rotor position, -receiving information about operating parameters from the master microcontroller.

Electronic commutation
The main purpose of a slave microcontroller is to replace a mechanical commutator with an electronic commutator. Transistors are switched in a given commutation sequence to pass current through motor windings and to set the rotor in motion. The commutation sequences for transistor switches are presented in Table 1. Table 1 Commutation sequences for transistor switches when the motor turns right (clockwise)

Zenon Syroka
Microprocessor outputs AH, BH and CH supply upper transistor switches. Microprocessor outputs AL, BL and CL supply lower transistor switches. Numbers 1 to 6 in the top row of the table represent commutation steps. Two transistors are excited in each step. The commutation sequence has to be reversed to turn the motor in the left (counterclockwise) direction. The code for the function that switches transistors is presented in Figure 10. The function initiates one of the six commutation steps to pass current through windings. For example, step 0 calls function AH_BL () which powers the upper transistor A and the lower transistor B.
The function that powers transistors sets the value of microcontroller outputs to 9 and 6 to send a PWM signal to the transistor driver (Fig. 11). Pulses are generated by DC to AC inverters and by frequency converters. Pulse width is modulated by switching transistors on and off. This approach supports easy and high-precision control.

Determining rotor position in a BLDC motor
Information about rotor position is required for the correct operation of a brushless motor. The controller is equipped with a rotor position sensor. Hall effect sensors mounted on coil windings send information about rotor position to the microcontroller by outputting a value of 0 or 1.
The combination of values output by three sensors describes rotor position (Fig. 12). When rotor position has been detected and the time of the next commutation step has been determined, the program calls a function that activates the next transistor pair. The code that checks the combination of values output by Hall effect sensors is presented in Figure 13. If sensors C and A output 1, the outputHall variable is assigned a value that corresponds to the next commutation step.

I²C communication and motor control
The information about motor speed and direction is transmitted by the master microcontroller via the I²C data bus. The relevant data is sent in the form of values and ASCII characters. The slave microcontroller receives data and calls the corresponding functions. The code that enables slave microcontrollers to receive data is presented in Figure 14. The receiveInformation function is called when information is received. The transmitted information is composed of ASCII characters such as u, b, h and q, and values in the range of 0 to 255. The if statement in the function code defines the direction of motor rotation. The unipolar or bipolar control mode has to be selected when the controller is activated. Character u denotes the unipolar mode, and character b denotes the bipolar mode. The direction of motor rotation which determines the direction of drive is determined in the next step. Value 1 indicates that the motor is moving in the right (clockwise) direction, and value 2 indicates that the motor is moving in the left (counterclockwise) direction. Values higher or equal to 3 denote motor speed. The higher the value, the faster the commutation sequence. Character h denotes electric braking, and the microcontroller activates two motor windings until character q is received.

Summary
The designed controller for brushless DC motors has been patented (Syroka, krajewSki 2019) for use in commercial applications. The device can be applied in electric vehicles for controlling BLDC motors mounted in wheels. It was developed as part of a research project at the University of Warmia and Mazury in Olsztyn (Books -Digital Control, Syroka 2019) dedicated to the construction of electric vehicles and electric drives that rely on renewable sources of energy.