Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

State Table Compiler

State Table Compiler

2009-09-15 by Chuck Hackett

I have written a state table compiler in my past life on a different
platform using Yacc & Lex but I have to believe that one exists for Windows
that I can use to compile state tables for my AVR resident state machine.

I’m assuming that I would have to customize it for my target state machine
environment.

A nice feature would be a companion program that would produce a state
transition diagram for the state table as documentation.

Anyone have a favorite SM compiler?

(I just found one on SourceForge at http://smc.sourceforge.net/ but I have
not studied it yet)
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Re: [AVR-Chat] State Table Compiler

2009-09-15 by David Kelly

On Tue, Sep 15, 2009 at 02:43:15PM -0500, Chuck Hackett wrote:
> I have written a state table compiler in my past life on a different
> platform using Yacc & Lex but I have to believe that one exists for
> Windows that I can use to compile state tables for my AVR resident
> state machine.

Perhaps you should consider installing Cygwin so as to have yacc and lex
to run your own state machine compiler under Windows?

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Re: [AVR-Chat] State Table Compiler

2009-09-15 by Ned Konz

On Sep 15, 2009, at 12:43 PM, Chuck Hackett wrote:

> I have written a state table compiler in my past life on a different
> platform using Yacc & Lex but I have to believe that one exists for  
> Windows
> that I can use to compile state tables for my AVR resident state  
> machine.
>
> I’m assuming that I would have to customize it for my target state  
> machine
> environment.
>
> A nice feature would be a companion program that would produce a state
> transition diagram for the state table as documentation.
>
> Anyone have a favorite SM compiler?
>
> (I just found one on SourceForge at http://smc.sourceforge.net/ but  
> I have
> not studied it yet)


I prefer the Quantum Platform package; it supports hierarchical state  
machines, has an AVR port available, and skips the separate  
representation of the state machine (i.e. it is not table based). So  
its RAM footprint is very small.

http://state-machine.com

I've used it extensively on the AVR (I wrote the first port, but you  
can download the "official" one now). I have also written a version in  
Ruby.

It's available both under the GPL v2 license, as well as a commercial  
license with reasonable rates.

-- 
Ned Konz
Seattle, WA
360-629-1091
http://bike-nomad.com

Re: [AVR-Chat] State Table Compiler

2009-09-16 by Dave Hylands

Hi Chuck,

On Tue, Sep 15, 2009 at 12:43 PM, Chuck Hackett <egroupscdh@up844.us> wrote:
> I have written a state table compiler in my past life on a different
> platform using Yacc & Lex but I have to believe that one exists for Windows
> that I can use to compile state tables for my AVR resident state machine.

I wrote one which you can find over here:
<http://websvn.hylands.org/listing.php?repname=Projects&path=%2Fhost%2Fsmc%2F&rev=0&sc=0>

It's also yacc and lex based, although the .c files are there, so you
shouldn't need yeacc or lex to make it work.

> A nice feature would be a companion program that would produce a state
> transition diagram for the state table as documentation.

It doesn't do that.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/

RE: [AVR-Chat] State Table Compiler

2009-09-16 by Chuck Hackett

> From: Dave Hylands
> 
> ....
> I wrote one which you can find over here:
> <http://websvn.hylands.org/listing.php?repname=Projects&path=%2Fhost%2F
> smc%2F&rev=0&sc=0>
> 
> It's also yacc and lex based, although the .c files are there, so you
> shouldn't need yeacc or lex to make it work.

Dave,

Interesting, is there some place I can get documentation for the state
machine description syntax & semantics?  I can dig it out of the Lex/Yacc
code but a PDF would be easier :-)
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

RE: [AVR-Chat] State Table Compiler

2009-09-16 by Chuck Hackett

> From: David Kelly
> ....
> Perhaps you should consider installing Cygwin so as to have yacc and
> lex to run your own state machine compiler under Windows?

That's the way I'm leaning at the moment.

In my intended usage the State Machine capabilities (events, actions, etc.)
are fixed when the AVRs are installed in the field.  The AVRs are
controlling small scale ride-on railroad signals.  Each AVR is the same but
implements different logic based on which track segments it is connected to,
the particular signals it is controlling, etc.

When the AVR nodes are configured different State Tables are downloaded to
them over a network based on the tasks, rules, etc. that they need to
implement due on their location, inputs assigned to them, outputs they
drive, etc.  "Moves and Changes", corrections, etc. to the signal system
logic are made by replacing the state table, no "SM Engine" code changes.
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

RE: [AVR-Chat] State Table Compiler

2009-09-16 by Chuck Hackett

> From: Ned Konz
> ....
> I prefer the Quantum Platform package; it supports hierarchical state
> machines, has an AVR port available, and skips the separate
> representation of the state machine (i.e. it is not table based). So
> its RAM footprint is very small.
> 
> http://state-machine.com
> ....

In my current need, the SMs are table based and (possibly) changed at run
time.

I've noted it for future reference ... thanks.
 
Cheers,

Chuck Hackett
"Good judgment comes from experience, experience comes from bad judgment"
7.5" gauge Union Pacific Northern (4-8-4) 844
http://www.whitetrout.net/Chuck

Re: [AVR-Chat] State Table Compiler

2009-09-16 by Dave Hylands

Hi Chuck,

On Tue, Sep 15, 2009 at 6:26 PM, Chuck Hackett <egroupscdh@up844.us> wrote:
>> From: Dave Hylands
>>
>> ....
>> I wrote one which you can find over here:
>> <http://websvn.hylands.org/listing.php?repname=Projects&path=%2Fhost%2F
>> smc%2F&rev=0&sc=0>
>>
>> It's also yacc and lex based, although the .c files are there, so you
>> shouldn't need yeacc or lex to make it work.
>
> Dave,
>
> Interesting, is there some place I can get documentation for the state
> machine description syntax & semantics?  I can dig it out of the Lex/Yacc
> code but a PDF would be easier :-)

Sorry - I haven't put one together yet.

cruise.smc shows most of the syntax.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/

Re: [AVR-Chat] State Table Compiler

2009-09-16 by Bob Paddock

Take a look at Ragel:

http://www.complang.org/ragel/



-- 
http://www.wearablesmartsensors.com/
http://www.softwaresafety.net/
http://www.designer-iii.com/
http://www.unusualresearch.com/


[Non-text portions of this message have been removed]

Re: [AVR-Chat] State Table Compiler

2009-09-16 by Ned Konz

On Sep 15, 2009, at 6:42 PM, Chuck Hackett wrote:

>> From: Ned Konz
>> ....
>> I prefer the Quantum Platform package; it supports hierarchical state
>> machines, has an AVR port available, and skips the separate
>> representation of the state machine (i.e. it is not table based). So
>> its RAM footprint is very small.
>>
>> http://state-machine.com
>> ....
>
> In my current need, the SMs are table based and (possibly) changed  
> at run
> time.
>
> I've noted it for future reference ... thanks.

Do you need:

  - hierarchical states?

  - Actions on transitions?

- Guards on transitions? (of course, this can be put in external code)

  - Arbitrary code executed for actions, or could you call pre- 
existing functions with arguments of your specification?

  - "do" actions (periodically, while in a state)? Or could these be a  
response to a timer event?


You might look at: Micah Martin's Statemachine framework for Ruby http://statemachine.rubyforge.org
and at http://blog.8thlight.com/articles/2006/11/17/understanding-statemachines-part-1-states-and-transitions

This can be installed (once you have Ruby installed) by just saying:

gem install statemachine

This will let you skip the yacc/lex stuff, and just use Ruby as the  
interpreter and compiler.

The package provides:

   - hierarchical state machine definition
   - entry and exit actions for states
   - default action for states
   - states and events are just names
   - actions are just names

The attached is an example of generation (in this case, it makes C  
structure definitions, but it could easily make any binary format you  
want).

It takes this source text:

$vending_machine = Statemachine.build do
   trans :waiting, :dollar, :paid, :indicatePaid
   trans :paid, :selection, :waiting, :vend
   trans :waiting, :selection, :waiting
   trans :paid, :dollar, :paid
end

$stateIds = { :root => 1, :waiting => 2, :paid => 3 }
$stateIds.default = 0
$eventIds = { :dollar => 1, :selection => 2 }
$eventIds.default = 0
$actionIds = { :indicatePaid => 1, :vend => 2 }
$actionIds.default = 0


and produces this C code (hypothetical structs for state and  
transition):

struct state states[] = {
	// state waiting, super root
	{ .id = 2, .superstate = 1,
	.entry_action = 0, .exit_action 0,
	// transitions
	.transitions = {
		// transition on event selection from waiting to waiting (action=)
		{ .event = 2, .destination_id = 2, .action = 0 },
		// transition on event dollar from waiting to paid  
(action=indicatePaid)
		{ .event = 1, .destination_id = 3, .action = 1 },
	}},
	// state paid, super root
	{ .id = 3, .superstate = 1,
	.entry_action = 0, .exit_action 0,
	// transitions
	.transitions = {
		// transition on event selection from paid to waiting (action=vend)
		{ .event = 2, .destination_id = 2, .action = 2 },
		// transition on event dollar from paid to paid (action=)
		{ .event = 1, .destination_id = 3, .action = 0 },
	}},
}

As you can see, all the representation of your state machine is here  
(or could be, if I'd finished it).

All you would need to do is extend the state and statemachine classes  
to be able to output whatever representation you wanted to execute  
(like I did here with C code generation) and maybe add arguments to  
actions (if needed).

  ----------




Ned Konz

[Non-text portions of this message have been removed]

Re: [AVR-Chat] State Table Compiler

2009-09-16 by Ned Konz

On Sep 16, 2009, at 9:47 AM, Ned Konz wrote:

>
> On Sep 15, 2009, at 6:42 PM, Chuck Hackett wrote:
>
>>> From: Ned Konz
>>> ....
>>> I prefer the Quantum Platform package; it supports hierarchical  
>>> state
>>> machines, has an AVR port available, and skips the separate
>>> representation of the state machine (i.e. it is not table based). So
>>> its RAM footprint is very small.
>>>
>>> http://state-machine.com
>>> ....
>>
>> In my current need, the SMs are table based and (possibly) changed
>> at run
>> time.
>>
>> I've noted it for future reference ... thanks.
>
> Do you need:
>
>  - hierarchical states?
>
>  - Actions on transitions?
>
> - Guards on transitions? (of course, this can be put in external code)
>
>  - Arbitrary code executed for actions, or could you call pre-
> existing functions with arguments of your specification?
>
>  - "do" actions (periodically, while in a state)? Or could these be a
> response to a timer event?
>
>
> You might look at: Micah Martin's Statemachine framework for Ruby http://statemachine.rubyforge.org
> and at http://blog.8thlight.com/articles/2006/11/17/understanding-statemachines-part-1-states-and-transitions
>
> This can be installed (once you have Ruby installed) by just saying:
>
> gem install statemachine
>
> This will let you skip the yacc/lex stuff, and just use Ruby as the
> interpreter and compiler.
>
> The package provides:
>
>   - hierarchical state machine definition
>   - entry and exit actions for states
>   - default action for states
>   - states and events are just names
>   - actions are just names
>
> The attached is an example of generation (in this case, it makes C
> structure definitions, but it could easily make any binary format you
> want).


Of course, the attached file got removed.

Here is the source:

require 'rubygems'
require 'statemachine'

$vending_machine = Statemachine.build do
   trans :waiting, :dollar, :paid, :indicatePaid
   trans :paid, :selection, :waiting, :vend
   trans :waiting, :selection, :waiting
   trans :paid, :dollar, :paid
end

$stateIds = { :root => 1, :waiting => 2, :paid => 3 }
$stateIds.default = 0
$eventIds = { :dollar => 1, :selection => 2 }
$eventIds.default = 0
$actionIds = { :indicatePaid => 1, :vend => 2 }
$actionIds.default = 0


module Statemachine
   class Transition
     def dumpCCode
       puts "\t\t// transition on event #{@event} from #{@origin_id}  
to #{@destination_id} (action=#{@action})"
       puts "\t\t{ .event = #{$eventIds[@event]}, .destination_id =  
#{$stateIds[@destination_id]}, .action = #{$actionIds[@action]} },"
     end
   end

   class State
     def stateid
       @id
     end

     def dumpCCode
       puts "\t// state #{@id}, super #{@superstate.stateid}"
       puts "\t{ .id = #{$stateIds[@id]}, .superstate =  
#{$stateIds[@superstate.stateid]},"
       puts "\t.entry_action =  
#{$actionIds[@entry_action]}, .exit_action #{$actionIds[@exit_action]},"
       puts "\t// transitions"
       puts "\t.transitions = {"
       # pp @transitions
       @transitions.each_value { |tr| tr.dumpCCode }
       puts "\t}},"
     end
   end

   class Statemachine
     def dumpCCode
       puts "struct state states[] = {"
       @states.each_value { |st| st.dumpCCode }
       puts "}"
     end
   end
end

$vending_machine.dumpCCode

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.