diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Real-Time Systems/RTS_A8/src | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Real-Time Systems/RTS_A8/src')
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.cpp | 84 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.h | 45 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Display.cpp | 111 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Display.h | 37 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Fahren.cpp | 44 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Fahren.h | 25 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Notbremse.cpp | 34 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Notbremse.h | 30 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/POS.h | 69 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Task.cpp | 51 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Task.h | 28 | ||||
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/main.cpp | 78 |
12 files changed, 636 insertions, 0 deletions
diff --git a/Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.cpp b/Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.cpp new file mode 100644 index 0000000..4d7c027 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.cpp @@ -0,0 +1,84 @@ +/* + * ApplicationWindow.cpp + * + * Created on: 01.12.2010 + * Author: sven + */ +#include <GL/gl.h> +#include <GL/glut.h> +#include "ApplicationWindow.h" + +ApplicationWindow::ApplicationWindow(int width,int height,int bpp) +:mWidth(width),mHeight(height),mBpp(bpp),mCaption("RTS_A8"),mState(0) +{ +} + +ApplicationWindow::~ApplicationWindow() +{ + destroy(); +} + +bool ApplicationWindow::create() +{ + int a=0; + glutInit(&a, NULL); + glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); + glutInitWindowSize(mWidth, mHeight); + glutCreateWindow(mCaption.c_str()); + + /* + * Now Initialize OpenGL (ES) + */ + glEnable(GL_LINE_SMOOTH); + glShadeModel(GL_SMOOTH); + glDisable(GL_ALPHA_TEST); + glAlphaFunc(GL_EQUAL, 1.0f); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glViewport(0, 0, mWidth, mHeight); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glClearColor(0.0f,0.0f,0.0f,1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // ist das hier notwendig? + glOrtho(0, mWidth, 0, mHeight, 0, 100); + + return true; +} + +//-------------------------------------------------------------------- +bool ApplicationWindow::destroy() +{ + return true; +} + +//-------------------------------------------------------------------- +void ApplicationWindow::setResolution( int width, int height ) +{ + mWidth = width; + mHeight = height; +} + +//-------------------------------------------------------------------- +void ApplicationWindow::setColorDepth( int bpp ) +{ + mBpp = bpp; +} + +//-------------------------------------------------------------------- +void ApplicationWindow::setCaption( const std::string& caption ) +{ + mCaption = caption; +} + +void ApplicationWindow::setFullscreen( bool enable ) +{ + if( enable ) + { + mState |= FULL_SCREEN; + } + else + { + mState &= ~FULL_SCREEN; + } +} diff --git a/Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.h b/Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.h new file mode 100644 index 0000000..e747e2b --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/ApplicationWindow.h @@ -0,0 +1,45 @@ +/* + * ApplicationWindow.h + * + * Created on: 01.12.2010 + * Author: sven + */ + +#ifndef APPLICATIONWINDOW_H_ +#define APPLICATIONWINDOW_H_ + +#include <string> + +class ApplicationWindow { +public: + ApplicationWindow(int width,int height,int bpp); + virtual ~ApplicationWindow(); + bool create(); + bool destroy(); + void setResolution( int width, int height ); + void setColorDepth( int bpp ); + void setCaption( const std::string& caption ); + void setFullscreen( bool enable = true ); + + enum + { + OPENED_WINDOW = 0x0001, + FULL_SCREEN = 0x0002 + }; +private: + int mWidth; + int mHeight; + int mBpp; + + std::string mCaption; + char mState; + + //---------------------------------------------------------------- + /// + /// copy constructor and assign operator + /// set private + ApplicationWindow(const ApplicationWindow& src); + ApplicationWindow& operator=(const ApplicationWindow& src); +}; + +#endif /* APPLICATIONWINDOW_H_ */ diff --git a/Master/Real-Time Systems/RTS_A8/src/Display.cpp b/Master/Real-Time Systems/RTS_A8/src/Display.cpp new file mode 100644 index 0000000..7f8e925 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Display.cpp @@ -0,0 +1,111 @@ +/* + * Display.cpp + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#include "Display.h" +#ifndef POS_H_ + #include "POS.h" +#endif +#include <iostream> +#include <pthread.h> +#include <GL/gl.h> +#include <GL/glu.h> +#include <GL/glut.h> +#include <stdlib.h> + +using namespace std; + +extern POS vehiclePosition; +extern double lineDir; +const int WIN_WIDTH = 600; +const int WIN_HEIGHT = 600; +const int WIN_BPP = 32; + +// because of static +Display* Display::msDispPtr = NULL; + +void* dispInit(void*) { + ApplicationWindow appWin(WIN_WIDTH,WIN_HEIGHT,WIN_BPP); + if(!appWin.create()) { + cout << "error creating app window. exiting" << endl; + exit(1); + } + Display::msDispPtr->mStarted = true; + glutDisplayFunc(displayFunc); + glutMainLoop(); + return NULL; +} + +Display::Display(int t) +:Task(t),mStarted(false) +{ + Display::msDispPtr = this; + sem_init(&dispSem,0,0); + pthread_create(&mDispThread,NULL,dispInit,NULL); +} + +Display::~Display() { +} + +void Display::execute() +{ + cout << "...Display::execute T="<< T <<endl; + pthread_mutex_unlock(&vehicleMutex); + while(true) { + pthread_mutex_lock(&vehicleMutex); + vehiclePosition.display("P="); +// cout << "Abstand = " << Abstand(p,lineDir) << endl; + pthread_mutex_unlock(&vehicleMutex); + if(Display::msDispPtr != NULL && mStarted) { + sem_post(&dispSem); + } + waitForNextCycle(); + } + return; +} +Display* Display::getDisplay() +{ + return Display::msDispPtr; +} +void displayFunc() { + double posX,posY,dir; + const int size = 8; + + pthread_mutex_lock(&vehicleMutex); + posX = vehiclePosition.x; + posY = vehiclePosition.y; + dir = lineDir; + pthread_mutex_unlock(&vehicleMutex); + + double px = WIN_WIDTH; + double m = tan(dir*PI/180); + double py = px*m; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glLineWidth(5.0); + glBegin(GL_LINES); + glColor3f(0.0f,0.0f,1.0f); + //origin + glVertex3f(0.0f,0.0f,0.0f); + //point + glVertex3f((float)px,(float)py,0.0f); + glEnd(); + + glBegin(GL_QUADS); + glColor3f(0.0f,1.0f,0.0f); + glVertex3f(posX-size,posY+size,0.0f); + glVertex3f(posX+size,posY+size,0.0f); + glVertex3f(posX+size,posY-size,0.0f); + glVertex3f(posX-size,posY-size,0.0f); + glEnd(); + + glFlush(); + glutSwapBuffers(); + glutPostRedisplay(); + + sem_wait(&Display::msDispPtr->dispSem); +} diff --git a/Master/Real-Time Systems/RTS_A8/src/Display.h b/Master/Real-Time Systems/RTS_A8/src/Display.h new file mode 100644 index 0000000..dd3446a --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Display.h @@ -0,0 +1,37 @@ +/* + * Display.h + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#ifndef DISPLAY_H_ +#define DISPLAY_H_ + +#include "ApplicationWindow.h" +#include "Task.h" + +extern pthread_mutex_t vehicleMutex; +extern const double PI; +extern const int WIN_WIDTH; +extern const int WIN_HEIGHT; +extern const int WIN_BPP; + +void displayFunc(); + +class Display : public Task { +public: + Display(int); + virtual ~Display(); + virtual void execute(); + friend void displayFunc(); + friend void* dispInit(void*); + static Display* getDisplay(); +private: + static Display* msDispPtr; + pthread_t mDispThread; + bool mStarted; + sem_t dispSem; +}; + +#endif /* DISPLAY_H_ */ diff --git a/Master/Real-Time Systems/RTS_A8/src/Fahren.cpp b/Master/Real-Time Systems/RTS_A8/src/Fahren.cpp new file mode 100644 index 0000000..12c77a0 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Fahren.cpp @@ -0,0 +1,44 @@ +/* + * Fahren.cpp + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#include "Fahren.h" +#ifndef POS_H_ + #include "POS.h" +#endif +#include <iostream> +using namespace std; + +extern POS vehiclePosition; +extern double lineDir; + +Fahren::Fahren(int t, int v, int d) +:Task(t),V(v),D(d) +{ +} + +Fahren::~Fahren() { +} +void Fahren::execute() +{ + cout << "...Fahren::execute T="<< T << endl; + while(true) { + pthread_mutex_lock(&vehicleMutex); + double realV = (T*V) / 1000; + vehiclePosition.move(realV,D); + double entf = Abstand(vehiclePosition,lineDir); + pthread_mutex_unlock(&vehicleMutex); +// cout << "entf: " << entf << endl; + const int deltha = 1; + if(entf < 0) + D += deltha; + else if (entf > 0) + D -= deltha; + + waitForNextCycle(); +// cout << "Fahren::execute next cycle" << endl; + } +} diff --git a/Master/Real-Time Systems/RTS_A8/src/Fahren.h b/Master/Real-Time Systems/RTS_A8/src/Fahren.h new file mode 100644 index 0000000..987f4f7 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Fahren.h @@ -0,0 +1,25 @@ +/* + * Fahren.h + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#ifndef FAHREN_H_ +#define FAHREN_H_ + +#include "Task.h" + +extern pthread_mutex_t vehicleMutex; + +class Fahren : public Task { +public: + Fahren(int,int,int); + virtual ~Fahren(); + virtual void execute(); +private: + int V; + int D; +}; + +#endif /* FAHREN_H_ */ diff --git a/Master/Real-Time Systems/RTS_A8/src/Notbremse.cpp b/Master/Real-Time Systems/RTS_A8/src/Notbremse.cpp new file mode 100644 index 0000000..dc7f02f --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Notbremse.cpp @@ -0,0 +1,34 @@ +/* + * Notbremse.cpp + * + * Created on: 01.12.2010 + * Author: sven + */ + +#include "Notbremse.h" +#include <iostream> +#include <stdlib.h> +#include <cmath> +using namespace std; + +Notbremse::Notbremse(int t) +:Task(t),DIST_THRESHOLD(50) +{ +} + +Notbremse::~Notbremse() { + // TODO Auto-generated destructor stub +} +void Notbremse::execute() { + cout << "...Notbremse::executeT=" << T <<endl; + while(true) { + pthread_mutex_lock(&vehicleMutex); + double dist = Abstand(vehiclePosition,lineDir); + if(abs(dist) > DIST_THRESHOLD) { + cout << "NOTBREMSE" << endl; + exit(1); + } + pthread_mutex_unlock(&vehicleMutex); + waitForNextCycle(); + } +} diff --git a/Master/Real-Time Systems/RTS_A8/src/Notbremse.h b/Master/Real-Time Systems/RTS_A8/src/Notbremse.h new file mode 100644 index 0000000..86f092a --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Notbremse.h @@ -0,0 +1,30 @@ +/* + * Notbremse.h + * + * Created on: 01.12.2010 + * Author: sven + */ + +#ifndef NOTBREMSE_H_ +#define NOTBREMSE_H_ + +#include "Task.h" +#ifndef POS_H_ + #include "POS.h" +#endif +#include <pthread.h> + +extern pthread_mutex_t vehicleMutex; +extern POS vehiclePosition; +extern double lineDir; + +class Notbremse: public Task { +public: + Notbremse(int t); + virtual ~Notbremse(); + virtual void execute(); +private: + const double DIST_THRESHOLD; +}; + +#endif /* NOTBREMSE_H_ */ diff --git a/Master/Real-Time Systems/RTS_A8/src/POS.h b/Master/Real-Time Systems/RTS_A8/src/POS.h new file mode 100644 index 0000000..490838a --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/POS.h @@ -0,0 +1,69 @@ +/* + * POS.h + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#ifndef POS_H_ +#define POS_H_ + +#include <iostream> +#include <cmath> + +using namespace std; + +extern const double PI; + +struct vector3d { + double x; + double y; + double z; + vector3d(double px,double py):x(px),y(py),z(1) {}; + vector3d():x(0),y(0),z(1) {}; + double length() { + return sqrt( (x*x) + (y*y) ); + } + double scalarProduct(const vector3d& l) { + return (x*l.x)+(y*l.y)+(z*l.z); + } + void crossProduct(const vector3d& l,vector3d& res) + { + res.x = ( (y*l.z) - (z*l.y) ); + res.y = ( (z*l.x) - (x*l.z) ); + res.z = ( (x*l.y) - (y*l.x) ); + } + void normalize() + { + double len = length(); + x = x / len; + y = y / len; + z = z / len; + } +}; + +struct POS { + double x; + double y; + void move(double v, double dir) + { +// cout << "v: " << v << " dir: " << dir <<endl; + double dx = v * cos(dir*PI/180); + double dy = v * sin(dir*PI/180); +// cout << "dx: " << dx << " dy: " << dy <<endl; + x += dx; + y += dy; + } + void display(const char* prefix) { + cout << prefix << " "<< x << " " << y << endl; + } +}; + +#ifndef ABSTAND +#define ABSTAND + +extern double Abstand(POS p,double Ldir); + +#endif + +#endif /* POS_H_ */ diff --git a/Master/Real-Time Systems/RTS_A8/src/Task.cpp b/Master/Real-Time Systems/RTS_A8/src/Task.cpp new file mode 100644 index 0000000..e0bce66 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Task.cpp @@ -0,0 +1,51 @@ +/* + * Task.cpp + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#include "Task.h" +#include <pthread.h> +#include <iostream> +#include <sys/time.h> +#include <signal.h> + +using namespace std; + +void* timer_main(void* task) { + Task* t = (Task*) task; + while(true) { + usleep(t->T*1000); + sem_post(&t->threadSem); + } + return NULL; +} + +void* execute_main(void* task) { + cout << "Start execution..." << endl; + Task* t = (Task*) task; + t->execute(); + + return NULL; +} + +Task::Task(int t=1000) +:T(t) +{ + sem_init(&this->threadSem,0,0); + + pthread_t timerthread; + pthread_t executethread; + + pthread_create(&timerthread,NULL,&timer_main,(void*) this); + pthread_create(&executethread,NULL,&execute_main,(void*) this); +} + +Task::~Task() { +} + +void Task::waitForNextCycle() +{ + sem_wait(&this->threadSem); +} diff --git a/Master/Real-Time Systems/RTS_A8/src/Task.h b/Master/Real-Time Systems/RTS_A8/src/Task.h new file mode 100644 index 0000000..be3b377 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Task.h @@ -0,0 +1,28 @@ +/* + * Task.h + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#ifndef TASK_H_ +#define TASK_H_ + +#include <semaphore.h> + +class Task { +public: + Task(int); + virtual ~Task(); +protected: + int T; // period in ms + sem_t threadSem; +public: + friend void* timer_main(void* task); + friend void* execute_main(void* task); + virtual void execute() = 0; +protected: + void waitForNextCycle(); +}; + +#endif /* TASK_H_ */ diff --git a/Master/Real-Time Systems/RTS_A8/src/main.cpp b/Master/Real-Time Systems/RTS_A8/src/main.cpp new file mode 100644 index 0000000..fc3feed --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/main.cpp @@ -0,0 +1,78 @@ +/* + * main.cpp + * + * Created on: 30.11.2010 + * Author: istsveise + */ +#include "Task.h" +#include "Display.h" +#include "Fahren.h" +#ifndef POS_H_ + #include "POS.h" +#endif +#ifndef NOTBREMSE_H_ + #include "Notbremse.h" +#endif + +#include <iostream> +#include <stdlib.h> +#include <pthread.h> +#include <semaphore.h> +#include <cmath> + +using namespace std; + +// global mutex to protect vehicle +pthread_mutex_t vehicleMutex; + +POS vehiclePosition; +double lineDir; +const double PI = 3.14159265359; + +double Abstand(POS p,double Ldir) +{ + vector3d origin; + vector3d point(p.x,p.y); + double polx = 1.0; + double ldirRad = Ldir*PI/180; + double m = tan(ldirRad); + double poly = polx*m; + vector3d pointOnLine(polx,poly); + vector3d normVec; + origin.crossProduct(pointOnLine,normVec); + normVec.normalize(); + double d = normVec.scalarProduct(point); + return d; +} + +void InitAll(int x) +{ + pthread_mutex_init(&vehicleMutex,NULL); + pthread_mutex_lock(&vehicleMutex); + lineDir = x; + vehiclePosition.x = 0; + vehiclePosition.y = 0; + pthread_mutex_unlock(&vehicleMutex); +} + +int main(int argc,char* argv[]) +{ + InitAll(30); // direction of line [0, 90] + + sleep(1); + + Display D(100); + Notbremse N(100); + + cout << "Los geht's...!" << endl; + Fahren F(/* period in ms */ 100, + /* speed in pixels per second */ 20, + /* start direction [0, 90] */ 0); + + //sleep for ever + sem_t block; + sem_init(&block,0,0); + sem_wait(&block); + + return EXIT_SUCCESS; +} |
