Proto-App / Proto-Android

Realtime circuit simulator for Android platform.
100 stars 2 forks source link

how to write a script #246

Closed Jurgen2003 closed 1 year ago

Jurgen2003 commented 1 year ago

@Proto-App With javascript I am not so familiar but can java. Make calculations for example voltage input 1 -volt input 2. Is voltage output 1 or 1 +2 is output or 1×2 so there is a calculation. Of the variable on input 1 and 2 one. Variable makes output 1

can you program the operation of an opamp in a javascript ic ? Please if possible.I would like to understand how you write that script

Regards jurgen

Proto-App commented 1 year ago

@Jurgen2003 JavaScript is quite similar to Java, please check great examples on https://www.w3schools.com/js/ to learn basic concepts.

Jurgen2003 commented 1 year ago

@Proto-App I've already seen that link and read through it I'm just asking for an explanation here because I don't understand it. I can't get on with my. I look forward to the update with subcircuits and want to try until then with javascript which I do not understand and that site does not help I do not get it that's it. Problem and no matter what I type an error message in English which I don't understand either. I would like to continue with my schakelijk
Greetings Jurgen

Proto-App commented 1 year ago

@Jurgen2003 What kind of logic do you need? We can create small script and explain some parts. Regarding errors, all messages are always in English.

Jurgen2003 commented 1 year ago

@Proto-App The basis of my circuit is that over 2 inputs a voltage is measured and thus an output is made thus. Are 1 and 2 equal output 0 is 1 higher than 2 output 1 positive and output 2 negative . And input 1 lower than input 2 is the output 1 negative and output 2 positive also it is important that the output voltage is equal to the difference over the 2 inputs so the greater the difference the greater the output. and that output can be controlled with a gain factor
If that basis is there and I can paste it into script and assign the inputs and outputs with the numbers I could go further Greetings Jurgen

Proto-App commented 1 year ago

@Jurgen2003 Sorry but we don't understand your description. Could you make it with your native language?

Jurgen2003 commented 1 year ago

@Proto-App Ik wil werken met analoge/ variable voltage

Dus met javascript. Het meten/lezen van een in komend voltage. En die. Beïnvloeden door een analoge /variable voltage op een andere ingang en dat als uitgang

Screenshot_20230605_125122_P R O T O.jpg

Screenshot_20230605_125051_P R O T O.jpg

![Screenshot_20230605_125047_P R O T O.jpg](https://github.![Screenshot_20230605_125020_P R O T O.jpg](https://github.com/Proto-App/Proto-Android/assets/128260279/006d1267-f648-4351-b050-534458fb3f56)

4609-9329-06feb60c6125)

![Uploading Screenshot_20230605_12![Screenshot_20230605_125108_P R O T O.jpg](https://github.com/Proto-App/Proto-Android/assets/128260279/f0ac27c8-2c67-![Screenshot_20230605_125013_P R O T O.jpg](https://github.com/Proto-App/Proto-Android/assets/128260279/a529e8ce-74ff-48af-b312-8f76edc50f44)

ng Screenshot_20230605_125108_P R O T O.jpg …]()

Screenshot_20230605_125032_P R O T O.jpg

![Uploading Screenshot_20230605_125013_P R O T O.jpg …]()

Voorbeeld. Op ingang 1 staat 5 volt ( tussen de 0 en 10 op. Ingang 2 staat. 3 volt daarmee rekenen of. De 2 plus 5 of de 2 - 5 of keer. En naar mee een uitgang maken

I want to work with analog/variable voltage

So with javascript. Measuring/reading an incoming voltage. And that. Influenced by an analog /variable voltage on another input and that as an output

Example. On input 1 is 5 volts ( between 0 and 10 on. Input 2 states. 3 volts with that count or. The 2 plus 5 or the 2 - 5 or times. And to along make an output.

See the photos as examples. So I want to be able to regulate but the voltage on the input and outputs is variable

But then with the inputs and outputs in such a javascript ic to make the control more compact and less component but programmed so that more space remains Or is it easier to wait for the subcircuits and in them. Make. That will come anyway

Proto-App commented 1 year ago

@Jurgen2003 On your screenshots you have dependent sources. If you want to use java script then use Script IC, it looks like below:

js

Jurgen2003 commented 1 year ago

Screenshot_20230605_215832_P R O T O.jpg

@Proto-App I know that to with those sources can make the calculation input a +b a-b. A×b. I want to make such calculation with inputs make in java script and send an output there but how. Do you make a script that reads such an input on voltage and calculate with that
So following screen. How to make a javascript that controls the 5×2 or 5-2 or 5+2.

Proto-App commented 1 year ago

@Jurgen2003 Ok, now we understand your requirements :) So you have to choose script IC (2 in - 2 out), tap on "settings" button and choose "Script" option. Then paste following code in editor and click "Save" button:


function setupTerminals() {
    out0.setLowVoltage(0.0); //Can be removed, but you can set voltage in low state (not used here)
    out0.setHighVoltage(in0.getVoltage() + in1.getVoltage()); // out0 voltage = input 0 + input 1
    out1.setLowVoltage(0.0); //Can be removed, but you can set voltage in low state (not used here)
    out1.setHighVoltage(in0.getVoltage() - in1.getVoltage()); // out1 voltage = input 0 - input 1
}

function driveOutputs() {
    //turn output to high state 
    out0.setState(true);
    out1.setState(true);
}

//code loop executes both functions
setupTerminals();
driveOutputs();
Proto-App commented 1 year ago

@Jurgen2003 js2

Jurgen2003 commented 1 year ago

thanks @Proto-App for the help and the script i now understand how to make the script measure the voltage and calculate with 2 inputs i programmed the 4 in 4 out and 8 in 8 out as well javascript is handy to work with but one more question

can you also add a fixed value to the input and output which can be calculated further see pictures

for example input .0 x ( a value ) + input.1 x ( a value ) or input.0 - input.1 x (a value ) the x

Screenshot_20230606_163045_P R O T O Screenshot_20230606_163201_P R O T O Screenshot_20230606_163154_P R O T O

Jurgen2003 commented 1 year ago

Is it possible, after calculating 2 inputs and the corresponding. Increase and decrease factor (I managed to do that) to increase or decrease the output by a factor

Jurgen2003 commented 1 year ago

dear @Proto-App i am going to understand java script and program whole circuits in it and it works a only i want to amplify some outputs for example input1 is 1v - input2 500mv (0.5v ) so output is 950mv (0.95v) where should you type the x10 to get 9.5 out so a magnified factor on the output
greetings jurgen

Proto-App commented 1 year ago

Hi @Jurgen2003 You should change following line in code:

out1.setHighVoltage(in0.getVoltage() - in1.getVoltage()); // out1 voltage = input 0 - input 1

to:

out1.setHighVoltage((in0.getVoltage() - in1.getVoltage()) * 10.0); // out1 voltage = (input 0 - input 1) * 10

or you can extract variable and code will be more readable:

let outputVoltage = (in0.getVoltage() - in1.getVoltage()) * 10.0;
out1.setHighVoltage(outputVoltage);
Jurgen2003 commented 1 year ago

Thanks@Proto-App I will try to increase an output after a calculation is it also possible to limit an output so minimum voltage and maximum voltage there between a control value
So min 2 v. And max 8 v on the output. And the input. From 0 to. 10 have. Greetings Jurgen

Proto-App commented 1 year ago

Sure, you can make own "clamp" function and limit input/output in custom range, good resource to start: https://webtips.dev/webtips/javascript/how-to-clamp-numbers-in-javascript

Jurgen2003 commented 1 year ago

@Proto-App glad it can. It would be very helpful if I could I have already read and polled that site a few times. Keep getting errors. How. Which. Code is. For min and max limit.

Jurgen2003 commented 1 year ago

I manage to program entire scheme with multiple calculations. With multiple inputs providing information. And with that control the outputs
But. I would also with the info on inputs. Want to limit a minimum voltage and maximum voltage I know with the clamp function it can but every time programming. Do I get errors joe works that clamp function
Thanks in advance

Proto-App commented 1 year ago

@Jurgen2003 Please paste your code here and write where you want to limit your input/output.

Jurgen2003 commented 1 year ago

Dear @Proto-App
I want to begin to understand Suppose you have this setup how it ic. Input 0 +input 1 = output 0. So. Out0.setHighVoltage in0.getVoltage ()+in1.getVoltage ()) But how do you tell the ic that you want to limit with inputs 3 and 4
So. Input 3 minimum voltage and input 4 maximum input 1 to 4 are controlled
It is an example to see and understand the script

Or. With fixed numbers typed but the input can be changed to any value or input

An input limit would also be nice to know how it works

Screenshot_20230614_001301_P R O T O.jpg

Proto-App commented 1 year ago

@Jurgen2003 It is quite easy:

function clamp(num, min, max) {
     return Math.min(Math.max(num, min), max);
}

function setupTerminals() {
    out0.setLowVoltage(0.0); //Can be removed, but you can set voltage in low state (not used here)
    let outVoltage = clamp(in0.getVoltage() + in1.getVoltage(), 2, 12)    //clamp out voltage between 2v - 12v
    out0.setHighVoltage(outVoltage);
    out1.setLowVoltage(0.0); //Can be removed, but you can set voltage in low state (not used here)
    out1.setHighVoltage(in0.getVoltage() - in1.getVoltage()); // out1 voltage = input 0 - input 1
}

function driveOutputs() {
    //turn output to high state 
    out0.setState(true);
    out1.setState(true);
}

//code loop executes both functions
setupTerminals();
driveOutputs();
Jurgen2003 commented 1 year ago
Thanks@Proto-App  I'm going to work on it