In the recent few decades, technology and engineering have evolved in a way that we have never imagined before. Among many aspects of technology, robots can be considered one of the most effecting aspects on our world. From industry to home automation and medicine. Robots are playing a major role in our life.
Robots can have many forms, for example: we have humanoid robots, wheeled robots, robotic arms etc...
One the simplest and easy to handle forms of robots are the ones with wheels. So today let us learn how to build a simple robot car using some sensors and driver circuits.
To make it more challenging, we are going to make it an autonomous car. So, it can drive itself by itself.
we will use Hexabitz modules to build our robot car. The main reason for that is the reliability of Hexabitz modules.
Tools:1. Hexabitz IR Module (H08R6).
2. Hexabitz MOSFET Switch (H0FR6)
3. FTDI USB to UART Serial cable.
step1 (Making it real):
First things first, we need to build the array of the modules. One of the most amazing qualities of HexaBitz modules is that it gives us the opportunity to choose any form we like to assemble our modules based on that form.
I have decided to make it look like an arc but you can choose any form you like.
The reason for choosing the arc from is that it gives me the ability to place the IR sensors on the sides and the front so the car can use these sensors readings to figure out which direction to take.
step 2 (Making it move):So now we have to think about how the car should move.
The car is supposed to move forward unless there is an obstacle in front of it.
In that case, it is going to stop and change its direction based on the space on the left side and the right side.
If the left side has more space, the car is going to take the left direction. On the other hand, if the right side has more space, the car is going to take the right direction.
But how does the car change its direction?
Well, it is a tricky idea. For the car to move right, the right motor has to stop and the left motor has to rotate. This is going to make the car turn to the right. And For the car to move left, the left motor has to stop and the right motor has to rotate.
So to make that happen, we have to send the readings of the three IR sensor modules to both the MOSFET modules.
Since we are using HexaBitz modules, this is going to be a piece of cake. The reason is that the modules are supported with many useful and easy to use APIs.
You can refer to the website of HexaBitz to know more about the modules and the steps to use the APIs. I will attach the website link in the end.
Now, let us walk together through the code.
So in order to send the readings from the IR modules we have to add the readings variables to the BOS (Bitz Operating System).
BOS.trace = TRACE_NONE;
AddBOSvar(FMT_FLOAT, (uint32_t) & sensorFront);Secondly, we have to choose whether we want to have the reading in (mm) or (cm):
SetRangeUnit(UNIT_MEASUREMENT_MM);Thirdly, we have to update the readings values and save them to the variables which we have added earlier to the BOS:
Stream_ToF_Memory(10, portMAX_DELAY, &sensorFront);Finally, we have to send the values to the selected modules:
In my modules array, the left MOSFET module is refered to by the number assigned to it in the topology file which is (2) (The topology file contains all the information about the modules locations in the array), while the right MOSFET module is refered to by the number (3).
Therefore, I have to specify the module number which I would like to send the readings to:
WriteRemote(2, (uint32_t) &sensorFront, 1, FMT_FLOAT, 0);In this line of code, we send the front sensor reading to the left MOSFET module.
WriteRemote(3, (uint32_t) &sensorFront, 1, FMT_FLOAT, 0);In this line of code we send the front sensor reading to the right MOSFET module.
We do the same steps in the other two IR modules.
Now to recieve the readings in the MOSFET module we also have to add the recieving variable to the BOS:
AddBOSvar(FMT_FLOAT, (uint32_t) &sensorFront);After executing this line, the value of "sensorFront" will be updated every time the IR module sends a new reading.
We do the same steps to add the other two readings.
Based on the received readings, the MOSFET modules can know whether they should turn the motors ON or OFF using the theory we agreed on earlier.
The code for the left MOSFET module:
for(;;)
{
if(sensorFront < MAX_DIS) // MAX_DIS = 700 mm
{
Relay_off();
Delay_ms(10);
if(sensorLeft < sensorRight)
{
Relay_on(12);
Delay_ms(100);
}
else
{
Relay_off();
}
}
else
{
Relay_on(12);
Delay_ms(100);
}
}
if(sensorFront < MAX_DIS) // MAX_DIS = 700 mmIn this line of code, the Microconroller will execute the block of code to change the car direction if the distance between it and the upcoming obstacle is equal to MAX_DIS.
if(sensorLeft < sensorRight)Here, we check whether there is more space to the right or not.
Relay_on(12);Turn the motor ON for 12 ms.
Relay_off();Turn the motor OFF.
The code for the right MOSFET:
for(;;)
{
if(sensorFront < MAX_DIS)
{
Relay_off();
Delay_ms(10);
if(sensorLeft < sensorRight)
{
Relay_off();
}
else
{
Relay_on(10);
Delay_ms(100);
}
}
else
{
Relay_on(10);
Delay_ms(100);
}
}So, we can see from the two codes that if there is no obstacle in front of the car, both motors will be ON and the car will keep going forward.
I hope you enjoyed reading this article. Follow me if you would like to see more projects in the future. Feel free to write any comments, I will always be happy to help.
Video link:
Good Luck.
https://hexabitz.com/












Comments