|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools |
Rating:
|
Display Modes |
|
|
|
#1
|
||||
|
||||
|
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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
Re: Team 67 C++ Example Code
Quote:
I can try to clarify later, if someone else doesn't beat me to it, if that was confusing. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
Re: Team 67 C++ Example Code
Quote:
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. |
|
#8
|
||||
|
||||
|
Re: Team 67 C++ Example Code
Quote:
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);
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);
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);
|
|
#9
|
||||
|
||||
|
Re: Team 67 C++ Example Code
Quote:
That makes perfect sense. |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
|||
|
|||
|
Re: Team 67 C++ Example Code
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.
|
|
#12
|
|||||
|
|||||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |