Robotic programming with User Defined Functions!

Vidyasagar Academy has designed one simple technique to program your robot using simple coding shortcuts. These shortcuts are called as User Defined Functions.

Details of user defined functions

User defined functions are the type of simplified functions defined by the user as per his requirement and necessity in a particular program or code. You can create your own simplified user defined functions once you read this article.

We are presenting here almost all such simple user defined functions which will help you to write any program in extremely simple coding style.

How to use user defined function?

Suppose we want to move the robot in forward direction when both its IR sensors are on white surface. The conventionally we write the robotic program as follows.

if ((LS==0b10000000)&(RS==0b00000010))
   {
     PORTB=0b00001001;
   }

So when both the IR senses are on white surface, the robot moves forward. However, we can simplify such complicated coding using user defined functions as follows.

How to start with it?

First open your PC and right click on the desktop. Go to “New” and then click on “Text Document” to create a new text file. Delete its extension as .txt and change its name as “functions.c”. The Windows will alert you not do this. Ignore it and click Yes as shown below.

Windows alert

MS Windows gives alert when we try to change the extension of a file

Now right click on this “functions.c” file icon and open it in “Notepad” application. Then copy paste all the following code into it and save it.

After that create one project like BLFR or any other, into your installed robotics application of AVR Studio4. When you create project, the project will be automatically saved on the given path e.g. say on desktop.

Open this project folder and copy paste the “functions.c” file, which you just created, into this folder.

After that go to the coding area of your project in AVR Studio4 and write down one line, into it at the beginning, just below the crystal frequency definition line.

#include "functions.c" // this file will control all your simplified functions and commands

That’s all! Now you are all setup! Now you can use following simple commands to control your robot.

GoForward(); // Write this function anywhere you like to move your robot in forward direction

GoBackward(); // Write this function anywhere you like to move your robot in backward direction 

In this way, read and write all the functions, given in the “functions.c” file and use them in any program, as explained.

Important Note: Remember that the “functions.c” must be copy pasted in your every project folder and the command line #include “functions.c” must be included in every program at the beginning.

/*
	These are the user defined functions created by Vidyasagar Academy.
	With these functions student can write any robotic program
	in simple steps.
	However, we suggest to study the in-built functions of AVR Studio4
	and understand their importance. 
	Refer our course book - LEARN ROBOTICS IN SIMPLE STEPS
	- written by Prof. D.S.Vidyasagar
	Visit out website: www.vsagar.org
*/

/* ****** Standard User Defined Functions ******** */

	void GoForward()
	{
		PORTB=0x09; // robot moves forward
	}

	void GoBackward()
	{
		PORTB=0x06; // robot moves backward
	}

	void Stop()
	{
		PORTB=0; // robot stops
	}

	void TurnLeft()
	{
		PORTB=1; // robot takes soft left turn
	}

	void TurnRight()
	{
		PORTB=8; // robot takes soft right turn
	}

	void PowerTurnLeft()
	{
		PORTB=5; // power left turn
	}

	void PowerTurnRight()
	{
		PORTB=0x0A; // power right turn
	}

	void Beep()
	{
		PORTB=0x10; // use this to produce beeping sound
	}

/* ===== Functions for standard delay values ===== */

	void Delay100ms()
	{
		_delay_ms(100);
	}

	void Delay200ms()
	{
		_delay_ms(200);
	}

	void Delay300ms()
	{
		_delay_ms(300);
	}

	void Delay400ms()
	{
		_delay_ms(400);
	}

	void Delay500ms()
	{
		_delay_ms(500);
	}

	void Delay600ms()
	{
		_delay_ms(600);
	}

	void Delay700ms()
	{
		_delay_ms(700);
	}

	void Delay800ms()
	{
		_delay_ms(800);
	}

	void Delay900ms()
	{
		_delay_ms(900);
	}

	void Delay1s()
	{
		_delay_ms(1000);
	}

	void Delay2s()
	{
		_delay_ms(2000);
	}

	void Delay3s()
	{
		_delay_ms(3000);
	}

	void Delay4s()
	{
		_delay_ms(4000);
	}

	void Delay5s()
	{
		_delay_ms(5000);
	}

/* ===== Functions related to sensor connections ===== */

	void LS_PA7
	{
		LS=PINA&0b10000000;
	}

	void RS_PA1
	{
		LS=PINA&0b00000010;
	}

/* ************* Do not edit this file **************** */

Comments on this entry are closed.