A rainy day and a few hours to kill proved it's not too hard to create a ladder logic to PICAXE Basic converter. The trick was in realising that, if one has an 'ASCII art' text file which represents the ladder program, it's mostly just a matter of stepping through that from top right and heading leftwards to determine the code to generate.
A rather quick and dirty attempt has most things a ladder program would use supported -
Digital input pins: [X
n], [I
n], [CB
n], [C.
n]
Digital output pins: (Y
n), (O
n), (CR
n), [B.
n]
Digital output states as input: [Y
n], [O
n], [CR
n]
Digital variables: (Vn), [Vn]
Digital flip-flop variables: (F
n), [V
n], [F
n], [Q
n]
Countdown Timers: (T
n=seconds), [T
n]
Branching: -.- -^- -+- -&-
Signal inversion: (/
xx), [/
xx], -/-
Signal reporting via SERTXD: -#-
That allows the standard on and off button control of a LED to be created with -
Code:
|---[X0]---.---[/X1]---------(Y7)---|
| | |
|---[Y7]---' |
Set input 'X0' (pin C.0) high to turn the output 'Y7' (pin B.7) LED on, set that low and set input 'X1' (pin C.1) high to turn it off again. The '[Y7]' input is the state of the (Y7) output.
The code for that is similar to the following -
Code:
dirB.7 = 1
Do
bit0 = pinC.0 | outpinB.7
bit1 = pinC.1 ^ 1 & bit0
outpinB.7 = bit1
Loop
The full code which can be simulated in PE6 is -
Code:
#Picaxe 20M2
#Terminal Off
#No_Data
; .-----_-----.
; : :
; pinX1 --| C.1 B.6 |-
; pinX0 --| C.0 B.7 |-- outpinY7
; `-----------'
Symbol pinX0 = pinC.0
Symbol pinX1 = pinC.1
Symbol outpinY7 = outpinB.7 : Symbol dirY7 = dirB.7
PowerOnReset:
dirY7 = 1 ; outpinY7
MainLoop:
; |---[X0]---.---[/X1]------(Y7)---|
; | | |
; |---[Y7]---' |
bit0 = pinX0 | outpinY7
bit1 = pinX1 ^ 1 & bit0
outpinY7 = bit1
Goto MainLoop
I am not that familiar with ladder programming but it seems to be able to handle most things one would want from ladder programming with a PICAXE. Because it's thrown together it's not capable of taking any random ladder program at present, one cannot for example do -
Code:
|---[X0]---.---[X1]---.------(Y7)---|
| | | |
| `---[X2]---' |
But that can be worked round with -
Code:
|---[X0]-------&-------------(Y7)---|
| | |
|---[X1]---.---' |
|---[X2]---' |
Or more simply, by just arranging the order -
Code:
|---[X1]---.---[X0]----------(Y7)---|
| | |
|---[X2]---' |
As noted timers are implemented -
Code:
|---[X0]------------------(T1=10)---|
| |
|---[T1]---------------------(Y7)---|
Which will keep the Y7 LED on for 10 seconds after X0 is released. But I am not convinced my timers are well implemented.
I can free-run flash an output 10 seconds on, 20 seconds off with -
Code:
|--[/T1]--[/T2]---(T1=10)--(T2=30)--|
| |
|--[T1]-----------------------(Y7)--|
But the following, which looked to me like it would work, has a subtle issue with T2 never triggering. T1 hits zero at the end of the ladder so it gets reset on the next iteration through the ladder before T2 has a chance to trigger -
Code:
|---[/T1]---[/T2]---------(T1=10)---|
|---[/T1]---[/T2]---------(T2=20)---|
| |
|---[T1]---------------------(Y7)---|
Maybe I've just accidentally burnt my first virtual factory down
I guess I'm going to have to read up on PLC's, ladder programming, and timers in particular.
It doesn't help that every PLC manufacturer seems to have done things slightly different, often use different naming conventions.
One thing which would be useful to know from someone who is more familiar with ladder programming; is what the following should do -
Code:
|---[X0]-----------(T1=10)---(Y7)---|
Is output Y7 set from the X0 input which also sets the timer or is Y7 set while the T1 timer is set, non-zero ?
If anyone wants to create a not too complicated ladder program and post that I can see what that generates. And if anyone can think of anything I am missing let me know.
Being PICAXE it could make sense to allow analogue inputs [A0>=10], servo (S1=150), and PWM (P1=75%) control, but those are not supported yet. It's also hard coded for the 20M2 at present.
Ladder programming does get the occasional mention on the forum and, if it does seem useful, I can look to see how best to turn what I have into something more easily usable by others.