2022-11-21 21:39:54 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "rec/robotino/api2/all.h"
|
2022-12-20 11:42:17 +01:00
|
|
|
#include <iostream>
|
2022-11-21 21:39:54 +01:00
|
|
|
|
|
|
|
using namespace rec::robotino::api2;
|
2022-12-20 11:42:17 +01:00
|
|
|
using namespace std;
|
2022-11-21 21:39:54 +01:00
|
|
|
bool _run = true;
|
|
|
|
|
|
|
|
class MyCom : public Com
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyCom()
|
|
|
|
: Com( "example_wallfollow" )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void errorEvent( const char* errorString )
|
|
|
|
{
|
|
|
|
std::cerr << "Error: " << errorString << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connectedEvent()
|
|
|
|
{
|
|
|
|
std::cout << "Connected." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connectionClosedEvent()
|
|
|
|
{
|
|
|
|
std::cout << "Connection closed." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void logEvent( const char* message, int level )
|
|
|
|
{
|
|
|
|
std::cout << message << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyBumper : public Bumper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyBumper()
|
|
|
|
: bumped( false )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void bumperEvent( bool hasContact )
|
|
|
|
{
|
|
|
|
bumped |= hasContact;
|
|
|
|
if (true == bumped){
|
|
|
|
std::cout << "Bumper has " << ( hasContact ? "contact" : "no contact") << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bumped;
|
|
|
|
};
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
double x_distance = 0;
|
|
|
|
double y_distance = 0;
|
|
|
|
double phi_absolute = 0;
|
|
|
|
unsigned int sequence = 0;
|
|
|
|
|
|
|
|
class MyOdometry: public Odometry{
|
|
|
|
void readingsEvent(double x,double y, double phi, float vx, float vy, float omega, unsigned int seq ){
|
|
|
|
cout << "X: " << x << " Y: " << y << " Richtung: " << phi << " Sequenz: " << seq << endl;
|
|
|
|
x_distance = x;
|
|
|
|
y_distance = y;
|
|
|
|
phi_absolute = phi;
|
|
|
|
sequence = seq;
|
|
|
|
}
|
|
|
|
};
|
2022-11-21 21:39:54 +01:00
|
|
|
|
|
|
|
MyCom com;
|
2022-12-20 11:42:17 +01:00
|
|
|
MyBumper bumper;
|
2022-11-21 21:39:54 +01:00
|
|
|
OmniDrive omniDrive;
|
2022-12-20 11:42:17 +01:00
|
|
|
MyOdometry odo;
|
|
|
|
|
|
|
|
|
2022-11-21 21:39:54 +01:00
|
|
|
|
|
|
|
void init( const std::string& hostname )
|
|
|
|
{
|
|
|
|
|
|
|
|
// Connect
|
|
|
|
std::cout << "Connecting...";
|
|
|
|
com.setAddress( hostname.c_str() );
|
|
|
|
|
|
|
|
com.connectToServer( true );
|
|
|
|
|
|
|
|
if( false == com.isConnected() )
|
|
|
|
{
|
|
|
|
std::cout << std::endl << "Could not connect to " << com.address() << std::endl;
|
|
|
|
rec::robotino::api2::shutdown();
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "success" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
|
|
|
|
bool DriveorRotate = true; //TRUE == FAHRENB
|
|
|
|
void rotateLeft(){
|
|
|
|
int Rotation = 0;
|
|
|
|
int turning = 0;
|
|
|
|
cout << "Enter Rotation (2.96 Ganze Umdrehung): ";
|
|
|
|
cin >> Rotation;
|
|
|
|
|
|
|
|
if (0 <= Rotation){
|
|
|
|
turning = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1 == turning){
|
|
|
|
com.processEvents();
|
|
|
|
omniDrive.setVelocity(0, 0, -0.4);
|
|
|
|
cout << phi_absolute;
|
|
|
|
rec::robotino::api2::msleep(100);
|
|
|
|
// cout << "X: " << x << " Y: " << y << "Richtung: " << phi << "Sequenz: " << s << endl;
|
|
|
|
if (Rotation <= phi_absolute){
|
|
|
|
turning = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
com.processComEvents();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void rotateRight(){
|
|
|
|
int Rotation = 0;
|
|
|
|
int turning = 0;
|
|
|
|
cout << "Enter Rotation (2.96 Ganze Umdrehung): ";
|
|
|
|
cin >> Rotation;
|
|
|
|
|
|
|
|
if (0 <= Rotation){
|
|
|
|
turning = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1 == turning){
|
|
|
|
com.processEvents();
|
|
|
|
omniDrive.setVelocity(0, 0, 0.4);
|
|
|
|
cout << phi_absolute;
|
|
|
|
rec::robotino::api2::msleep(100);
|
|
|
|
// cout << "X: " << x << " Y: " << y << "Richtung: " << phi << "Sequenz: " << s << endl;
|
|
|
|
if (Rotation <= phi_absolute){
|
|
|
|
turning = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
com.processComEvents();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-11-21 21:39:54 +01:00
|
|
|
void drive()
|
|
|
|
{
|
2022-12-20 11:42:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Straight = 0, Backwards = 1, RightTop = 2, Right = 3, BottomRight = 4, BottomLeft = 5, Left = 6, TopLeft = 7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char w;
|
|
|
|
odo.set(0,0,0,-1);
|
|
|
|
|
|
|
|
|
2022-11-21 21:39:54 +01:00
|
|
|
while( com.isConnected() && false == bumper.value() && _run )
|
|
|
|
{
|
2022-12-20 11:42:17 +01:00
|
|
|
|
|
|
|
rec::robotino::api2::msleep(100);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO: your code here
|
2022-11-21 21:39:54 +01:00
|
|
|
//omniDrive.setVelocity( xspeed, yspeed, rotspeed );
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
system("stty raw");
|
|
|
|
w = getchar();
|
|
|
|
// terminate when "." is pressed
|
|
|
|
system("stty cooked");
|
|
|
|
system("clear");
|
|
|
|
std::cout << w << " was pressed."<< std::endl;
|
2022-11-21 21:39:54 +01:00
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if (w == 'w'){
|
|
|
|
omniDrive.setVelocity(0.2, 0, 0);
|
|
|
|
}
|
2022-11-21 21:39:54 +01:00
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if (w == 'a'){
|
|
|
|
omniDrive.setVelocity(0, -0.2, 0);
|
|
|
|
}
|
2022-11-21 21:39:54 +01:00
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if (w == 's'){
|
|
|
|
omniDrive.setVelocity(-0.2, 0, 0);
|
|
|
|
|
|
|
|
}
|
2022-11-21 21:39:54 +01:00
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if (w == 'd'){
|
|
|
|
omniDrive.setVelocity(0, 0.2, 0);
|
|
|
|
}
|
2022-11-21 21:39:54 +01:00
|
|
|
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if (w == 'r'){
|
|
|
|
rotateRight();
|
|
|
|
}
|
2022-11-21 21:39:54 +01:00
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if (w == 'q'){
|
|
|
|
rotateLeft();
|
|
|
|
}
|
2022-11-21 21:39:54 +01:00
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
if(w == '.') {
|
|
|
|
system("stty cooked");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-21 21:39:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
com.processEvents();
|
|
|
|
rec::robotino::api2::msleep(10);
|
2022-11-21 21:39:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-11-21 21:39:54 +01:00
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
com.disconnectFromServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
std::string hostname = "172.27.1.1";
|
|
|
|
if( argc > 1 )
|
|
|
|
{
|
|
|
|
hostname = argv[1];
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:42:17 +01:00
|
|
|
|
2022-11-21 21:39:54 +01:00
|
|
|
|
|
|
|
//main program routine starts in here
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//connect to your robotino (or try at least)
|
|
|
|
init( hostname );
|
|
|
|
//drive (this contains a while loop)
|
|
|
|
drive();
|
|
|
|
//exit
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
catch( const rec::robotino::api2::RobotinoException& e )
|
|
|
|
{
|
|
|
|
std::cerr << "Com Error: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
catch( const std::exception& e )
|
|
|
|
{
|
|
|
|
std::cerr << "Error: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
std::cerr << "Unknow Error" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec::robotino::api2::shutdown();
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
std::cout << "Press any key to exit..." << std::endl;
|
|
|
|
rec::robotino::api2::waitForKey();
|
|
|
|
#endif
|
|
|
|
}
|
2022-12-20 11:42:17 +01:00
|
|
|
|