Go to Post The robot uprising has begun. - SpoonMechanic [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 16-02-2012, 20:40
inkspell4's Avatar
inkspell4 inkspell4 is offline
Registered User
FRC #3650 (Robo Raptors)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Maryland
Posts: 326
inkspell4 will become famous soon enough
Functions in C++

If i was to create a header file that would control a motor would i declare it as such:


#ifndef CODE_H
#define CODE_H

#include "WPILib.h"

class Code {
private:
Timer Time;
public:
Code();
void TurnForTime(Jaguar* Jag,float speed, float time);
void Wait(float time);
~Code();
};

#endif
__________________
Chesapeake Regional : 2013, 2012, 2011
Rookie Year: 2011
2013 RoboRaptors Team 3650 Programming Team Captain
Team Website : http://www.roboraptorsfrcteam3650.com/index.html
_____________________________________________
Reply With Quote
  #2   Spotlight this post!  
Unread 18-02-2012, 08:47
inkspell4's Avatar
inkspell4 inkspell4 is offline
Registered User
FRC #3650 (Robo Raptors)
Team Role: Programmer
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Maryland
Posts: 326
inkspell4 will become famous soon enough
_____________________
__________________
Chesapeake Regional : 2013, 2012, 2011
Rookie Year: 2011
2013 RoboRaptors Team 3650 Programming Team Captain
Team Website : http://www.roboraptorsfrcteam3650.com/index.html
_____________________________________________
Reply With Quote
  #3   Spotlight this post!  
Unread 18-02-2012, 16:31
DjScribbles DjScribbles is offline
Programming Mentor
AKA: Joe S
FRC #2474 (Team Excel)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Niles MI
Posts: 284
DjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to behold
Re: Functions in C++

As a quick note, when you post code, if you the CODE tags (click the # button in the post formatting options), you can post code much prettier formatting.

Code:
#ifndef CODE_H
#define CODE_H

#include "WPILib.h"

class Code {
private:
    Timer Time;
public:
    Code();
    void TurnForTime(Jaguar* Jag,float speed, float time);
    void Wait(float time);
    ~Code();
};

#endif
The formatting of this code is correct, and if your just giving some example of your formatting your fine.
When you call the function TurnForTime, your syntax would look like this:

codeObject.TurnForTime(&myJagObject,1.0,5.0);


But... if this is the actual header file you are creating (to go along with some cpp file) then I'd like to caution you on a few things:
a) Code is a bad name to give anything, while it may seem nitpicky to say this, it's very important to be as descriptive as possible with what you call things.
b) The Wait function you have shown is already provided by the WPILib; and using a wait function will keep all other code from executing (so your motors will continue running a particular speed until the blocking ends), which is ok as long as your doing it on purpose
c) If your doing this because your having trouble adding a regular function outside of the ones provided by the template, then this is a bit overkill (I remember one of your prev post, so I'm just checkin ). If you'd like a bit of help with structuring whatever it is your trying to do, feel free to PM me with a description of what your doing and your robot.cpp code(or post the code here) and I can try to help.
Reply With Quote
  #4   Spotlight this post!  
Unread 18-02-2012, 16:44
DjScribbles DjScribbles is offline
Programming Mentor
AKA: Joe S
FRC #2474 (Team Excel)
Team Role: Mentor
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Niles MI
Posts: 284
DjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to beholdDjScribbles is a splendid one to behold
Re: Functions in C++

Also there are a few other ways you could do what your trying to do (or what it looks like your trying to do)

Somewhere above your class you can declare a macro like this:

Code:
#include "WPILib.h"
//this will take a jaguar object (not a pointer to it) 
#define TURN_FOR_TIME(jagObj,speed,duration) {jagObj.Set(speed);Wait(duration);}
The way the above works is different from a function call in a few subtle (but important) ways, the way this works is anyplace you call TURN_FOR_TIME(...) it will essentially be like you actually typed all of the code inside the brackets (rather than jumping to a function and executing).

Macros are handy for doing simple operations like this in a way that looks nicer and more organized in your code

the following would do the exact same thing given the above #define
Code:
void TeleopContinuous(void) 
{
    //The second line is exactly what the compiler sees when you type the first line
    //The two lines are exactly the same, a macro is kinda like an automatic copy and paste
    TURN_FOR_TIME(myJag, 1.0, 2.0);
    {myJag.Set(1.0);Wait(2.0);};
}
Hope this helps (and isn't too much info )
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


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

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