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

Adding pause_function to Speed Dreams robots #13

Open
wants to merge 7 commits into
base: ACC
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions cmake/internaldeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ MACRO(ADD_SDLIB_INCLUDEDIR)
FIND_PATH(SDLIB_SENSORS_INCLUDE_DIR positionTracker.h PATHS ${INCLUDE_CANDIDATE} /usr/include /usr/local/include)
MARK_AS_ADVANCED(SDLIB_SENSORS_INCLUDE_DIR)
SET(SDLIB_EXTRA_INCLUDEDIR SDLIB_SENSORS_INCLUDE_DIR)
ELSEIF(SDLIB_LIB STREQUAL "gamepause")
IF(IN_SOURCETREE)
SET(INCLUDE_CANDIDATE ${SOURCE_DIR}/src/libs/gamepause)
ELSE(IN_SOURCETREE)
SET(INCLUDE_CANDIDATE ${SD_INCLUDEDIR_ABS})
ENDIF(IN_SOURCETREE)
FIND_PATH(SDLIB_GAMEPAUSE_INCLUDE_DIR gamepause.h PATHS ${INCLUDE_CANDIDATE} /usr/include /usr/local/include NO_DEFAULT_PATH)
FIND_PATH(SDLIB_GAMEPAUSE_INCLUDE_DIR gamepause.h PATHS ${INCLUDE_CANDIDATE} /usr/include /usr/local/include)
MARK_AS_ADVANCED(SDLIB_GAMEPAUSE_INCLUDE_DIR)
SET(SDLIB_EXTRA_INCLUDEDIR SDLIB_GAMEPAUSE_INCLUDE_DIR)
ELSEIF(SDLIB_LIB STREQUAL "math")
IF(IN_SOURCETREE)
SET(INCLUDE_CANDIDATE ${SOURCE_DIR}/src/libs/math)
Expand Down
29 changes: 29 additions & 0 deletions src/drivers/ACC/ACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
//including the needed gamepause library functionality
#include <gamepause.h>
gamepause::TimeMeasurement timebox;

#include <positionTracker.h>

Expand Down Expand Up @@ -117,6 +120,11 @@ static void newrace(int index, tCarElt* car, tSituation *s) {
PLogACC->error("socket failed! %s\n", strerror(errno));
}

int optval = 1;
setsockopt(serverSock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));



struct sockaddr_in srv_addr, cli_addr;
bzero(&srv_addr, sizeof(srv_addr));
srv_addr.sin_family = AF_INET;
Expand Down Expand Up @@ -161,6 +169,10 @@ static void readAllBytes(void *buf, int socket, unsigned int size) {

/* Drive during race. */
static void drive(int index, tCarElt* car, tSituation *s) {

//pauses the RaceEngine
timebox.TimedRacePause();

/* ACC */
g_tracker.updatePosition(car, s, curTrack);

Expand Down Expand Up @@ -239,6 +251,23 @@ static void drive(int index, tCarElt* car, tSituation *s) {
car->_brakeRRCmd = cdi.brakerr();
car->_brakeCmd = cdi.brakefl() + cdi.brakefr() + cdi.brakerl() + cdi.brakerr() / 4.0;
car->_gearCmd = cdi.gear();

//resume the RaceEngine
timebox.TimedRaceResume();

//triggering the calculations which are written to the global values
timebox.avgcalc();
timebox.mincalc();
timebox.maxcalc();

//printing the values to the Speed Dreams console as info
GfLogInfo("Elapsed time during stop of the Game Engine (last step): %d milliseconds\n",timebox.getDuration());
GfLogInfo("Elapsed time during stop of the Game Engine (%d steps): %d milliseconds\n",timebox.getStopcounter(),timebox.getTotalduration());
GfLogInfo("Minimum time per stop: %d milliseconds\n",timebox.getMincounter());
GfLogInfo("Average time per stop: %d milliseconds\n",timebox.getAvgcounter());
GfLogInfo("Maximum time per stop: %d milliseconds\n",timebox.getMaxcounter());


}

/* End of the current race */
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/ACC/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) # protobuf generated files

SET(ROBOT_SOURCES ${ROBOT_NAME}.cpp ${PROTO_SRCS} ${PROTO_HDRS})

ADD_SDLIB_INCLUDEDIR(robottools tgf sensors)
ADD_SDLIB_INCLUDEDIR(robottools tgf sensors gamepause)

ROBOT_MODULE(NAME ${ROBOT_NAME} VERSION 1.0.0 SOVERSION 1.0.0
INTERFACE WELCOME
SOURCES ${ROBOT_SOURCES}
)

ADD_SDLIB_LIBRARY(${ROBOT_NAME} robottools tgf sensors)
ADD_SDLIB_LIBRARY(${ROBOT_NAME} robottools tgf sensors gamepause)

ADD_PROTOBUF_INCLUDEDIR()
ADD_PROTOBUF_LIBRARY(${ROBOT_NAME})
4 changes: 2 additions & 2 deletions src/drivers/human/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ SET(ROBOT_NAME "human")
SET(ROBOT_INTERFACE WELCOME)
SET(ROBOT_SOURCES ${ROBOT_NAME}.cpp)

ADD_SDLIB_INCLUDEDIR(robottools tgfclient sensors)
ADD_SDLIB_INCLUDEDIR(robottools tgfclient sensors gamepause)

ROBOT_MODULE(NAME ${ROBOT_NAME} VERSION 1.0.0 SOVERSION 1.0.0
INTERFACE ${ROBOT_INTERFACE}
SOURCES ${ROBOT_SOURCES})

ADD_SDLIB_LIBRARY(${ROBOT_NAME} robottools tgfclient sensors)
ADD_SDLIB_LIBRARY(${ROBOT_NAME} robottools tgfclient sensors gamepause)

# For data associated to the robot module, see data/drivers/human/CMakeLists.txt
44 changes: 43 additions & 1 deletion src/drivers/human/human.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@
*/

#include <humandriver.h>
#include <robot.h>
#include <algorithm>


#include <gpsSensor.h>
//including the needed gamepause library functionality
#include <gamepause.h>

//Instantiating the timebox object to have time measurements for stops of the robot
gamepause::TimeMeasurement timebox;


static HumanDriver robot("human");
int counter = 0;

static void initTrack(int index, tTrack* track, void *carHandle, void **carParmHandle, tSituation *s);
static void drive_mt(int index, tCarElt* car, tSituation *s);
Expand Down Expand Up @@ -237,9 +247,41 @@ drive_mt(int index, tCarElt* car, tSituation *s)
static void
drive_at(int index, tCarElt* car, tSituation *s)
{
//counter of simulation steps
counter++;
//every 500 simulation steps do a RacePause to show functionality
if (counter % 250 == 0){

//pauses the RaceEngine
//gamepause::RacePause(); //static function without time measurement
timebox.TimedRacePause(); //with time measurement

//randomly sleep to test calculations afterwards
//sleep(3);
sleep(rand()/ 1000000000);

//resume the RaceEngine
//gamepause::RaceResume(); //static function without time measurement
timebox.TimedRaceResume(); //with time measurement


//triggering the calculations which are written to the global values
timebox.avgcalc();
timebox.mincalc();
timebox.maxcalc();

//printing the values to the Speed Dreams console as info
GfLogInfo("Elapsed time during stop of the Game Engine (last step): %d milliseconds\n",timebox.getDuration());
GfLogInfo("Elapsed time during stop of the Game Engine (%d steps): %d milliseconds\n",timebox.getStopcounter(),timebox.getTotalduration());
GfLogInfo("Minimum time per stop: %d milliseconds\n",timebox.getMincounter());
GfLogInfo("Average time per stop: %d milliseconds\n",timebox.getAvgcounter());
GfLogInfo("Maximum time per stop: %d milliseconds\n",timebox.getMaxcounter());
}//sample implementation of the pause function for robots


gps.update(car);
vec2 myPos = gps.getPosition();
printf("Players's position according to GPS is (%f, %f)\n", myPos.x, myPos.y);
//printf("Players's position according to GPS is (%f, %f)\n", myPos.x, myPos.y);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be readded


robot.drive_at(index, car, s);
}//drive_at
Expand Down
1 change: 1 addition & 0 deletions src/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ADD_SUBDIRECTORY(robottools)
ADD_SUBDIRECTORY(learning)
ADD_SUBDIRECTORY(math)
ADD_SUBDIRECTORY(portability)
ADD_SUBDIRECTORY(gamepause)


# Work-in-progress
Expand Down
24 changes: 24 additions & 0 deletions src/libs/gamepause/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
INCLUDE(../../../cmake/macros.cmake)


ADD_INTERFACE_INCLUDEDIR()
ADD_SDLIB_INCLUDEDIR(tgf)

SET(GAMEPAUSE_HEADERS gamepause.h)

SET(GAMEPAUSE_SOURCES gamepause.cpp)

SD_ADD_LIBRARY(gamepause SHARED ${GAMEPAUSE_SOURCES} ${GAMEPAUSE_HEADERS})

ADD_SDLIB_LIBRARY(gamepause tgf)

IF(WIN32)
SD_INSTALL_FILES(BIN TARGETS gamepause)
ELSE(WIN32)
SD_INSTALL_FILES(LIB lib TARGETS gamepause)
ENDIF(WIN32)

SD_INSTALL_FILES(INCLUDE gamepause FILES ${GAMEPAUSE_HEADERS})


INCLUDE_DIRECTORIES(../../modules/userinterface/legacymenu)
138 changes: 138 additions & 0 deletions src/libs/gamepause/gamepause.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/***************************************************************************

created : Sat Mar 18 23:16:38 CET 2000
copyright : (C) 2000 by Eric Espie
email : [email protected]
version : $Id: human.cpp 5522 2013-06-17 21:03:25Z torcs-ng $

***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "legacymenu.h"
#include "gamepause.h"
#include <algorithm>
#include <chrono>

namespace gamepause{


TimeMeasurement::TimeMeasurement()
{
stopcounter,avgcounter,mincounter,maxcounter,duration,totalduration = 0;

}

TimeMeasurement::~TimeMeasurement()
{

}





void
TimeMeasurement::TimedRaceResume()
{

//resetting the duration
duration = 0;

RaceResume();

//Taking the stopvalue time
stopvalue = std::chrono::system_clock::now();


//calculating duration between the given startvalue and the taken stopvalue time
duration = std::chrono::duration_cast<std::chrono::milliseconds>(stopvalue - startvalue).count();


//adding of the actual calculated duration to the total amount of the game being paused
totalduration += duration;


//updating the stopcounter, which indicates how often the game was stopvalueped.
stopcounter++;


}//Resuming the race for the robots

void
TimeMeasurement::TimedRacePause()
{
//Taking the startvalue time
startvalue = std::chrono::system_clock::now();

RacePause();

}//Pausing the race for the robots

void
TimeMeasurement::maxcalc()
{

//calculating, returning and writing the maximum value to the globally available variable
maxcounter = std::max(maxcounter,duration);


}//calculating maximum duration time taken for one step


void
TimeMeasurement::mincalc()
{

//if it is the first simulation step set minval as totest
//otherwise minval will always be zero
if (stopcounter == 1) mincounter = duration;

//calculating and writing the minimum value to the globally available variable
mincounter = std::min(mincounter,duration);


}//calculating minimum duration time taken for one step

void
TimeMeasurement::avgcalc()
{
//calculating, returning and writing the average value to the globally available variable
avgcounter = totalduration / stopcounter;

}//calculating average duration time taken all previous steps and durations



void
RaceResume()
{

//see whether the game is started with a gui and sound and resume the sound
if (LegacyMenu::self().soundEngine())
LegacyMenu::self().soundEngine()->mute(false);


//Resuming the RaceEngine
LmRaceEngine().start();
}

void
RacePause()
{
//see whether the game is started with a gui and sound and resume the sound
if (LegacyMenu::self().soundEngine())
LegacyMenu::self().soundEngine()->mute(true);

//stopvalueping the RaceEngine
LmRaceEngine().stop();
}

}
Loading