chiark / gitweb /
fixes from bessar fettling session
[trains.git] / hostside / stopgap-controller
index fe475d844aad988d2db644324b993ff6f70e39a3..0c4f3342c2e3846e6b58429e665cecfc22a77c45 100755 (executable)
@@ -1,21 +1,36 @@
-#!/usr/bin/tclsh8.4
+#!/usr/bin/tclsh8.2
+# used like this:
+# liberator:hostside> ssh bessar 'cd things/trains-bessar/hostside && ./stopgap-controller' | ./gui-displayer -
 
-set testonly 1
+set testonly 0
+#set testonly 1
 set port /dev/ttya0
+#set port /dev/ttyS0
 
 set ch(funcsevery) 10
-set ch(speeddirnevery) 15
+set ch(speeddirnevery) 30
 set ch(scale) 1
 
 set ch(minint) 5000
 # unset always
-
-set m xx
-set segs xx
-set polarity 908000
+# set always 0
+set nmrawhich 0
+set lastptchosen xx
+
+set polmsg(l) 908000
+set polmsg(x) 90f802
+set polmsg(y) 90807c
+set pname l
+set m {}
+set nmradiv 0
+set segs {xx yy}
+set segsasgot {xx yy}
 set pq {} ;# unset: cdu charged and waiting
-set speeddirn {}
-set funcs {}
+set speeddirn ff7f
+#set speeddirn ffff80c3fbcced7f
+#set speeddirn_fixed {speed126 2 80 0}
+set speeddirn_fixed {}
+set funcs ff7f
 # unset pointpos($point)
 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
 
@@ -28,8 +43,9 @@ proc gui_init {} {
     gui "M A2 0"
     gui "M A5 0 J"
     gui "M A6 0 J"
+    gui "EOE"
     if {[info exists watchdog]} { gui "P 1" }
-    if {![regexp {^90} $polarity]} { gui_polarity }
+    gui_polarity
     foreach seg [array names segdetect] {
        gui "D1 $seg"
     }
@@ -66,7 +82,7 @@ proc fail_now {} {
     debug "failing now"
     fconfigure $p -blocking yes
     gui "P 0"
-    tellpic 20
+    tellpic 10
     exit 1
 }
 
@@ -74,39 +90,34 @@ proc fail {m} {
     global watchdog p
     catch { after cancel $watchdog; unset watchdog }
     puts "failing $m"
-    tellpic a001 ;# 16ms
-    after 2000 fail_now
+    tellpic 9801 ;# 16ms
+    after 1000 fail_now
     fileevent $p readable {}
 }
 
 proc gui_polarity {} {
-    foreach seg {
-       X8
-       X9
-       X10
-       X1
-       X2
-       X3
-       X4
-       X5
-       X6
-       X7
-    } {
-       gui "R $seg"
+    global pname
+    set 1 {}
+    switch -exact $pname {
+       l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
+       x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
+       y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
+    }
+    foreach v {0 1} {
+       foreach seg [set $v] {
+           gui "R $v $seg"
+       }
     }
 }
 
-proc polarity {m} {
-    global polarity
-    debug "polarising $m"
-    tellpic $m
-    if {[string compare $m $polarity]} {
-       gui_polarity
-    }
-    set polarity $m
+proc polarity {newpname} {
+    global pname polmsg
+    debug "polarising $newpname"
+    if {![string compare $pname $newpname]} return
+    tellpic $polmsg($newpname)
+    set pname $newpname
+    gui_polarity
 }
-proc polarity_l {} { polarity 908000 }
-proc polarity_x {} { polarity 97ff7f }
 
 proc pt_now {how point pos xtra} {
     set msg a0[lindex $point $pos]
@@ -127,6 +138,17 @@ proc pt_must {point newpos} {
     pt_now immed $point $pos {}
     set pq {}
 }
+proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
+    upvar #0 pointpos($ifpoint) ifpos
+    if {![info exists ifpos] || $ifpos != $ifposwant} return
+    pt_must $thenpoint $thenpos
+}
+
+proc badwatchdog {} {
+    global pq
+    puts "watchdog - oh well"
+    if {![info exists pq]} { set pq {} }
+}
 
 proc pm_charged {} {
     global pq
@@ -147,11 +169,13 @@ proc randbyte {} {
     return $x
 }
 
-proc pt_maybe {point} {
-    global always
+proc pt_maybe {point oneisright} {
+    global always lastptchosen
     if {[info exists always]} {
        set pos $always
     } else {
+       if {![string compare $point $lastptchosen]} return
+       set lastptchosen $point
        set x [randbyte]
        set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
        debug "chose point $point pos=$pos (x=$x)"
@@ -159,7 +183,7 @@ proc pt_maybe {point} {
     pt_must $point $pos
 }
 
-proc s0 {seg} {
+proc s0 {seg} {
     upvar #0 segdetect($seg) segd
     if {![info exists segd]} {
        debug "segment $seg = already"
@@ -176,9 +200,10 @@ proc s0t {seg} {
     unset segd
     gui "D0 $seg"
 }
-proc s1 {seg} {
+proc s1 {seg} {
     upvar #0 segdetect($seg) segd
     if {![info exists segd]} {
+       pm_detect $v
        debug "segment $seg ! (overwrites =)"
     } elseif {[string length $segd]} {
        debug "segment $seg ! (cancels =)"
@@ -193,73 +218,116 @@ proc s1 {seg} {
 
 proc pm_maydetect {d seg} {
     switch -exact $seg {
-       06 { s$d X10 }
-       09 { s$d X8 }
-       0a { s$d X6 }
-       04 { s$d X5 }
-       02 { s$d X7 }
-       07 { s$d X9 }
-       14 { s$d A5 }
-       20 { s$d A6 }
-       1a { s$d A4 }
-       10 { s$d A2 }
-       03 { s$d X1 }
-       05 { s$d X3 }
-       16 { s$d A3 }
-       1c { s$d A1 }
-       08 { s$d X2 }
-       0b { s$d X4 }
+       06 { s$d $seg X10 }
+       09 { s$d $seg X8 }
+       0a { s$d $seg X6 }
+       04 { s$d $seg X5 }
+       02 { s$d $seg X7 }
+       07 { s$d $seg X9 }
+       14 { s$d $seg A5 }
+       20 { s$d $seg A6 }
+       1a { s$d $seg A4 }
+       10 { s$d $seg A2 }
+       03 { s$d $seg X1 }
+       05 { s$d $seg X3 }
+       16 { s$d $seg A3 }
+       1c { s$d $seg A1 }
+       08 { s$d $seg X2 }
+       0b { s$d $seg X4 }
     }
 }
 
+#proc pm_nodetect {seg} {
+#    global segsasgot
+#    if {![string compare $seg [lindex $segsasgot 1]]} {
+#      set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
+#    }
+#}
+
 proc pm_detect {seg} {
-    global segs
-    switch -exact $seg {
-       07 - 06 { polarity_l }
-       16 - 1c - 1a - 10 - 03 - 05 - 08 - 0b { polarity_x }
+    global segs pname segsasgot
+    if {[string compare $seg [lindex $segsasgot 1]]} {
+       set segsasgot [list [lindex $segsasgot 1] $seg]
+    }
+    if {[lsearch -exact $segs $seg] < 0} {
+       set segs $segsasgot
+    }
+    debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
+#    if {[lsearch -exact {
+#      06 09 0a 04 02 07 14 20
+#      0b 08 1c 16
+#      1a 10 03 05
+#    } $seg] < 0} return
+    switch -exact $pname$seg {
+       l16 - l1c - l08 - l0b { polarity y }
+       l10 - l1a - l03 - l05 { polarity x }
+       x07 - x04 - x0a { polarity l }
+       x16 - x1c - x14 - x0b { polarity y }
+       y06 - y04 - y0a { polarity l }
+       y20 - y10 - y1a - y05 { polarity x }
     }
     switch -exact $seg {
-       14 - 20 { pt_must "02 03 A5" 1; pt_must "42 43 A6" 1 }
        04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
-       03 - 05 { pt_must "00 01 X7" 0 }
-       08 - 0b { pt_must "40 41 X8" 0 }
+       05 { pt_must "00 01 X7" 0 }
+       0b { pt_must "40 41 X8" 0 }
        16 - 1c { pt_must "02 03 A5" 0 }
        1a - 10 { pt_must "42 43 A6" 0 }
-    }
-    if {[lsearch -exact $segs $seg] < 0} {
-       set segs [list [lindex $segs end] $seg]
+       14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
+       20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
     }
     switch -exact [join $segs -] {
-       07-02 { pt_maybe "00 01 X7" }
-       02-07 { pt_maybe "02 03 A5" }
-       06-09 { pt_maybe "40 41 X8" }
-       09-06 { pt_maybe "42 43 A6" }
+       02-07 { pt_maybe "02 03 A5" 1 }
+       07-02 { pt_maybe "00 01 X7" 0 }
+       09-06 { pt_maybe "42 43 A6" 0 }
+       06-09 { pt_maybe "40 41 X8" 1 }
     }
 }
 
+proc tellnmra {m} {
+#    global nmrawhich speeddirn funcs
+#    set m 0x$m
+#    for {set i 0} {$i < $m} {incr i} {
+#      tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
+#      set nmrawhich [expr {!$nmrawhich}]
+#    }
+}
+
 proc watchdog {} {
-    global watchdog testonly speeddirn funcs
+    global watchdog testonly speeddirn funcs nmradiv
     catch { after cancel $watchdog }
     set watchdog [after 50 watchdog]
-    tellpic_q 9808$speeddirn$funcs ;# 128ms
+    tellpic_q 9808 ;# 128ms
+    if {[incr nmradiv] > 35} {
+       tellpic_q $speeddirn$funcs
+       set nmradiv 0
+    }
 }
 
 proc pm_hello {} {
     debug "got hello, starting up"
-    tellpic 21
+    tellpic 11
     gui "P 1"
     watchdog
+    changewhat
+    tellnmra 01
 }
 
-proc frompic {m} {
+proc fp {m} {
     debug "<< $m"
+}
+
+proc frompic {m} {
+    set v [lindex $m 1]
     switch -glob [lindex $m 0] {
-       09 { pm_hello }
-       28 { pm_charged }
-       9[0-7] { pm_maydetect 0 [lindex $m 1] }
-       9? { pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
+       01 - 02 { tellnmra $m }
+       09 { fp $m; pm_hello }
+       07 { puts "short circuit"; exit 1 }
+       0d { fp $m; badwatchdog }
+       28 { fp $m; pm_charged }
+       9[0-7] { fp $m; pm_maydetect 0 $v }
+       9? { fp $m; pm_maydetect 1 $v }
        0a - [234567]? { puts "pic debug $m" }
-       * { fail "pic unknown $m" }
+       * { fp $m; fail "pic unknown $m" }
     }
 }
 
@@ -269,16 +337,20 @@ proc onreadp_test {} {
 }
 
 proc onreadp {} {
-    global p m
+    global p m rand
     while 1 {
        set c [read $p 1]
-       if {![string llength $c]} {
+       if {![string length $c]} {
            if {[eof $p]} { error "eof on device" }
            return
        }
        binary scan $c H* x
+       if {![info exists rand]} {
+           fp ...$x
+           return
+       }
        lappend m $x
-       if {[regexp {^[89a-f]} $x]} {
+       if {[regexp {^[0-7]} $x]} {
            if {![regexp {^x} $m]} {
                frompic $m
            }
@@ -289,9 +361,10 @@ proc onreadp {} {
 
 proc newspeeddirn {} {
     set b1 0x[randbyte]
-    set speed [expr {($b1 * $b1) / 516}]
+    set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
     set b2 0x[randbyte]
     set dirn [expr {$b2 / 128}]
+    set dirn 0
     debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
     return "speed126 2 $speed $dirn"
 }
@@ -305,35 +378,55 @@ proc newfuncs {} {
 
 proc maybechange {thing} {
     global $thing ch
-    set rb 0x[randbyte][randbyte]
-    if {
-       $rb / 65536.0 >
-       1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
-    } {
-       debug "maybechange $thing rb=$rb no"
-       return 0
+    upvar #0 ${thing}_fixed fixed
+    if {![info exists fixed]} {
+       set rb 0x[randbyte][randbyte]
+       if {
+           $rb / 65536.0 >
+           1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
+       } {
+           debug "maybechange $thing rb=$rb no"
+           return 0
+       }
+       debug "maybechange $thing rb=$rb yes ..."
+       set l [new$thing]
+    } else {
+       debug "fixed $thing $fixed"
+       set l $fixed
+       if {![llength $l]} { return 0 }
     }
-    debug "maybechange $thing rb=$rb yes ..."
-    set l [new$thing]
     set bin [eval exec ./hostside-old -s/dev/stdout $l]
     binary scan $bin H* x
     debug "changed $thing=$x"
-    set $thing $x
+    set $thing ff$x
     return 1
 }
 
 proc changewhat {} {
-    global ch
+    global ch chwa
+    catch { after cancel $chwa }
     if {[maybechange speeddirn] || [maybechange funcs]} {
        set interval $ch(minint)
     } else {
        set interval 1000
     }
-    after $interval changewhat
+    set chwa [after $interval changewhat]
+}
+
+proc onreadcmd {} {
+    if {[gets stdin l] < 0} {
+       if {[eof stdin]} {
+           puts "GUI exit 0"
+           fail "stopgap-controller got eof, quitting"
+           fileevent stdin readable {}
+       }
+       return
+    }
+    eval $l
 }
 
 proc setup {} {
-    global port p rand testonly
+    global port p testonly
     if {!$testonly} {
        set p [open $port {RDWR NONBLOCK} 0]
     
@@ -347,17 +440,37 @@ proc setup {} {
                -blocking false -buffering none
 
        fileevent $p readable onreadp
+       fconfigure stdin -blocking false
+       fileevent stdin readable onreadcmd
     } else {
        set p stdin
        fconfigure stdin -blocking false
        fileevent stdin readable onreadp_test
     }
 
+    after 250 setup_complete
+}
+
+proc setup_complete {} {
+    global rand
     set rand [open /dev/urandom {RDONLY} 0]
     fconfigure $rand -encoding binary -translation binary
+    tellpic 0a
+}
 
-    changewhat
+
+#----------
+# for keyboard control
+
+proc ask_fast {} {
+    global speeddirn_fixed; set speeddirn_fixed {speed126 2 126 0}
+}
+proc ask_slow {} {
+    global speeddirn_fixed; set speeddirn_fixed {speed126 2 10 0}
 }
+proc ask_randspeed {} {
+    global speeddirn_fixed; catch { unset speeddirn_fixed }
+}    
 
 setup
 gui_init