Monday 21 May 2012

Simulating Traffic lights on runtime not a ideal state machine

In this example i am trying to simulate traffic light systems, where the lights will glow based on the sensor update, the threads used are pthreads from posix library and i am using very basic of gcc compiler for compiling. This traffic light manages the four directions of traffic and gives each a time to relay the traffic.

Here we go.... Happy understanding.

#include<iostream>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
#include<cstdlib>
using namespace std;
//class to simulate the timer 

class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}

int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}

void reset()
{

// use this function to reset the timer
T1=0;
T2=0;
}

~timer() //oops a distructor
{
T1=0;
T2=0;
}
};
 timer t1;

// real time volatile i dont let the compiler use assumptions for me, no //optimizations  for me my dear
volatile bool g_red;
volatile bool g_green;
volatile bool g_yellow;
volatile bool stat=true;
volatile bool g_censor1;
volatile bool g_censor2;
volatile bool g_censor3;
volatile bool g_censor4;
volatile bool g_fyellow=true;

void check()
{
if(g_green && stat)
{
cout<<"Green Light"<<endl;
stat=false;
}
if(g_yellow && g_fyellow)
{
g_yellow=false;
cout<<"Yellow"<<endl;

if(g_censor1)
{
cout<<"++++++North get ready to stop"<<endl;
}
if(g_censor2)
{
cout<<"++++++South Get Ready to stop"<<endl;
}
if(g_censor3)
{
cout<<"++++++East Get Ready stop"<<endl;
}
if(g_censor4)
{
cout<<"++++++West Get Ready stop"<<endl;
}
}
if(g_red)
{
cout<<"=====RED=========="<<endl;
stat=true;
g_yellow=false;
g_green=false;
g_fyellow=true;
}
}



void north()
{
cout<<"\t\tControlling North"<<endl;
t1.start();
while(g_censor1 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=true;
g_censor3=false;
g_censor4=false;
}
void south()
{
cout<<"\t\tControlling South"<<endl;
t1.start();
while(g_censor2 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=false;
g_censor3=true;
g_censor4=false;
}
void east()
{
cout<<"\t\tControlling East"<<endl;
t1.start();
while(g_censor3 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=false;
g_censor3=false;
g_censor4=true;
}
void west()
{
cout<<"\t\tControlling West"<<endl;
t1.start();
while(g_censor4 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=true;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}

void *fun(void *arg)
{
cout<<"Censor Update"<<endl;
int num=rand();
if(num%2==0)
{
g_censor1=true;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
if(num%3==0)
{
g_censor1=false;
g_censor2=true;
g_censor3=false;
g_censor4=false;
}
if(num%5==0)
{
g_censor1=false;
g_censor2=false;
g_censor3=true;
g_censor4=false;
}
if(num%7==0)
{
g_censor1=false;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
if(g_censor1)
{
cout<<"++++++North Vechicle censor"<<endl;
}
if(g_censor2)
{
cout<<"++++++South Vechicle censor"<<endl;
}
if(g_censor3)
{
cout<<"++++++East Vechicle censor"<<endl;
}
if(g_censor4)
{
cout<<"++++++West Vechicle censor"<<endl;
}
}

int main()
{
//g_censor1=true;

pthread_t t1;
while(1)
{
try
{
//pthread_create(&t1,0,fun,0);
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
north();
//pthread_create(&t1,0,fun,0);
south();
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
east();
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
west();
//pthread_join(t1,0);
throw 1;
}
catch(int)
{
cout<<"exception";
pthread_create(&t1,0,fun,0);
}
}
return 0;
}

No comments:

Post a Comment

Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc