Hey there, I am a fairly new programmer and so I just need some help with a few main problems. First of all, how do we put a deadband into the joystick so that we have no ghosting? In pseudocode that would be:
If joystick value < .1 and >-.1
joystick value = 0.
where .1 would be our “dead” area limit.
also how would we set up a motor to run to either the value of an axis on a joystick or a button? more pseudocode:
if button 2 is true then run motor 1 at 1/4 speed
or something like :
set motor 1 value to axis 3 value
i know how i would have to phrase it just not the actual syntax or location.
Your questions imply that you don’t know C++. If that’s the case, why are you using it to program your robot? You’ll need some assistance close at hand in order to do an effective job.
What is your programming experience? Depending on what you already know, there might be different ways to tell or show you what you need to do.
I have programmed one year in LabView and one year in C++. The old programmer who taught me programming wasnt a very good teacher and so i came away with hardly any knowledge and because of that towards the end of last build season we asked for help from another local team who only knew c++, so we switched. Its turned out to work very well but I need still need help with programming.
I tried the code that you gave but its not working, what specific location does it need to be in inside my robot code? Also is there a way to add the deadband directly to the reading from the axis via the GetRawAxis or GetX/GetY commands? Or do macros work the best?
You can put the macros in a separate file (e.g. macros.h) and then include it in your C++ file. For example:
In MyRobot.cpp:
#include "macros.h"
class MyRobot: public SimpleRobot
{
}
Or you can put the macros directly in your MyRobot.cpp file replacing the #include line.
And what do you mean when you said it’s not working? Did it give you compile error or you have no error compiling and the code is downloading and running but it doesn’t give you the deadband effect?
You don’t have to use macros. You can expand them directly inline to your GetX(), GetY() calls but it would be a lot of typing and high risk of typos.
float x = DEADBAND_INPUT(joystick.GetX());
float x = DEADBAND_INPUT(-joystick.GetY());
go?
when I wrote them in my code right below the rest of the code (which is directly under the #include “WPILib.h”) i could not build the code without getting the error “‘leftStick’ was not declared in this scope”. leftStick is what our 1st joystick is named, we have 2 for tank drive. I also switched out joystick in the above code for leftStick.
I assume if you are using the SimpleRobot template, you will have something similar to the code below. Since I don’t have Wind River in front of me, I am writing the code from my memory, so some names may be wrong and the code may not be exactly the same as the template but it gives you an idea where to put things.
#include "WPILib.h"
#include "macros.h" //Or put the macros here directly.
class MyRobot: public SimpleRobot
{
Joystick leftStick;
Joystick rightStick;
RobotDrive drive;
MyRobot():
leftStick(1), //Please use correct Joystick port
rightStick(2), //Please use correct Joystick port
drive(1, 2) //Please use correct PWM channels
{
}
...
...
void OperatorControl()
{
while (IsEnable() && IsOperatorControl())
{
float leftPower = DEADBAND_INPUT(-leftStick.GetY());
float rightPower = DEADBAND_INPUT(-rightStick.GetY());
drive.TankDrive(leftPower, rightPower);
...
...
Wait(0.05);
}
}
};
I assume that you already have the Joystick created, I am just gonna call it joystickcontroller.
And I am going to throw it all inside a method for functionality.
#include “math.h”
// Make sure you have that ^^^^
float DriverController::GetSpeedAxis()
{
float val = -joystickcontroller->GetY();
if( fabs(val) <= SPEED_AXIS_DEADBAND)
val = 0;
return val;
}//GetSpeedAxis
SPEED_AXIS_DEADBAND is a constant… can be replaced by any number
The fabs call is the call for absolute value. This only works if the deadband that you want to set is the same for both the positive and negative values on the joystick. Since the deadband is the same in both the positive and negative direction, then by using the fabs call we can eliminate some needless code.
The motor code that you would want:
I am going to use Victor for this example, can be easily switched to work with a Jaguar just by changing the words Victor to Jaguar. I am going to call the joystick method that I gave to you earlier on this comment to get the value from the joystick. Lets just pretend they are in the same class for now.
Victor *vic = new Victor(VICTOR_PORT);
vic->Set(GetSpeedAxis());
Now what that did is take the value from the joystick and assign that -1 to 1 value to the Victor which then controls the motor itself.
To assign it a value as to whether a button is pressed:
Victor *vic = new Victor(VICTOR_PORT);
if(joystickcontroller->GetRawButton(1))
vic->Set(1);
else
vic->Set(0);
the value 1 when I called GetRawButton is the button that you wish to get, the button configuration are different for each different type of controller.