Exemplo n.º 1
0
int main(int *argc, char argv[]) {


	udp_init_client(&conn, 9999, "192.168.0.1");
	
	


	pthread_mutex_init(&UDP_mut,NULL);
	pthread_mutex_init(&newPIval_mut,NULL);
	pthread_mutex_init(&newSIG_mut,NULL);
	

	pthread_t PI_thread;
	pthread_t listen_thread;
	pthread_t respond_thread;

	pthread_create(&PI_thread, 	NULL,	PIreg,	NULL );
	pthread_create(&listen_thread, 	NULL,	UDPlis,	NULL );
	pthread_create(&respond_thread, NULL,	UDPresp, NULL );
	
	pthread_join(PI_thread,NULL);
	pthread_join(listen_thread,NULL);
	pthread_join(respond_thread,NULL);


	
	return 0;
}
Exemplo n.º 2
0
int main(int *argc, char argv[]) {


    char sendBuf[100];
    char receiveBuf[100];
    struct timespec waitTime;
    clock_gettime(CLOCK_REALTIME, &waitTime);
    udp_init_client(&conn, 9999, "192.168.0.1");

    //VARIABLES FOR REGULATING
    double error, integral, u, y;
    double reference = 1;
    double Kp = 10;
    double Ki = 800;
    double period = PERIOD;

    strncpy(sendBuf,"START", sizeof(sendBuf));
    udp_send(&conn,sendBuf,sizeof(sendBuf)); //strlen(sendBuf)+1

    //strncpy(sendBuf,"SET:12.3456",sizeof(sendBuf));
    //udp_send(&conn,sendBuf,sizeof(sendBuf));


    int numRep = RUNTIME/PERIOD;

    int i;
    for ( i = 0 ; i < numRep ; i++ ) {
        timespec_add_us(&waitTime,PERIOD*1000000);
        clock_nanosleep(&waitTime);

        strncpy(sendBuf,"GET", sizeof(sendBuf));
        udp_send(&conn,sendBuf,sizeof(sendBuf));
        udp_receive(&conn,receiveBuf,sizeof(receiveBuf));


        char * numVal = receiveBuf + 8;
        y = atof(numVal);

        //printf("Y-value from server is %f\n",y);



        //WE HAVE A Y-VALUE, DO PI REGULATING
        error = reference - y;
        integral = integral + (error * period);
        u = Kp * error + Ki * integral;

        setU(u);


    }


    strncpy(sendBuf,"STOP", sizeof(sendBuf));
    udp_send(&conn,sendBuf,sizeof(sendBuf));

    return 0;
}
Exemplo n.º 3
0
int main()
{
	int err=0;
	
	//Init mutexes and semaphores
	init_sem_and_mutexes();	
	
	// Init UDP connection struct
	err = udp_init_client(&tinfo_connection.conn, UDP_PORT, SERVER_IP);
	if (err!=0)
	{
		perror("Error connecting to server");
	}
	
    	//Start all 3 threads
	
	pthread_t udprecv, pidctrl, sighandl;

	err = pthread_create(&udprecv, NULL, UDP_listener, NULL);
	if (err!=0)
	{
		perror("Thread Creation failed");
	}

	err = pthread_create(&pidctrl, NULL, PIDctrl, NULL);
	if (err!=0)
	{
		perror("Thread Creation failed");
	}
	
	err = pthread_create(&sighandl, NULL,signalHandler,NULL );
	if (err!=0)
	{
		perror("Thread Creation failed");
	}	
	
	// Start simulation

	pthread_mutex_lock(&tinfo_connection.sendlock);

	if((udp_send(&tinfo_connection.conn, "START", sizeof("START")))==-1)
	{
		printf("Failed to start simulation");	
	}
	pthread_mutex_unlock(&tinfo_connection.sendlock);
	
	//Join thread
	err = pthread_join(pidctrl, NULL);  
    	if (err != 0) {  
        	perror("Joining of thread failed stoped");    
    	}
	
	// Stop simulation
	pthread_mutex_lock(&tinfo_connection.sendlock);

	if((udp_send(&tinfo_connection.conn, "STOP", sizeof("STOP")))==-1)
	{
		perror("Failed to stop simulation");	
	}
	pthread_mutex_unlock(&tinfo_connection.sendlock);
	
	//Destroy all semaphores and mutexes
	destroy_sem_and_mutexes();

	return NULL;      
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: oldgeezr/misc
int main (void)
{
	int port = 9999;
	char *ip = "192.168.0.1";
	char msg[BUF_LEN];
	pthread_t pi, listener;

#if WITH_SIGNAL
	pthread_t signal_handler;
#endif

	struct timespec start, stop;

	// Init mutexes
	pthread_mutex_init(&y_mutex, NULL);
	pthread_mutex_init(&udp_mutex, NULL);

	sem_init(&pi_sem, 0 ,1);
#if WITH_SIGNAL
	sem_init(&signal_sem, 0, 1);
#endif

	// UDP init
	udp_init_client(&conn, port, ip);
	printf("UDP Client initialized\n");

	// Spawn  UDP listener
	pthread_create(&listener, NULL, udp_listen, NULL);

	// Send start command
	strcpy(msg, "START");
	pthread_mutex_lock(&udp_mutex);
	udp_send(&conn, msg, strlen(msg)+1);
	pthread_mutex_unlock(&udp_mutex);
	printf("START sent\n");

	// Spawn pi controller and signal acknowledger
	pthread_create(&pi, NULL, pi_controller, NULL);
#if WITH_SIGNAL
	pthread_create(&signal_handler, NULL, signal_ack, NULL);
#endif

	// Set get command
	strcpy(msg, "GET");

	// Init clocks
	clock_gettime(CLOCK_REALTIME, &start);
	clock_gettime(CLOCK_REALTIME, &stop);

	// Define run time
	timespec_add_us(&start, 0);
	timespec_add_us(&stop, END);

	while ((start.tv_sec <= stop.tv_sec) && (start.tv_nsec < stop.tv_nsec))
	{
		// Send get command
		pthread_mutex_lock(&udp_mutex);
		udp_send(&conn, msg, strlen(msg)+1);
		pthread_mutex_unlock(&udp_mutex);

		// Add timer TICK to start
		timespec_add_us(&start, TICK);
		// Wait for next tick
		clock_nanosleep(&start);
	}

	// Send stop command to server
	strcpy(msg, "STOP");
	udp_send(&conn, msg, strlen(msg)+1);
	printf("Stop sent\n");

	// Destroy mutexes
	pthread_mutex_destroy(&y_mutex);
	pthread_mutex_destroy(&udp_mutex);

	// Destroy semaphores
	sem_destroy(&pi_sem);
#if WITH_SIGNAL
	sem_destroy(&signal_sem);
#endif

	// Kill threads
	run_listen = 0;
	run_pi = 0;
#if WITH_SIGNAL
	run_signal = 0;
#endif

	// Join threads
	pthread_join(pi, NULL);
	pthread_join(listener, NULL);

#if WITH_SIGNAL
	pthread_join(signal_handler, NULL);
#endif

	// Close UDP
	udp_close(&conn);

	return 0;
}
Exemplo n.º 5
0
int main(void){
    struct udp_conn udpcon;
    
    
    
	udp_init_client(&udpcon, port, server_ip);
    
    char buffer[255];
           
	struct PID_parameters parameters= (struct PID_parameters){
		10,
		800,
		0,
		1,
		period,
	};
	double y, u;
	int i=0;
	struct timespec next;
	clock_gettime(CLOCK_REALTIME, &next);		
	timespec_add_us(&next, (int)(period*1000000));
	udp_send(&udpcon, "START", 6);
	while(1){
	    udp_send(&udpcon, "GET", 4);
	    
	    while(1){
	        udp_receive(&udpcon, buffer, 255);
	        if (buffer[0]=='G'){
	            break;
	        }
	    }
	    
	    y = doubleFromReply(buffer);
	    u = PID_controller(parameters, y);
	    
	    setFromDouble(buffer,u);
	    udp_send(&udpcon, buffer, 255);
	    clock_nanosleep(&next);
	    timespec_add_us(&next, (int)(period*1000000));
	    if (i++*period >= 0.5){
	        break;
	    }
	}
	
	udp_send(&udpcon, "STOP",5);
	printf("stop\n");
	return 0;
}

void setFromDouble(char *buffer, double u){
    buffer[0]='S';
    buffer[1]='E';
    buffer[2]='T';
    buffer[3]=':';
    char dummy[255];
    sprintf(dummy, "%f", u);
    char* ptr = dummy;
    int i=4;
    while(*ptr !='\0'){
        buffer[i++]=*ptr;
        ptr++;
    }
    buffer[i]='\0';
    return;
}