Aliases

Can you make a temp variable, give it like 4 aliases and use anyone of them? This may be very effective if you use put and get statements because of our variable space.

I haven’t seen anything that would keep you from doing this…

*Originally posted by Steven Carmain *
**Can you make a temp variable, give it like 4 aliases and use anyone of them? This may be very effective if you use put and get statements because of our variable space. **

We’ve done exactly this the last few years.

The way I understand it, you can use as many aliases as you want because all the variables are turned into memory addresses when tokenized.

When a varable is aliased is the value the same for all aliased names?

*Originally posted by rust710 *
**When a varable is aliased is the value the same for all aliased names? **

Yes. All aliases for a given variable have the same value. When the code is tokenized, all aliases point to the same spot in memory, so they are essentially the same variable.

so then why alias a variablle to different names? if they all equal the same thing, why not just use the same one again and again, it would save code and space. Is it just for ease and readability?

-Kesich

*Originally posted by Anthony Kesich *
**so then why alias a variablle to different names? if they all equal the same thing, why not just use the same one again and again, it would save code and space. Is it just for ease and readability?

-Kesich **

That’s the main reason. The other reason is if you want to access smaller portions of a variable. For example, relay1_fwd is an alias of the relayA variable, but only points to one bit inside it.

*Originally posted by Anthony Kesich *
**Is it just for ease and readability?

-Kesich **

Bingo. When your robot breaks down next year and you urgently need it to do a demonstration to get some team funds, you’ll want to know what the heck you were thinking when you wrote the code.

I always use the default names for the SERIN so I know what’s being input, then I alias them for my code so that I know what I’m doing. It helps the train of thought because then you can read it more like a sentence.

*Originally posted by Anthony Kesich *
so then why alias a variablle to different names? if they all equal the same thing, why not just use the same one again and again, it would save code and space. Is it just for ease and readability?

You don’t lose any program space when you alias variables. When the code is compiled (or tokenized if you prefer) all the names are converted to memory addresses. The processor has no idea that you’ve created aliases. All it gets is a command to fetch some data from a memory location.

Besides code readability, a reason to use aliases the way Jnadke said is to quickly change the input mapping. If you want to switch your drive stick to port 2 instead of port 1 and you don’t have any aliases, you have to change all the occurrances of p1_x and p1_y to p2_x and p2_y respectively. If you use aliases such as drive_stick_x and drive_stick_y, all you have to do is change the alias in one place and you’re done.

You should do the same for your outputs as well. Or you can write your motor output speeds to scratchpad RAM locations (such as s_left_drive_speed) and then fetch your speed to the correct pwm variable right before the Serout.

Those are just a few tricks to make changing input & output mappings less of a headache.

ahh i see…, so it makes it much easier. (you’ll have to excuse me, im a rookie programmer, err the only programmer on a rookie team, and ive taught myself all of the code, so i have learned to question EVERYTHING)

-Kesich

P.S. I’ve had that problem many times before, changing ports that is, and i find it to be a pain in th neeck, expecially when you miss ONE of them and it destroys the entire operation.