Go to Post And I will most definitely never forget watching this bald, brown thin wisp of a human being charging up to us across the sidelines after the semifinals ended waving his hands and shouting "New Strategy!!!!!! New Strategy!!!!!!" as he rallied the troops for our Finals push. - Travis Hoffman [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-12-2008, 22:05
Straberrie's Avatar
Straberrie Straberrie is offline
The Plague
AKA: Rebecca
FRC #0375 (Robotic Plague)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2007
Location: New York
Posts: 59
Straberrie will become famous soon enough
Pointers and Tank Drive?

Hi! I am trying to program a Tank drive and am using the programming guide for C++ as well as the Simple Template found in File - New - Example etc... in the Workbench.

In the programming guide, the Operator control code looks like this

void OperatorContol (void)

{
while (1)
{
drivetrain.Tank (&stick1, &stick2);
}
}

and well, the header included in the program is "WPILib.h"; I have the same header included and I have the variables defined exactly the way they are defined in the guide, but when I try to build the project, it states the following
:C:/WindRiver/workspace/SimpleTemplate/MyRobot.cpp:31: error: `stick1' undeclared (first use this function):

How do pointers work?? In the guide, the joystick and pointer explanation is omitted *at least in the one I have, sorry if i skipped something*. So, is there anything I can read about pointers?? Or programming tank drive? Because I can't figure out whats wrong! *and yes, I am sorta kinda new to this, so if I made a silly error or am asking something stupid, sorry*

Thank you!!!!!!!!!
__________________
Team 375: The Robotic Plague
Current Robot: I-Chihuahua
Last Event: Big Apple Breakaway
  #2   Spotlight this post!  
Unread 18-12-2008, 13:21
Racer26 Racer26 is offline
Registered User
no team
Team Role: Alumni
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 2,229
Racer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond repute
Re: Pointers and Tank Drive?

well, in my experience with the C++ for the cRIO with WPILib.h,

in the definition for your robot class, you need to have something like this:

Joystick* leftStick;
Joystick* rightStick; //the *'s mean they're a pointer.
RobotDrive* myRobot;

Then in the constructor, you need to have something like:

leftStick = new Joystick(1); //joystick in DS port 1
rightStick = new Joystick(2); //DS port 2
myRobot = new RobotDrive(1,2,3,4); //4 motor drive, with the motors connected to PWM1,2,3,4, on sidecar 1.

and then in the operator control bit, you need something like:

myRobot->TankDrive(leftStick, rightStick);
  #3   Spotlight this post!  
Unread 18-12-2008, 14:50
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Pointers and Tank Drive?

Quote:
Originally Posted by Straberrie View Post
C:/WindRiver/workspace/SimpleTemplate/MyRobot.cpp:31: error: `stick1' undeclared (first use this function)
This means that within MyRobot.cpp, the compiler doesn't know what stick is.

If you copy/pasted the example code that is on page 14, it will not work and you will get this exact error. The reason is that stick1 is never declared (hence the error). Look at the 3rd line of code in the sample code
Code:
Joystick stick(1);
It looks like there was a mistake and they forgot to add the declaration for the correct sticks (maybe it started as arcade drive?). Based on the usage, try replacing that line with declarations for stick1 (and stick2 which should also be throwing an error) and this error should go away.
Code:
Joystick stick1(1);
Joystick stick2(2);
Your issue isn't with pointers, but rather with undeclared variables. If you wanted to use pointers, you can use 1075guy's suggestion as a reference point.
  #4   Spotlight this post!  
Unread 18-12-2008, 18:12
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 590
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Pointers and Tank Drive?

In that example (it might be a little confusing), it takes the sample program built into Workbench and looks at it a few lines at a time. So in each of those little snippets of code, you aren't seeing the whole program. If you look up a few code samples, you can see the declarations.
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
  #5   Spotlight this post!  
Unread 18-12-2008, 18:21
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Pointers and Tank Drive?

Brad - I understand the declarations are above (page 12), but the code on page 14 seems to be the combination of the snippets above. It would make sense that that would be the case. However, as I stated, the code on page 14 wouldn't compile because of the undeclared stick1 and stick2. If the original user copy/pasted the example verbatim, I believe that my explanation would explain the issue.

In case there are different doc versions, I'm referring to doc version .2 from Oct 15.
  #6   Spotlight this post!  
Unread 18-12-2008, 18:42
Straberrie's Avatar
Straberrie Straberrie is offline
The Plague
AKA: Rebecca
FRC #0375 (Robotic Plague)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2007
Location: New York
Posts: 59
Straberrie will become famous soon enough
Re: Pointers and Tank Drive?

#include "WPILib.h"



class RobotDemo : public SimpleRobot
{
RobotDrive *Bot; // robot drive system
Joystick *stickRight;
Joystick *stickLeft;
DriverStation *ds; // driver station

public:
RobotDemo(void)
{
ds = DriverStation::GetInstance();
Bot = new RobotDrive(1, 2); // create robot drive base
stickRight = new Joystick(1);
stickLeft = new Joystick(2);
GetWatchdog().SetExpiration(100);
}



and the Operator:

void OperatorControl(void)
{
while (1)
{
Bot.TankDrive(&stickLeft, &stickRight);
}
}
};

START_ROBOT_CLASS(RobotDemo);



I edited it, but...

C:/WindRiver/workspace/ll/MyRobot.cpp:38: error: request for member `TankDrive' in `((RobotDemo*)this)->RobotDemo::Bot', which is of non-class type `RobotDrive*'

Thats what it says... If its not too much to ask, whats wrong??
__________________
Team 375: The Robotic Plague
Current Robot: I-Chihuahua
Last Event: Big Apple Breakaway
  #7   Spotlight this post!  
Unread 18-12-2008, 18:46
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Pointers and Tank Drive?

Now that Bot is a pointer, you need to use the arrow operator to access the TankDrive method. You also need to remove the & from stickLeft and stickRight that was turning them into pointers. TankDrive expects two pointer inputs and since stickLeft and stickRight are already pointers, you can pass them in directly. Try this out:

Bot->TankDrive(stickLeft, stickRight);

Last edited by Dave Scheck : 18-12-2008 at 18:48.
  #8   Spotlight this post!  
Unread 18-12-2008, 18:51
Straberrie's Avatar
Straberrie Straberrie is offline
The Plague
AKA: Rebecca
FRC #0375 (Robotic Plague)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2007
Location: New York
Posts: 59
Straberrie will become famous soon enough
Re: Pointers and Tank Drive?

void OperatorControl(void)
{
while (1)
{
Bot->TankDrive(&stickLeft, &stickRight);
}
}
};


I tried that and here are the errors that r showing up! The only line highlighted as having an error is "Bot->TankDrive(&stickLeft, &stickRight);"




C:/WindRiver/workspace/ll/MyRobot.cpp: In member function `virtual void RobotDemo::OperatorControl()':
C:/WindRiver/workspace/ll/MyRobot.cpp:38: error: no matching function for call to `RobotDrive::TankDrive(Joystick**, Joystick**)'
C:/WindRiver/vxworks-6.3/target/h/WPILib/RobotDrive.h:43: note: candidates are: void RobotDrive::TankDrive(GenericHID*, GenericHID*)
C:/WindRiver/vxworks-6.3/target/h/WPILib/RobotDrive.h:44: note: void RobotDrive::TankDrive(GenericHID*, unsigned int, GenericHID*, unsigned int)
C:/WindRiver/vxworks-6.3/target/h/WPILib/RobotDrive.h:45: note: void RobotDrive::TankDrive(float, float)
C:\WindRiver\vxworks-6.3\host\x86-win32\bin\make.exe: *** [SimpleTemplate_partialImage/Debug/Objects/ll/MyRobot.o] Error 1
Build Failed in Project 'll' (Process Exit Value was 2): 2008-12-18 18:47:20 (Elapsed Time: 00:02)
__________________
Team 375: The Robotic Plague
Current Robot: I-Chihuahua
Last Event: Big Apple Breakaway
  #9   Spotlight this post!  
Unread 18-12-2008, 18:53
Straberrie's Avatar
Straberrie Straberrie is offline
The Plague
AKA: Rebecca
FRC #0375 (Robotic Plague)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2007
Location: New York
Posts: 59
Straberrie will become famous soon enough
Re: Pointers and Tank Drive?

Nevermind! I just tried exactly what you said without "&" and ur worked!!! YAY!

__________________
Team 375: The Robotic Plague
Current Robot: I-Chihuahua
Last Event: Big Apple Breakaway
  #10   Spotlight this post!  
Unread 18-12-2008, 19:13
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 590
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Pointers and Tank Drive?

Quote:
Originally Posted by Straberrie View Post
void OperatorControl(void)
{
while (1)
{
Bot->TankDrive(&stickLeft, &stickRight);
}
}
};


I tried that and here are the errors that r showing up! The only line highlighted as having an error is "Bot->TankDrive(&stickLeft, &stickRight);"
This is confusing.

There are two ways of declaring objects: either as an instance of the object or a pointer to the object. In the case of the instance the variable represents the object, and it is created at the time of the declaration. In the case of a pointer you are only creating the space to store the address of the object, but the object remains uncreated. With pointers you have to create the object using the new operator. Look at these two snippets of code to see the difference.
Code:
Joystick stick1(1);   // this is an instance of a Joystick object called stick1
stick1.GetX();         // instances are dereferenced using the dot (.) operator
bot->ArcadeDrive(stick1);  // and can be passed to methods as a reference

Joystick * stick2;    // this is a pointer to an uncreated Joystick object
stick2 = new Joystick(1);  // this creates the instance of the Joystick object
stick2->GetX();       // pointers are dereferenced with the arrow (->)
bot->ArcadeDrive(stick2);  // and can be passed as pointers (notice, no &)
A friend just suggested that this example itself might be a little confusing, and this is going to get kind of geeky. The ArcadeDrive method in the library is taking advantage of a feature of C++ called function overloading. This allows us to have two methods with the same name that differ by the argument list. In the first ArcadeDrive(stick1), the variable stick1 is passed as a reference to a Joystick object. In the second ArcadeDrive(stick2), it is being passed as a pointer to a Joystick object. There are actually two methods in the RobotDrive object, both called ArcadeDrive that each take a different type of argument. The cool thing is that the compiler figures out which one to call. The library is built this way to let it adopt with the style of programmers that prefer to use pointers while at the same time accommodating those who prefer to use references.

Does this help?
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute

Last edited by BradAMiller : 18-12-2008 at 19:28. Reason: improved explanation
  #11   Spotlight this post!  
Unread 18-12-2008, 19:24
Straberrie's Avatar
Straberrie Straberrie is offline
The Plague
AKA: Rebecca
FRC #0375 (Robotic Plague)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2007
Location: New York
Posts: 59
Straberrie will become famous soon enough
Re: Pointers and Tank Drive?

Now i understand the way pointers and instances, etc work!!! Thank you so much!!!

and yep! Helps loads!
__________________
Team 375: The Robotic Plague
Current Robot: I-Chihuahua
Last Event: Big Apple Breakaway
  #12   Spotlight this post!  
Unread 18-12-2008, 18:52
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 590
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Pointers and Tank Drive?

Quote:
Originally Posted by Dave Scheck View Post
Brad - I understand the declarations are above (page 12), but the code on page 14 seems to be the combination of the snippets above. It would make sense that that would be the case. However, as I stated, the code on page 14 wouldn't compile because of the undeclared stick1 and stick2. If the original user copy/pasted the example verbatim, I believe that my explanation would explain the issue.

In case there are different doc versions, I'm referring to doc version .2 from Oct 15.
Dave -

Sorry about that, I missed that part of the example and just fixed it (in the next update). Thanks for pointing it out.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
  #13   Spotlight this post!  
Unread 18-12-2008, 18:13
Straberrie's Avatar
Straberrie Straberrie is offline
The Plague
AKA: Rebecca
FRC #0375 (Robotic Plague)
Team Role: Programmer
 
Join Date: Feb 2008
Rookie Year: 2007
Location: New York
Posts: 59
Straberrie will become famous soon enough
Re: Pointers and Tank Drive?

Thank you so much!!!!!

this explains it!
__________________
Team 375: The Robotic Plague
Current Robot: I-Chihuahua
Last Event: Big Apple Breakaway
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
omni drive vs tank |20807 61|2|_ Technical Discussion 49 17-01-2006 13:54
Tank Drive help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Idaman323 Programming 3 18-02-2005 15:17
Tank Drive Idaman323 Programming 14 15-02-2005 09:01
Swerve/crab drive and tank turning activemx Technical Discussion 9 23-02-2004 18:26
tank drive Stormhammer Programming 11 29-01-2004 23:08


All times are GMT -5. The time now is 00:09.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi