A quick programming question

Hey everybody. So recently I was working on the famous, almighty “tracking system” that we are all talking about. I got most of the math worked out, so now working on the gyroscope code part to increase the accuracy of the gyro as both an angular measurement and a compass sensor. So of course I was looking at QCC2’s powerful gyro codes that got them the world champion: https://github.com/JMMcKinneyWPI/GyroPIDLibrary

And I got a little question.

So they have this struct accounting for all gyro variables, and there are user functions directly communicating with that global struct, like this:

void gyroSetPort(int sensorPort)
	gyro.sensorPort = sensorPort;

The gyro.sensorPort is defined in the struct as an integer.

But later in the sample code we see this thing in pre-auton:


So that got me confused: how can you give something like “in1”, which should be a string, to an integer gyro.sensorPort? So you can directly give a string to an integer? And how would the string be interpreted? In this case, if you give “in1” to the integer variable, would the reading be “1”?


So the nuances programming aren’t really my specialty, but it doesn’t look like in1 is being passed as a string because there are no quotes around it, right?
Instead, it looks like they are calling a variable or macro that was probably in the #pragma/#define of the original code this example was cut from. As it sits right now, I don’t think that line would compile correctly because the variable or macro in1 hasn’t been created/defined yet.

This is similar to using port1, port2, port3, etc. for motor control. In the #pragma configs, you can see that when you define an analog sensor in the sensors setup, it shows up as:

#pragma config(Sensor, in1,    sensor_gyro,    sensorGyro)

Where in1 is the port, sensor_gyro would be the name that you define, and sensorGyro is the type of sensor. This is why you can use your custom defined names to access either the SensorValue] array or the motor] array.

Also, if you want to access digital ports in this way, you would use dgtl1, dgtl2, etc.

And, as capow08 pointed out, this isn’t passing a string because there aren’t double quotes around it. Instead, you are passing in the variable in1.

in1 is a variable that RobotC recognizes as the analog read port 1 on the cortex. If you were to call the SensorValue() function you could pass in1 as a parameter and it would return the value of a read on analog port 1. I made that parameter in the struct so that you can set the gyro to any of the analog ports in robotC by passing it as the parameter (ie. in1, in2 etc.)

I hope this answers your question

So ROBOTC recognizes in1 as some sort of special integer that designates the sensor port. Like how it automatically recognizes PI. Interesting. Thanks a lot!

BTW the gyro library absolutely rocks. I recommend it to all programmers interested in gyro programming.

You’re welcome. I believe that there might be an enumerator or just a constant in RobotC somewhere that just sets that as a certain integer which is the index in the SensorValue array that corresponds to that port.

I’m glad the library is getting some use and that it’s helpful. I thought it might be really nice to have this season.

ROBOTC reads the configuration and generates a list of defines most likely.

It probably behind the scenes spits out:

#define in1 1

in your code. Tabor should be able to confirm this (since he kind of has access to the source code atm).

Just found this. It has everything you appear to be looking for if you needed to know specific values. http://www.robotc.net/wikiarchive/Data_Types#dataType_tSensors

Thank you. This is exactly what I was looking for. I took a screenshot and pasted it here so everyone can see:


So it turns out that every sensor port is designated an integer number. So you can actually call SensorValue[1] and it would mean the same thing as SensorValue[in2]. Quick experiment: I plugged this thing into ROBOTC and hit compile, no error.

Interesting stuff. Thanks everybody.
robotc vex sensor port designation.jpg
test stuff.JPG