Ladder Logic for Arduino.
In this article the way how to run program in ladder logic from LDmicro on Arduino is presented. LDmicro is free SW to write and simulate ladder logic programs. When is ladder logic program is successfully simulated it could be compiled for AVR or PIC processor. However ATmega used in Arduino is not supported. Fortunately LDmicro can “compile” code into ANSI C. So, let’s take a look how we can run this code on Arduino.
Little Background
When we select ANSI C Code as Microcontroler type in Settings menu of LDmicro after compile we will get C file. This file contains all logic but it requires header file ladder.h which is missing. So, we have to implement this file. Let say we have input contact called “button” then we have to implement function:
BOOL Read_U_b_Xbutton(void) { return digitalRead(10); // button on digital pin 10 }
And similar to others input and output pins. Actually we have to write the layer between leader logic program and real HW of Arduino. And it could be more than simple call of digitalRead or digitalWrite. For example we could control servos, read data from proximity sensor and so on. Ability to write this layer is very powerful. Ladder logic could be used for core logic of application and C only for HW layer.
For more information read actual generated C file. It contains more information.
Generating ladder.h
If we need just direct control of digital IO pins of Arduino ladder.h could be easily generated for us. I wrote PHP script that can do this job. It requires generated code and mapping between variable name we use in ladder logic and pin numbers of Arduino. It will generate ladder.h with all nessesary functions and setup function (correctly calls pinMode for all used digital pin). Script could be used as console application or it’s also available online.
Step by step
Let’s do it step by step. Complete example is available for download.
1. Write code in LDmicro.
2. Select as Microcontroller “ANSI C Code”.
3. Compile it as ladder.cpp (save it as *.cpp file not *.c, otherwise Arduino won’t compile it)
4. Prepare mapping for IO pins of Arduino. Its text file called pinmap.ini. See example for details.
5. Generate ladder.h file by ladder-gen.php.
d0” for arduino digital pin 0, “d1” for pin 1 and so on.
6. Create Arduino sketch.
#include "ladder.h" void setup() { PlcSetup(); } void loop() { PlcCycle(); delay(10); }
See example for better sketch implementation (this one is for illustration and it's not accurate for timers).
7. Compile it by Arduino editor and upload it.
8. That’s all.
Conclusion
I hope presented method will help you start with ladder logic for Arduino. If you have any suggestion or find a bug in script, let me know.
Acutally I tried but its not happenning
ReplyDeleteIt is showing this error:
ladder.cpp:15:20: error: ladder.h: No such file or directory
ladder.cpp:42: error: 'BOOL' does not name a type
ladder.cpp:45: error: 'BOOL' does not name a type
ladder.cpp:48: error: 'SWORD' does not name a type
ladder.cpp:51: error: 'BOOL' does not name a type
ladder.cpp:52: error: variable or field 'Write_U_b_Y13' declared void
ladder.cpp:52: error: 'BOOL' was not declared in this scope
ladder.cpp: In function 'void PlcCycle()':
ladder.cpp:61: error: 'I_b_mcr' was not declared in this scope
ladder.cpp:64: error: 'I_b_rung_top' was not declared in this scope
ladder.cpp:68: error: 'U_i_Tnew' was not declared in this scope
ladder.cpp:73: error: 'U_i_Tnew' was not declared in this scope
ladder.cpp:76: error: 'Write_U_b_Y13' was not declared in this scope
It looks like you have omitted to generate ladder.h file.
Deleteif u upload the procedural video that may help a lot. thanks!!
ReplyDeleteI am running this on linux.
ReplyDeleteWhere does the pinmap.ini come into play?
This looks real exciting but I just quite don't understand.
Thanks
Dave
pinmap.ini contains mapping between variable names in the LDmicro and actual digital pins of the Arduino (variable name → pin number). Ladder-gen.php use this file to generate ladder.h. If this file is missing default mapping (“d0” → pin 0, “d1” → pin 1 and so on) is used.
DeleteWhy I can't use PWM in LDmicro using this method? Is there any way to use it?
ReplyDeleteUnfortunately LDmicro doesn’t support pwm instruction in C code. However you have access to any variables in LDmicro program from C code.
DeleteSo it’s possible to use variable called e.g. “pwn” in LDmicro program and after every PlcCycle set arduino PWM output according to this variable. Variable name in C will be something like “U_i_xxx”.
Example: http://gist.github.com/2602423#file_pwm.pde (see variable U_i_pwm on line 18)
I tried to upload the example code you provided onto my arduino, and i received numerous errors. Could you give any insight into what is wrong? I am Arduino IDE 1.0
ReplyDeleteThank you for notice. I have made some changes and now I should work fine (tested in 0023 and 1.0 IDE version). Please, download new version and let me know if it works.
Deletecan you show how get the ladder.h file
DeleteThe easiest way:
Delete1) Assume you have your program in LDmicro and you have compiled it into ladder.cpp. Input and output pins in LDmicro program are called “d0” for arduino digital pin 0, “d1” for digital pin 1 and so on.
2) Copy content of ladder.cpp into http://adam.horcica.cz/tools/ladder-gen/ and press generate. The result is content of your ladder.h file.
pinMode of arduino pins are determined automatically (algorithm is simple, every time when you use pin as output, the pin mode is set to output otherwise it's input).
If you want to have different names for input and output pins you have to use ladder-gen.php as described in article. However you need PHP interpreter installed on your computer.
i am a student working on arduino project
Deletei want to use ladder-gen.php (No previous knowledge of php)
can you tell me how to use this tool
php ladder-gen.php [path-to-sketch-folder]
This comment has been removed by the author.
ReplyDelete