Sunteți pe pagina 1din 9

PROIECT

LA DISCIPLINA

SISTEME DE CALCUL IN TIMP REAL


ANUL UNIVERSITAR 2010/2011

Profesori:
Studenti:

Patrascu Constantin
Popescu Bogdan
Nitu Alina

Popescu Cezar
Stanca Patricia
Grupa:

10406

1. TEMA PROIECTULUI
Sistemul de functionare al unui lift
Se cere modelarea unui sistem de control al unui lift
pentru un imobil cu 4 etaje. Se pleaca de la premiza ca
acesta nu poate pleca cu usile deschise si fara a i se indica
etajul la care trebuie sa ajunga. Numarul etajului este indicat
folosint intrarile numerice DI0->DI3 de pe placa PCM3718.
Pentru simularea inchiderii usilor se foloseste DI4 de pe
aceeasi placa. Aplicatia trebuie sa mai cuprinda si un
algoritm de optimizare pentru tratarea comenziilor primite.
Interfata grafica trebuie sa cuprinda cele 4 etaje simbolic
desenate printr-un dreptunghi care sa demonstreze
functionalitatea. Liftul trebuie sa se opreasca la etajul indicat
de numarul intrarii digitale care a fost comutata.

2. IMPLEMENTAREA SOLUTIEI
Implementarea de mai jos presupune initial verificarea
repetata
a
inchiderii
usilor
prin
apelarea
functiei
ReadDataAndDisplay(). Dupa ce avem confirmarea ca usile
sunt inchise preluam in variabila nr_et_nou numarul etajului la
care trebuie sa ajunga liftul si stabilim numarul de etaje care
trebuie parcurse pana la destinatie. Urmeaza mai apoi
simularea deplasarii liftului care va parcurge un etaj in
aproximativ 2 secunde. Dupa ce liftul a ajuns la destinatie se
asteapta 5 secunde pentru ca pasagerii sa poata parasii liftul si
se va relua intregul algoritm.
/*
****************************************************************************************
* Program
: DIO_SOFT.CPP
* Description
: Demo program for digital input-output function
*
* APIs used
: DRV_DeviceOpen,DRV_DeviceClose, DRV_DioReadBit.DRV_DioWriteBit
*
*
DRV_GetErrorMessage
*
****************************************************************************************
*/
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include "Include\driver.h"
/******************************
* Local function declaration *
******************************/
#define
#define
#define
#define

FALSE 0
TRUE 1
BOARD_TYPE_PCM3718 0
BOARD_TYPE_PCM3780 1

void ErrorHandler(DWORD dwErrCde);


void ErrorStop(long*, DWORD);
void Menu();
void OpenAndConfigureBoard( int board_type);
int ReadDataAndDisplay();
void WriteDataAndDisplay();
void ReadEventOnDI();
int evet_count_flag = FALSE;
int read_operation_PCM3718 = FALSE;
int read_operation_PCM3780 = FALSE;
DWORD
ULONG
long

dwErrCde;
lDevNum;
lDriverHandle;

ULONG ulDataLength;
DWORD dwPortMode[3];
DWORD dwEventCount;
BOOL
bPrint;
USHORT usEventType;
PT_EnableEvent EventSetting;
PT_CheckEvent ptCheckEvent;
int nr_et=0;
int nr_et_nou=0;
USHORT
USHORT
USHORT
USHORT

usChan;
usState;
usPort;
usMaxPort;

DEVFEATURES devFeatures;
PT_DeviceGetFeatures ptDevGetFeatures;
PT_DioReadBit ptDioReadBit;
PT_DioWriteBit ptDioWriteBit;
char
chIput;
int port_val[8]; // value read from the port
/************************ MAIN **********************************/
void main()
{
/* display the menu in order to make a choce */
Menu();
}//main
/**********************************************************************
* Function: ErrorHandler
*
Show the error message for the corresponding error code
* input:
dwErrCde, IN, Error code
* return:
none
**********************************************************************/
void ErrorHandler(DWORD dwErrCde)
{
char szErrMsg[180];
DRV_GetErrorMessage(dwErrCde, szErrMsg);
printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
}// end ErrorHandler
/**********************************************************************
* Function:
ErrorStop
*
Release all resource and terminate program if error occurs
* Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
*
dwErrCde, IN, Error code.
* return:
none
**********************************************************************/
void ErrorStop(long *pDrvHandle, DWORD dwErrCde)
{
//Error message
ErrorHandler(dwErrCde);
printf("Program terminated!\n");
//Close device
DRV_DeviceClose(pDrvHandle);
exit(1);
}// end ErrorStop
/**********************************************************************
* Function:
Menu
*
Display the options
* Paramaters: none
*
* return:
none
**********************************************************************/
void Menu(){

OpenAndConfigureBoard(BOARD_TYPE_PCM3718);
read_operation_PCM3718 = TRUE;
while(1)
{
int k = ReadDataAndDisplay();
while(k!=1)
{
k = ReadDataAndDisplay();
}
int i;
for(i=0;i<4;i++)
{
if(port_val[i]==1)
nr_et_nou=i;
}
int nr_pasi=abs(nr_et-nr_et_nou);
for(i=1;i<=nr_pasi;i++)
{
Sleep(2000);
if(nr_et>nr_et_nou)
nr_et--;
else
nr_et++;
printf("Liftul se afla la etajul %d.\n",nr_et);
}
printf("Liftul a ajuns!");
Sleep(5000);
}
read_operation_PCM3718 = FALSE;
// Close device
dwErrCde = DRV_DeviceClose(&lDriverHandle);
} // end menu
/**********************************************************************
* Function:
OpenAndConfigureBoard
*
Open the driver for the selected device ,and read
*
from the keyboard several settings
* Paramaters: none
*
* return:
none
**********************************************************************/
void OpenAndConfigureBoard(int board_type){
//Step 1: Display hardware and software settings for running this example
printf("Before running this example, please\n");
printf("use the device installation utility to add the device.\n");
//Step 2: Input parameters
printf("\nPlease input parameters:");
printf("\nDevice Number (check the device installation utility): ");
scanf("%d", &lDevNum);
//Step 3: Open device
dwErrCde = DRV_DeviceOpen(lDevNum, &lDriverHandle);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
printf("Program terminated!\n");
printf("Press any key to exit....");
getch();
exit(1);
}// end if
// Step 4: Read the features of the device
ptDevGetFeatures.buffer = &devFeatures;

ptDevGetFeatures.size = sizeof(DEVFEATURES);
dwErrCde = DRV_DeviceGetFeatures(lDriverHandle,(LPT_DeviceGetFeatures)&ptDevGetFeatures);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
//end if
if (devFeatures.usMaxDIChl<1)
{
dwErrCde = DRV_DeviceClose(&lDriverHandle);
printf("\n Device not support DI function.");
printf("\nPress any key to exit....");
getch();
exit(1);
}
usMaxPort = (devFeatures.usMaxDIChl+7)/8 - 1;
//printf("\nPlease specify the port:(0~%d)", usMaxPort);
if(board_type == BOARD_TYPE_PCM3718){
// choose the port for I/O operation
if(read_operation_PCM3718){
usPort = 1 ; // PORT 1 only for READ
}
if(! read_operation_PCM3718){
usPort = 0 ;// PORT 0 only for WRITE
}
}// end if
if(board_type == BOARD_TYPE_PCM3780){
// choose the port for I/O operation
if(read_operation_PCM3780){
usPort = 0 ; // PORT A only for READ
}
if(!read_operation_PCM3780){
usPort = 1 ;// PORT B only for WRITE
}
} // end if
//////////////////// this big "if" is only for PCM 3780 board //////////////////////
if(evet_count_flag){
// Set Port DI mode
ulDataLength = sizeof(dwPortMode);
dwErrCde = DRV_DeviceGetProperty(lDriverHandle, CFG_DioPortDirection,(void*)dwPortMode,
&ulDataLength);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
} // end if
if (dwPortMode[usPort]){
dwPortMode[usPort] = 0; // set it as IN
dwErrCde = DRV_DeviceSetProperty(lDriverHandle, CFG_DioPortDirection, (void*)dwPortMode,
ulDataLength);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
} // end if
}
//end if
} // end if evet_count_flag

} // end OpenAndConfigureBoard
/**********************************************************************
* Function:
ReadDataAndDisplay
*
Read the bits from the chosen Digital In port and
*
displays the read values from the pins
* Paramaters: none
*
* return:
none
**********************************************************************/
int ReadDataAndDisplay(){
// Step 1: Read state from the specified channel
for(int j=0;j<5;j++){
ptDioReadBit.port = 1; // input port
ptDioReadBit.bit = j;
// input channel
ptDioReadBit.state = &usState; // returned data
dwErrCde = DRV_DioReadBit(lDriverHandle, (LPT_DioReadBit)&ptDioReadBit);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return 0;
}
// Step 2: Display reading data
port_val[j] = usState;
if(j==4 && usState==1)
{
return 1;
}
}

// end for

return 0;
}

// end

ReadDataAndDisplay()

/**********************************************************************
* Function:
WriteDataAndDisplay
*
Write the selected bits from a selected ports
*
with logical 0 or 1 ; and displays the output value
* Paramaters: none
*
* return:
none
**********************************************************************/
void WriteDataAndDisplay(){
do{
printf("\nOutput Channel (0 to 7): ");
scanf("%d", &usChan);
printf("\nOutput State (0 or 1): ");
scanf("%d", &usState);
// Step 1: Turn on/off the specified channel
ptDioWriteBit.port = usPort; // output port
ptDioWriteBit.bit
= usChan; // output channel
ptDioWriteBit.state = usState; // output state
dwErrCde = DRV_DioWriteBit(lDriverHandle,
(LPT_DioWriteBit)&ptDioWriteBit);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
// Step 2: Display ouptut data
printf("\nOutput data = %d\n", usState);
printf("\nPress any key to continue, press Esc to exit...");

chIput = getch();
}while(chIput!=VK_ESCAPE);
} // end WriteDataAndDisplay
/**********************************************************************
* Function:
ReadEventOnDI
*
Read an Event occured on PORT A ,pin 0 and increment a
*
counter to report the events. The counter is displayed.
* Paramaters: none
*
* return:
none
**********************************************************************/
void ReadEventOnDI(){
// Step 1: Enable DI of channel with interrupt by DRV_EnableEvent
EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + usChan + usPort * 8;
with interrupt
EventSetting.Enabled = 1;
// event DI with interrupt
EventSetting.Count = 1;
// each interrupt generates an event

// event type: DI

dwEventCount = 0;
dwErrCde = DRV_EnableEvent( lDriverHandle, &EventSetting );
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
printf("Program terminated!\n");
printf("Press any key to exit....");
getch();
exit(1);
}
// Step 2: Check event by DRV_CheckEvent in while loop, exit at pressing
//
any key. If DRV_CheckEvent returns successful, increases
//
dwEventCount
ptCheckEvent.EventType = &usEventType;
ptCheckEvent.Milliseconds = 300;
printf("\nWaiting DI interrupt signal.\n");
printf("Press any key to exit the waiting loop....\n");

while (!kbhit())
{
dwErrCde = DRV_CheckEvent( lDriverHandle, &ptCheckEvent );
//Check function calling successful
if (dwErrCde == SUCCESS)
{
//Check desired event generation
if (usEventType == (ADS_EVT_INTERRUPT_DI0 + usChan + usPort*8))
{
dwEventCount++;
bPrint = true;
}
if (bPrint)
{
printf("\rEvent count:
bPrint = false;
}

%lu",dwEventCount);

}
}
if (kbhit()) getch();
// Step 3: Disable event by DRV_EnableEvent
EventSetting.EventType = ADS_EVT_INTERRUPT_DI0 + usChan + usPort*8;
with interrupt

// event type: DI

EventSetting.Enabled = 0;
// event DI with interrupt
EventSetting.Count = 1;
// each interrupt generates an event
dwErrCde = DRV_EnableEvent( lDriverHandle, &EventSetting );
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
printf("Program terminated!\n");
printf("Press any key to exit....");
getch();
exit(1);
}
}