compiler/tests/extras/PPCLRPSourcesResource.st
changeset 515 b5316ef15274
child 516 3b81c9e53352
equal deleted inserted replaced
502:1e45d3c96ec5 515:b5316ef15274
       
     1 "{ Package: 'stx:goodies/petitparser/compiler/tests/extras' }"
       
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
       
     5 TestResource subclass:#PPCLRPSourcesResource
       
     6 	instanceVariableNames:'sources'
       
     7 	classVariableNames:''
       
     8 	poolDictionaries:''
       
     9 	category:'PetitCompiler-Extras-Tests-LRP'
       
    10 !
       
    11 
       
    12 
       
    13 !PPCLRPSourcesResource methodsFor:'accessing'!
       
    14 
       
    15 sources
       
    16     sources isNil ifTrue:[ 
       
    17         sources := #(
       
    18             doranewbridge
       
    19             doraultratouch
       
    20             escapament
       
    21             follower
       
    22             linebounderfollower
       
    23             linecrossfollower
       
    24             rtimer
       
    25             stairsclimber
       
    26             stopwatch
       
    27             timer
       
    28         ) collect:[:e | self perform: e].                
       
    29     ].
       
    30     ^ sources
       
    31 
       
    32     "Created: / 30-07-2015 / 19:04:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    33 ! !
       
    34 
       
    35 !PPCLRPSourcesResource methodsFor:'sources - individual'!
       
    36 
       
    37 doranewbridge
       
    38 ^ ';; A Random Space Explorer behavior for a differential drive robot
       
    39 ;; Wall collisions are detected with the ultrasonic sensor
       
    40 
       
    41 (var speed :=[20])
       
    42 (var timeout := [1000 atRandom])
       
    43 (machine Dora
       
    44   (state forward
       
    45     (onentry [
       
    46       robot motorA startAtSpeed: speed.
       
    47       robot motorD startAtSpeed: speed.
       
    48     ])
       
    49     (onexit [robot fullStop]))
       
    50   (state shock
       
    51     (onentry [
       
    52       robot motorA startAtSpeed: speed negated.
       
    53       robot motorD startAtSpeed: speed negated.
       
    54     ])
       
    55     (onexit [robot fullStop]))
       
    56   (state turn
       
    57     (onentry [
       
    58       robot motorA startAtSpeed: speed.
       
    59       robot motorD startAtSpeed: speed negated
       
    60     ])
       
    61     (onexit [
       
    62       robot fullStop.
       
    63       timeout := 1000 atRandom
       
    64     ]))
       
    65   (ontime 500 shock -> turn st)
       
    66   (ontime timeout turn -> forward tf)
       
    67   (on tooclose forward -> shock fs)
       
    68   (event tooclose [robot sensor2 read < 200])
       
    69 )
       
    70 (spawn Dora forward)
       
    71 '
       
    72 
       
    73     "Created: / 30-07-2015 / 17:39:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    74 !
       
    75 
       
    76 doraultratouch
       
    77 ^ ';; A Random Space Explorer behavior for a differential drive robot
       
    78 ;; Wall collisions are detected with the ultrasonic sensor and touch sensors
       
    79 
       
    80 (var speed :=[20])
       
    81 (var timeout := [1000 atRandom])
       
    82 (machine Dora
       
    83   (state forward
       
    84     (onentry [
       
    85       robot motorA startAtSpeed: speed.
       
    86       robot motorD startAtSpeed: speed.
       
    87     ])
       
    88     (onexit [robot fullStop]))
       
    89   (state shock
       
    90     (onentry [
       
    91       robot motorA startAtSpeed: speed negated.
       
    92       robot motorD startAtSpeed: speed negated.
       
    93     ])
       
    94     (onexit [robot fullStop]))
       
    95   (state turn
       
    96     (onentry [
       
    97       robot motorA startAtSpeed: speed.
       
    98       robot motorD startAtSpeed: speed negated
       
    99     ])
       
   100     (onexit [
       
   101       robot fullStop.
       
   102       timeout := 1000 atRandom
       
   103     ]))
       
   104   (ontime 500 shock -> turn st)
       
   105   (ontime timeout turn -> forward tf)
       
   106   (on tooclose forward -> shock fs)
       
   107   (on shockLeft forward -> shock sl)
       
   108   (on shockRight forward -> shock sf)
       
   109   (event tooclose [robot sensor2 read < 200])
       
   110   (event shockLeft [robot sensor4 read = 1])
       
   111   (event shockRight [robot sensor1 read = 1])
       
   112 )
       
   113 (spawn Dora forward)
       
   114 
       
   115 '
       
   116 
       
   117     "Created: / 30-07-2015 / 17:40:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   118 !
       
   119 
       
   120 escapament
       
   121 ^ ';;; A simple escapement
       
   122 (machine esc
       
   123         (state tick)
       
   124         (state tock)
       
   125         (ontime 500 tick -> tock tito)
       
   126         (ontime 500 tock -> tick toti)
       
   127 )
       
   128 (spawn esc tick)'
       
   129 
       
   130     "Created: / 30-07-2015 / 17:37:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   131 !
       
   132 
       
   133 follower
       
   134 ^ ';; A line follower for a differential drive robot
       
   135 ;; Uses the Lego color sensor,
       
   136 ;; position is front and center, and pointed to the line (down)
       
   137 (var sensor := [robot sensor3])
       
   138 (var mright := [robot motorA])
       
   139 (var mleft := [robot motorD])
       
   140 (var speed := [4])
       
   141 (machine follower
       
   142         (state init
       
   143                 (onentry [sensor setMode: #Mode2])
       
   144         )
       
   145         (state moving
       
   146                 (onentry [
       
   147                         mright startAtSpeed: speed * 2.
       
   148                         mleft startAtSpeed: speed * 2.
       
   149                         ])
       
   150                 (onexit [robot fullStop])
       
   151         )
       
   152         (state looking
       
   153                 (machine lookalgo
       
   154                         (var looktime := [1000])
       
   155                         (state lookright
       
   156                                 (onentry 
       
   157                                         [mright startAtSpeed: speed negated.
       
   158                                         mleft startAtSpeed: speed.]))
       
   159                         (state lookleft
       
   160                                 )
       
   161                         (state centerfromright
       
   162                                 (onentry 
       
   163                                         [mright startAtSpeed: speed.
       
   164                                         mleft startAtSpeed: speed negated.]))
       
   165                         (state centerfromleft
       
   166                                 (onentry 
       
   167                                         [mright startAtSpeed: speed negated.
       
   168                                         mleft startAtSpeed: speed.])
       
   169                                 (onexit [looktime := looktime * 2]))
       
   170                         (ontime looktime lookright -> centerfromright tlrb)
       
   171                         (ontime looktime centerfromright -> lookleft tlr)
       
   172                         (ontime looktime lookleft -> centerfromleft tfail)
       
   173                         (ontime looktime centerfromleft -> lookright tfailb)
       
   174                         )
       
   175                 (onentry (spawn lookalgo lookright))
       
   176                 )
       
   177         (eps init -> moving tinit)
       
   178         (on out moving -> looking tms)
       
   179         (event out [(sensor read = 1) not])
       
   180         (event in  [sensor read = 1])
       
   181         (on in looking -> moving tsm)
       
   182 )
       
   183 (spawn follower init)
       
   184 
       
   185 '
       
   186 
       
   187     "Created: / 30-07-2015 / 17:40:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   188 !
       
   189 
       
   190 linebounderfollower
       
   191 ^ ';; Line following robot for a differential drive robot
       
   192 ;;; that "bounces" off the left hand side of the line
       
   193 ;; Uses the Lego color sensor,
       
   194 ;; position is front and center, and pointed to the line (down)
       
   195 (var sensor := [robot sensor3])
       
   196 (var mright := [robot motorA])
       
   197 (var mleft := [robot motorD])
       
   198 (machine linebounce
       
   199         (state init
       
   200                 (onentry [sensor setMode: #Mode2.]))
       
   201         (eps init -> white iw)
       
   202         (state white
       
   203                 (onentry [
       
   204                         mright startAtSpeed: 4.
       
   205                         mleft startAtSpeed: 18])
       
   206                 (onexit [mright stop. mleft stop]))
       
   207         (state black
       
   208                 (onentry [
       
   209                         mright startAtSpeed: 10.
       
   210                         mleft startAtSpeed: -4])
       
   211                 (onexit [mright stop. mleft stop]))
       
   212         (on seeblack white -> black wb)
       
   213         (on seewhite black -> white bw)
       
   214         (event seeblack [sensor read = 1])
       
   215         (event seewhite [(sensor read = 1) not])
       
   216 )
       
   217 (spawn linebounce init)
       
   218 
       
   219 '
       
   220 
       
   221     "Created: / 30-07-2015 / 17:41:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   222 !
       
   223 
       
   224 linecrossfollower
       
   225 ^ ';;; Line following for a differential drive robot
       
   226 ;;; goes forward by always crossing the line
       
   227 ;; Uses the Lego color sensor,
       
   228 ;; position is front and center, and pointed to the line (down)
       
   229 (var sensor := [robot sensor3])
       
   230 (var mright := [robot motorA])
       
   231 (var mleft := [robot motorD])
       
   232 (var speed := [15])
       
   233 (machine linecross
       
   234         (state left
       
   235                 (onentry [mright startAtSpeed: speed]))
       
   236         (state crossfl  
       
   237                 (onexit [mright stop]))
       
   238         (state right
       
   239                 (onentry [mleft startAtSpeed: speed]))
       
   240         (state crossfr
       
   241                 (onexit [mleft stop]))  
       
   242         (on black right -> crossfr rlx)
       
   243         (on black left -> crossfl lrx)
       
   244         (on white crossfr -> left rl)
       
   245         (on white crossfl -> right lr)
       
   246         (event black [sensor read = 1])
       
   247         (event white [(sensor read = 1) not])
       
   248 )
       
   249 (spawn linecross left)
       
   250 
       
   251 '
       
   252 
       
   253     "Created: / 30-07-2015 / 17:41:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   254 !
       
   255 
       
   256 rtimer
       
   257 ^ ';;a resettable timer with 10 sec intervals
       
   258 (var minute := [0])
       
   259 (machine rtimer
       
   260         (state zero)
       
   261         (state ten)
       
   262         (state twenty)
       
   263         (state thirty)
       
   264         (state fourty)
       
   265         (state fifty
       
   266                 (onexit [minute := minute + 1]))
       
   267         (ontime 10000 zero -> ten toten)
       
   268         (ontime 10000 ten -> twenty totwenty)
       
   269         (ontime 10000 twenty -> thirty tothirty)
       
   270         (ontime 10000 thirty -> fourty tofourty)
       
   271         (ontime 10000 fourty -> fifty tofifty)
       
   272         (ontime 10000 fifty -> zero tozero)
       
   273         (var doreset := [0])
       
   274         (state init
       
   275                 (onentry [minute := 0.
       
   276                         doreset := 0]))
       
   277         (on resetting *-> init reset)
       
   278         (eps init -> zero go)
       
   279         (event resetting [doreset = 1])
       
   280 )
       
   281 (spawn rtimer zero)
       
   282 '
       
   283 
       
   284     "Created: / 30-07-2015 / 17:38:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   285 !
       
   286 
       
   287 stairsclimber
       
   288 ^ ';;; A stairs climber building using the MindStorm expansion set
       
   289 ;;; Use the new JetStorm Bridge funcionality
       
   290 ;;; Use ontime transition to estimate the second step of the climbing
       
   291 ;;; Watch example in: http://youtu.be/6HXcKwMO8Fo
       
   292 
       
   293 (var backWheelsSpeed := [-50])
       
   294 (var frontWheelsSpeed := [-50])
       
   295 (var climbSpeed := [-50])
       
   296 (var startGyro := [0])
       
   297 (var deltaGyro := [4])
       
   298 (machine Stair
       
   299   (state forward
       
   300     (onentry [
       
   301       robot motorA startAtSpeed: backWheelsSpeed.
       
   302       robot motorB startAtSpeed: frontWheelsSpeed.
       
   303       startGyro := robot sensor2 read.
       
   304       ]
       
   305     )
       
   306     (onexit [
       
   307       robot fullStop.
       
   308       ]
       
   309     )
       
   310   )
       
   311 
       
   312   (state climb
       
   313     (onentry [
       
   314       robot motorD startAtSpeed: climbSpeed.
       
   315       robot motorB startAtSpeed: frontWheelsSpeed.
       
   316       ]
       
   317     )
       
   318     (onexit [
       
   319       robot fullStop.
       
   320       ]
       
   321     )
       
   322   )
       
   323 
       
   324   (state forward2
       
   325     (onentry [
       
   326       robot motorA startAtSpeed: backWheelsSpeed.
       
   327       robot motorB startAtSpeed: frontWheelsSpeed.
       
   328       ]
       
   329     )
       
   330   )
       
   331 
       
   332   (state climb2
       
   333     (onentry [
       
   334       robot motorD startAtSpeed: climbSpeed negated.
       
   335       ]
       
   336     )
       
   337     (onexit [
       
   338       robot fullStop 
       
   339       ]
       
   340     )
       
   341   )
       
   342 
       
   343   (state stop
       
   344     (onentry [robot fullStop])
       
   345   )
       
   346 
       
   347   (on incline forward -> climb incline)
       
   348   (on finishClimb climb -> forward2 finishClimb)
       
   349   (event finishClimb [robot sensor3 read == 1])
       
   350   (event incline [robot sensor2 read > (deltaGyro + startGyro)])
       
   351 
       
   352   (ontime 3000 forward2 -> climb2 for2)
       
   353   (ontime 6000 climb2 -> forward loop)
       
   354 )
       
   355 (spawn Stair forward)
       
   356 
       
   357 '
       
   358 
       
   359     "Created: / 30-07-2015 / 17:42:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   360 !
       
   361 
       
   362 stopwatch
       
   363 ^ ';; A stopwatch.
       
   364 ;; Inspect the variables start and reset,
       
   365 ;; and from the inspectors change the values
       
   366 (machine stopwatch
       
   367         (var start := [false])
       
   368         (var reset := [false])
       
   369         (var seconds := [0])
       
   370         (state waiting )
       
   371         (on e_starting waiting -> tick t_st)
       
   372         (event e_starting [start])
       
   373         (state tick)
       
   374         (state tock (onexit [seconds := seconds + 1]))
       
   375         (ontime 500 tick -> tock t_tito)
       
   376         (ontime 500 tock -> tick t_toti)
       
   377 
       
   378         (on e_reset *-> resetting t_reset)
       
   379         (event e_reset [reset])
       
   380         (state resetting
       
   381                 (onentry [reset := false. seconds := 0] ))
       
   382         (eps resetting -> tick t_et)
       
   383 
       
   384         (on e_stop *-> waiting t_es)
       
   385         (event e_stop [start not])
       
   386 )
       
   387 (spawn stopwatch waiting)
       
   388 '
       
   389 
       
   390     "Created: / 30-07-2015 / 17:38:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   391 !
       
   392 
       
   393 timer
       
   394 ^ ';; a timer with 10 sec intervals
       
   395 
       
   396 (var minute := [0])
       
   397 (machine timer
       
   398         (state zero)
       
   399         (state ten)
       
   400         (state twenty)
       
   401         (state thirty)
       
   402         (state fourty)
       
   403         (state fifty
       
   404                 (onexit [minute := minute + 1]))
       
   405         (ontime 10000 zero -> ten toten)
       
   406         (ontime 10000 ten -> twenty totwenty)
       
   407         (ontime 10000 twenty -> thirty tothirty)
       
   408         (ontime 10000 thirty -> fourty tofourty)
       
   409         (ontime 10000 fourty -> fifty tofifty)
       
   410         (ontime 10000 fifty -> zero tozero)
       
   411 )
       
   412 (spawn timer zero)
       
   413 
       
   414 '
       
   415 
       
   416     "Created: / 30-07-2015 / 17:39:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   417 ! !
       
   418 
       
   419 !PPCLRPSourcesResource class methodsFor:'documentation'!
       
   420 
       
   421 version_HG
       
   422 
       
   423     ^ '$Changeset: <not expanded> $'
       
   424 ! !
       
   425