robertobucher / pysimCoder

Block diagram editor and real time code generator for Python
GNU General Public License v3.0
142 stars 31 forks source link

How to make the block like ±|u|^v #61

Closed protfor closed 4 months ago

protfor commented 1 year ago

I'm trying to make the block like the block like ±|u|^v.

Under folders "/pysimCoder-master/resources/blocks/blocks/", with reference to ".xblk blocks", under folders "/pysimCoder-master/BlockEditor/", using defBlocks.py or xblk2Blk.py, I'm trying to make the block like the block like ±|u|^v but I haven't been able to do it.

So I would appreciate it if you could provide samples targeting ±|u|^v.

Best regard.

robertobucher commented 1 year ago

Hi

sorry for late replay, but I'm involved in an important presentation for tomorrow.

I'll send you the files in a couple of days.

Roberto

protfor commented 1 year ago

Dear Roberto-san,

Thank you very much. Nice to meet you, my name is protfor.

Thank you for replying politely, even though I made an inquiry online on GitHub, and in addition, while you have a job right now.

I think there may be some points that I have not studied, but I will be saved little by little, so please teach me.

robertobucher commented 1 year ago

I've added your block to the repository. I did a little description about creating new blocks. The example generates this specific block and gives (I hope) all the required steps to generate new blocks in the future.

NewBlock.pdf

protfor commented 1 year ago

Dear Roberto-san,

Thanks very much to your sincere description,

Sincerely^V

K|u|^v.pdf

protfor commented 1 year ago

Dear Roberto-san,

Long time no see. I want to create a block only with the Python program without c program.

I have summarized the situation that I am specifically thinking about in the attached file. 20230518.docx

I would appreciate it if you could reply when you have time.

robertobucher commented 1 year ago

I can't open your docx file. Can you send a PDF?

protfor commented 1 year ago

Dear Roberto-san,

Good morning. Thank you for your prompt reply.

I will send "20230518.pdf". In addition, the relevant files are: "UPOW3.xblk" "upowBlk3.py" "upowBlk3.c" "untitled_test.dgm"

20230518.pdf upowBlk3.zip

robertobucher commented 1 year ago

I hope that I have correctly understand your problem.

You can't implement code in python in the .py file (in this case upowBlk3.py file). This file is only used to generate a specific class element which contains all the information required to generate the code. It doesn't perform any action to the block.

protfor commented 1 year ago

Dear Roberto-san,

My question was poorly (I asked based on my trial process). Nonetheless, thanks for your reply.

Rearranged the question to 20230522.pdf. I would appreciate if you could check it when you have time. 20230522.pdf mathsqrt.zip

Best regard.

robertobucher commented 1 year ago

It is possible but it is a bad idea! pysimCoder generates C-Code in order to have a very short execution time and give the possibility to execute tasks in realtime. If you want to program some blocks as python code, you have to generate a wrapper from C to python. It is possible, not so difficult to be implemented, but it will incease the execution time of the RT task... You can program your C block with something like this example:

include

int main() { Py_Initialize(); PyRun_SimpleString("from time import time,ctime\n"); PyRun_SimpleString("print('Today is', ctime(time()))\n");

if (Py_FinalizeEx() < 0) { exit(1); } return 0; }

or something like

pyMath.py

import numpy as np

def determinant(a): print(a) return(np.linalg.det(a))

def inverse(a): b = np.linalg.inv(a) print(b) return b

and this C-Code

define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

include

include <numpy/arrayobject.h>

int main() { PyObject pName; PyObject pModule; PyObject pFun1, pFun2; PyObject pVal; PyObject pArgs; PyObject *pArray;

double matin[3][3]; double *matout; int i,j;

npy_intp dims[2]; double det; void * ret;

Py_Initialize();

pName = PyUnicode_FromString("myMath"); pModule = PyImport_Import(pName); Py_DECREF(pName);

if(!pModule) { Py_DECREF(pModule); exit(1); }

pFun1 = PyObject_GetAttrString(pModule, "determinant"); if(!pFun1) { Py_DECREF(pFun1); exit(1); }

pFun2 = PyObject_GetAttrString(pModule, "inverse"); if(!pFun2) { Py_DECREF(pFun2); exit(1); }

/ Matrix dimensions / dims[0] = 3; dims[1] = 3;

/ Fill the matrix / for(i=0;i<3;i++){ for(j=0;j<3;j++){ matin[i][j] = 3i+j; } } / Change one value to have determinant <> 0 */ matin[2][2] = 0.0;

import_array();

/ Create pArray using PyArray_SimpleNewFromData / .....

/ Set the array as function args / pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs,0, (PyObject *) pArray);

pVal = PyObject_CallObject(pFun1, pArgs);

/ Get the return value using PyFloat_AsDouble / ..................

pVal = PyObject_CallObject(pFun2, pArgs);

/ Get the array Data using matout = PyArray_Data(.....) / .........

/* Print the inverse matrix */

for(i=0;i<3;i++){ printf("| "); for(j=0;j<3;j++){ printf("%10.4lf",matout[3*i+j]);
} printf(" |\n"); }

Py_Finalize(); }

As you can see it is more simple to implement all the functions directly in C-Code!

protfor commented 1 year ago

Dear Roberto-san,

Good morning. Thank you for your sincere response to my poor idea. In addition, I would like to thank the professor for giving me a concrete idea.

I will try to implement the professor's teachings in my environment. (My current environment is Ubuntu 22.04lts !) (If it does not work well in my implementing, please let me ask you a question again.)

Best regard.