Matlab Variables in LSL script - matlab

I have Matlab code with variable (value varies frequently, say 10 times/Sec) and I want to use that variable's value in second life script i.e. LSL Script. I read somewhere to do this with HTTPRequest/Response methods, but my Matlab code is developed for desktop application. And I do not know much about LSL script.

I think you have quite a bit of learning ahead of you. First you need to choose an interface to the Matlab data. It supports access via many languages, including common Internet-friendly languages. You'll need to get that data to the Internet somehow so that it can communicate with a region server running. Since it supports access via external languages, I think you're in good shape there.
The main question in my mind is whether you are referring to dynamic data. Is the data changing, and you want to provide access to real-time updates in your LSL script? Or are you referring to a historical recording of data that was changing 10 times per second (in other words, a data file)?
If the data is static, and not too large, you could just convert it into a text format, include it in the Contents of the prim with the LSL script, and read that with the Notecard functions in LSL.
Alternatively, you could somehow push it to a file on a hosted website, then use llHTTPRequest from your LSL script to fetch it. You can also do this if it's changing infrequently, and occasional updates are sufficient.
However, if the data is constantly changing, and ideally if MatLab has a way of notifying a remote Internet webserver (for example, if it does HTTP PUTs to a URL with regular updates), or if you can write a script at your end to fetch the data from MatLab and send it to an LSL object, you could directly contact an object in-world via LSL's HTTP-In functionality. See llRequestURL and the http_request event documentation.
There are some good examples on those pages.

Related

Read / run a MATLAB script from a URL

Fairly simply question, but I cannot find an answer via Google or on Stack.
I have a use-case where it would be highly-preferable to simply read a .m MATLAB script from a URL.
How should I do this correctly?
<disclaimer>
Clearly only do this with files you have complete control of (and/or find a solution with better validation). This is a "dangerous" method as there is no check that you're not about to run a file which, for example, copies your entire harddrive to Bob's computer before corrupting it all. Bob and Alice might be spending the whole evening laughing at your embarrassing holiday photos.
Treat this more as a proof of concept than a how-to, it addresses your problem but by no means should be used in production code.
</disclaimer>
You'll need to use eval to evaluate code. Every time I mention eval I feel compelled to point out it's not recommended, in particular in this case because you could be evaluating whatever random code is living in that file on the web! In this case your only alternative is to save the file locally and call run.
Then you can use
eval(urlread('http://myscript.m'))
or, since urlread is not recommended (from the docs), you can use webread and specify that the output should be text in the options
eval(webread('http://myscript.m', weboptions('ContentType', 'text')))
Using webread appears to be really slow, not sure why when it's the recommended function. In light of this, urlread might be preferable.
There is a note in the webread docs which suggests you wouldn't even need to specify the weboptions
If a web service returns a MATLABĀ® file with a .m extension, the function returns its content as a character vector.
Although you suggested that webread returned a uint8 variable which didn't work.
If you'd like to save a file from a URL then run it, you can use websave and str2func like so:
fcnName = 'newscript'; % Name for the function/script file
websave([fcnName '.m'], 'http://myscript.m'); % Download and save it
fcn = str2func(fcnName); % Create function handle
fcn(); % Evaluate function/script
It should of course go without saying that you want to be really sure you can trust the source of the file, otherwise you're gonna have a bad time.

Implement a Read_I_Point FB in TwinCAT 2 BeckHoff PLC

What I want to do is to create a FB to read the signal of input point when it is given a TAG NAME.
For example, if the tag name is "I_motor_sensor", so "I_motor_sensor" is send to Read_I_Point FB, then the FB will give the current status back. So is there a way to do this?
Yes, you can read the symbol using FB_ReadAdsSymByName block in the DataExchange library. Info here.. It will take more than one scan though, so should be considered "heavy".
If you want to read them quickly, create a dictonary of references to the variables of interest to you in an intialisation routine, and access them via their key (which you will use a string for).
There's a TwinCAT3 dictonary class in my freely available library, Intecre Utilities, available here. You could try to adapt that for TC2.

UniData List all avaiable subroutines / All parameters

I am trying to wrap some UniData Subroutines to SOAP Web Service. I'm planning to use C# and UODOTNET library (IBM U2 Data Management Interface for .NET). Also I'm looking to create an engine to read all the available Subroutines from data server and also reads all the required parameters and dynamically generate required codes for Web Service.
My code would be something like this:
var session = UniObjects.OpenSession(
"192.168.0.1",
"user",
"password",
"account"
);
var cmd = session.CreateUniCommand();
cmd.Command = "LIST SUBURB.INDEX"; // ?????
cmd.Execute();
var res = cmd.Response;
Question 1: Is there any command that I can use to retrieve the list of all available subroutines?
Question 2: Is there any command that I can use to retrieve list of all parameters for specific subroutine?
Cheers
The short answer is no.
The longer answer is yes, but with a lot of work.
Since you are asking this question, I'm going to assume you are missing a lot of generally knowledge about the platform. Hence to be able to do this you'll need to:
Learn about how VOC works, specifically how executable code can be catalogued here.
Learn about the CATALOG and how cataloguing programs globally, locally and direct differ.
Understand how your system in particular is designed. Some places have everything directly catalogued in the VOC, others are a mix. If the former, it'll be easier for your question.
Once you understand the above, you'll know how to get a list of all executable programs from VOC, local catalog and global catalog. For example, a simplified example for the VOC is the UniQuery command LIST VOC WITH F1="C".
The hard part is getting the parameter list, of which there isn't any system command. To do this you have 2 options.
Reverse engineer the byte code of every subroutine and tease out the number of parameters
If you have access to the related source code, parse it to generate the list.
Just wanted to add a comment on this one, in UniData there is a MAKE.MAP.FILE command that will identify Programs and Subroutines ( and the number of parameters) which puts the information in the '_MAP_' file. This does not tell you what the parameters are used for, but it helps.

Why does the Function Module name of a Smartform change (sometimes)?

Not really critical question, but i'm curious
I am working on a form and sometimes the generated function name is /1BCDWB/SF00000473, and sometimes /1BCDWB/SF00000472. This goes back and forth.
Does anyone know what's the idea behind this? Cuz i'm quite sure it's not a bug (or i might be wrong on that).
It is not a bug. You always have to use SSF_FUNCTION_MODULE_NAME to determine the actual function module name and call it dynamically using CALL FUNCTION l_function_module.
Smartform FMs are tracked by internal numbering and thats saved in the table STXFADMI. You would always notice the different number in Development System if you have deleted any existing Form. Similarly, you would also notice the different number in your Quality system based on the sequence the forms are imported in QAS and the forms as well (as test forms are not migrated to QAS.
Similar behavior is also true for Adobe Form generated FMs.
You need to understand that every smartform has a different interface and hence the automatically generated function module needs to have different import parameters.
Due to this reason the 'SSF*' FMs generate a FM specific for your smartform. The name of the 'generated' FM changes when you migrate from one system to another. And that's the reason why you should use a variable while calling the 'generated' fm and not hardcode it.
The same goes with Adobe form as someone has rightly said in this thread.

ESS workflow for R project/package development

Can anyone share his experience on workflow for R peject development under ESS? I tried several times to learn emacs but I have not get it yet. I can understand ESS as an editor, but is there a project view in ESS? what's the efficient ways to set up/view R project directory, coding, and testing, and how's ESS has an edge to facilitate the whole process?
Do you use ESS as a good R editor only or tend to emulate a R IDE environment within ESS?
Thanks for any advices.
It sounds like you're asking two separate questions.
One question concerns workflow and the other concerns using ESS.
As I use StatET and Eclipse, I'll just share my experience regarding the workflow aspect of your question.
As with Vincent I also follow something like the workflow set out by Josh Reich here (also see Hadley's useful comments):
Workflow for statistical analysis and report writing
Although it can vary between projects, I tend to have a couple of main R files
import.R: this imports data files and does any necessary cleaning and manipulation
analyse.R: This generates the output that I need for any final report
main.R: This calls import.R and analyse.R
The aim is for import.R and analyse.R to represent the complete and final workflow for producing the final results of any analyses.
In terms of a directory structure for an analysis project, I'll often also have the following folders
data: for storing any raw data files
meta: for storing meta data, such as variable labels, scoring systems for tests, recoding information, etc.
output: for storing any graphics, tables, or text generated by my analyses that I might want to incorporate into an external program
temp: When exploring the data and brainstorming analyses, I like to type code into files instead of using the console. I tend to label these temp1.R, temp2.R, temp3.R. I store these in a temp folder. That way I have a permanent record that's easily accessible. If the analyses become final they get incorporated into one of the main R files (i.e., import.R or analysis.R)
functions: If I think that a function will be needed across a couple of projects, I often place it one function per file or a set of related functions in a file in a folder called functions. This makes it relatively easy to reuse functions across projects, when the formal requirements of package development are more than needed.
library: If I want to create some general functions that I think will be project specific, I'll place them in this folder
save: A folder to store any saved R objects
StatET and Eclipse make it easy to interact with such a file system.
Of course, given all the R gurus that use ESS and Emacs, I'm sure it also handles interactions with the file system well.
I'm not exactly sure what you expect as an answer on this one. I, for one, have stolen (and adapted) a system that was suggested here a little while ago (by Josh Reich):
Create a folder for every project, and split up your work in a bunch of different .R files:
Load.R for getting your raw data into R;
Prep.R for cleaning the data, recoding variables, etc.;
Func.R for coding any custom functions you will need for evaluation; and
Eval.R for running your final stuff.
If that doesn't fit your style, just change it.
Then, you can either have a master file to call each of the parts one after each other (good for reproducibility), or save at different stages and have the individual scripts load the appropriate data (good if some of the prep work is very computationally/time intensive).
**
On a different note, the trick that is posted at the link really helped me get into ESS. It turns Shift-Enter into a one-stop-ESS-shop: http://www.kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
Others have given you some good ideas about how to setup your directory/file structure for a project.
You also asked about "project views," in which case you might want to look into the Emacs Code Browser (ECB).
You can find some screen shots of it in action on its site, here:
http://ecb.sourceforge.net/screenshots/index.html