View Single Post
  #4   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