Go to Post The problem with anecdotal evidence is that you never seem to get the whole anecdote. - Lea DeFoote [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 30-04-2013, 18:39
pipsqueaker pipsqueaker is offline
Registered User
FRC #1124
 
Join Date: Apr 2013
Location: Avon
Posts: 59
pipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to all
[Q] How are we supposed to use threads in our robot code?

Hey, I'm a new programmer, and right now me and one of my friends are involved in porting our robot code to c++. One big thing that's been throwing me off however is c++'s apparent lack of threads. Whenever I google it I get suggestions to use the Boost library, which I have no idea how to use with WPILib. Results are even harder to find because of the clash of terminology (Google keeps confusing forum threads with code threads), so the end result is that I'm really not sure how (or if) I can thread the code.

I'm building off of the SimpleTemplate project, if you wanted to know.

Thanks!
Reply With Quote
  #2   Spotlight this post!  
Unread 30-04-2013, 19:10
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,050
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: [Q] How are we supposed to use threads in our robot code?

Quote:
Originally Posted by pipsqueaker View Post
Hey, I'm a new programmer, and right now me and one of my friends are involved in porting our robot code to c++. One big thing that's been throwing me off however is c++'s apparent lack of threads. Whenever I google it I get suggestions to use the Boost library, which I have no idea how to use with WPILib. Results are even harder to find because of the clash of terminology (Google keeps confusing forum threads with code threads), so the end result is that I'm really not sure how (or if) I can thread the code.

I'm building off of the SimpleTemplate project, if you wanted to know.

Thanks!
Last time I checked (around 1.38), Boost will work on vxWorks. However, compiling it into your robot code will be annoying. [edit] So don't use that on the robot unless you know what you're doing. [/edit]

Use the Task class instead, that allows you to create additional threads using WPILib.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff

Last edited by virtuald : 30-04-2013 at 21:48. Reason: clarification
Reply With Quote
  #3   Spotlight this post!  
Unread 30-04-2013, 21:43
pipsqueaker pipsqueaker is offline
Registered User
FRC #1124
 
Join Date: Apr 2013
Location: Avon
Posts: 59
pipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to allpipsqueaker is a name known to all
Re: [Q] How are we supposed to use threads in our robot code?

Are you saying that I have to use boost and WPILib's task class? I looked on WPILib's task documentation page , and it doesn't look like I need to. Just looking for clarification.

Also, on the above page, it says the task::run takes the arguments of types FUNCPTR and int. just asking, the fcptr is just the function name without any arguments or parentheses, correct? Also the arguments are of type int, does that mean I can only pass integers?

Sorry for bombarding you with questions but, like I said, I'm new to this and have no coders on the team experienced enough with c++ to help us.
Reply With Quote
  #4   Spotlight this post!  
Unread 30-04-2013, 21:47
connor.worley's Avatar
connor.worley connor.worley is offline
Registered User
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Mar 2011
Rookie Year: 2010
Location: Berkeley/San Diego
Posts: 601
connor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond repute
Re: [Q] How are we supposed to use threads in our robot code?

I would take a look at pthreads.
__________________
Team 973 (2016-???)
Team 5499 (2015-2016)
Team 254 (2014-2015)

Team 1538 (2011-2014)
2014 Driver (25W 17L 1T)
日本語でOK
Reply With Quote
  #5   Spotlight this post!  
Unread 30-04-2013, 21:53
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,050
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: [Q] How are we supposed to use threads in our robot code?

Quote:
Originally Posted by pipsqueaker View Post
Are you saying that I have to use boost and WPILib's task class?
No.

Quote:
Also, on the above page, it says the task::run takes the arguments of types FUNCPTR and int. just asking, the fcptr is just the function name without any arguments or parentheses, correct?
Yes.

Quote:
Also the arguments are of type int, does that mean I can only pass integers?
Sorta. Since it's C++ and it allows you to shoot yourself in the foot, you can technically pass in anything that is the same size as an integer. Be wary of this behavior, as integer sizes may vary on other platforms (such as your PC).

I recommend searching these forums for more information, other people have asked the same questions.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #6   Spotlight this post!  
Unread 30-04-2013, 21:55
Toa Circuit's Avatar
Toa Circuit Toa Circuit is offline
Thaddeus Maximus
AKA: Thad Hughes
FRC #4213 (MetalCow Robotics)
Team Role: Leadership
 
Join Date: Nov 2012
Rookie Year: 2012
Location: Shirley, IL
Posts: 131
Toa Circuit is an unknown quantity at this point
Re: [Q] How are we supposed to use threads in our robot code?

You will never need to use Boost or any external libraries for any multithreading in C++ for FRC, so save yourself some headaches and just WPILib because it works like a charm.

Here's some code that makes a separate thread to flip the state of a relay, which is spawned at TeleopInit, and killed off at DisabledInit.

Code:
class Robot: public SimpleRobot{
  Task secondThread;
  Relay *relay;
 public:
  Robot():
   secondThread("SecondThread",(FUNCPTR) Robot::threadFunction)
  {
   relay=new Relay(1);
  }

  void threadFunction(UINT32 relayPointer){
   Relay *rly=(Relay*)relayPointer;
   while(true){
    rly->Set(Relay::kForward);
    Wait(1);
    rly->Set(Relay::kReverse);
    Wait(1);
   }
  }

  void Disabled(){
   secondThread.Stop();
  }

  void Teleop(){
   secondThread.Start((UINT32) relay);
  }
};

START_ROBOT_CLASS(Robot)
;
See also:
http://www.chiefdelphi.com/forums/sh...ad.php?t=82398

If you need explanation of anything in this post, just refer to that post^
__________________

2012 Head of Programming and Electrical
2013-14 Overall Team Captain and Programming Head
2012-14 Mentor of FLL Team Power Surge
2014 Dean's List Finalist
2014 CIR Xerox Creativity Award
Webpage

Last edited by Toa Circuit : 30-04-2013 at 22:01. Reason: Realizing a few things
Reply With Quote
  #7   Spotlight this post!  
Unread 08-05-2013, 15:51
bob.wolff68's Avatar
bob.wolff68 bob.wolff68 is offline
Da' Mentor Man
FRC #1967
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2007
Location: United States
Posts: 157
bob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nice
Re: [Q] How are we supposed to use threads in our robot code?

My team produced a class this year which worked quite well and is quite extensible. All the source is on github. I also gave a pretty complete training discussion about how the class works and why in:

http://www.chiefdelphi.com/forums/sh...87#post1232587

If you have questions after reading that, feel free to contact me or post here. or there.

Thanks,
bob
__________________
~~~~~~~~~~~~~~~~~~~
Bob Wolff - Software from the old-school
Mentor / C / C++ guy
Team 1967 - The Janksters - San Jose, CA
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 12:51.

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