Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
blocks:advanced_scripting:example1 [2019-02-12 20:20] admin created |
blocks:advanced_scripting:example1 [2019-03-06 15:51] (current) admin Foxed typo in initComms |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== User Script Case Study ====== | ====== User Script Case Study ====== | ||
- | Here's a rather interesting case study that shows how a user script can be utilized as a bridge between a position tracking sensor and the scroll position of a scroll block. The project involves a 5 meter long wall with a printed " | + | Here's a rather interesting case study that shows how a user script can be utilized as a bridge between a position tracking sensor and the scroll position of a scroll block. The project involves a 5 meter long wall with a printed " |
{{ : | {{ : | ||
- | To interact with another part of the wall, the visitor can push the screen to the desired position. Hence, the screen | + | To interact with another part of the wall, the visitor can push the screen to the desired position. Hence, the screen |
- | The sensor | + | The barcode reader |
{{: | {{: | ||
- | {{ : | + | {{ : |
- | The sensor uses a laser to read the barcode strip, streaming the resulting data over a serial (RS-232) connection, which in its turn connects to Blocks through a [[blocks: | ||
- | Here's the complete script used to read the position data, scale it as appropriate and apply the result to the scroll position of the display spot. The script is saved in a file named LeuzeScript.ts in the script/user directory of the Blocks root. It compiles to a file named LeuzeScript.js, | + | The sensor uses a laser to read the barcode strip, streaming the resulting data over a serial RS-232 connection, which in its turn connects to Blocks through a [[blocks: |
+ | |||
+ | To save some time in getting the complete solution working, no driver was written for this device. Instead, the code that received data from the sensor was rolled into the user script. Another option could have been to write a driver that published a property corresponding to the most recent position readout. That would have simplified the user script at the expense of writing //both// a driver and a user script. | ||
+ | |||
+ | Here's the complete script used to read the position data, scale it as appropriate and apply the result to the scroll position of the display spot. | ||
< | < | ||
Line 32: | Line 35: | ||
export class LeuzeScript extends Script { | export class LeuzeScript extends Script { | ||
networkPort: | networkPort: | ||
- | lastPosition: | + | lastPosition: |
timeout: CancelablePromise< | timeout: CancelablePromise< | ||
Line 42: | Line 45: | ||
// Get the network TCP connection used to talk to the device. | // Get the network TCP connection used to talk to the device. | ||
this.networkPort = < | this.networkPort = < | ||
+ | |||
+ | this.networkPort.subscribe(' | ||
+ | this.dataReceived(message.text) | ||
+ | ); | ||
if (this.networkPort.connected) // Handle connected BEFORE script starts | if (this.networkPort.connected) // Handle connected BEFORE script starts | ||
Line 55: | Line 62: | ||
} | } | ||
- | /** Hook up event handler | + | /** What needs to be one when we connect (or re-connect) to the reader |
*/ | */ | ||
doWhenConnected() { | doWhenConnected() { | ||
- | this.networkPort.subscribe(' | ||
- | this.dataReceived(message.text) | ||
- | ); | ||
this.initComms(); | this.initComms(); | ||
this.resetTimeout(); | this.resetTimeout(); | ||
Line 69: | Line 73: | ||
*/ | */ | ||
initComms() { | initComms() { | ||
- | if (this.networkPort.connected) {} | + | if (this.networkPort.connected) { |
this.networkPort.sendText(' | this.networkPort.sendText(' | ||
this.networkPort.sendText(' | this.networkPort.sendText(' | ||
Line 102: | Line 106: | ||
*/ | */ | ||
const posType = typeof position; | const posType = typeof position; | ||
- | if (posType === 'Number' && this.lastPosition !== position) { | + | if (posType === 'number' && this.lastPosition !== position) { |
// Was indeed a number, and different from last position received | // Was indeed a number, and different from last position received | ||
this.lastPosition = position; | this.lastPosition = position; | ||
Line 120: | Line 124: | ||
The script has plenty of comments explaining what's going on, but here are a few additional highlights. | The script has plenty of comments explaining what's going on, but here are a few additional highlights. | ||
+ | * The script must be saved in a file named LeuzeScript.ts in the script/user directory of the Blocks root. | ||
+ | * It compiles to a file named LeuzeScript.js, | ||
+ | * The script doesn' | ||
+ | * The class name must match the filename (minus the .ts/.js file extension). | ||
+ | * Several // | ||
+ | * The script must have a constructor that takes a single ScriptEnv parameter and calls super with this parameter. | ||
+ | * It uses the imported Network system object to obtain a reference to the network device named 'Leuze over Moxa', which has been added with that name under //Manage, Network TCP/UDP// in Blocks, with its IP address and port number set to the ones assigned to the MOXA interface. | ||
+ | * It subscribes to the // | ||
+ | * When data is received, the payload in // | ||
+ | * The sensor needs to be told to start streaming data. This is done by the // | ||
+ | * If the sensor is disconnected or lose power, it will revert back to its silent state, and must be told to start streaming data again. This is managed by the // | ||
+ | * The connection between Blocks and the MOXA nPort interface may be established before the script is started or after. Both these cases are accounted for in the constructor, | ||
+ | |||
+ | ==== Transforming the sensor data to a scroll position ==== | ||
+ | |||
+ | The code that binds the data received from the sensor to the scroll position of the spot is found in the // | ||
+ | |||
+ | Finally, the readout from the sensor is scaled to a normalized value (0...1), the Display Spot is obtained from the Spot system object, and scrolled to desired position. | ||
+ | |||
+ | ==== Credit where credit is due ==== | ||
+ | |||
+ | The idea described above was devised and implemented by Sierk Janszen, Wilfred de Zoete and Jean-Paul Coenraad at [[https:// | ||