Saturday, December 13, 2014

An Introduction to robotics-2

In previous post I talked about some background information about what kind of things you have to consider when planning to build a robot. In this post I’m going to extend that to another prospective. I’m going  to give you some very simple but useful information about several things you want to know about basic electronics and programming in general. and also I assume the reader has at least some basic knowledge in C programming and how to use Arduino IDE and other associated softwares.

As I told earlier it’s easy to design a robot(or any other design) if we breakdown the whole thing into several pieces and designing each one carefully. There are few advantages of this approach.

  • You will have a good understanding about each section and how they all fit together in a much larger system and the functionality of each section.


  • When something goes wrong (believe me it will) you don’t have to check each and every part, just the part associated with the fault, it will narrow down your scope and saves some time.

  • You can reuse what you have done in a project in another project if it’s suits with the requirements. It will save your effort since you don’t need to do everything from scratch .  Ex: if you wrote a function to compare two input values and send the result to main function rather than doing everything in the main, you will be able to use that function in some other instance with fewer or no changes.


  • Take your time to design and think about what you are going to do before you are actually going to do it. Thinking is one of the most important things in building something. It will help you to save your time, money and effort.



Some thoughts on embedded system programming 

There is no huge difference between writing a program to a PC and for a microcontroller .but there are few yet very important aspects that you have to remember when writing programs to a microcontroller.

  • Microcontrollers have very limited amount  of resources(memory and processing power)to work with. Therefore you have to be very careful about the size of your program and the memory it’s going to use during the execution.


  • Most of the time you will be using C or a derivation of C to program. Therefore it’s a good thing that you are comfortable with using C as a language. C is wildly use in embedded systems because it’s a well structured and easy to learn language. If you are a working most of your time in high level languages you might feel C is very low level language but that’s the thing what makes C is very suitable for system programming.

 
In this first project I didn't use much complicated things but I think there is a concept that you have to be familiar with, Interrupts. Interrupts are one of the fundamental things in embedded   environment. An interrupt is simply an asynchronous process with respect to a main program which is used to get information to the microcontroller from outside world or within the system. There are 2 types of interrupts.

  • Hardware interrupts
  • Timer interrupts


A signal from a push button press is an example for a Hardware interrupts and sending a pulse to the system using a clock provided to indicate something to the system is an example for a time interrupt. As I said interrupt is a technique used to get inputs into a system. There is another way to do this .It’s called polling. Checking for an input within the main program constantly is called polling. Using interrupts have a significant advantage over polling in embedded system because interrupts are asynchronous .Interrupts can happen anywhere within your main and you don’t need to worry about that once you set up an ISR (Interrupt Service Routine) to handle that interrupt. I’ll provide some links where the reader can get more information about interrupts . It’s very important to understand the role of interrupts and how to use them effectively in programming.

Electronics  used in the project

Since I’m using Arduino as my control system(Brain) to the robot it made lot of things easy for me. Although I didn’t want to worry about building a system from scratch there was a big constrain . Arduino is a good platform for robotics but it cannot provide enough current to drive heavy loads like DC motors. It’s maximum output current is 40mA and voltage is +5V.So I had to think about a way to drive 2 DC motors without destroying my Uno board. In electronics it’s done by using a H-bridge circuit. A H- bridge consists of MOSFET transistors that can control both speed and direction of the motors. I used a L293D chip to control the motors.   There are motor driver shield that can drive upto 4 DC motors but the price was too high for me and also I needed only one  since an IC can control 2 DC motors and it costs me only 3.95 AUD. You can easily purchase these type of  IC in web cheaper.




L293D IC



Pin configuration of the IC














  • Voltage range : According to the datasheet L293D can output   4.5V to 36V.My motors working voltage is around 7.2V.there for it’s ok to use this chip.

  • Output current is 600mA and maximum output current is 1.2A. My motors draw 450mA(each) in normal operation and the stall current is about 1.17A.So The IC can drive these 2 motors without much trouble.


This is a great video I followed to get this thing work. I’ll explain more about the chip when required. 

 Pin description of the IC

Enable 1,2 – These pins control the motors. If they are  high motors work ,if they are low motors are   disabled. Connect these pins to +5V pin in the Uno  board for this project.

GND – Simply connect these 4 pins to ground of the Uno .

Vss  - Power supply to the chip.(+5V).  
             
Vs  -  Power input to the motors. You can connect your battery output to this pin.                                                       
Input 1,2 – These are used to control the motor1.by setting pins like High, Low or Low, High you can change the rotation direction of the motor. Never set both pins in High as it will damage the chip. Setting both pins low will turn off the motor.

Input 3,4 – These are used to control motor2.Same functionality as Input 1,2.

Output 1,2 and 3,4 – These 4 pins connects to the motor 1 and 2.


Sensors

HC-SR04 Sensor-Pins from left-VCC,Trig,Echo and GND

 I used a conventional HC-SR04 ultrasonic sensor as my sensor to detect the obstacles and send that measured value to the microcontroller. All you have to know is that this module sends out an HF wave and by detecting a reflected wave t can calculate the distance. Obviously You have to write a program to do so. In my experience these are good to detect things that are away up to 80cm after that it might give you wrong readings but since it’s very cheap  it’s very good sensor for a simple robot project.

Pin description of the sensor


VCC – power input to the pin (+5V)

GND – connects to the ground pin of the Uno.

Trig -  Setting this pin high will emit a HF wave from the sensor.

Echo – When the sensor detects an HF wave it sets this pin to high and by using a function called pulseIn() we can find out the time taken by a wave to return to the sensor. This value is in milliseconds and it has to be divided by 2 in order to get the value for the time taken by the wave to reach to the obstacle from sensor.

So these are some things that I believe you have to be familiar in order to build a simple robot. I’m not going give you every detail about how I built my robot since it’s inappropriate and you can use these guidelines to design your own robot. In the next 2 posts I hope to discuss more on the structure of the programs I wrote for the robot. 

Picture of my robot-Walt


Further Readings

Please refer to these websites and documents to improve your knowledge.


















No comments:

Post a Comment