diff options
| author | Omar Rizwan <omar@omar.website> | 2023-06-30 18:11:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-30 18:11:12 +0000 |
| commit | 8a8a749eb2afbf6b0636c280554c32219fff1dd2 (patch) | |
| tree | a4fca4989a76d0e17c2d17ffe8ad1ad349111df2 /virtual-programs | |
| parent | Left-align labels, watch for .folk.temp files (diff) | |
| parent | Fix load librt to work on folk0 also (diff) | |
| download | folk-8a8a749eb2afbf6b0636c280554c32219fff1dd2.tar.gz folk-8a8a749eb2afbf6b0636c280554c32219fff1dd2.zip | |
Merge pull request #42 from FolkComputer/osnr/camera-pipeline
Run Camera in subprocess
Diffstat (limited to 'virtual-programs')
| -rw-r--r-- | virtual-programs/camera.folk | 37 | ||||
| -rw-r--r-- | virtual-programs/new-program-web-editor.folk | 9 | ||||
| -rw-r--r-- | virtual-programs/regions.folk | 3 | ||||
| -rw-r--r-- | virtual-programs/shapes.folk | 2 | ||||
| -rw-r--r-- | virtual-programs/tags-and-calibration.folk | 5 | ||||
| -rw-r--r-- | virtual-programs/web-editor.folk | 188 |
6 files changed, 146 insertions, 98 deletions
diff --git a/virtual-programs/camera.folk b/virtual-programs/camera.folk new file mode 100644 index 00000000..b865392e --- /dev/null +++ b/virtual-programs/camera.folk @@ -0,0 +1,37 @@ +if {$::isLaptop} return + +namespace eval ::Camera { + variable WIDTH 1280 + variable HEIGHT 720 +} + +On process { + source pi/Camera.tcl + source pi/AprilTags.tcl + Camera::init 1280 720 + AprilTags::init + + puts "Camera tid: [getTid]" + + while true { + set cameraTime [time { + set grayFrame [Camera::grayFrame] + }] + set aprilTime [time { + set tags [AprilTags::detect $grayFrame] + }] + + Commit { + Claim the camera frame is $grayFrame + + Claim the camera time is $cameraTime + Claim the AprilTag time is $aprilTime + + foreach tag $tags { + Claim tag [dict get $tag id] has center [dict get $tag center] size [dict get $tag size] + Claim tag [dict get $tag id] has corners [dict get $tag corners] + } + } + Step + } +} diff --git a/virtual-programs/new-program-web-editor.folk b/virtual-programs/new-program-web-editor.folk index 692b00e0..5797365b 100644 --- a/virtual-programs/new-program-web-editor.folk +++ b/virtual-programs/new-program-web-editor.folk @@ -113,9 +113,9 @@ Wish the web server handles route "/new" with handler { ws.close(); } ws.onmessage = (msg) => { - if (msg.data.startsWith("ERROR:")) { + if (msg.data.startsWith("Error:")) { const errorEl = document.getElementById("error"); - if (msg.data == "ERROR: {}") { + if (msg.data === "Error:") { errorEl.style.backgroundColor = ""; errorEl.innerText = ""; } else { @@ -153,7 +153,10 @@ Wish the web server handles route "/new" with handler { if {$::isLaptop} { Step } `); setTimeout(() => { - send(`list ERROR: [Statements::findMatches [list {${program}} has error /err/ with info /errorInfo/]]`); + send(` +set errors [Statements::findMatches [list {${program}} has error /err/ with info /errorInfo/]] +join [list "Error:" {*}[lmap e $errors {dict get $e errorInfo}]] "\n" +`); }, 500); } let jobid; diff --git a/virtual-programs/regions.folk b/virtual-programs/regions.folk index 1702bbfa..8f320b42 100644 --- a/virtual-programs/regions.folk +++ b/virtual-programs/regions.folk @@ -1,5 +1,4 @@ namespace eval ::vec2 { - namespace import ::tcl::mathop::+ ::tcl::mathop::- ::tcl::mathop::* proc add {a b} { list [+ [lindex $a 0] [lindex $b 0]] [+ [lindex $a 1] [lindex $b 1]] } @@ -21,8 +20,6 @@ namespace eval ::vec2 { proc dot {a b} { expr {[lindex $a 0]*[lindex $b 0] + [lindex $a 1]*[lindex $b 1]} } - namespace import ::tcl::mathfunc::max ::tcl::mathfunc::min - namespace import ::tcl::mathop::/ proc distanceToLineSegment {a v w} { set l2 [vec2 distance $v $w] if {$l2 == 0.0} { diff --git a/virtual-programs/shapes.folk b/virtual-programs/shapes.folk index 25c615c5..e5a67fe5 100644 --- a/virtual-programs/shapes.folk +++ b/virtual-programs/shapes.folk @@ -68,6 +68,8 @@ When /someone/ wishes /p/ draws a /color/ /shape/ offset /offsetVector/ & /p/ ha set y [expr {$y + $offsetY}] } + # puts "drawing $shape at $x $y" + set adjustedWidth [expr {$width * 0.25}] set x [expr { $x * 1.3}] set y [expr { $y * 1.25}] diff --git a/virtual-programs/tags-and-calibration.folk b/virtual-programs/tags-and-calibration.folk index f3155612..cf252de6 100644 --- a/virtual-programs/tags-and-calibration.folk +++ b/virtual-programs/tags-and-calibration.folk @@ -1,5 +1,10 @@ if {$::isLaptop} { return } +namespace eval ::Camera { + variable WIDTH 1280 + variable HEIGHT 720 +} + package require math::linearalgebra namespace import ::math::linearalgebra::add \ ::math::linearalgebra::sub \ diff --git a/virtual-programs/web-editor.folk b/virtual-programs/web-editor.folk index 6669d9fa..ae14f9b4 100644 --- a/virtual-programs/web-editor.folk +++ b/virtual-programs/web-editor.folk @@ -1,117 +1,121 @@ Wish the web server handles route {/page/(.*)$} with handler { if {[regexp -all {/page/(\d*)$} $path whole_match program_id]} { - set filename "../folk-printed-programs/$program_id.folk" + set filename "../folk-printed-programs/$program_id.folk" set fp [open $filename r] set file_data [read $fp] close $fp } elseif {[regexp -all {/page/(.*)$} $path whole_match program_id]} { - set filename "virtual-programs/$program_id.folk" + set filename "virtual-programs/$program_id.folk" set fp [open $filename r] set file_data [read $fp] close $fp } html [string map [list file_data [htmlEscape $file_data] program_id $program_id file_name $filename] { - <html> - <body> - <div> - <span id="status">Status</span> - <button onclick="handleSave()">Save</button> - <button id="print" onclick="handlePrint()">Print</button> - </div> - <textarea id="code" style="width: 100%;height: 95vh;">file_data</textarea> - <pre id="error"></pre> - <script> - const isVirtualProgram = 'file_name'.includes('virtual-programs'); + <html> + <body> + <div> + <span id="status">Status</span> + <button onclick="handleSave()">Save</button> + <button id="print" onclick="handlePrint()">Print</button> + </div> + <textarea id="code" style="width: 100%;height: 95vh;">file_data</textarea> + <pre id="error"></pre> + <script> + const isVirtualProgram = 'file_name'.includes('virtual-programs'); - if (isVirtualProgram) { - document.getElementById("print").disabled = true; - } + if (isVirtualProgram) { + document.getElementById("print").disabled = true; + } - const codeEle = document.getElementById("code"); - function uuidv4() { - return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => - (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) - ); - } + const codeEle = document.getElementById("code"); + function uuidv4() { + return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => + (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) + ); + } - // Cmd + S || Ctrl + S => Save - document.addEventListener('keydown', function(e) { - if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && e.keyCode == 83) { - e.preventDefault(); - handleSave(); - } - }, false); - // Cmd + P || Ctrl + P => Print - document.addEventListener('keydown', function(e) { - if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && e.keyCode == 80) { - e.preventDefault(); - handlePrint(); - } - }, false); + // Cmd + S || Ctrl + S => Save + document.addEventListener('keydown', function(e) { + if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && e.keyCode == 83) { + e.preventDefault(); + handleSave(); + } + }, false); + // Cmd + P || Ctrl + P => Print + document.addEventListener('keydown', function(e) { + if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && e.keyCode == 80) { + e.preventDefault(); + handlePrint(); + } + }, false); - let ws; - let send; - function wsConnect() { - ws = new WebSocket(window.location.origin.replace("http", "ws") + "/ws"); - send = function(s) { ws.send(s); } + let ws; + let send; + function wsConnect() { + ws = new WebSocket(window.location.origin.replace("http", "ws") + "/ws"); + send = function(s) { ws.send(s); } - ws.onopen = () => { - document.getElementById('status').innerHTML = "<span style=background-color:seagreen;color:white;>Connnected</span>"; - }; - ws.onclose = window.onbeforeunload = () => { - document.getElementById('status').innerHTML = "<span style=background-color:red;color:white;>Disconnnected</span>"; - setTimeout(() => { wsConnect(); }, 1000); - }; - ws.onerror = (err) => { - document.getElementById('status').innerText = "Error"; - console.error('Socket encountered error: ', err.message, 'Closing socket'); - ws.close(); - } - ws.onmessage = (msg) => { - if (msg.data.startsWith("ERROR:")) { - const errorEl = document.getElementById("error"); - if (msg.data == "ERROR: {}") { - errorEl.style.backgroundColor = ""; - errorEl.innerText = ""; - } else { - errorEl.style.backgroundColor = "#f55"; - errorEl.innerText = msg.data; - } - } - } - }; - wsConnect(); + ws.onopen = () => { + document.getElementById('status').innerHTML = "<span style=background-color:seagreen;color:white;>Connnected</span>"; + }; + ws.onclose = window.onbeforeunload = () => { + document.getElementById('status').innerHTML = "<span style=background-color:red;color:white;>Disconnnected</span>"; + setTimeout(() => { wsConnect(); }, 1000); + }; + ws.onerror = (err) => { + document.getElementById('status').innerText = "Error"; + console.error('Socket encountered error: ', err.message, 'Closing socket'); + ws.close(); + } + ws.onmessage = (msg) => { + if (msg.data.startsWith("Error:")) { + const errorEl = document.getElementById("error"); + if (msg.data === "Error:") { + errorEl.style.backgroundColor = ""; + errorEl.innerText = ""; + } else { + errorEl.style.backgroundColor = "#f55"; + errorEl.innerText = msg.data; + } + } + } + }; + wsConnect(); - function handleSave() { - const code = document.getElementById("code").value; - send(` - set fp [open file_name w] - puts -nonewline $fp {${code}} - close $fp - puts "Saved program_id.folk" - `); + function handleSave() { + const code = document.getElementById("code").value; + send(` + set fp [open file_name w] + puts -nonewline $fp {${code}} + close $fp + puts "Saved program_id.folk" + `); if (isVirtualProgram) { send(`EditVirtualProgram file_name {${code}}`) - } + } - setTimeout(() => { - send(`list ERROR: [Statements::findMatches [list program_id has error /err/ with info /errorInfo/]]`); - }, 500); - } - let jobid; - function handlePrint() { - const code = document.getElementById("code").value; - jobid = String(Math.random()); - send(`Assert web wishes to print program program_id with code {${code}} with job id {${jobid}}`); - setTimeout(500, () => { - send(`Retract web wishes to print program program_id with code {${code}} with job id {${jobid}}`); - }); - } - </script> - </body> - </html> + setTimeout(() => { + send(` +set errors [Statements::findMatches [list program_id has error /err/ with info /errorInfo/]] +join [list "Error:" {*}[lmap e $errors {dict get $e errorInfo}]] "\n" +`); + }, 500); + } + + let jobid; + function handlePrint() { + const code = document.getElementById("code").value; + jobid = String(Math.random()); + send(`Assert web wishes to print program program_id with code {${code}} with job id {${jobid}}`); + setTimeout(500, () => { + send(`Retract web wishes to print program program_id with code {${code}} with job id {${jobid}}`); + }); + } + </script> + </body> + </html> }] } |
