Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / artificial-intelligence

A beginner's guide to Design and Program AI Robots; C#, C++ (Part 1)

4.77/5 (19 votes)
2 Jul 2014CPOL13 min read 99.9K   1.5K  
If you are good at highschool maths, basic c++ and know a little bit about electronics, you can follow this series of tutorials to make your DIY artificial intelligent robots.

Introduction

This article provides a [noobest] starters guide to applied robotics for those who have basic knowledge of C#, C, electronics and obviously, highschool trigonometry and physics and want to test their codding skill on something that really "works", something that physically moves and responds to human actions.

I remember the part of my life when i could write "Hello! World" applications in c++, could make simple graphical calculators in c# and disassemble (and then successfully re-assemble) nearly all the household electronics items at my home. (I'm not going in far future, it was around the year 2008 and i had just ended high school). I started googling to find out how Ii could make robots at home. Scaring things  crawled through my sight like "DLD", "Parallel port interfacing", "Microcontrollers", "PLC", "PIC", "Servo-motors", "ADC", "DAC" God knows how confused i was andi didn't knew where to start or what to learn first! but now i do. thats why i'm summing up all that knowledge gathered from different websites, books, experience and my teacher's lectures in this article.

Requirements

To follow through the parts of this series, you must have:

Sound knowledge of C#, C++, high school trigonometry, basic electronics and obviously, [Patience + determination].

A computer/laptop with both Serial Port and Parallel Port and runnig a Windows OS. (Win 8 not preferred)

I don't intend to scare you off, in fact, what i'm saying is that you must not get frustrated because sometimes a simple bug tracking in codes and hardware may takes nights of consideration.

Contents

My article will contain comprehensive and "enough to start-over" knowledge of the following parts of robotics. At the end of this series, the follower will be able to boast off his/her first AI Robot.

You will learn in the following sequence. (The text of headings in the article may differ )

  1. Choosing the type of interface/ control (Article PART 1)
    1. Selection of hardware
    2. Selection of programming language
  2. Getting the required things ready (Article PART 1)
  3. Preparing basic tools for our practicals
    1. Preparing IDE
    2. Preparing the simulators
    3. Preparing hardware
    4. Codding of the base framework for any type of robot
  4. Operating some output devices through our computer
    1. Testing our systems for the first time
    2. Interfacing relays and switches with computer in GUI
    3. GUI interfacing of a servo motor with the Computer
    4. GUI interfacing of simple DC variable speed motor
  5. Measuring physical quantities
    1. Measurement of room temperature on Computer
    2. Measuring distance with SONAR
    3. Measuring speeds of motors
    4. Measuring angle between two physical links
  6. Learning some very powerful tools for our robot
    1. Interfacing a camera for measuring distance of objects
    2. Speech synthesize/recognition system for the robots
    3. Face detection
  7. Combining the knowledge; Creating our first robot
    1. Designing/programming of a simple 4DOF Robotic arm
    2. Detecting colored balls in sight and changing them to computer language
    3. Teaching the system to detect faces, respond to voice commands and pick demanded balls.


I have helped two of my fellows in the same things. One of them made a humanoid robot, ROBOx. 4feet tall ROBOx is able to walk, talk and interact with things placed around it.

1. Choosing the type of Interface

An interface is the methodology used to bridge the commutation of two entities. Like, Computer Monitors provide an interface to machine and human and our LAN card provides the interface between computer and the network. In fact, there may be a thousand answers to this question and all being right. Lets get know one of them that is really easy to practice at home and is powerful enough to be used for commercial products.

If you don't know the following terms, follow the links by clicking them.

Parallel Port (The purple 25 Pin Port located at the back of PC)

Serial Port (The greenish 9 pin port located at the back of PC as well)

Microcontroller (A small IC which can be used to run a C++ program on it's own. It has got its own RAM, ROM, Processor and other things to run a C++ written program)

The general interfaces we are going to develop are:

1. The one between us (human beings) and computer (a simple GUI)

2. Computer and microcontroller

3. Microcontroller and motors/sensors.

In fact, the process can be described as:

Step 1. Desktop computer orders the microcontroller through a Serial port about what to do

Step 2. microcontroller drives motors, actuators and other output devices

Step 3. Microcontroller gets the feed back by reading from sensors.

Step 4. Microcontroller also tells the PC about the feed back.

Step 5. Computer shows us what actually happened when we ordered the computer to do something

Isn't it what a robot should do? exactly. Now, when we have got the basic scheme of what is interfacing and what are the major parts of a robot, lets get to some more details.

1.1. Microcontrollers

A microcontroller mounted on a bread board

(Most) microcontrollers are IC's which need to be powered on by providing (in our article) with a 5V DC power source. This could also be the power coming out of USB port, which actually operates at nearly 5V.

Microcontrollers hold a compiled program in machine language which was originally written in a computer language, in our case, in C language. Just like we declare variables in programing and assigne values to them and get them back, micro controllers come with some in-built and predefined "Variable like" spaces, Registers which have pre-defined and dedicated names as well just similar to variables. In our case, these registers are mostly 8-bits wide. Values assigned to some of these registers are not only saved in the memory, but also depicted on some of the pins of microcontroller IC's as voltage output. 1 being ~5V and 0 being ~0V.

So, by chaniging the values of these registers and reading them back, microcontrollers can be used to operate relays and transistors (according to the C++ program given to them) that can further be used to drive motors.

I chose from the vast variety of microcontrollers, the Intel AVR Architecture and more specifically Atmel MegaAVR Family. These devices are cheap to buy, (start from 1USD), easy to be programmed, readily programmable at home without buying costly programmers and have all of the necessary components and capabilities to be utilized in our robots.

1.2. Why involve the computers if microcontrollers just act like them?

If these microcontrollers are so powerful why do we mess up in interfacing a PC and stuff? one may ask, and must ask. Otherwise my reasoning would go into waste and the neccessity would be killed before the invention coming to realization.

The problem with microcontrollers is their very small System clock speed (not the wall clock speed, clockof a microcontroller does the similar as what does the processor in your computer) and small programmable memory. And the things are even worse than you are imagiuning right now. Lets give you a quick comparison.

  • Our computers have processsor speeds ranging from a couple 900Mhz to a couple Ghz and multi core processors while one of the most powerful microcontrollers is equipped with only 120Mhz. The one we are going to use is 16Mhz.
  • We usually have a couple of GB ram in computer, microcontrollers have 1 couple MB, the one we are going to use has 512Bytes!!
  • Our computers have upto a couple TB of hard drives which are self writable. Controllers have a couple KBs.

When we are talking about robots, we talk about Vision systems, speech synthesize and Artificial Intelligence, Microcontrollers are no match to the capacity of desktop computers/laptops.

So! we have found a midway between both solutions. A hybrid system that will be centrally programmed and taught all the intelligence on the desktop computer and to interact with Motors, actuators, sensors, camera's and many other devices, we will have microcontrollers that will obey the command transferred through Serial port of the main computer.

Please note:

In this article, for the purpose of demonstration, all the coding and electronics is done for Atmega 16L/A.

2. Getting the things ready

2.1. Making your first Microcontroller Support Board

This simple tool is going to hold your microcontroller, power it up and give it some essentials to start up.

2.1.1 The bread Board

I think its time you met your future best friend, Mr. Bread Board.

This marvellous simple invention lets you hook up different electronic comonents on any of it's hundreds of holes which have GENERIC electrical connections from the inside.

2.1.2 The datasheet

Datasheets are always the ultimate answer to most of tthe user questions about components. We only use them for reference, the is no need to read all of it anytime. THIS is the datasheet of atmega16, we are going to use in future discussion. download it and save at an easy-to-access location.

2.1.3. The componenets

Our support board will contain some basic components:

  • A 5V power supply given in the vertical strips, so that we can use them any where in our circuit when we need it.
  • An LED, which comes through a resister and connected between 5V. Read THIS small article, it will also be benificial for some other concepts.
  • An auto and manual reset circuit (a debounce circuit)
  • A crystal of 16Mhz. This componenet actually gives pulses to controller at exactly 16Mhz friquency so that the conroller runs at 16Mhz.
  • And, ofcourse, a microcontroller, and for this specific article, the Atmel ATmega16

You are giong to connect these components on the breadboard.

For the power, you have two options,

  • Use the 5V coming out of USB connector. As the picture given earlier shows the illustration of USB port, use a broken USB cable to extract the GND and 5V wires from the connector. DON"T CONNECT IT TO BREAD BOARD YET.
  • Use a DC adopter which says any thing above 6V DC and below 3VDC in the output. alongwith the 7805 voltage regulator IC shown in the picture. (9V Suuply is recomended, not required.)7805 connections

Now, you have to mount the components on the bread board. Follow the following schematic for the purpose.

  • Atmega 16 connection

Now, connect the 5V power to VCC and AVCC of controller as well as the GND.

Power LED, D1 should light up. If it doesn't the common causes are,

  • No power in supply
  • Loose connections
  • Reversed connections of supply, 7805 or LED
  • Faulty components

Find your pronlem, correct it, get you LED lit or, PANIC!

The later is never recomended because it does no good.

2.2 Getting the programmer/burner

2.2.1 Intro and things to be done

(Not Burn, Burn)

The programmer is the device which copies the program from computer to microcontroller.

Mostly, either of the 2 kinds of programmers used to program/burn controllers are:

Universal programmers (like THIS)

In-System-Programmer (ISP), like THIS

We are going to use the ISP. ISP's are echonomical, faster and portable. They don't require an IC to be unmounted from the main circuit, the only down side, they can program only a limited set of controller IC's.

Many programmer are out there in the market, you can go buy one or make your own. Trust me, the later is not as hard as it sounds and in most cases, more economical way to get a programmer.

Follow THIS link and get your ISP ready.

NOTE: You must NOT use the breadboard for the programmer, as it may create complications and even never work successfully. use a Wire wrap board and soldering iron. For guide on soldering, HERE is a very good video guide.

Also, download the ISP programmer software from attachments and install it in C:\ISPProg

2.2.2 My own In-System Programmer

Following diagram is of one ISP I personally use for SPI communication and programming. I have got it from the same link as mentioned above.

And here are the Visuals of one very simple and RAW programmer. I use Prallalel port extension cable with this programmer.

2.3. Plug-in everything together

Find the parallel port in your PC

Plug-in your ISP in the parallel port, hook it up on the support board and power the board up.

Now open the ISP programmer software under windows and select "AVR" from the drop down list at the top. Now, goto "Setup". selectse "SI Prog", apply and click on "READ SIGNATURE".

This is the most critical point in seting up the basics.

You must get "ATMEGA16L" as the detected device on this screen. If it says "Bad or unkown signature", try one of these solutions.

  • Verify all the connections according to the IKA logic website's link given above.
  • Change clock speed to 1000000Hz
  • Change pinout to "AEC Prog" or "ISP Prog" in the setup
  • reduce the length of wires used in programmer (cable between controller and programmer must be 1ft at most)
  • Verify a perfect 5V on the supply of programmer side
  • Verfiy that 7805 is not heating up. Else, use a heat sink on the 7805 IC.
  • And obviously, you can contact me so that we could discuss the resolve.

2.4. Install the Software

2.4.1 The Recomended IDE's

The development environment we are going to use for applicaitons on computer side is "MS Visual Studio", preferably, V2008 or V2010. Get a DVD/Internet copy, and install it. Assure the installetion of MS Visual C# and .Net framework 3.5 during installetion wizard however, you may skip the following:

  • MS Office development,
  • MS Visual C++, VB and other languages
  • MS mobile device development tools
  • Web development tools

The development environment going to be used for controller side programs is HP Infotech Codevision AVR . unfortunately, the evaluation version. You are encouraged to purchase the full version but evaluation will also suffice for the isntance. Search and download it HERE.

2.4.2 Alternate IDE (OPTIONAL)

If you can't find Codevision AVR, you can also use VMLAB, a freeware, but you will loose the crisp of CodeWizard AVR and intellisense of codevision AVR.

  • Download VMLab from HERE. Install to C:\VMLAB
  • Download WINAVR from HERE. Install to C:\WINAVR
  • Install Both in root of C drive.
  • Remember this for creating any projects:
  • Goto Project > New Project

Future reference

2.4.3 ISP programmer

If you have already downloaded the ISP programmer from section 2.2, skip this section.

Download the ISP programmer from attached file and install it in C:\ISPProg

2.4.4 ISIS Proteus

This powerful software is a full solution for electronis circuits simulation, debugging and making PCB's. The utility is not compulsory for you because you may not want to buy the license (unless you resist to humiliate the developers and download full proteus 7 professional from a torrent).

2.5 Concluding Part 1

By the end of this part, you must have learned the following and if you haven't you can always google it or in worst case, ask me.

  • Detecting an ATMEGA16L IC via ISP on computer programmer software
  • How to install MS Visual Studio, (MS VS08), codevision AVR and/or VMLAB
  • The DO's and DON'Ts while powering a microcontroller support board.
  • Making a 5V DC regulated supply
  • General purpose soldering routines.

In next part, we will learn How to:

  • Writing your "hello, world!" program for microcontroller (an LED blinking application)
  • Simulating the code in real time simulation of microcontroller circuit.
  • Programing you Microcontroller for the first time, and for the second time as well.

Do give your feed back, i'm always here to help you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)