Go to Post We don't use fancy tools like that, but we might need some wood glue or something :) - MrForbes [more]
Home
Go Back   Chief Delphi > Technical > Control System > FRC Control System
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 28-10-2008, 21:22
David Doerr's Avatar
David Doerr David Doerr is offline
Registered User
FRC #4478 (Materia Oscura)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2002
Location: Milford, Michigan, USA
Posts: 75
David Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud of
Team 67 C++ Example Code

We've posted Beta Test C++ example teleop code from Team 67 on the FIRST Forums. We're working on our autonomous code -- the skeleton of that can be seen as well.

Dave D
Reply With Quote
  #2   Spotlight this post!  
Unread 29-10-2008, 09:29
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,505
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Team 67 C++ Example Code

Dave,

I noticed many teams using this format:

->

For instance, something like

Joystick->Set(0)

Can you also use a period:

Joystick.Set(0)

Several websites suggest the two are interchangeable when using classes and objects.
Reply With Quote
  #3   Spotlight this post!  
Unread 29-10-2008, 11:26
AustinSchuh AustinSchuh is offline
Registered User
FRC #0971 (Spartan Robotics) #254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 1999
Location: Los Altos, CA
Posts: 800
AustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond repute
Re: Team 67 C++ Example Code

-> and . can not be interchanged.

x->y() is a shortcut for (*x).y(), so you use . when x is an object, and -> when x is an pointer to an object. It is exactly the same as accessing members of a structure in C.
Reply With Quote
  #4   Spotlight this post!  
Unread 29-10-2008, 12:47
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,505
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Team 67 C++ Example Code

Thanks. For someone who's never used pointers, classes, objects, members, and structures trying to read this code is brutal.

I still don't get the reason behind wanting to use a pointer rather than just a variable.
Reply With Quote
  #5   Spotlight this post!  
Unread 29-10-2008, 18:12
AustinSchuh AustinSchuh is offline
Registered User
FRC #0971 (Spartan Robotics) #254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 1999
Location: Los Altos, CA
Posts: 800
AustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond repute
Re: Team 67 C++ Example Code

Quote:
Originally Posted by Tom Line View Post
I still don't get the reason behind wanting to use a pointer rather than just a variable.
Say you have a structure. It has a variable in it that you want to modify with a function. If you were to just pass the structure into the function, the compiler would pass a copy of the structure to the function, so any changes you did to the structure wouldn't change the original. This isn't the behavior you want. So instead, you pass a pointer to the structure to the function. That pointer is a number saying where the structure is in memory, so when the compiler makes a copy of the pointer to pass into the function, that still points to the correct place in memory. You can then modify the structure in your function, and it all works.

I can try to clarify later, if someone else doesn't beat me to it, if that was confusing.
Reply With Quote
  #6   Spotlight this post!  
Unread 29-10-2008, 21:53
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,505
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Team 67 C++ Example Code

You explanation is clear (I've spent the last couple days reading about classes, objects, structures, constructors, and pointers).

When I learned programming - and perhaps I'm dating myself here - I was always taught you write a function to return a single value. Send the values you need into the procedure, and it returns a result.

In a case where you MUST have a procedure return multiple results (and I was always taught that this was sloppy programming) you could always pass by reference, or use an (evil evil) global variable.

See Kevin Watson's C-code for an example of how I was taught to code using functions.

I'm sure I'm missing something since the whole class / pointer system is new to me. But I'm struggling to see a reason to program using them as opposed to using functions and returning values. This is probably just because I haven't gotten into the level of complexity where they become useful.
Reply With Quote
  #7   Spotlight this post!  
Unread 29-10-2008, 23:32
AustinSchuh AustinSchuh is offline
Registered User
FRC #0971 (Spartan Robotics) #254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 1999
Location: Los Altos, CA
Posts: 800
AustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond repute
Re: Team 67 C++ Example Code

Quote:
Originally Posted by Tom Line View Post
I'm sure I'm missing something since the whole class / pointer system is new to me. But I'm struggling to see a reason to program using them as opposed to using functions and returning values. This is probably just because I haven't gotten into the level of complexity where they become useful.
With C++, it starts becoming useful when you get into inheritance, and all the other OOP stuff. I haven't checked the new WPI c++ libs to see if this is a valid example, but a Victor class and Jaguar class could both inherit a motor controller class, and everywhere in your code where you manipulate an instance of a motor controller, it could really be an instance of a Victor class or a Jaguar class, and you don't have to know. So you could swap out your Victor for a Jaguar, change 1 line in your code where you create an instance of the class, and all your other code using the motor controller would still work (providing victors and jaguars don't have that different electrical characteristics).

Pointers are also available in last year's code also, but they weren't used much since you couldn't really allocate memory, and didn't have a need for it. I believe my team used pointers to pass in a structure containing our PID constants and the variable storing up the data to use for the I part of PID and the D part of PID. The function would return the motor power and update the structure.

If there was a C library available for the bot, we could easily program everything without pointers or structures, so most of this isn't exactly necessary, but instead allows us to do more. My personal opinion is that you can do everything you need to in C with pointers and structures, and not need classes, but it takes a bit more thinking and planning.
Reply With Quote
  #8   Spotlight this post!  
Unread 30-10-2008, 17:44
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: Team 67 C++ Example Code

Quote:
Originally Posted by Tom Line View Post
But I'm struggling to see a reason to program using them as opposed to using functions and returning values. This is probably just because I haven't gotten into the level of complexity where they become useful.
A main reason that pointers are used was mentioned above: to reduce the number of copies that need to be done to perform your functionality.

Take image processing for example. Say you have a function that takes an image data structure as an input and the goal is to invert the color. In a pass by value case, your function and call may look like
Code:
ImageType invert(ImageType input)
{
   ImageType output;
   /* do stuff to populate output with inverted input*/
   return output;
}



ImageType img;
/* assume the image is populated at this point */
img = invert(img);
In this case, there are 3 copies of the image floating around. If the images are 5M a piece, that's 15M. Those large copies can really slow your system down; especially in a system with a small amount of resources.

If you take the pass by reference case, you would have something like
Code:
void invert(ImageType *img)
{
   /* do stuff to invert img*/
}





ImageType img;
/* assume the image is populated at this point */
invert(&img);
Here, there is only one copy of the image. The passed argument is a pointer, and is most definitely smaller than the 5M image.

Pointers take some getting used to, but they can greatly improve your performance when passing large amounts of data around.

The only caveat is that it only improves things if your processor supports pointers well. In the recent IFI controllers, there was no native pointer support and pointer processing required many, many lines of assembly to accomplish. In that case, it was more efficient to pass the data around by value.

Another big reason for using pointers is to support multiple return values. Say you wanted had a function that took in an integer i , and returned both 2 * i and 3 * i. You can't really do this directly with pass by value (you could if you returned it in a struct), but with pass by reference, you can do the following
Code:
void func(int i, int *j, int *k)
{
  *j = 2 * i;
  *k = 3 * i;
}


int x, y;
func(3, &x, &y);
I hope this helps to things for you.
Reply With Quote
  #9   Spotlight this post!  
Unread 30-10-2008, 21:19
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
Re: Team 67 C++ Example Code

I was wondering if it would be possible to attach the code file to this thread in addition to the thread on the FIRST forums? I don't have access to an account on the FIRST forum, but I would like to take a look at the code as well.

Thanks-
DanDon

[EDIT]

Just set up an account on the FIRST forums. Contrary to my original assumptions, accounts are not only for the main team contacts, which is why I thought that I would not be able to have access to one. Thanks to Dave Scheck for letting me know.

[/EDIT]
__________________

Last edited by DanDon : 31-10-2008 at 00:01.
Reply With Quote
  #10   Spotlight this post!  
Unread 31-10-2008, 12:31
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,505
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Team 67 C++ Example Code

Quote:
Originally Posted by Dave Scheck View Post
A main reason that pointers are used was mentioned above: to reduce the number of copies that need to be done to perform your functionality.

Take image processing for example. Say you have a function that takes an image data structure as an input and the goal is to invert the color. In a pass by value case, your function and call may look like
Code:
ImageType invert(ImageType input)
{
   ImageType output;
   /* do stuff to populate output with inverted input*/
   return output;
}



ImageType img;
/* assume the image is populated at this point */
img = invert(img);
In this case, there are 3 copies of the image floating around. If the images are 5M a piece, that's 15M. Those large copies can really slow your system down; especially in a system with a small amount of resources.

If you take the pass by reference case, you would have something like
Code:
void invert(ImageType *img)
{
   /* do stuff to invert img*/
}





ImageType img;
/* assume the image is populated at this point */
invert(&img);
Here, there is only one copy of the image. The passed argument is a pointer, and is most definitely smaller than the 5M image.

Pointers take some getting used to, but they can greatly improve your performance when passing large amounts of data around.

The only caveat is that it only improves things if your processor supports pointers well. In the recent IFI controllers, there was no native pointer support and pointer processing required many, many lines of assembly to accomplish. In that case, it was more efficient to pass the data around by value.

Another big reason for using pointers is to support multiple return values. Say you wanted had a function that took in an integer i , and returned both 2 * i and 3 * i. You can't really do this directly with pass by value (you could if you returned it in a struct), but with pass by reference, you can do the following
Code:
void func(int i, int *j, int *k)
{
  *j = 2 * i;
  *k = 3 * i;
}


int x, y;
func(3, &x, &y);
I hope this helps to things for you.
THANK YOU!

That makes perfect sense.
Reply With Quote
  #11   Spotlight this post!  
Unread 18-11-2008, 13:00
David Doerr's Avatar
David Doerr David Doerr is offline
Registered User
FRC #4478 (Materia Oscura)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2002
Location: Milford, Michigan, USA
Posts: 75
David Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud ofDavid Doerr has much to be proud of
Re: Team 67 C++ Example Code

We hosted a demo and presentation at Milford High School last Saturday, November 15. Here is a link to our First Forums post.

http://forums.usfirst.org/showthread.php?p=19678&

There you can find our C++ demo code for that day and links to our ppt presentations.

DaveD

Last edited by David Doerr : 18-11-2008 at 21:32.
Reply With Quote
  #12   Spotlight this post!  
Unread 23-11-2008, 18:14
Joe Hershberger Joe Hershberger is offline
National Instruments
AKA: jhersh
FRC #2468 (Appreciate)
Team Role: Mentor
 
Join Date: Nov 2005
Rookie Year: 1997
Location: Austin, TX
Posts: 148
Joe Hershberger is a name known to allJoe Hershberger is a name known to allJoe Hershberger is a name known to allJoe Hershberger is a name known to allJoe Hershberger is a name known to allJoe Hershberger is a name known to all
Re: Team 67 C++ Example Code

Quote:
Originally Posted by AustinSchuh View Post
-> and . can not be interchanged.

x->y() is a shortcut for (*x).y(), so you use . when x is an object, and -> when x is an pointer to an object. It is exactly the same as accessing members of a structure in C.
In C++, you can also define a "reference" to an object. References allow you to use the same syntax that you use with the object (object.method()), but you get the same behavior as a pointer (not copied, etc). WPILib for C++ makes use of references and use of them is recommended over pointers.
Reply With Quote
Reply


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
[TBA]: Example Code Addition SamC The Blue Alliance 5 15-07-2011 16:11
Team 1114 Example C++ Code Pat Fairbank FRC Control System 2 12-11-2008 22:30
Example code TEAM1949 Programming 10 15-03-2006 16:00
Example gyro code released. Kevin Watson Programming 60 17-03-2005 18:32
Autonomous Code Example Matthew_H Robotics Education and Curriculum 24 21-11-2003 10:02


All times are GMT -5. The time now is 21:17.

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