Welcome at the open source repository TwinCAT meets project Euler!
This repository contains a TwinCAT project written in Structured Text which solves Project Euler problems.
I created this repository because I thought it would be fun to see how far we can come with Structured Text.
But mostly because I hope that we learn some cool ST tips and tricks from each other while solving the problems. We perhaps might need some additional math functions/libraries as well..
The project is a TwinCAT stand alone PLC project. The PLC project contains a base function block named 'FbProblemSolverBase' which contains the base logic. The results are verified with TCUnit in the main program 'PRG_TEST'.
FbProblemSolverBase is loosely based on PLCOpens ETRIG pattern and implements some logic to control states like 'Busy' and 'Done' and has a 'Execute' property to start the problem solving process. The results are returned with a STRING property called: 'Result'.
All new problem solving function blocks should inherit from FbProblemSolverBase. In the new problem solving function block base simply call the base function block:
Super^();
The problem solving logic should be implemented in the overriden method: 'CyclicAction'. This action will be called cyclic until the output parameter: 'complete' is set the true.
METHOD PROTECTED CyclicAction
VAR_OUTPUT
complete : BOOL;
result : STRING;
END_VAR
// Logic
result := '123';
complete := TRUE;
In case a function block needs specific cleanup after the process has completed, override the method 'ResetAction' and implement any cleanup actions here. This may take more than one cycle. As with the 'CyclicAction' this is controlled with the output parameter 'Complete'.
This project uses TCUnit to verify the results. Because the structure of the problem solving function blocks is the same, the testing can be done with one function block: 'FbProblemTester'. This function block uses FB_init to acquire the function block to test and the expected result as STRING.
To add a new problem in the test sequence just add them in the variable list of 'PRG_TEST':
PROGRAM PRG_TEST
VAR
_problem1 : Problem1;
_problem1Test : FbProblemTester(_problem1, '233168');
_problem2 : Problem2;
_problem2Test : FbProblemTester(_problem2, '4613732');
END_VAR
/- Project
/- - POUs
/- - - Problems
/- - - - Base
/- - - - - FbProblemSolverBase
/- - - - - IProblemSolver
/- - - - Problem1 (FB)
/- - - - Problem2 (FB)
/- - - - ........ (FB)
/- - - - Add your new problem here (FB)
/- - - Tests
/- - - - FbProblemTester (FB)
/- - - PRG_TEST (PRG)
This project is a stand alone project. To run it download the project and either in de same solution or in a different solution create a TwinCAT XAE project matching your hardware. Import the stand alone project as described here and you should be ready to run the PLC project!
Yes! The purpose of this project is to have some fun with Structured Text and learn from each other. Everyone is welcome to solve a problem. To solve a Project Euler problem follow these steps:
- Clone this repository
- Add a new function block which inherits from FbProblemSolverBase.
- Pick your problom from Project Euler and implement your solution in the method Cyclic action.
- Add your function block to the test sequence in PRG_TEST
- Verify that all tests pass.
- Open a pull request with your additions.