Attach variables - embedded

I want to take two variables (in and in2) and put them together, for example:
in = 1;
in2 = 3;
pin = in.in2; // I want this to set pin to 13
The arduino IDE tells me that in is not a class, so what syntax would I use to accomplish this?
EDIT: I figured out a different way to do it, you can just take in. multiply it by 10 and then set pin to the sum of in plus in2

If your two variables are definitely integers then pin = (in*10)+in2; would work.
If not, read them into strings (maybe with in.toString(), language dependent) and just do pin = int.parse(in.toString()+in2.toString());
(Although, again dependent on language, you may have to do something other than int.parse [in C# you should use int.TryParse() for example])

Try this, I wrote it in C but you get the gist. turn the two items into strings then concatenate and parse it as an integer.
pin = int.Parse((string)in + (string)in2);

Related

X and Y inputs in LabVIEW

I am new to LabVIEW and I am trying to read a code written in LabVIEW. The block diagram is this:
This is the program to input x and y functions into the voltage input. It is meant to give an input voltage in different forms (sine, heartshape , etc.) into the fast-steering mirror or galvano mirror x and y axises.
x and y function controls are for inputting a formula for a function, and then we use "evaluation single value" function to input into a daq assistant.
I understand that { 2*(|-Mpi|)/N }*i + -Mpi*pi goes into the x value. However, I dont understand why we use this kind of formula. Why we need to assign a negative value and then do the absolute value of -M*pi. Also, I don`t understand why we need to divide to N and then multiply by i. And finally, why need to add -Mpi again? If you provide any hints about this I would really appreciate it.
This is just a complicated way to write the code/formula. Given what the code looks like (unnecessary wire bends, duplicate loop-input-tunnels, hidden wires, unnecessary coercion dots, failure to use appropriate built-in 'negate' function) not much care has been given in writing it. So while it probably yields the correct results you should not expect it to do so in the most readable way.
To answer you specific questions:
Why we need to assign a negative value and then do the absolute value
We don't. We can just move the negation immediately before the last addition or change that to a subtraction:
{ 2*(|Mpi|)/N }*i - Mpi*pi
And as #yair pointed out: We are not assigning a value here, we are basically flipping the sign of whatever value the user entered.
Why we need to divide to N and then multiply by i
This gives you a fraction between 0 and 1, no matter how many steps you do in your for-loop. Think of N as a sampling rate. I.e. your mirrors will always do the same movement, but a larger N just produces more steps in between.
Why need to add -Mpi again
I would strongly assume this is some kind of quick-and-dirty workaround for a bug that has not been fixed properly. Looking at the code it seems this +Mpi*pi has been added later on in the development process. And while I don't know what the expected values are I would believe that multiplying only one of the summands by Pi is probably wrong.

Switching pin to output in NXP LPC24xx

Suppose our program has just started after Reset, so all FIODIRs are 0 and corresponding pins are inputs, and therefore they are in the third state (right?).
And suppose I want instaneously switch some pin to output with 1. Then probably I'll write this:
FIOxDIR_bit.Px_xx = 1;
FI0xSET_bit.Px_xx = 1;
But what will be state of the pin between these two commands? Presumably output with 0.
Another option to perform such switching is:
FI0xSET_bit.Px_xx = 1;
FIOxDIR_bit.Px_xx = 1;
But the datascheet claims:
If any pin is configured as an input or a secondary function, writing 1 to the corresponding bit in the IOSET has no effect.
Probably the same is true about FIOSET as well.
And that's exactly what we do in our latter code snippet. Though in my expierence this approach works well, but I'd like to have some theoretical basis for this.

re-use the same set name multiple times in Gams

I defined a set in GAMS to represent users number. I need to use the set multiple times to define transmission power for each user, the channel quality...etc. However, I think in GAMS you can not use the name of the set for different variables, My question is do I need to define a different set for each variable?
Code example:
set I number of users /i1,i2/ ;
Parameters
CP(I) circuit power per user /
i1 10
i2 10 /
h(I) channel quality /
i1 48.9318
i2 106.2280/ ;
Thank you in advance for any help or for any hints.
No, you don't need to define different sets if you always want to refer to the same elements (users in your case). It is actually the idea of sets to do exactly this. So, your example code is just right.
You can also look at a simple example like this one here: http://www.gams.com/modlib/libhtml/trnsport.htm
There you will see, that the sets i and j are used all over for different parameters, variables and equations.
I hope that helps,
Lutz

Multiple random number generators in Objective C

In my current project I need multiple random number generators because I need to be able to repeat their sequences independently from each other.
So far I did not find any way to achieve this with the standard objective-c random number generators, because they only have one global state.
I think having an random number generator class would solve my problem. I could create several instances which I could reset individually.
Is something like this already available? I was not able to find any random number generator implementation in objective c. I would like to avoid implementing it myself because I have no experience with random numbers and I think it is something that's hard to get right.
I have a random class, based on the Mersenne Twister algorithm, which you can get from my dropbox here.
It's rather old, and isn't compiled for ARC, but that doesn't make it any less good :)
Example code:
MTRandom *randWithSeed = [[MTRandom alloc] initWithSeed:12345];
double d = [rand nextDouble];
int i = [rand nextInt];
MTRandom *timeBasedRand = [MTRandom new]; // seeds with current time
double d2 = [timeBasedRand nextDouble];
int i2 = [timeBasedRand nextInt];
EDIT: If you want to be really cool, you can use this:
Source
Have you tried
srandom(seed);
and then calling
random();
? If the seeds are the same then you should get the same sequence of random numbers.

Specifying variable range in Verilog using for loop

I am trying to write this code:
for (i = 0; i <= CONST - 1'b1; i = i + 1'b1)
begin : loop_inst
if (i < 3)
begin
if (changed[i] & !done_q[i])
begin
writedata[3-i] = en[i];
writedata[2-i:0] = readdata[2-i:0];
writedata[15:4-i] = readdata[15:4-i];
end
end
else
...
Basically, the location of the bit I am trying to write to (en) changes depending on which address I am talking to, depending on i. This code is not synthesizable because i is not a constant.
Is there any other workaround to this? The only workaround I know is writing out those three statements CONST times. I am hoping I DON'T have to do that in the end. Is there any other solution?
It looks like you're trying to copy readdata to writedata all the time, but fill in the LSBs with en if certain special case conditions are met. I'm also going to assume that the for loop you have is in an always block, and that you're intending to build combo logic.
The for loop as you've it written doesn't make much sense to me from a hardware perspective. A for loop is used for building arrays of logic, and as you've
written it you'll have at least 3 logic cones trying to set values on the entire writedata bus. (If it generates anything at all, it'll be some weird priority structure).
That said, it's probably the range selects that your compiler is complaining about, ie the writedata[2-i:0] rather than the writedata[3-i] = en[i]; (anything with : in the part select). If you want to do something along those lines, you can use 'indexed part selects' ( +: or -:) but there are better solutions in this case.
I'd rewrite it as follows - assuming I've assumed correctly :)
always #( /*whatever*/ ) begin
// default assignment
writedata = readdata;
// overwrite some bits in writedata for special cases
for(i=0; i<3; i++) begin
if( changed[i] & !done_q[i] )
writedata[3-i] = en[i];
end
end
In this code, I'm setting writedata to readdata, and then tweaking the resulting value of writedata if the special cases are in play. The for loop is building 3 logic cones, one for each of the bits in writedata[3:1]. I'd double-check if the bit mapping is what you intend -ie, mapping en[2:0] on to writedata[1:3].