calc.c

来自「CNC 的开放码,EMC2 V2.2.8版」· C语言 代码 · 共 752 行 · 第 1/2 页

C
752
字号
/* Classic Ladder Project *//* Copyright (C) 2001-2006 Marc Le Douarain *//* http://www.multimania.com/mavati/classicladder *//* http://www.sourceforge.net/projects/classicladder *//* February 2001 *//* -------------- *//* Refresh a rung *//* -------------- *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Lesser General Public *//* License as published by the Free Software Foundation; either *//* version 2.1 of the License, or (at your option) any later version. *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *//* Lesser General Public License for more details. *//* You should have received a copy of the GNU Lesser General Public *//* License along with this library; if not, write to the Free Software *//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#ifdef MODULE#include <linux/string.h>#ifdef RTAI#include "rtai.h"#include "rtai_sched.h"#endif#else#include <stdio.h>#include <string.h>#endif#ifdef __RTL__#include <rtlinux_signal.h>#endif#include "classicladder.h"#include "global.h"#include "vars_access.h"#include "arithm_eval.h"#include "manager.h"#ifdef SEQUENTIAL_SUPPORT#include "calc_sequential.h"#endif#include "calc.h"void InitRungs(){    int NumRung;    int x,y;    for (NumRung=0;NumRung<NBR_RUNGS;NumRung++)    {        RungArray[NumRung].Used = FALSE;        strcpy(RungArray[NumRung].Label,"");        strcpy(RungArray[NumRung].Comment,"");        for (y=0;y<RUNG_HEIGHT;y++)        {            for(x=0;x<RUNG_WIDTH;x++)            {                RungArray[NumRung].Element[x][y].Type = ELE_FREE;                RungArray[NumRung].Element[x][y].ConnectedWithTop = 0;                RungArray[NumRung].Element[x][y].VarType = 0;                RungArray[NumRung].Element[x][y].VarNum = 0;                RungArray[NumRung].Element[x][y].DynamicInput = 0;                RungArray[NumRung].Element[x][y].DynamicState = 0;                RungArray[NumRung].Element[x][y].DynamicVarBak = 0;                RungArray[NumRung].Element[x][y].DynamicOutput = 0;            }        }    }    InfosGene->FirstRung = 0;    InfosGene->LastRung = 0;    InfosGene->CurrentRung = 0;    RungArray[0].Used = TRUE;}/* Set DynamicVarBak (Element) to the right value before calculating the rungs *//* for detecting rising/falling edges used in some elements */void PrepareRungs(){    int NumRung;    int x,y;    char StateElement;    for (NumRung=0;NumRung<NBR_RUNGS;NumRung++)    {        for (y=0;y<RUNG_HEIGHT;y++)        {            for(x=0;x<RUNG_WIDTH;x++)            {                if ( (RungArray[NumRung].Element[x][y].Type==ELE_RISING_INPUT)                    || (RungArray[NumRung].Element[x][y].Type==ELE_FALLING_INPUT) )                {                    StateElement = ReadVar(RungArray[NumRung].Element[x][y].VarType,RungArray[NumRung].Element[x][y].VarNum);                    if (RungArray[NumRung].Element[x][y].Type==ELE_FALLING_INPUT)                        StateElement = !StateElement;                    RungArray[NumRung].Element[x][y].DynamicVarBak = StateElement;                }            }        }    }}void InitTimers(){    int NumTimer;    for (NumTimer=0; NumTimer<NBR_TIMERS; NumTimer++)    {        TimerArray[NumTimer].Preset = 0;        TimerArray[NumTimer].Base = TIME_BASE_SECS;        strcpy( TimerArray[NumTimer].DisplayFormat, "%.1fs" );    }}void PrepareTimers(){    int NumTimer;    for (NumTimer=0; NumTimer<NBR_TIMERS; NumTimer++)    {        TimerArray[NumTimer].Value = TimerArray[NumTimer].Preset;        TimerArray[NumTimer].InputEnable = 0;        TimerArray[NumTimer].OutputDone = 0;        TimerArray[NumTimer].OutputRunning = 0;    }}void InitMonostables(){    int NumMonostable;    for (NumMonostable=0; NumMonostable<NBR_MONOSTABLES; NumMonostable++)    {        MonostableArray[NumMonostable].Preset = 0;        MonostableArray[NumMonostable].Base = TIME_BASE_SECS;        strcpy( MonostableArray[NumMonostable].DisplayFormat, "%.1fs" );    }}void PrepareMonostables(){    int NumMonostable;    for (NumMonostable=0; NumMonostable<NBR_MONOSTABLES; NumMonostable++)    {        MonostableArray[NumMonostable].Value = 0;        MonostableArray[NumMonostable].Input = 0;        MonostableArray[NumMonostable].OutputRunning = 0;        MonostableArray[NumMonostable].InputBak = 0;    }}void InitCounters(){    int NumCounter;    for (NumCounter=0; NumCounter<NBR_COUNTERS; NumCounter++)    {        CounterArray[NumCounter].Preset = 0;    }}void PrepareCounters(){    int NumCounter;    for (NumCounter=0; NumCounter<NBR_COUNTERS; NumCounter++)    {        CounterArray[NumCounter].Value = 0;        CounterArray[NumCounter].ValueBak = 0;        CounterArray[NumCounter].InputReset = 0;        CounterArray[NumCounter].InputPreset = 0;        CounterArray[NumCounter].InputCountUp = 0;        CounterArray[NumCounter].InputCountUpBak = 0;        CounterArray[NumCounter].InputCountDown = 0;        CounterArray[NumCounter].InputCountDownBak = 0;        CounterArray[NumCounter].OutputDone = 0;        CounterArray[NumCounter].OutputEmpty = 0;        CounterArray[NumCounter].OutputFull = 0;    }}void InitArithmExpr(){    int NumExpr;    for (NumExpr=0; NumExpr<NBR_ARITHM_EXPR; NumExpr++)        strcpy(ArithmExpr[NumExpr].Expr,"");}void InitIOConf( ){	int NumConf;	int NbrConf;	int Pass;	StrIOConf * pConf;	for( Pass=0; Pass<2; Pass++)	{		NbrConf = (Pass==0)?NBR_INPUTS_CONF:NBR_OUTPUTS_CONF;		for( NumConf=0; NumConf<NbrConf; NumConf++ )		{			pConf = (Pass==0)?&InfosGene->InputsConf[ NumConf ]:&InfosGene->OutputsConf[ NumConf ];			pConf->FirstClassicLadderIO = -1;			pConf->DeviceType = 0;			pConf->SubDevOrAdr = 0;			pConf->FirstChannel = 0;			pConf->NbrConsecutivesChannels = 1;			pConf->FlagInverted = 0;		}	}}char StateOnLeft(int x,int y,StrRung * TheRung){    char State = 0;    int PosY;    char StillConnected;    // directly connected to the "left"? if yes, ON !    if (x==0)        return 1;    /* Direct on left */    if (TheRung->Element[x-1][y].DynamicOutput)        State = 1;    /* Up */    PosY = y;    StillConnected = TheRung->Element[x][PosY].ConnectedWithTop;    while( (PosY>0) && StillConnected)    {        PosY--;        if (TheRung->Element[x-1][PosY].DynamicOutput)            State = 1;        if ( !(TheRung->Element[x][PosY].ConnectedWithTop) )            StillConnected = FALSE;    }    /* Down */    if (y<RUNG_HEIGHT-1)    {        PosY = y+1;        StillConnected = TheRung->Element[x][PosY].ConnectedWithTop;        while( (PosY<RUNG_HEIGHT) && StillConnected)        {            if (TheRung->Element[x-1][PosY].DynamicOutput)                State = 1;            PosY++;            if (PosY<RUNG_HEIGHT)            {                if ( !(TheRung->Element[x][PosY].ConnectedWithTop) )                    StillConnected = FALSE;            }        }    }    return State;}/* Elements : -| |- and -|/|- */char CalcTypeInput(int x,int y,StrRung * UpdateRung,char IsNot,char OnlyFronts){    char State;    char StateElement;    char StateVar;    StateElement = ReadVar(UpdateRung->Element[x][y].VarType,UpdateRung->Element[x][y].VarNum);    if (IsNot)        StateElement = !StateElement;    StateVar = StateElement;    if (OnlyFronts)    {        if (StateElement && UpdateRung->Element[x][y].DynamicVarBak)            StateElement = 0;    }    UpdateRung->Element[x][y].DynamicState = StateElement;    if (x==0)    {        State = StateElement;    }    else    {        UpdateRung->Element[x][y].DynamicInput = StateOnLeft(x,y,UpdateRung);        State = StateElement && UpdateRung->Element[x][y].DynamicInput;    }    UpdateRung->Element[x][y].DynamicOutput = State;    UpdateRung->Element[x][y].DynamicVarBak = StateVar;    return State;}/* Element : --- */char CalcTypeConnection(int x,int y,StrRung * UpdateRung){    char State;    char StateElement;    StateElement = 1;    if (x==0)    {        State = StateElement;    }    else    {        UpdateRung->Element[x][y].DynamicInput = StateOnLeft(x,y,UpdateRung);        State = StateElement && UpdateRung->Element[x][y].DynamicInput;    }    UpdateRung->Element[x][y].DynamicState = State;    UpdateRung->Element[x][y].DynamicOutput = State;    return State;}/* Elements : -( )- and -(/)- */char CalcTypeOutput(int x,int y,StrRung * UpdateRung,char IsNot){    char State;    State = StateOnLeft(x,y,UpdateRung);    UpdateRung->Element[x][y].DynamicInput = State;    UpdateRung->Element[x][y].DynamicState = State;    if (IsNot)        State = !State;    WriteVar(UpdateRung->Element[x][y].VarType,UpdateRung->Element[x][y].VarNum,State);    return State;}/* Elements : -(S)- and -(R)- */char CalcTypeOutputSetReset(int x,int y,StrRung * UpdateRung,char IsReset){    char State;    UpdateRung->Element[x][y].DynamicInput = StateOnLeft(x,y,UpdateRung);    State = UpdateRung->Element[x][y].DynamicInput;    UpdateRung->Element[x][y].DynamicState = State;    if (State)    {        if (IsReset)            State = 0;  /* reset */        else            State = 1;  /* set */        WriteVar(UpdateRung->Element[x][y].VarType,UpdateRung->Element[x][y].VarNum,State);    }    return State;}/* Element : -(J)- */int CalcTypeOutputJump(int x,int y,StrRung * UpdateRung){    char State;    int Goto = -1;    State = StateOnLeft(x,y,UpdateRung);    if (State)        Goto = UpdateRung->Element[x][y].VarNum;    UpdateRung->Element[x][y].DynamicInput = State;    UpdateRung->Element[x][y].DynamicState = State;    return Goto;}/* Element : -(C)- */int CalcTypeOutputCall(int x,int y,StrRung * UpdateRung){    char State;    int CallSrSection = -1;    State = StateOnLeft(x,y,UpdateRung);    if (State)        CallSrSection = SearchSubRoutineWithItsNumber( UpdateRung->Element[x][y].VarNum );    UpdateRung->Element[x][y].DynamicInput = State;    UpdateRung->Element[x][y].DynamicState = State;    return CallSrSection;}/* Element : Timer (2x2 Blocks) */void CalcTypeTimer(int x,int y,StrRung * UpdateRung){    StrTimer * Timer;    Timer = &TimerArray[UpdateRung->Element[x][y].VarNum];    // directly connected to the "left"? if yes, ON !    if (x==0)    {        Timer->InputEnable = 1;    }    else    {        Timer->InputEnable = StateOnLeft(x-1,y,UpdateRung);    }    if (!Timer->InputEnable)    {        Timer->OutputRunning = 0;        Timer->OutputDone = 0;        Timer->Value = Timer->Preset;    }    else    {        if (Timer->Value>0)        {            Timer->Value = Timer->Value - InfosGene->MsSinceLastScan;            Timer->OutputRunning = 1;            Timer->OutputDone = 0;        }        else        {            Timer->OutputRunning = 0;            Timer->OutputDone = 1;        }    }    UpdateRung->Element[x][y].DynamicOutput = Timer->OutputDone;    UpdateRung->Element[x][y+1].DynamicOutput = Timer->OutputRunning;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?