pysimCoder
pysimCoder copied to clipboard
How to make the block like ±|u|^v
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.
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
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.
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.
Dear Roberto-san,
Thanks very much to your sincere description,
-
I, who is a fan of pysimCoder, was able to accomplish it like the attached file.
-
I would like introduce myself, 20 years ago, I majored in control at university and used MATLAB (I never had the chance to use Simulink..).
On the other hand, recently, when I asked children and students what they were doing, I learned that they were having fun with Simulink and GitHub.
-
From the above, for me, Simulink, which is an extension of MATLAB, does not lead to my growth, so I would like to relearn this opportunity with pysimCoder.
Sincerely^V
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.
I can't open your docx file. Can you send a PDF?
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"
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.
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.
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 <Python.h>
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 <Python.h> #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!
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.