0% found this document useful (0 votes)
78 views1 page

OOP for Mobile Robot in C++

The document describes how to create an object-oriented C++ program to model the movement and orientation of a mobile robot. It defines a Robot class with methods to initialize the robot's position, move it forwards/backwards by a distance, rotate it by degrees, and print its position. A main() function tests the class by moving a robot around a rectangle and returning it to its starting point, printing the position after each movement.

Uploaded by

SAKTHIVEL A
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views1 page

OOP for Mobile Robot in C++

The document describes how to create an object-oriented C++ program to model the movement and orientation of a mobile robot. It defines a Robot class with methods to initialize the robot's position, move it forwards/backwards by a distance, rotate it by degrees, and print its position. A main() function tests the class by moving a robot around a rectangle and returning it to its starting point, printing the position after each movement.

Uploaded by

SAKTHIVEL A
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Tutorial: robot.cpp
  • Tutorial: robot.h
  • Tutorial: Object-Oriented Programming using C++
  • Tutorial: robotMain.cpp

Tutorial: Object-Oriented Programming using C++ Tutorial: robot.

h
! Define an object to represent the position of a mobile robot on a 2- #define PI 3.14159265
dimensional grid and the direction in which it is facing.
! Write a constructor method. class robot {
! Write methods to move the robot forwards and backwards by a private:
certain distance. double x;
! Write a method to rotate the robot clockwise by a certain number double y;
of degrees. double r; // rotation clockwise wrt due north
! Write a method to print the position and orientation of the robot. public:
! Write a test program that makes the robot traverse a rectangle robot ();
and return to its starting point. The program should print the void forwards (double distance);
position of the report after each movement. void backwards (double distance);
void rotate (double degrees);
void print_position ();
Hint: the following can be used to calculate sine and cosine where x (a };
double) is in degrees:
#define PI 3.14159265
#include <math.h>
sin(x * PI / 180.0)
cos(x * PI / 180.0)

Tutorial: [Link] Tutorial: [Link]


#include <iostream.h> #include "robot.h"
#include <math.h> void main()
#include "robot.h"
{
robot::robot ()
{ robot myRobot;
x = 0.0; myRobot.print_position();
y = 0.0; // up
r = 0.0; [Link](100);
} myRobot.print_position();
void robot::forwards (double distance)
// right
{
x += distance * sin(r * PI / 180.0); [Link](90);
y += distance * cos(r * PI / 180.0); [Link](200);
return; myRobot.print_position();
} // down
void robot::backwards (double distance) [Link](-90-360);
{
x -= distance * sin(r * PI / 180.0);
[Link](50);
y -= distance * cos(r * PI / 180.0); [Link](50);
return; myRobot.print_position();
} // left
void robot::rotate (double degrees) [Link](-90+360*2);
{ [Link](200);
r += degrees;
if ((r >= 360) || (r <= -360))
myRobot.print_position(); // back at origin
r = r - (double) floor(r/360)*360; }
if (r < 0) --> c++ -o [Link] [Link] [Link]
r = r + 360; --> [Link]
return; X: 0, Y: 0, R: 0
} X: 0, Y: 100, R: 0
void robot::print_position ()
{ X: 200, Y: 100, R: 90
cout << "X: " << x << ", Y: " << y << ", R: " << r << "\n"; X: 200, Y: 3.58979e-07, R: 0
return; X: 0, Y: -7.17959e-07, R: 270
} -->

Tutorial: Object-Oriented Programming using C++
!
Define an object to represent the position of a mobile robot on a 2-
dimens

You might also like