Hi, my programming team is trying to use three gyroscopes on our robot this year and we know that there are only two FPGA ports that can actively integrate the inputs and give us an angle. We are currently trying to figure out how to integrate the rate that we can obtain with using an analog channel in order to give us the displacement of the gyroscope but are struggling. Has anyone had any success with this? If so how did you manage it?
Like you mentioned, just use an analog input on the analog cRIO module. Be sure that the gyro is wired correctly too.
Then in Java, simply create a new gyro object.
Gyro newGyro = new Gyro(1); // '1' is what channel the input is attached to on the module. Change it if necessary.
// The below code is to get the current angle of the gyro.
double gyroAngle;
gyroAngle = newGyro.getAngle();
Does that help?
We have the first two of our gyros working with that code, however the third one cannot perform that code because there are only two FPGA ports that are active on the crio. We need to write an integral to take the rate that plugging the gyro into the analog port and using the analogchannel in the code.
You could try using simple trapezoidal integration:
read the new_rate
new_position = ½(new_rate + previous_rate)*cycle_time
previous_rate = new_rate
“cycle_time” would be 0.02 seconds if you’re doing this in TeleOp
Is that what you meant?
*
*
Yes that’s exactly what we needed! thanks a bunch! do you have any recommendation as to how we would implement this, mainly refreshing the gyro value after we use it the first time?
I don’t understand the question. What do you mean by “refreshing the gyro value after you use it the first time”?
The code I showed you should be in TeleOp so that it runs every time TeleOp runs (which is about once every .02 seconds).
That is to say, every time TeleOp runs you should read the new rate from the gyro and calculate the new position.
*
*
How would the code work so that the previous rate and the new rate are not the same values? How would you introduce that delay into the program?
That depends what template is being used. In the Simple Template you add your own loop and it will not necessarily run every 20 ms, the TeleopPeriodic method of the Iterative template will run ~ every 20ms provided your code in that method returns in less than 20ms.
It also depends on what you are using the gyro for and if it has to keep track of it’s orientation starting at the very beginning of the match. To do that you could make an object called something like CustomGyro that extends AnalogChannel and add the appropriate variables and code Ether listed. Instantiate a copy of your object in RobotInit, then call your CustomGyro.Integrate method from AutonomousPeriodic, TeleopPeriodic and DisabledPeriodic (if the robot might be turning at the beginning of auto and continue rotating a bit in disabled, otherwise leave this one out to avoid drift)
If you really need precision you probably need to calculate the delta time each time you go to integrate instead of trusting it to be ~20ms.
I’m not intimately familiar with the FRC Java Framework so I can’t comment in detail about RufflesRidge’s comments above, but I will add the following remarks:
Whatever rate you are doing the integration at, that’s the cycle_time you should use in the integration. And, as mentioned above, if that rate is not expected to be very steady, it might help to measure the actual elapsed time each cycle and use that elapsed time in your calculation.
If you are concerned about gyro drift, you should add a button that the driver can press during the match to zero the gyro when it’s at a known position.
*
*