|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
C++ Tank Drive Help
Please help with the errors and how to use a xbox controller with it!
I am getting a few errors with my code! - Error: expected identifier before numeric constant - Error: ISO C++ forbids declarations of parameter with no type - Error: expected ',' or '...' before numeric constant -Syntax Error Code: #include "WPILib.h" class RobotDemo : public SimpleRobot { RobotDrive drivetrain(1, 2); Joystick leftStick(1); Joystick rightStick(2); public: RobotDemo() { GetWatchdog().SetEnabled(false); } void Autonomous() { } void OperatorControl() { while (1) // loop forever { drive.Tank(leftStick, rightStick); // drive with the joysticks Wait(0.005); } } }; START_ROBOT_CLASS(RobotDemo); |
|
#2
|
||||
|
||||
|
Re: C++ Tank Drive Help
In C++, you can't initialize non-const member variables at declaration. They need to initialized in the constructor. An easy way to do this is to use C++ initialization lists (more info on which can be found here).
Instead of writing your code like this: Code:
RobotDrive drivetrain(1, 2);
Joystick leftStick(1);
Joystick rightStick(2);
public:
RobotDemo()
{
GetWatchdog().SetEnabled(false);
}
Code:
RobotDrive drivetrain;
Joystick leftStick;
Joystick rightStick;
public:
RobotDemo() : drivetrain(1, 2), leftStick(1), rightStick(2)
{
GetWatchdog().SetEnabled(false);
}
Also, if you want to access the second joystick on the XBox controller, you need to use the GetRawAxis() function instead of declaring a second joystick. This also means that you have to pass the values of the two axes to the TankDrive function instead of the joystick object. The button and joystick mappings for the XBox controller can be found in this post, but I'll copy the relevant portion below: Quote:
Code:
drivetrain.TankDrive(joystick.GetY(), joystick.GetRawAxis(5); Code:
#include "WPILib.h"
class DemoRobot : public SimpleRobot
{
RobotDrive drivetrain;
Joystick xbox;
public:
DemoRobot() :
drivetrain(1, 2), xbox(1)
{
GetWatchdog().SetEnabled(false);
}
void Autonomous() {}
void OperatorControl()
{
while (IsOperatorControl()) {
drivetrain.TankDrive(xbox.GetY(), xbox.GetRawAxis(5));
Wait(0.005);
}
}
};
START_ROBOT_CLASS(DemoRobot);
![]() - Domenic |
|
#3
|
|||||
|
|||||
|
Re: C++ Tank Drive Help
Typically a compiler error message is accompanied by the line on which the error was detected. In C/C++, that is sometimes the line after the one with the actual problem. What line is identified as causing each of the errors you see?
You're defining your RobotDrive object as "drivetrain" but you're trying to call the Tank method of something called "drive". I don't know that it's the cause of your compile errors, but it looks suspicious to me. |
|
#4
|
||||
|
||||
|
Re: C++ Tank Drive Help
It is also unlikely that you mean what is stated regarding leftstick and rightstick. What is in the code says that joystick controller #1 (the first xbox controller) will be the left-stick while the right part of the tank drive will be controlled by joystick #2 (the 2nd xbox controller). What you more likely want to do is use particular parts of a single joystick - a particular axis of joystick #1 for the left and a particular part/axis of joystick #1 for the right.
One of my students (holly4win) posted in a separate thread today about a "joystick investigator" program our team wrote which you might find useful to getting this information - see the thread at http://www.chiefdelphi.com/forums/sh...d.php?t=103156 Thanks, bob |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|