PDA

View Full Version : speed


ddlawrence
03-23-09, 07:04 PM
Hi. I have an application of 4 state machines with 6 pages of code. I need to run
it at least 1000 times a second minimum, 10000 times a second preferred.
I am skipping some encoder teeth and I am trying to figure out why.
Is there a way to estimate how many instructions can run each machine cycle?

thanks............don

RMDumse
03-23-09, 07:23 PM
There is information somewhere on a program called TTick of T' which allows you to time how long a word takes to run. You can even put your four machines into one definition, and call it to get the time to do all, but realize, the time will depend on the state the machine is in, and how many transitions it goes through, et. al.

RMDumse
03-23-09, 07:27 PM
Also look at the 'Pod's download page. Read the appnote labeled IsoMax(TM) Timing.

RMDumse
03-23-09, 07:32 PM
I wasn't able to find TTICK searching the forum, although I'm pretty sure it was in here... anyway. Here it is again:


HEX
( Use 0C00 for '803,'805 1200 for '807s)
0C00 CONSTANT SYS_BASE EEWORD

( TimerC3 158 offset for newer IsoMax
( TimerD3 178 for older
SYS_BASE 158 + CONSTANT TMRC3_BASE EEWORD

TMRC3_BASE CONSTANT TMR_CMP1 EEWORD
TMRC3_BASE 5 + CONSTANT TMR_CNTR EEWORD

( Need a Time Offset, to adjust time before and after execution of test word
DECIMAL 28 CONSTANT T0 EEWORD

: T' ( i*x -- j*x n ) ' CFA
TMR_CNTR @ >R EXECUTE TMR_CNTR @ R> ( end start )
2DUP U< IF - TMR_CMP1 @ + ELSE - THEN T0 -
; EEWORD

( Use on DUP
( Put initial value on stack to be dupped
( follow with T'
( Print result
( Drop original and dupped numbers.
( 1 T' DUP . DROP DROP
( 2 3 T' + . DROP
( Some more actual examples:
( 1 1 T' * . 9 OK
( . 1 OK
( : X 5 2 / ; OK
( T' X . DROP 88 OK
( 5 2 T' / . DROP 52 OK

ddlawrence
03-24-09, 06:38 PM
Hi. I will look at that code example. thanks.

Looking around the forum, you said that if a machine only
has one state, it should be written as a macro. Is there an
example or explanation of this?

thanks......don

RMDumse
03-24-09, 08:31 PM
I think I might have said, a machine with a single state should be written as a thread, rather than a macro. If I said that, give me a link and I'll correct it.

A thread is a straight line bit of code with no backwards branches with one entry and one exit. It runs fast, and it probably has an IF THEN conditional to deal with events that don't need to store the history of what happened when like a state machine does.

Here's a sensor scanner routine I used in the Mini-Sumo code. It is a thread that conditions the sensors each time through the background execution.


: SENSOR-SCAN
LDET-CNT LDET? DUP REDLED SET CLEAR/COUNT
RDET-CNT RDET? DUP GRNLED SET CLEAR/COUNT
LLINE-CNT LLINE? CLEAR/COUNT
RLINE-CNT RLINE? CLEAR/COUNT
; EEWORD

ddlawrence
03-26-09, 11:33 PM
Hi. You did say thread, now that you mention it. Sorry.
The example you gave me is a WORD, and it only
runs when it is called, correct? I need something to
run continuously, ie every machine cycle.
I do not know how to run something in background.
Is that described somewhere? Here is the code segment
of the single state state machine, it listens for CAN
messages:
...
MACHINE CANRX EEWORD
ON-MACHINE CANRX
APPEND-STATE LISTEN EEWORD
IN-STATE LISTEN
CONDITION RXF?
CAUSES
DC5 @ FF AND ><
DC6 @ FF AND OR AZIMUTHFB !
RXCLR
AZIMUTHFB ?
AZIMUTH @ AZIMUTHFB @ = IF
YELLED OFF
THEN
THEN-STATE LISTEN TO-HAPPEN IN-EE

MACHINE-CHAIN JOYSTICKDRIVER
8STATE
4STATE
CANRX
END-MACHINE-CHAIN EEWORD

: MAIN
NOHOME SET-STATE
NOHOMET SET-STATE
LISTEN SET-STATE
CANINIT
PE2 OFF
EVERY 1388 CYCLES SCHEDULE-RUNS JOYSTICKDRIVER
; EEWORD \ WAS 1F4 CYCLES
AUTOSTART MAIN
SAVE-RAM

As I am relatively new, let me know if you see any glaring errors.

thanks..........don

RMDumse
03-27-09, 07:59 AM
The example you gave me is a WORD, and it only runs when it is called, correct? I need something to run continuously, ie every machine cycle.
I do not know how to run something in background.
Is that described somewhere?

Yes, a thread is a word with one entry point, and one exit point and the code flows smoothly from beginning to end with no backwards branches.

Here is you code in thread form of the single state state machine, it listens for CAN messages:
...
: LISTEN
IN-STATE LISTEN
RXF?
IF
DC5 @ FF AND ><
DC6 @ FF AND OR AZIMUTHFB !
RXCLR
AZIMUTHFB ?
AZIMUTH @ AZIMUTHFB @ = IF
YELLED OFF
THEN
THEN
EEWORD

MACHINE-CHAIN JOYSTICKDRIVER
8STATE
4STATE
LISTEN
END-MACHINE-CHAIN EEWORD

: MAIN
NOHOME SET-STATE
NOHOMET SET-STATE
CANINIT
PE2 OFF
EVERY 1388 CYCLES SCHEDULE-RUNS JOYSTICKDRIVER
; EEWORD \ WAS 1F4 CYCLES
AUTOSTART MAIN
SAVE-RAM

So here instead of the state machine that always goes to the same transition and checks the same conditional, you use a thread that checks the sam conditional with a forward branching IF and runs the action if it is true. It is insalled in the machine chain just like a machine, except it is simpler.

ddlawrence
03-27-09, 06:35 PM
Hi. Yes that is it! I didn't know you can put a word in the machine chain.
Very useful.
thanks..........don

RMDumse
03-28-09, 08:43 AM
So far, MACHINE-CHAIN and END-MACHINE-CHAIN are synonyms for : and ; . In the future, the language might evolve some error checking, and become more strict about what can be in a chain, but right now, it is open to allow any definition to be in the chain. As long as you realize this could be tightened in the future, now you can mix anything you'd do in a colon defintion with the machine chain.

Yes, it is a very powerful useful concept, to be able to install either machine or thread into background operation.