Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added OOP and ROS assignments. #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added OOP and ROS assignments.
abinashmeher999 committed Mar 12, 2014
commit 8764454f72685eeaafb94c0e8ec364c776bc14d4
29 changes: 29 additions & 0 deletions Abinash/OOP_Assignment/Board.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Board.h"
#include <cstdlib>
#include <ctime>

//void Board::mapped(Image img,int *x,int *y,chan *pa)
//{


//}

Board::Board()
{
srand((unsigned int)time(NULL));
Chan *x;
int i,j,k;
x=&A;
x->rn=99;
x->gn=99;
x->bn=99;//Board::mapped(img,&height,&width,&A);
for (i=0;i<99;i++)
{
x->r[i].x=rand()%640;
x->g[i].x=rand()%640;
x->b[i].x=rand()%640;
x->r[i].y=-(rand()%480);
x->g[i].y=-(rand()%480);
x->b[i].y=-(rand()%480);
}
}
28 changes: 28 additions & 0 deletions Abinash/OOP_Assignment/Board.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef BOARD_H
#define BOARD_H
#include "Position.h"
#include "Image.h"

typedef struct{
char c;
Position p;
}pix;

struct Chan{
Position r[100];int rn;
Position g[100];int gn;
Position b[100];int bn;
};

class Board
{
public:
int height,width;
Chan A;
Board();
private:
//void mapped(Image,int*,int*,chan *pa);

};

#endif // BOARD_H
4 changes: 4 additions & 0 deletions Abinash/OOP_Assignment/Dice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "Dice.h"
#include <ctime>
#include <cstdlib>

13 changes: 13 additions & 0 deletions Abinash/OOP_Assignment/Dice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DICE_H
#define DICE_H


class Dice
{
public:
virtual int roll()=0;
protected:
private:
};

#endif // DICE_H
10 changes: 10 additions & 0 deletions Abinash/OOP_Assignment/Dice6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "Dice6.h"
#include <ctime>
#include <cstdlib>


int Dice6::roll()
{
srand((unsigned int)time(NULL));
return (rand()%6)+1;//ctor
}
15 changes: 15 additions & 0 deletions Abinash/OOP_Assignment/Dice6.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef DICE6_H
#define DICE6_H

#include "Dice.h"


class Dice6 : public Dice
{
public:
int roll();
protected:
private:
};

#endif // DICE6_H
63 changes: 63 additions & 0 deletions Abinash/OOP_Assignment/Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "Game.h"
#include <conio.h>

void Game::get_players()
{
int i;
std::cout<<"Enter the number of players\n";
std::cin>>no_players;
Set=new Player [no_players];
winset=new Player* [no_players];
for (i=0;i<no_players;i++)
{
Set[i].id=i;
}
}

void Game::trialturn()
{
int i;
for (i=0;i<no_players;i++)
{
getch();
Set[i].Getdice(dice.roll(),&bd1);
std::cout<<"\tPlayer_"<<Set[i].id<<" @ "<<Set[i].pos.x<<","<<Set[i].pos.y<<" With score="<<Set[i].score<<"\n"<<std::endl;
}
return;
}

void Game::StartGame()
{
int j,k,l;

Player* tmpset=new Player [no_players];
for (l=0;l<no_players;l++)
{
tmpset[l]=Set[l];
}
for (j=0;no_winners==0;j++)
{
std::cout<<"Round--"<<j+1<<std::endl;
Game::trialturn();
for (k=0;k<no_players;k++)
{
if (tmpset[k].pos.x==Set[k].pos.x && tmpset[k].pos.x==Set[k].pos.x)
{

winset[no_winners++]=&Set[k];

}
}
for (l=0;l<no_players;l++)
{
tmpset[l]=Set[l];
}
}
return;
}

Game::Game()
{
no_winners=0;
}

27 changes: 27 additions & 0 deletions Abinash/OOP_Assignment/Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef GAME_H
#define GAME_H
#include "Player.h"
#include "Dice6.h"
#include "Board.h"
#include "Dice.h"


class Game
{
public:
void StartGame();
void get_players ();//takes no of players and makes players with unique ids
Player** winset;
int no_winners;
Game();
private:

int no_players;

void trialturn();//executes one turn for each player
Player *Set;
Dice6 dice;
Board bd1;
};

#endif // GAME_H
6 changes: 6 additions & 0 deletions Abinash/OOP_Assignment/Image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Image.h"

//Image::Image(IplImage *img)
//{
// pointer=img;
//}
17 changes: 17 additions & 0 deletions Abinash/OOP_Assignment/Image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef IMAGE_H
#define IMAGE_H
//#include <cv.h>
//#include <cxcore.h>
//#include <highgui.h>


class Image
{
public:
// IplImage *pointer;
// Image(IplImage* );
protected:
private:
};

#endif // IMAGE_H
57 changes: 57 additions & 0 deletions Abinash/OOP_Assignment/OOPs PS.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="OOPs PS" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/OOPs PS" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/OOPs PS" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="Board.cpp" />
<Unit filename="Board.h" />
<Unit filename="Dice.cpp" />
<Unit filename="Dice.h" />
<Unit filename="Dice6.cpp" />
<Unit filename="Dice6.h" />
<Unit filename="Game.cpp" />
<Unit filename="Game.h" />
<Unit filename="Image.cpp" />
<Unit filename="Image.h" />
<Unit filename="Player.cpp" />
<Unit filename="Player.h" />
<Unit filename="Position.cpp" />
<Unit filename="Position.h" />
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
71 changes: 71 additions & 0 deletions Abinash/OOP_Assignment/OOPs PS.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# depslib dependency file v1.0
1394618834 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\main.cpp
<iostream>
<ctime>
<cstdlib>
"Position.h"
"Board.h"
"Player.h"
"Image.h"
"Dice.h"
"Game.h"
"Dice6.h"

1394456855 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\position.h

1394456844 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\position.cpp
"Position.h"

1394456386 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\src\position.cpp
"Position.h"

1394456386 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\src\position.h

1394562841 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\board.cpp
"Board.h"
<cstdlib>
<ctime>

1394558684 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\board.h
"Position.h"
"Image.h"

1394556573 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\image.h

1394518589 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\dice.cpp
"Dice.h"
<ctime>
<cstdlib>

1394518570 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\dice.h

1394560446 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\dice6.cpp
"Dice6.h"
<ctime>
<cstdlib>

1394476911 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\dice6.h
"Dice.h"

1394619055 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\game.cpp
"Game.h"
<conio.h>

1394618834 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\game.h
"Player.h"
"Dice6.h"
"Board.h"
"Dice.h"

1394619179 c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\player.h
"Position.h"
"Board.h"
<iostream>
<cmath>

1394556587 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\image.cpp
"Image.h"

1394619758 source:c:\users\abinash\skydrive\documents\code chef\new folder\oops ps\player.cpp
"Player.h"

79 changes: 79 additions & 0 deletions Abinash/OOP_Assignment/OOPs PS.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<ActiveTarget name="Debug" />
<File name="Position.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="475" topLine="8" />
</Cursor>
</File>
<File name="Game.h" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="219" topLine="8" />
</Cursor>
</File>
<File name="Dice.h" open="1" top="0" tabpos="12" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="86" topLine="0" />
</Cursor>
</File>
<File name="Board.h" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="233" topLine="6" />
</Cursor>
</File>
<File name="Dice6.cpp" open="1" top="0" tabpos="13" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="155" topLine="0" />
</Cursor>
</File>
<File name="Game.cpp" open="1" top="0" tabpos="10" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="445" topLine="0" />
</Cursor>
</File>
<File name="Dice.cpp" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="57" topLine="0" />
</Cursor>
</File>
<File name="Dice6.h" open="1" top="0" tabpos="11" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="111" topLine="0" />
</Cursor>
</File>
<File name="Image.h" open="1" top="0" tabpos="14" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="163" topLine="0" />
</Cursor>
</File>
<File name="Player.cpp" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2369" topLine="101" />
</Cursor>
</File>
<File name="Image.cpp" open="1" top="0" tabpos="15" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="79" topLine="0" />
</Cursor>
</File>
<File name="Player.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="239" topLine="0" />
</Cursor>
</File>
<File name="Position.h" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="109" topLine="0" />
</Cursor>
</File>
<File name="Board.cpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="186" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
120 changes: 120 additions & 0 deletions Abinash/OOP_Assignment/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "Player.h"

void Player::upgrade_score(int* x,int y)
{
switch (y){
case 1:
*x+=10;
break;
case 2:
case 4:
case 6:
(*x)+=y*2;
break;
default:
*x+=y;
break;
}
return;
}

void Player::shift_position(Position *x,int y,Board *bd)
{
int i,j,k,flag=-1;
float minim,dist;

switch (y){
case 1:
{
for (i=0,flag=-1;i<(bd->A.bn);i++)
{

if (((bd->A).b[i].x)>(x->x) && ((bd->A).b[i].y)<(x->y))
{
dist=((bd->A.b[i].x)-(x->x))*((bd->A.b[i].x)-(x->x))+((bd->A.b[i].y)-(x->y))*((bd->A.b[i].y)-(x->y));
dist=sqrt(dist);
if (i==0)minim=dist;
if (dist<minim){
minim=dist;
flag=i;
}
}
}

if(flag!=-1){(x->x)=(bd->A).b[flag].x;(x->y)=(bd->A).b[flag].y;}

}
break;
case 2:
case 4:
case 6:
{
for (j=0,flag=-1;j<(bd->A.rn);j++)
{

if (((bd->A).r[j].x)>(x->x) && ((bd->A).r[j].y)<(x->y))
{
dist=((bd->A.r[j].x)-(x->x))*((bd->A.r[j].x)-(x->x))+((bd->A.r[j].y)-(x->y))*((bd->A.r[j].y)-(x->y));
dist=sqrt(dist);
if (j==0)minim=dist;
if (dist<minim){
minim=dist;
flag=j;
}
}
}

if(flag!=-1){(x->x)=(bd->A).r[flag].x;(x->y)=(bd->A).r[flag].y;}

}
break;
default:
{
for (k=0,flag=-1;k<(bd->A.gn);k++)
{

if (((bd->A).g[k].x)>(x->x) && ((bd->A).g[k].y)<(x->y))
{
dist=((bd->A.g[k].x)-(x->x))*((bd->A.g[k].x)-(x->x))+((bd->A.g[k].y)-(x->y))*((bd->A.g[k].y)-(x->y));
dist=sqrt(dist);
if (k==0)minim=dist;
if (dist<minim){
minim=dist;
flag=k;
}
}
}

if(flag!=-1){(x->x)=(bd->A).g[flag].x;(x->y)=(bd->A).g[flag].y;}

}
break;
}
return;
}

void Player::Getdice(int dice,Board *bd)
{

upgrade_score(&score,dice);
//std::cout<<"P2";
shift_position(&pos,dice,bd);
switch (dice){
case 1:
std::cout <<"\tPlayer_"<<this->id<<" Gets a "<<dice<<" Moves to Blue Circle"<<std::endl;
break;
case 2:
case 4:
case 6:
std::cout <<"\tPlayer_"<<this->id<<" Gets a "<<dice<<" Moves to Red Circle"<<std::endl;
break;
default:
std::cout <<"\tPlayer_"<<this->id<<" Gets a "<<dice<<" Moves to Green Circle"<<std::endl;
break;
}
}

Player::Player()
{
score=0;
}
20 changes: 20 additions & 0 deletions Abinash/OOP_Assignment/Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "Position.h"
#include "Board.h"
#include <iostream>
#include <cmath>

class Player
{
public:
Position pos;
int score,id;
void Getdice(int,Board*);
Player();
private:
void shift_position(Position*,int,Board*);
void upgrade_score(int*,int);
};

#endif // PLAYER_H
7 changes: 7 additions & 0 deletions Abinash/OOP_Assignment/Position.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "Position.h"

Position::Position()
{
x=0;
y=0;//ctor
}
15 changes: 15 additions & 0 deletions Abinash/OOP_Assignment/Position.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef POSITION_H
#define POSITION_H


class Position
{
public:
int x;
int y;
Position();
protected:
private:
};

#endif // POSITION_H
Binary file added Abinash/OOP_Assignment/bin/Debug/OOPs PS.exe
Binary file not shown.
14 changes: 14 additions & 0 deletions Abinash/OOP_Assignment/include/Position.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef POSITION_H
#define POSITION_H


class Position
{
public:
int x,y;
void Position();
protected:
private:
};

#endif // POSITION_H
27 changes: 27 additions & 0 deletions Abinash/OOP_Assignment/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Position.h"
#include "Board.h"
#include "Player.h"
#include "Image.h"
#include "Dice.h"
#include "Game.h"
#include "Dice6.h"

using namespace std;

int main()
{
int b;
srand((unsigned int)time(NULL));
Game bgame;
bgame.get_players();
bgame.StartGame();
cout<<"Winners :"<<endl;
for (b=0;b<bgame.no_winners;b++)
{
cout<<"Player_"<<bgame.winset[b]->id<<endl;
}
return 0;
}
Binary file added Abinash/OOP_Assignment/obj/Debug/Board.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/Dice.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/Dice6.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/Game.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/Image.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/Player.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/Position.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/main.o
Binary file not shown.
Binary file added Abinash/OOP_Assignment/obj/Debug/src/Position.o
Binary file not shown.
6 changes: 6 additions & 0 deletions Abinash/OOP_Assignment/src/Position.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Position.h"

Position::Position()
{
x=0;y=0;
}
12 changes: 12 additions & 0 deletions Abinash/OOP_Assignment/src/Position.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef POSITION_H
#define POSITION_H


class Position
{
public:
int x,y;
Position();
};

#endif // POSITION_H
32 changes: 32 additions & 0 deletions Abinash/ROS_Assignment/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
rosbuild_add_executable(talker src/talker.cpp)
rosbuild_add_executable(listener src/listener.cpp)
1 change: 1 addition & 0 deletions Abinash/ROS_Assignment/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(shell rospack find mk)/cmake.mk
Binary file added Abinash/ROS_Assignment/bin/hello
Binary file not shown.
Binary file added Abinash/ROS_Assignment/bin/listener
Binary file not shown.
Binary file added Abinash/ROS_Assignment/bin/talker
Binary file not shown.
14 changes: 14 additions & 0 deletions Abinash/ROS_Assignment/mainpage.dox
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
\mainpage
\htmlinclude manifest.html

\b beginner_tutorials

<!--
Provide an overview of your package.
-->

-->


*/
17 changes: 17 additions & 0 deletions Abinash/ROS_Assignment/manifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<package>
<description brief="beginner_tutorials">

beginner_tutorials

</description>
<author>Abinash Meher</author>
<license>BSD</license>
<review status="unreviewed" notes=""/>
<url>http://ros.org/wiki/beginner_tutorials</url>
<depend package="std_msgs"/>
<depend package="rospy"/>
<depend package="roscpp"/>

</package>


8 changes: 8 additions & 0 deletions Abinash/ROS_Assignment/src/helloROS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello";
return 0;
}

66 changes: 66 additions & 0 deletions Abinash/ROS_Assignment/src/listener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// %Tag(FULLTEXT)%
#include "ros/ros.h"
#include "std_msgs/String.h"

/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
// %Tag(CALLBACK)%
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
// %EndTag(CALLBACK)%

int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line. For programmatic
* remappings you can use a different version of init() which takes remappings
* directly, but for most command-line programs, passing argc and argv is the easiest
* way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
ros::init(argc, argv, "listener");

/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;

/**
* The subscribe() call is how you tell ROS that you want to receive messages
* on a given topic. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. Messages are passed to a callback function, here
* called chatterCallback. subscribe() returns a Subscriber object that you
* must hold on to until you want to unsubscribe. When all copies of the Subscriber
* object go out of scope, this callback will automatically be unsubscribed from
* this topic.
*
* The second parameter to the subscribe() function is the size of the message
* queue. If messages are arriving faster than they are being processed, this
* is the number of messages that will be buffered up before beginning to throw
* away the oldest ones.
*/
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%

/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%

return 0;
}
// %EndTag(FULLTEXT)%
112 changes: 112 additions & 0 deletions Abinash/ROS_Assignment/src/talker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// %Tag(FULLTEXT)%
// %Tag(ROS_HEADER)%
#include "ros/ros.h"
// %EndTag(ROS_HEADER)%
// %Tag(MSG_HEADER)%
#include "std_msgs/String.h"
// %EndTag(MSG_HEADER)%

#include <sstream>

/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line. For programmatic
* remappings you can use a different version of init() which takes remappings
* directly, but for most command-line programs, passing argc and argv is the easiest
* way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
// %Tag(INIT)%
ros::init(argc, argv, "talker");
// %EndTag(INIT)%

/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
// %Tag(NODEHANDLE)%
ros::NodeHandle n;
// %EndTag(NODEHANDLE)%

/**
* The advertise() function is how you tell ROS that you want to
* publish on a given topic name. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. After this advertise() call is made, the master
* node will notify anyone who is trying to subscribe to this topic name,
* and they will in turn negotiate a peer-to-peer connection with this
* node. advertise() returns a Publisher object which allows you to
* publish messages on that topic through a call to publish(). Once
* all copies of the returned Publisher object are destroyed, the topic
* will be automatically unadvertised.
*
* The second parameter to advertise() is the size of the message queue
* used for publishing messages. If messages are published more quickly
* than we can send them, the number here specifies how many messages to
* buffer up before throwing some away.
*/
// %Tag(PUBLISHER)%
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
// %EndTag(PUBLISHER)%

// %Tag(LOOP_RATE)%
ros::Rate loop_rate(10);
// %EndTag(LOOP_RATE)%

/**
* A count of how many messages we have sent. This is used to create
* a unique string for each message.
*/
// %Tag(ROS_OK)%
int count = 0;
while (ros::ok())
{
// %EndTag(ROS_OK)%
/**
* This is a message object. You stuff it with data, and then publish it.
*/
// %Tag(FILL_MESSAGE)%
std_msgs::String msg;

std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
// %EndTag(FILL_MESSAGE)%

// %Tag(ROSCONSOLE)%
ROS_INFO("%s", msg.data.c_str());
// %EndTag(ROSCONSOLE)%

/**
* The publish() function is how you send messages. The parameter
* is the message object. The type of this object must agree with the type
* given as a template parameter to the advertise<>() call, as was done
* in the constructor above.
*/
// %Tag(PUBLISH)%
chatter_pub.publish(msg);
// %EndTag(PUBLISH)%

// %Tag(SPINONCE)%
ros::spinOnce();
// %EndTag(SPINONCE)%

// %Tag(RATE_SLEEP)%
loop_rate.sleep();
// %EndTag(RATE_SLEEP)%
++count;
}


return 0;
}
// %EndTag(FULLTEXT)%