basic plc implementation

This commit is contained in:
tmj-fstate
2020-06-29 15:27:52 +02:00
parent 0af9e84453
commit 065c490bea
7 changed files with 585 additions and 0 deletions

View File

@@ -45,6 +45,11 @@ struct light_array;
class particle_manager;
struct dictionary_source;
namespace plc {
using element_handle = short;
class basic_controller;
}
namespace scene {
struct node_data;
class basic_node;

View File

@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
//Q: 20160805 - odlaczenie pliku fizyki .pas od kompilacji
#include <map>
#include "hamulce.h"
#include "ladderlogic.h"
/*
MaSzyna EU07 locomotive simulator
Copyright (C) 2001-2004 Maciej Czapkiewicz and others
@@ -1600,6 +1601,8 @@ public:
int iProblem = 0; // flagi problemów z taborem, aby AI nie musiało porównywać; 0=może jechać
int iLights[2]; // bity zapalonych świateł tutaj, żeby dało się liczyć pobór prądu
plc::basic_controller m_plc;
int AIHintPantstate{ 0 }; // suggested pantograph setup
bool AIHintPantUpIfIdle{ true }; // whether raise both pantographs if idling for a while
double AIHintLocalBrakeAccFactor{ 1.05 }; // suggested acceleration weight for local brake operation

View File

@@ -1458,6 +1458,8 @@ void TMoverParameters::compute_movement_( double const Deltatime ) {
// automatic doors
update_doors( Deltatime );
m_plc.update( Deltatime );
PowerCouplersCheck( Deltatime, coupling::highvoltage );
PowerCouplersCheck( Deltatime, coupling::power110v );
PowerCouplersCheck( Deltatime, coupling::power24v );

394
ladderlogic.cpp Normal file
View File

@@ -0,0 +1,394 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "ladderlogic.h"
#include "parser.h"
#include "utilities.h"
#include "Logs.h"
namespace plc {
auto
basic_element::input() -> int & {
switch( type ) {
case basic_element::type_e::variable: {
return data.variable.value;
}
case basic_element::type_e::timer: {
return data.timer.value;
}
case basic_element::type_e::counter: {
return data.counter.value;
}
}
}
auto
basic_element::output() const -> int const {
switch( type ) {
case basic_element::type_e::variable: {
return data.variable.value;
}
case basic_element::type_e::timer: {
return ( data.timer.time_elapsed >= data.timer.time_preset ? data.timer.value : 0 );
}
case basic_element::type_e::counter: {
return ( data.counter.count_value >= data.counter.count_limit ? 1 : 0 );
}
}
}
auto
basic_controller::input( element_handle const Element ) -> int & {
return m_elements[ Element - 1 ].input();
}
auto
basic_controller::output( element_handle const Element ) const -> int const {
return m_elements[ Element - 1 ].output();
}
auto
basic_controller::load( std::string const &Filename ) -> bool {
m_program.clear();
m_updateaccumulator = 0.0;
m_programfilename = Filename;
cParser input( m_programfilename, cParser::buffer_FILE );
bool result { false };
while( true == deserialize_operation( input ) ) {
result = true; // once would suffice but, eh
}
return result;
}
auto
basic_controller::update( double const Timestep ) -> int {
if( false == m_timerhandles.empty() ) {
// update timers
m_updateaccumulator += Timestep;
auto const updatecount = std::floor( m_updateaccumulator / m_updaterate );
if( updatecount > 0 ) {
auto const updateamount = static_cast<short>( updatecount * 1000 * m_updaterate );
for( auto const timerhandle : m_timerhandles ) {
auto &timer{ element( timerhandle ) };
auto &timerdata{ timer.data.timer };
if( timer.input() > 0 ) {
timerdata.time_elapsed =
std::min<short>(
timerdata.time_preset,
timerdata.time_elapsed + updateamount );
}
else {
timerdata.time_elapsed = 0;
}
}
m_updateaccumulator -= m_updaterate * updatecount;
}
}
return run();
}
std::map<std::string, basic_controller::opcode_e> const basic_controller::m_operationcodemap = {
{ "ld", opcode_e::ld }, { "ldi", opcode_e::ldi },
{ "and", opcode_e::and }, { "ani", opcode_e::ani }, { "anb", opcode_e::anb },
{ "or", opcode_e::or }, { "ori", opcode_e::ori }, { "orb", opcode_e::orb },
{ "out", opcode_e::out }, { "set", opcode_e::set }, { "rst", opcode_e::rst },
{ "end", opcode_e::nop }
};
auto
basic_controller::deserialize_operation( cParser &Input ) -> bool {
auto operationdata{ Input.getToken<std::string>( true, "\n\r" ) };
if( true == operationdata.empty() ) { return false; }
operation operation = { opcode_e::nop, 0, 0, 0 };
cParser operationparser( operationdata, cParser::buffer_TEXT );
// HACK: operation potentially contains 1-2 parameters so we try to grab the whole set
operationparser.getTokens( 3, "\t " );
std::string
operationname,
operationelement,
operationparameter;
operationparser
>> operationname
>> operationelement
>> operationparameter;
auto const lookup { m_operationcodemap.find( operationname ) };
operation.code = (
lookup != m_operationcodemap.end() ?
lookup->second :
opcode_e::nop );
if( lookup == m_operationcodemap.end() ) {
log_error( "contains unknown command \"" + operationname + "\"", Input.Line() - 1 );
}
if( operation.code == opcode_e::nop ) { return true; }
if( false == operationelement.empty() ) {
operation.element =
find_or_insert(
operationelement,
guess_element_type_from_name( operationelement ) );
}
if( false == operationparameter.empty() ) {
auto const parameter{ split_index( operationparameter ) };
operation.parameter1 = static_cast<short>( parameter.second );
}
m_program.emplace_back( operation );
return true;
}
auto
basic_controller::insert( std::string const Name, basic_element Element ) -> element_handle {
m_elements.push_back( Element );
m_elementnames.push_back( Name );
auto const elementhandle{ static_cast<short>( m_elements.size() ) };
// for timers make note of the element in the timer list
if( Element.type == basic_element::type_e::timer ) {
m_timerhandles.push_back( elementhandle );
}
return elementhandle;
}
// runs one cycle of current program
auto
basic_controller::run() -> int {
m_accumulator.clear();
m_popstack = false;
auto programline { 1 };
for( auto const &operation : m_program ) {
// TBD: replace switch with function table for better readability/maintenance?
switch( operation.code ) {
case opcode_e::ld: {
if( m_popstack ) {
if( false == m_accumulator.empty() ) {
m_accumulator.pop_back();
}
m_popstack = false;
}
m_accumulator.emplace_back( output( operation.element ) );
break;
}
case opcode_e::ldi: {
if( m_popstack ) {
if( false == m_accumulator.empty() ) {
m_accumulator.pop_back();
}
m_popstack = false;
}
m_accumulator.emplace_back( inverse( output( operation.element ) ) );
break;
}
case opcode_e::and: {
if( m_accumulator.empty() ) {
log_error( "attempted AND with empty accumulator", programline );
break;
}
m_accumulator.back() &= output( operation.element );
break;
}
case opcode_e::ani: {
if( m_accumulator.empty() ) {
log_error( "attempted ANI with empty accumulator", programline );
break;
}
m_accumulator.back() &= inverse( output( operation.element ) );
break;
}
case opcode_e::anb: {
if( m_accumulator.size() < 2 ) {
log_error( "attempted ANB with empty stack", programline );
break;
}
auto const operand { m_accumulator.back() };
m_accumulator.pop_back();
m_accumulator.back() &= operand;
break;
}
case opcode_e::or: {
if( m_accumulator.empty() ) {
log_error( "attempted OR with empty accumulator", programline );
break;
}
m_accumulator.back() |= output( operation.element );
break;
}
case opcode_e::ori : {
if( m_accumulator.empty() ) {
log_error( "attempted ORI with empty accumulator", programline );
break;
}
m_accumulator.back() |= inverse( output( operation.element ) );
break;
}
case opcode_e::orb: {
if( m_accumulator.size() < 2 ) {
log_error( "attempted ORB with empty stack", programline );
break;
}
auto const operand{ m_accumulator.back() };
m_accumulator.pop_back();
m_accumulator.back() |= operand;
break;
}
case opcode_e::out: {
if( m_accumulator.empty() ) {
log_error( "attempted OUT with empty accumulator", programline );
break;
}
auto &target { element( operation.element ) };
auto const initialstate { target.input() };
target.input() = m_accumulator.back();
// additional operations for advanced element types
switch( target.type ) {
case basic_element::type_e::timer: {
target.data.timer.time_preset = operation.parameter1;
break;
}
case basic_element::type_e::counter: {
target.data.counter.count_limit = operation.parameter1;
// increase counter value on input activation
if( ( initialstate == 0 ) && ( target.input() != 0 ) ) {
/*
// TBD: use overflow-prone version instead of safe one?
target.data.counter.count_value += 1;
*/
target.data.counter.count_value =
std::min<short>(
target.data.counter.count_limit,
target.data.counter.count_value + 1 );
}
break;
}
}
// accumulator was published at least once, next ld(i) operation will start a new rung
m_popstack = true;
break;
}
case opcode_e::set: {
if( m_accumulator.empty() ) {
log_error( "attempted SET with empty accumulator", programline );
break;
}
if( m_accumulator.back() == 0 ) {
break;
}
auto &target { element( operation.element ) };
auto const initialstate { target.input() };
target.input() = m_accumulator.back();
// additional operations for advanced element types
switch( target.type ) {
case basic_element::type_e::counter: {
// NOTE: siemens counter behavior
// TODO: check whether this is true for mitsubishi
target.data.counter.count_limit = target.data.counter.count_value;
/*
if( ( initialstate == 0 ) && ( target.input() != 0 ) ) {
target.data.counter.count_value =
std::min<short>(
target.data.counter.count_limit,
target.data.counter.count_value + 1 );
}
*/
break;
}
}
// accumulator was published at least once, next ld(i) operation will start a new rung
m_popstack = true;
break;
}
case opcode_e::rst: {
if( m_accumulator.empty() ) {
log_error( "attempted RST with empty accumulator", programline );
break;
}
if( m_accumulator.back() == 0 ) {
break;
}
auto &target{ element( operation.element ) };
target.input() = 0;
// additional operations for advanced element types
switch( target.type ) {
case basic_element::type_e::counter: {
target.data.counter.count_value = 0;
break;
}
}
// accumulator was published at least once, next ld(i) operation will start a new rung
m_popstack = true;
break;
}
}
++programline;
}
return 0;
}
void
basic_controller::log_error( std::string const &Error, int const Line ) const {
ErrorLog(
"Bad plc program: \"" + m_programfilename + "\" "
+ Error
+ ( Line > 0 ?
" (line " + to_string( Line ) + ")" :
"" ) );
}
auto
basic_controller::guess_element_type_from_name( std::string const &Name ) const -> basic_element::type_e {
auto const name { split_index( Name ) };
if( ( name.first == "t" ) || ( name.first == "ton" ) || ( name.first.find( "timer." ) == 0 ) ) {
return basic_element::type_e::timer;
}
if( ( name.first == "c" ) || ( name.first.find( "counter." ) == 0 ) ) {
return basic_element::type_e::counter;
}
return basic_element::type_e::variable;
}
} // plc

173
ladderlogic.h Normal file
View File

@@ -0,0 +1,173 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "Classes.h"
namespace plc {
using element_handle = short;
// basic logic element.
class basic_element {
public:
// types
// rtti
enum class type_e {
variable,
timer,
counter,
};
// constructors
template<typename ...Args_>
basic_element( basic_element::type_e = basic_element::type_e::variable Type, Args_ ...Args );
// methods
// data access
auto input() -> int &;
auto output() const -> int const;
private:
// types
// cell content variants
struct variable {
std::int32_t value;
};
struct timer {
std::int32_t value;
short time_preset;
short time_elapsed;
};
struct counter {
std::int32_t value;
short count_limit;
short count_value;
};
// members
type_e type;
union {
variable variable;
timer timer;
counter counter;
} data;
// friends
friend class basic_controller;
};
class basic_controller {
public:
// methods
auto load( std::string const &Filename ) -> bool;
auto update( double const Timestep ) -> int;
// finds element with specified name, potentially creating new element of specified type initialized with provided arguments. returns: handle to the element
template<typename ...Args_>
auto find_or_insert( std::string const &Name, basic_element::type_e Type = basic_element::type_e::variable, Args_ ...Args ) -> element_handle;
// data access
auto input( element_handle const Element ) -> int &;
auto output( element_handle const Element ) const -> int const;
private:
//types
// plc program instruction
enum class opcode_e : short {
nop,
ld,
ldi,
and,
ani,
anb,
or,
ori,
orb,
out,
set,
rst,
};
struct operation {
opcode_e code;
short element;
short parameter1;
short parameter2;
};
// containers
using element_sequence = std::vector<basic_element>;
using name_sequence = std::vector<std::string>;
using operation_sequence = std::vector<operation>;
using handle_sequence = std::vector<element_handle>;
// methods
auto deserialize_operation( cParser &Input ) -> bool;
// adds provided item to the collection. returns: true if there's no duplicate with the same name, false otherwise
auto insert( std::string const Name, basic_element Element ) -> element_handle;
// runs one cycle of current program. returns: error code or 0 if there's no error
auto run() -> int;
void log_error( std::string const &Error, int const Line = -1 ) const;
auto guess_element_type_from_name( std::string const &Name ) const->basic_element::type_e;
inline
auto inverse( int const Value ) const -> int {
return ( Value == 0 ? 1 : 0 ); }
// element access
inline
auto element( element_handle const Element ) const -> basic_element const {
return m_elements[ Element - 1 ]; }
inline
auto element( element_handle const Element ) -> basic_element & {
return m_elements[ Element - 1 ]; }
// members
static std::map<std::string, basic_controller::opcode_e> const m_operationcodemap;
element_sequence m_elements; // collection of elements accessed by the plc program
name_sequence m_elementnames;
handle_sequence m_timerhandles; // indices of timer elements, timer update optimization helper
std::string m_programfilename; // cached filename of currently loaded program
operation_sequence m_program; // current program for the plc
std::vector<int> m_accumulator; // state accumulator for currently processed program rung
bool m_popstack { false }; // whether ld(i) operation should pop the accumulator stack or just add onto it
double m_updateaccumulator { 0.0 }; //
double m_updaterate { 0.1 };
};
template<typename ...Args_>
basic_element::basic_element( basic_element::type_e Type, Args_ ...Args )
: type{ Type }
{
switch( type ) {
case type_e::variable: {
data.variable = variable{ Args ... };
break;
}
case type_e::timer: {
data.timer = timer{ Args ... };
break;
}
case type_e::counter: {
data.counter = counter{ Args ... };
break;
}
default: {
// TBD: log error if we get here?
break;
}
}
}
template<typename ...Args_>
auto basic_controller::find_or_insert( std::string const &Name, basic_element::type_e Type, Args_ ...Args ) -> element_handle {
// NOTE: because we expect all lookups to be performed only (once) during controller (code) initialization
// we're using simple linear container for names, to allow for easy access to both elements and their names with the same handle
auto index { 1 };
for( auto const &name : m_elementnames ) {
if( name == Name ) {
return index;
}
++index;
}
// create and insert a new element if we didn't find existing one
return insert( Name, basic_element( Type, Args ... ) );
}
} // plc

View File

@@ -217,6 +217,7 @@
<ClCompile Include="gl\ubo.cpp" />
<ClCompile Include="gl\vao.cpp" />
<ClCompile Include="keyboardinput.cpp" />
<ClCompile Include="ladderlogic.cpp" />
<ClCompile Include="lightarray.cpp" />
<ClCompile Include="Logs.cpp" />
<ClCompile Include="material.cpp" />
@@ -396,6 +397,7 @@
<ClInclude Include="gl\ubo.h" />
<ClInclude Include="gl\vao.h" />
<ClInclude Include="keyboardinput.h" />
<ClInclude Include="ladderlogic.h" />
<ClInclude Include="light.h" />
<ClInclude Include="lightarray.h" />
<ClInclude Include="Logs.h" />

View File

@@ -465,6 +465,9 @@
<ClCompile Include="network\backend\asio.cpp">
<Filter>Source Files\application\network\backend</Filter>
</ClCompile>
<ClCompile Include="ladderlogic.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Globals.h">
@@ -851,6 +854,9 @@
<ClInclude Include="comparison.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ladderlogic.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="maszyna.rc">