View Full Version : SIOC and Encoders (ROTATE function, etc)
aVaTar
10-17-2011, 08:42 PM
Hi guys, I'm trying to understand the way SIOC handles rotary encoders in the variable processing function.
Say I bind a function to changes of encoder value, what value does it take as I turn the encoder?
Is it the successive increments starting at 0 e.g. 0 -> 1 -> 2 -> "+inf" and to "-inf" as encoder is turned the other way? If so then acceleration attribute makes sense as defining the speed sioc goes through the sequence.
Why then in the scripts for, say, MCP the heading value is directly assigned from the encoder?
e.g. MCP_HEADING = ROTATE 0 to 360, ENCODER_VALUE
That doesn't quite make sense to me... unless the MCP_HEADING takes the delta, adjusting the real heading somewhere?
Can someone clarify exactly what values are coming in from the hardware(into SIOC variable)?
Cheers!
fordgt40
10-18-2011, 04:16 AM
Try this site for excellent sioc coding advice. Also elswhere on the site is an example of using encoders in a nav1 example
http://www.lekseecon.nl/howto.html#rotaryencode
aVaTar
10-18-2011, 06:38 AM
Try this site for excellent sioc coding advice. Also elswhere on the site is an example of using encoders in a nav1 example
http://www.lekseecon.nl/howto.html#rotaryencode
Hi, thanks for you reply, but I have seen Nico's website and it doesn't answer the question. Neither does the help file coming with SIOC. I really want a doco stating exactly how the hardware interfacing works :)
So far what I've see are "working" examples... Problem is they don't work for me and the way I ended up writing my event processing function is by incrementing current value, say:
heading = heading + &ENCODER_VALUE.
Now that suggest to me that ENCODER_VALUE is the change in encoder's position and not absolute value
And here's Nico's example:
Var 1 Link IOCARD_ENCODER Input 40 Aceleration 2 Type 2
{
L0 = v1 // * -1 turning clockwise should be plus
v2 = ROTATE 0 359 L0
}
Var 2 // heading (0 .. 359)
Notice how v2 is assigned the value directly from the encoder? How does that work if encoder value is not absolute?
Is var2 taking a change in value and not the value itself?
Hope I explain my problem clear enough.
fordgt40
10-18-2011, 07:26 AM
In the example you quote above
Var 1 only provides the direction of encoder travel, it has no absolute value
Var 2 has the absolute value of the heading.
When the encoder moves then Var 2 value is inc/dec according to the direction of travel within the range of 0 to 360.
Var 2 does not receive an absolute value from the encoder. Var 2 will get its initial value from either a definition, or as a result of an arguement within another function
If you use the IOCPconsole within the main SIOC window you will see the variables within your script and their changing values arising from your h/w changes
aVaTar
10-18-2011, 07:32 AM
In the example you quote above
When the encoder moves then Var 2 value is inc/dec according to the direction of travel within the range of 0 to 360.
Var 2 does not receive an absolute value from the encoder.
That's what I don't get. I see assignment statement v2 = (Expression), where Expression is result of ROTATE function
Now how does rotate function get from increment/decrement to absolute heading??? It must return absolute value because its result is assigned to v2..
Now unless I'm missing something obvious here.. :)
Thanks for your help though, I appreciate it.
Edit:
When the encoder moves then Var 2 value is inc/dec
Just to be emphasize the point here. You say Var2 is incremented or decrements which implies dependency on current value which I don't see in ROTATE.
In other C++ you'd write: var2 += (var1), and note var2 = ROTATE(var1), because ROTATE doesn't know anything about var1 in this case.
fordgt40
10-18-2011, 07:40 AM
var 2 does not get an absolute value from the rotate function. Think in terms of Var 2 equals its current value inc or dec by the encoder movement direction within the range (for Var 2) of 0 to 359. The Rotate command does not have a persistent or global value.
aVaTar
10-18-2011, 07:48 AM
So hold on...
When I write var2=blah
does it mean var = var + blah? It doesn't make sense... Either var2=blah or var2 = var2 + blah, can't be both..
And I know that var2 = blah acts as assignment operator because if I hardcode var2 = 123 for example I get 123 and not var2 incremented by 123 every time.
Edit:
Is it maybe just special handling of variable assignment in the encoder routine?
PS: Sorry, I work as a programmer, so can't just accept some code without understanding :)
aVaTar
10-18-2011, 07:53 AM
Ah! I think some "global variable" magic happens with ROTATE and LIMIT functions!
I'm experimenting right now and what I see is when I use LIMIT function, var2 does not exceed certain value, so that means that either LIMIT keeps track of the actual value and doesn't let it drift outside the limits or some magic happens during assignment
nope,
var2 = blah does set the value of blah in var2
however, the ROTATE function will take the value of var2 and increment it with the increment value (which can be positive or negative) and then it will check the limits as stated in the first two parameters.
using the value of the encoder directly in the rotate function will allow to jump some values if you turn the rotary fast.
if you do not want that then you have to condition the encoder value to be +1 if >=0 and -1 if <= -1.
Greetz Peter
fordgt40
10-18-2011, 08:00 AM
Peter
Precisely :)
David
aVaTar
10-18-2011, 08:01 AM
Righto, so ROTATE, LIMIT etc functions do the "magic" bit. That's fine just as long as I know what happens.
Thanks for clearing that up man!
nope,
if you do not want that then you have to condition the encoder value to be +1 if >=0 and -1 if <= -1.
Yep that's exactly what I ended up doing yesterday, just following my nose and looking at what actually happens.
fordgt40
10-18-2011, 08:17 AM
Glad you got it "sorted". SIOC is powerful, but sometimes challenging, particularly the principle that the code attached to a variable is only executed when the variable changes value - caught me out many times :)
aVaTar
10-18-2011, 08:23 AM
Glad you got it "sorted". SIOC is powerful, but sometimes challenging, particularly the principle that the code attached to a variable is only executed when the variable changes value - caught me out many times :)
Yep me too... I was going nuts with the way it defied "standard practices" and that there was no clear documentation as to what exactly happens. Or I might have missed it when fiddling about yesterday.
Must admit that code attached to variables is very natural to me. They are called event handlers in JavaScript or a Window Procedure in Windows API. Then there're processor interrupts that function in a similar way with interrupt handler only executed when hw requests it(say encoder changes value :D ) Anyway that's off topic.
Thanks a lot guys, I appreciate your help!
PS. MCP is now coming along nicely :)
Hi guys,
SIOC's statement:
v2 = ROTATE 0 359 L0
has indeed the behaviour that Peter described; the ROTATE function takes the actual value of v2 before incrementing/decrementing, 'range wraparounding' and finally 'assigning'.
In C/C++ one would write:
v2 = Rotate(0, 359, L0, v2);
best regards,
Nico Kaan
Must admit that code attached to variables is very natural to me. They are called event handlers in JavaScript or a Window Procedure in Windows API.
Indeed. SIOC is an 'event driven scripting language', not a normal procedural programming language.
Read more about SIOC and 'the golden rule' here (http://www.lekseecon.nl/sioc.html).
regards,
Nico