I’ve been trying to work this out for a few days now. How do we get digital/analog inputs from the driver station in C++? We’re going to use a rotary encoder as a knob to have a fine adjustment with our turret. Any ideas? A quick response would be greatly appreciated
The DriverStation class has GetAnalogIn() and GetDigitalIn() methods for that.
Complete reference available here
thanks for your quick repsonse, however we’re a rookie team so when i took a look at that along with the other programmer, we both got headaches could you explain just a little?
Sure. In your code, you’ll need to have something like this:
class MyRobot : public SimpleRobot{
// Member variables (DigitalInputs, Joysticks, Jaguars, etc)
DriverStation *ds;
// Default constructor
MyRobot(){
// Initializations for all your other variables
ds = DriverStation::GetInstance();
}
void OperatorControl(){
float a_variable;
while ( IsOperatorControl() ){
OtherFunctionCalls();
a_variable = ds->GetAnalogIn(1); // returns value of whatever's connected to analog input 1 of the DS
if(ds->GetDigitalIn(2)) { // checks state of DS digital input 2
DoSomething();
}
else{
DoSomethingElse();
}
}
}
};
Does that clear things up a bit?
It’ll be slightly different if you’re using the IterativeRobot template instead of SimpleRobot, but the Driver Station function calls are the same.
Rather than using an encoder, it may be easier to just hook up a POT to in analog IO pins and use driverStation->GetAnalogIn(#) and turn this into a rotational value. The reason this may be easier is that to get a rotational value or really anything useful from the encoder in the driver station would take a bit of work. There is no Encoder class that can interface with the driver station as is.
oh wow, ha. thank you so much! yea that clears things up a lot now here’s one more question… how would we get rotary encoders working? we bought ours from digikey m/n EM14 - 14 ; since we were missing a key component to the ones supplied to us in the KoP. we’re not too sure on the coding for the encoders, everything we have tried has ended with Red Error Messages of Demise (REMoD) and any console outputs are 0. this is what we have for our code:
#include "Encoder.h"
class RobotDemo : public SimpleRobot
{
Encoder rotary;
public:
RobotDemo(void):
rotary(1, 2)
void Autonomous(void)
{
gyro.Reset();
int display;
while (IsAutonomous())
{
float anything;
while(argument)
{
GetWatchdog().SetEnabled(false);
rotary = new Encoder(1,2);
Wait(0.004);
display = rotary;
printf (" %d
", display);
}
}
}
void OperatorControl(void)
{
}
};
any ideas?
that would work great, however we have to rotate all the way around for our turret
Ok, couple of things to fix here:
1)You need to add a call to the Encoder class’s Start() function.
2)You’re creating a new Encoder in the autonomous. You don’t need that, you already created it at the constructor.
Here’s how things should look:
#include "Encoder.h"
class RobotDemo : public SimpleRobot
{
Encoder rotary;
public:
RobotDemo(void):
rotary(1, 2)
{
}
void Autonomous(void)
{
gyro.Reset();
rotary.Start();
int display;
while (IsAutonomous())
{
float anything;
while(argument)
{
GetWatchdog().SetEnabled(false);
Wait(0.004);
display = rotary.Get();
printf (" %d
", display);
}
}
}
void OperatorControl(void)
{
}
};
EDIT: Also, keep in mind that this only works for digital encoders connected to one of the cRIO’s digital sidecars. If what you want is a knob on the driver station, then as Sentient mentioned previously you’ll need to get a potentiometer and use the DriverStation GetAnalogIn() function.
ok, you’re in California? it’s 4:11AM here, we’ve been at our workshop for over 50 hours straight and you sir, just saved us. we give you a round of applause, and we seriously did. our programming team of two people literall stopped what we were doing and clapped. thank you so much for everything, if there is ever anything you need, we owe you big time. thanks again. seriously, thank you so much