Skip to content. | Skip to navigation

Personal tools

remote/edslog.cpp

remote/edslog.cpp

remote/edslog.cpp

/////////////////////////////////////////////////////////////////////////////
// EDSLOG.CPP
//
//  Last updated: 3/16/2005
//
//  This file contains code to reconstruct info from the
//  Engineering Data Stream (EDS) log.
/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// UpdateEDS - Add periodic EDS entries
void UpdateEDS()
{
    char temp[100],c;

    // Count number of EDS entries per second (retrieved and in queue)
    if (msTimer - EDSEntryTimer >= 1000)
    {
        CycleAv = EDSEntryCount;
        CycleMax = EDSQueueSize;
        EDSEntryTimer = msTimer;
        EDSEntryCount = 0;
        EDSQueueSize = 0;
    }

    if (EDSStep == 0)
    {
        if (LastEDSResponseOK)
        {
            // Ask for new message
            ClearSerialBuffer(Slave);
            SendToSlave("2\r");
            LastEDSCmd = '2';
        }
        else
        {
            // Ask to repeat last message
            ClearSerialBuffer(Slave);
            SendToSlave("3\r");
            LastEDSCmd = '3';
        }
        EDSTime = msTimer;
        EDSStep = 1;
    }
    else if (EDSStep == 1)
    {
        if ((msTimer - EDSTime) > EDSTimeout)
        {
            // There was a timeout (no response from slave)
            EDSError++;
            if (EDSError == 3)
                Log->AddX("EDS Error Messages Suspended",500,LIGHTRED,BRIEF);
            else if (EDSError < 3)
                Log->AddX("EDS Timeout",501,LIGHTRED,BRIEF);
            LastEDSResponseOK = 0;
            PutLED(12,326,SHECRED);
            EDSStep = 0;
        }
        while (SerialCharReady(Slave))
        {
            c = SerialGetChar(Slave);
            // On a carriage return, parse the serial command line
            if (c == 13)
            {
                ParseEDS(EDSResponse);
                ClearSerialBuffer(Slave);
                EDSRespLength = 0;
                EDSResponse[0] = 0;
                EDSStep = 2;
            }
            // On receiving our response character, flush the serial receive line
            else if (c == Response)
            {
                EDSResponse[0] = Response;
                EDSResponse[1] = 0;
                EDSRespLength = 1;
            }
            // Otherwise, add the character to the buffer
            else if (EDSRespLength < 79)
            {
                EDSResponse[EDSRespLength+1] = 0;
                EDSResponse[EDSRespLength] = c;
                EDSRespLength++;
            }
        }
    }
    else if (EDSStep == 2)
    {
        if (kbflag == 1)
        {
            ClearSerialBuffer(Slave);
            sprintf(temp,"9%02d%s",strlen(ECommand),ECommand);
            SendToSlaveWithChecksum(temp);
            LastEDSCmd = '9';
            kbflag = 0;
            EDSTime = msTimer;
            EDSStep = 3;
        }
        else
        {
            EDSStep = 0;
        }
    }
    else if (EDSStep == 3)
    {
        if ((msTimer - EDSTime) > EDSTimeout)
        {
            // There was a timeout (no response from slave)
            EDSError++;
            if (EDSError == 3)
                Log->AddX("EDS Error Messages Suspended",500,LIGHTRED,BRIEF);
            else if (EDSError < 3)
                Log->AddX("EDS Timeout",502,LIGHTRED,BRIEF);
            LastEDSResponseOK = 0;
            PutLED(12,326,SHECRED);
            EDSStep = 0;
        }
        while (SerialCharReady(Slave))
        {
            c = SerialGetChar(Slave);
            // On a carriage return, parse the serial command line
            if (c == 13)
            {
                ParseEDS(EDSResponse);
                ClearSerialBuffer(Slave);
                EDSRespLength = 0;
                EDSResponse[0] = 0;
                EDSStep = 0;
            }
            // On receiving our response character, flush the serial receive line
            else if (c == Response)
            {
                EDSResponse[0] = Response;
                EDSResponse[1] = 0;
                EDSRespLength = 1;
            }
            // Otherwise, add the character to the buffer
            else if (EDSRespLength < 79)
            {
                EDSResponse[EDSRespLength+1] = 0;
                EDSResponse[EDSRespLength] = c;
                EDSRespLength++;
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////
////NewEDSCommand: Pass the new keyboard command
void NewEDSCommand(char *comm)
{
    if (kbflag == 1)
        Log->Add("Command is already being processed",0);
    else
    {
        kbflag = 1;
        strcpy(ECommand,comm);
    }
}

/////////////////////////////////////////////////////////////////////////////
// ParseEDS: Parse a serial command line (response to EDS query)
void ParseEDS(char* line)
// This routine parses an EDS response from the serial port, and executes the
// appropriate code to accomplish the command.
{
    int msgs,len,messagenum;
    int EmptyMessage = 0;
    int e = 0;
    int hour,min,sec,hsec;
    struct dostime_t Time;
    char s[120];
    char ctemp;
    int i,j;
    int itemp,itemp1,itemp2,itemp3;
    float ftemp,ftemp1,ftemp2,ftemp3;
    int l,m,n,o;
    //int ParsedOK = 0;

    Log->Add("ParseEDS(%d): Parsing \"%s\"",VERBOSE,__LINE__,line);
    // Is message OK?
    if ((line[0] == Response) && (line[1] == Address) &&
            (line[2] == LastEDSCmd))
    {
        if (!VerifyChecksum(line))
        {
            if (line[2] == '9')
            {
                if (line[3] == '?')
                    Log->AddX("Free Form command syntax error",500,LIGHTRED,BRIEF);
                else if (line[3] == '0')
                    Log->AddX("Free Form command parsed",500,LIGHTGREEN,EXTRA);
                else
                    Log->AddX("Free Form command error",500,LIGHTRED,BRIEF);
                EmptyMessage = 1;
                //ParsedOK = 1;
            }
            else if (line[2] != '9')
            {
                if (sscanf(&line[3],"%2d%2d",&msgs,&len) == 2)
                {
                    // Is a message available?
                    if ((msgs != 0) && (len != 0))
                    {
                        if (sscanf(&line[7],"%2d%2d%2d%2d%3d",&hour,&min,
                                   &sec,&hsec,&messagenum) == 5)
                        {
                            Time.hour = hour;
                            Time.minute = min;
                            Time.second = sec;
                            Time.hsecond = hsec;

                            if (strlen(line) == len + 9)
                            {
                                strncpy(s,&line[18],len-9);
                                s[len-9] = 13;
                                s[len-8] = 0;
                                // Extract readings from EDS stream to put on-screen
                                if ((messagenum < 800) || (messagenum >= 900))
                                    EmptyMessage = 0;
                                switch (messagenum)
                                {
                                case 800:
                                    {
                                        if ((strlen(s) != 47) ||
                                                (sscanf(s,"C0D%8fC1D%8fC2D%8fC3D%8f",
                                                        &ftemp,&ftemp1,&ftemp2,&ftemp3) != 4))
                                            e = 800;
                                        else
                                        {
                                            AnalogData[0] = ftemp;
                                            AnalogData[1] = ftemp1;
                                            AnalogData[2] = ftemp2 ;
                                            AnalogData[3] = ftemp3;
                                        }
                                    }break;
                                case 801:
                                    {
                                        if ((strlen(s) != 8) ||
                                                (sscanf(s,"D%3X%1X",&itemp,&itemp1) != 2))
                                            e = 801;
                                        else
                                        {
                                            for (j=0;j<=2;j++)
                                            {
                                                Tilt.Dir[j] = (itemp & (1 << j)) >> j;
                                                Tilt.Cur[j] = (itemp & (1 << 3)) >> 3;
                                                Tilt.Pwr[j] = (itemp & (1 << 4)) >> 4;
                                                Tilt.Brk[j] = (itemp & (1 << 5)) >> 5;
                                                Tilt.Mon[j] = (itemp & (1 << (j+6))) >> (j+6);
                                                Tilt.Stp[j] = (itemp & (1 << (j+9))) >> (j+9);
                                            }
                                            PresVac.pressure = (itemp1 & 1);
                                            PresVac.vacuum = (itemp1 & (1 << 1)) >> 1;
                                        }
                                    }break;
                                case 802:
                                    {
                                        if (strlen(s) == 14)
                                        {
                                            if (sscanf(s,"D%cR%8f",&ctemp,&ftemp) != 2)
                                                e = 802;
                                            else
                                            {
                                                if (ctemp == 'A')
                                                {
                                                    DGHs[0].Reading = ftemp;
                                                    DGHs[0].ReadError = 0;
                                                }
                                                else if (ctemp == 'B')
                                                {
                                                    DGHs[1].Reading = ftemp;
                                                    DGHs[1].ReadError = 0;
                                                }
                                                else if (ctemp == 'C')
                                                {
                                                    DGHs[2].Reading = ftemp;
                                                    DGHs[2].ReadError = 0;
                                                }
                                            }
                                        }
                                        else if (strlen(s) == 7)
                                        {
                                            if (sscanf(s,"D%cRE",&ctemp) != 1)
                                                e = 802;
                                            else
                                            {
                                                if (ctemp == 'A')
                                                    DGHs[0].ReadError = 1;
                                                else if (ctemp == 'B')
                                                    DGHs[1].ReadError = 1;
                                                else if (ctemp == 'C')
                                                    DGHs[2].ReadError = 1;
                                            }
                                        }
                                    }break;
                                case 803:
                                    {
                                        if ((strlen(s) != 15) || (sscanf(s,"PT0%1dPT1%1dPT2%1d",
                                                                         &itemp,&itemp1,&itemp2) != 3))
                                            e = 803;
                                        else
                                        {
                                            if (itemp == TMOut)
                                                Tilt.Motion[0] = TMIn;
                                            else if (itemp == TMIn)
                                                Tilt.Motion[0] = TMOut;
                                            else
                                                Tilt.Motion[0] = itemp;
                                            if (itemp1 == TMOut)
                                                Tilt.Motion[1] = TMIn;
                                            else if (itemp1 == TMIn)
                                                Tilt.Motion[1] = TMOut;
                                            else
                                                Tilt.Motion[1] = itemp1;
                                            if (itemp2 == TMOut)
                                                Tilt.Motion[2] = TMIn;
                                            else if (itemp2 == TMIn)
                                                Tilt.Motion[2] = TMOut;
                                            else
                                                Tilt.Motion[2] = itemp2;
                                        }
                                    }break;
                                case 804:
                                    {
                                        if ((strlen(s) != 37) ||
                                                (sscanf(s,"S%1d%8f%8f%8f%8f",
                                                        &itemp,&ftemp,&ftemp1,&ftemp2,&ftemp3)) != 5)
                                            e = 804;
                                        else
                                        {
                                            PresVacMode = itemp;
                                            PresVac.ave = ftemp;
                                            PresVac.P = ftemp1;
                                            PresVac.I = ftemp2;
                                            PresVac.D = ftemp3;
                                        }
                                    }break;
                                case 805:
                                    {
                                        if ((strlen(s) != 12) ||
                                                (sscanf(s,"W%4d%4d",&itemp1,&itemp2)) != 2)
                                            e = 805;
                                        else
                                        {
                                            VacuumPulseWidth = itemp1;
                                            PressurePulseWidth = itemp2;
                                        }
                                    }break;
                                }
                                //ParsedOK = 1;
                            }
                            else
                                e = 5;
                        }
                        else
                            e = 4;
                    }
                    else
                        EmptyMessage = 1;
                }
                else
                    e = 3;
            } //a 9 command
        }
        else
            e = 2; //checksum wrong
    }
    else if ((line[0] == Response) && (line[1] == Address))
        e = 12;
    else if (line[0] == Response)
        e = 11;
    else
        e = 1;
    if (e)
    {
        LastEDSResponseOK = 0;
        if (EDSError == 3)
            Log->AddX("EDS Error Messages Suspended",500,LIGHTRED,BRIEF);
        else if (EDSError < 3)
            Log->AddX("EDS Response Error %d",500,LIGHTRED,BRIEF,e);
        EDSError++;
    }
    else
    {
        LastEDSResponseOK = 1;
        if (EDSError >= 3)
            Log->AddX("EDS Error Messages Resumed",500,LIGHTGREEN,0);
        EDSError = 0;
	
	// put the response in the master EDS log to be sent upstream
	MasterEDSLog->AddFromSlave(line);
	
        // Did we get a new message to display?
        if (!EmptyMessage)
        {
            // Add the EDSLog entry to the main log
            if (messagenum < 800)
            {
                s[strlen(s)-3] = 0;
                Log->AddXT(s,messagenum,LIGHTRED,BRIEF,Time);		// 0-799 are errors
            }
            else if (messagenum >= 900)
            {
                s[strlen(s)-3] = 0;
                Log->AddXT(s,messagenum,LIGHTGREEN,BRIEF,Time);	// 900-999 are successes
            }
            else
                Log->AddXT(s,messagenum,LIGHTGREEN,EXTRA,Time);  // 800-899 are low level data
            EDSEntryCount++;
            //EDSQueueSize = __max(EDSQueueSize,msgs);
            EDSQueueSize = (EDSQueueSize>msgs) ? EDSQueueSize : msgs;
        }
    }

    if (LastEDSResponseOK)
        PutLED(12,326,SHECGREEN);
    else
        PutLED(12,326,SHECRED);

    Tilt.DisplayAll();
    // Pressure/Vacuum chart and mode
    PresVacChart.Display();
    PresVac.Display();
}


Generated by GNU Enscript 1.6.5.2.
Document Actions