Introduction: Bluetooth-Controlled Car/Robot
Learn how to make a car/robot controller using a GreenPAK, HC06 bluetooth module, motor drivers and motors. The Silego GreenPAK CMIC is successfully configured to support the UART protocol in data sequence and synchronization. Additionally, it can be synchronized with many sensors that use UART. This allows great flexibility designing with Silego GreenPAK CMIC for controls using smart devices (cell phones, tablets, etc.).
This project contains 3 stages:
1. Android Application
2. Retrieving data coming from the Bluetooth module
3. Controlling the motors
Step 1: Compose an Application for Android Devices to Send Data to the Bluetooth Module.
We used MIT app inventor 2 web site to compose the application. On this web site, you can easily create an application with programming blocks, without any programming knowledge.
Common directions a car basically moves in are left, right, forward and backward. To specify 4 different actions, 2 bits are enough. After having specified car directional moves, let’s set the speed of the car. We can adjust the speed to 4 different settings, and 2 binary bits are enough for this. The data is sent to the Bluetooth module as 8 bit packages, and 5 bit data is enough for us.
Now we are able to create the application regarding these calculations. 4 buttons to define rotations, a list picker for devices and speed control, and a button for connection is enough. Also, we have to add bluetooth client and bluetooth server module to make a Bluetooth connection. To see directional buttons aligned, use the table arrangement.
After designing, we pass programming by clicking block section. Programming is quite easy with using Mix Application Inverter 2. You may add the block that you need by clicking components on the left side. In this part, we fill out the contents of the list pickers, and then line up the bluetooth devices so that the former match the devices, therefore, we arrange the numbers as ' 1, 2, 3, 4 ' for speed. You can find the project of this Android Application here.
Step 2: Getting Data From Bluetooth Module
HC06 module that we use for Bluetooth communication uses UART for communication protocol. HC06 sends 1 start bit, 8 data bit and 1 stop bit by default and also it has 9600 baud rate. Silego GreenPAK has SPI block. We are going to make some additions to the SPI blocks which allows getting data that comes from HC06 module. Initially, an external clock is needed for SPI protocol. However, data comes at a constant rate under UART protocol. Another difference is that there are no start or stop bits in SPI communication. We are going to use a start bit to get SPI clock going. UART protocol is in high power state when idle, and it passes to low power state when it starts communication (start bit - low).
We can use Falling Edge Detector to identify the start of communication. We connected this signal to the clock pin of a DFF and VDD to data pin for the connection to stay active during the connection. Thus, PDLY0 block is going to set connection flag by catching the falling edge when communication starts. UART protocol has 9600 baud rate which means it is able to transfer 9600 bits per second. To find the period of one cycle, you have to divide 1 second by 9600. If you start the clock running immediately, data and clock signals will be changed at the same time and it may cause data loss. That is why clock signal must be delayed for a half cycle. We applied this delay using CNT6/DLY6 block. We attained a higher frequency by setting the RC OSC Frequency from the OSC block as 2000 kHz. The cycles from which we attain higher frequencies have shorter periods, and the absolute timing resolution of the counter increases. Thus, we decrease the counter's error ratio.
CNT6 block is set in a way that it delays half a cycle (0,1042/2=0,0520 ms). We used another counter (CNT2) to obtain a cycle's duration (0,142 ms). We set this counter's clock as External Clock0. By applying logic and process, we connected the OUT1 signal that comes from OSC and CLK BEGIN signal to OSC's External Clock0 input. Thus, CNT2 will begin counting only when the connection starts. To give more detail, the counter blocks count the edges of the clock signals. If the counter constantly sends high or low signals to the CLK pin, the counter stops counting. Here, we AND’d CLK Begin signal with OUT1 signal and connected them to the External CLK0 input. In this way, at the moment when there is no connection, ANDing CLK Begin signal and OUT1 signal will always equal to 0 and CNT2 will stop counting.
But when connection is provided, CLK Begin signal will be "1", the result of the logic and operation will be OUT1 signal and it will have CNT2 keep counting. We connected the output of the CNT2 counter (its cycle duration was calculated beforehand) to the SCLK pin of the SPI block. Hence, we generated a clock cycle for every byte of data coming from HC06.
Finally, we set a delay in order to calculate the connection's expiration and to zero the connection. This counter will begin measuring the duration as soon as the connection starts and will end the connection when the data delivery is completed. We zeroed the connection after measuring the duration of 9 bytes. Zeroing the connection during stop byte makes the SPI block ready before a new connection is formed. Otherwise, if it is intended to be zeroed at the end of the duration of 10 bytes, there is the possibility of failure to capture the start byte for the new connection. We reset the signal coming from the out pin of the PLY5 block, SPI block, the counter with 1 cycle duration we had calculated as well as DFF0 which we use as the connection flag. However, unlike the other ones, we delayed the signal a little before resetting DFF0. If you reset the counter-delay blocks of the SLG46620V chip, the out chip of the block remains high for 1 cycle. If you reset CNT2 and DFF0 at the same time, CLK Begin signal remains low, as a result of which stop CNT2's clock stops. As CNT2 will remain high for 1 cycle and the clock will shift to low status constantly, CNT2 will not be able to complete 1 cycle and will steadily remain in high status. Therefore, we reset the CNT2 block first and then DFF0 after a few cycles.
Step 3: Control of the Motors/Car
At this stage, we change the car's speed and direction by changing the outputs of the DC motors. The car's direction and speed information is received via Bluetooth. As we receive the data coming through the UART protocol via SPI block, SPI Parallel Output block will be used here.
One of the differences between the UART and SPI communication is the sequence in which the bytes are sent. While MSB (Most Significant Bit) is sent initially with SPI protocol, it is sent last with UART protocol. Therefore, we will receive the bytes of the data with a reversed sequence. We linked the S1 and S0 bytes bytes to the PWM0 block. After the settings of PWM0 are applied as indicated above, the CNT8/DLY8 block is configured.
Now, we are able to receive PWM signal from the PWM0 block and adjust the signals necessary to set the car in motion. The SLG46620V CMIC cannot drive the DC motors directly. Therefore, we will be using a power supply and motor driver to control them. DC motor drivers run the motor by energizing the pins of the motor in accordance with the control pins that are usually named as M1-A, M1-B, M2-A and M2-B. We build a connection in our design as shown in Table 6.
The LUTs supply the proper logicsignals to the motor driver inputs. Thus, the motor out pins will remain low while the open-close signal is in the low state and the motors will not rotate.
For more complete details on how to implement a Bluetooth-Controlled Car/Robot download our full Bluetooth-Controlled Car/Robot app note.