Strange gear tooth sensor port issue

After a battle with a broken gear tooth sensor, we finally have two sensors working. Good.

We’re using MPLab and WPILib, which is nice. However, the problem is that the GetGTSensor code doesn’t seem to work as advertised.

The sensors are on ports 1 and 2 of the digital inputs. StartGTSensor(1,0) and StartGTSensor(2,0) work.

However (and this is the weird bit), the values for GT 1 must be retrieved with GetGTSensor(2) and the values for GT 2 must be retrieved with GetGTSensor(4).

Even odder, if StartGTSensor is used with an invert value of 1, GetGTSensor(1) will return -1 once the gear on port 1 starts moving, and GetGTSensor(3) will return -1 once the gear on port 2 starts moving.

What’s going on here?

Regards,
-scott

When you say get GTsensor code, is that kevin’s code, or your own?
What project are you using?
If it is your code, can you show us it?

WPILib’s “GetGTSensor” routine, from the latest release of WPILib.

The code is simply this:

void main(void) {
StartGTSensor(1, 0);
StartGTSensor(2, 0);

while (1) {
    printf("1: %d   2: %d   3: %d   4: %d

", GetGTSensor(1), GetGTSensor(2), GetGTSensor(3), GetGTSensor(4));
}
}

The gear tooth sensors are plugged into ports 1 and 2.

Output:

1: 0 2: <port 1 output> 3: 0 4: <port 2 output>

If I invert instead:

StartGTSensor(1, -1);
StartGTSensor(2, -1);

then I see this:

1: -1 2: <port 1 output> 3: -1 4: <port 2 output>

Regards,
-scott

The problem is probably with the printf() statement, not with the Gear Tooth Sensor code. You’re asking for four ints to be printed. It looks like GetGTSensor() returns a long. The first %d will show the first 16 bits of GetGTSensor(1), the second %d will show the rest of GetGTSensor(1), the third will show the first 16 bits of GetGTSensor(2), etc.

If this is the case, you can correct the problem either by printing the values using %ld, or by casting them as (int).

Oh, jeez. For some reason I thought that function returned int.

Yes, that probably explains it. Thanks.

Regards,
-scott, who apparently has been programming in Java for far too long.