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: GitHub - 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:
The gyro.sensorPort is defined in the struct as an integer.
But later in the sample code we see this thing in pre-auton:
gyroSetPort(in1);
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:
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.)
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.
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. https://vexforum.com/attachment.php?attachmentid=9394&stc=1&d=1433432660