Go to Post ...never do something because it is comfortable, do something because you WANT to do it. - Andrew Schreiber [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
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-2012, 19:51
tyandjel94 tyandjel94 is offline
Registered User
FRC #1647 (Iron Devils)
Team Role: Programmer
 
Join Date: Apr 2012
Rookie Year: 2009
Location: New Jersey
Posts: 14
tyandjel94 is an unknown quantity at this point
Multitasking in Java

Hi,

For next year we are planning to use threads so the robot can multitask better. Do you think this is a good idea and if so how would you go about doing it thanks.

I know you have to create a thread and runnable (like the one below) but where do you put them?

Code:
Thread nameOfThread = new Thread();

 Runnable trackingJob = new Runnable()
    {
//Particle Analysis Report

	

	public void run()
	  {
	    //Code here
	  }

    };
Thanks for your help.
__________________
-Tyler
Team 1647 Iron Devils
Reply With Quote
  #2   Spotlight this post!  
Unread 01-05-2012, 17:44
vinnie's Avatar
vinnie vinnie is offline
Registered User
FRC #3309 (Friarbots)
Team Role: College Student
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Los Angeles / Anaheim
Posts: 107
vinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond repute
Re: Multitasking in Java

The argument to the Thread constructor takes a Runnable. To start the Thread use the start() method, not run() (If you call run(), it won't be threaded at all).

I usually write a separate class that implements Runnable, then creates and starts a thread in the constructor. Take this simple class for an example:

Code:
public class ThreadedHello implements Runnable{
    public ThreadedHello(){
        new Thread(this).start();
    }

    public void run(){
        System.out.println("Hello World!");
    }
}
Then, to use this class I would just do this in another class:

Code:
ThreadedHello hello = new ThreadedHello();
//if it were a real object I would do something else besides create it
Reply With Quote
  #3   Spotlight this post!  
Unread 01-05-2012, 21:53
tyandjel94 tyandjel94 is offline
Registered User
FRC #1647 (Iron Devils)
Team Role: Programmer
 
Join Date: Apr 2012
Rookie Year: 2009
Location: New Jersey
Posts: 14
tyandjel94 is an unknown quantity at this point
Re: Multitasking in Java

Ok, that makes sense. Then would this (below) be how you would use threads with the simple robot template. Would you still need a while loop or once you start the thread it will keep running and call the Runnable. Thanks.

Code:
package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;

public class RobotTemplate extends SimpleRobot
{

    Joystick stickL = new Joystick(1);
    Joystick stickR = new Joystick(2);

    public void autonomous()
      {
      }

    public void operatorControl()
      {
	Thread drive = new Thread(driveJob);
	drive.start();
      }

    Runnable driveJob = new Runnable()
    {

	RobotDrive myDrive = new RobotDrive(1, 2, 3, 4);

	public void run()
	  {
	    myDrive.tankDrive(-stickL.getAxis(Joystick.AxisType.kY), -stickR.getAxis(Joystick.AxisType.kY));
	  }

    };
}
__________________
-Tyler
Team 1647 Iron Devils
Reply With Quote
  #4   Spotlight this post!  
Unread 01-05-2012, 23:33
vinnie's Avatar
vinnie vinnie is offline
Registered User
FRC #3309 (Friarbots)
Team Role: College Student
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Los Angeles / Anaheim
Posts: 107
vinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond repute
Re: Multitasking in Java

If you wanted to use it for something like driving the robot, it would probably be easiest to use the built-in Command classes, but if you have your heart set on using Thread or want to use it for something like socket communication (like I did with our robot), then you would need to have a while(true) loop inside your run method, otherwise it would only run once. If you would like an example of using the Command based model, I would be happy to post one, just let me know.
Reply With Quote
  #5   Spotlight this post!  
Unread 02-05-2012, 00:22
Lalaland1125 Lalaland1125 is offline
Registered User
AKA: Ethan Steinberg
FRC #2429
Team Role: Programmer
 
Join Date: Jan 2012
Rookie Year: 2011
Location: La Canada
Posts: 29
Lalaland1125 is an unknown quantity at this point
Re: Multitasking in Java

Does anyone know exactly where someone could find the threading guarantees we have with the WPILIB library? Can we set motors from separate threads? Can we call I/O functions(both networking and storage) concurrently? Are the constructors for the PWM-like classes free from contention? Etc.

Some of the newer programmers on my team want to look into multithreading next year and I am wondering whether we need to mutex the world in order for it to work well.
Reply With Quote
  #6   Spotlight this post!  
Unread 02-05-2012, 00:43
vinnie's Avatar
vinnie vinnie is offline
Registered User
FRC #3309 (Friarbots)
Team Role: College Student
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Los Angeles / Anaheim
Posts: 107
vinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond repute
Re: Multitasking in Java

Quote:
Originally Posted by Lalaland1125 View Post
Does anyone know exactly where someone could find the threading guarantees we have with the WPILIB library? Can we set motors from separate threads? Can we call I/O functions(both networking and storage) concurrently? Are the constructors for the PWM-like classes free from contention? Etc.

Some of the newer programmers on my team want to look into multithreading next year and I am wondering whether we need to mutex the world in order for it to work well.
I haven't seen it documented anywhere, but I have set motors within Commands, which is WPILib's overly simplified (to a fault I think) system of threading, which I would assume uses the default Thread class. I can't remember for sure whether I have controlled motors in a thread that I created myself, but I am pretty sure that there isn't a problem with it.

I have definitely read files and sockets within a Thread and that works fine.
Reply With Quote
  #7   Spotlight this post!  
Unread 02-05-2012, 12:45
tyandjel94 tyandjel94 is offline
Registered User
FRC #1647 (Iron Devils)
Team Role: Programmer
 
Join Date: Apr 2012
Rookie Year: 2009
Location: New Jersey
Posts: 14
tyandjel94 is an unknown quantity at this point
Re: Multitasking in Java

Ok so in each runnable just put while(true). Could you also put while(isTeleop())?

Code:
Runnable driveJob = new Runnable()
    {

	RobotDrive myDrive = new RobotDrive(1, 2, 3, 4);

	public void run()
	  {
                  while(true)
                     {
	             myDrive.tankDrive(-stickL.getAxis(Joystick.AxisType.kY), -stickR.getAxis(Joystick.AxisType.kY));
                     }
	  }

    };
If you are sending information from the laptop to the robot through udp, can you put a thread in and the robot won't get caught somewhere. So you will still be able to drive and run conveyors and shoot the ball.

Thanks
__________________
-Tyler
Team 1647 Iron Devils
Reply With Quote
  #8   Spotlight this post!  
Unread 02-05-2012, 14:49
Sunstroke Sunstroke is offline
Programmer
AKA: Joe Grinstead
FRC #3504 (Girls of Steel)
Team Role: Mentor
 
Join Date: Apr 2009
Rookie Year: 2009
Location: New England
Posts: 49
Sunstroke is an unknown quantity at this point
Re: Multitasking in Java

Quote:
Originally Posted by tyandjel94 View Post
Code:
while(true)
{
    myDrive.tankDrive(-stickL.getAxis(Joystick.AxisType.kY),-stickR.getAxis(Joystick.AxisType.kY));
}
Watch out, that while loop has the potential to freeze the code because it never rests, you need to put some kind of Thread.sleep(...) in there.

Alternatively, you could try to figure out the Command System which is better for doing the kind of job you've written down (although for UDP stuff, you'll have to use a thread).
Reply With Quote
  #9   Spotlight this post!  
Unread 02-05-2012, 18:51
tyandjel94 tyandjel94 is offline
Registered User
FRC #1647 (Iron Devils)
Team Role: Programmer
 
Join Date: Apr 2012
Rookie Year: 2009
Location: New Jersey
Posts: 14
tyandjel94 is an unknown quantity at this point
Re: Multitasking in Java

Ok thanks. Does it matter how many milliseconds you put in for Thread.sleep(...). Could it be really small? Also would you mind posting an example of using the Command classes. Thanks.
__________________
-Tyler
Team 1647 Iron Devils
Reply With Quote
  #10   Spotlight this post!  
Unread 02-05-2012, 19:38
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,102
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Multitasking in Java

Quote:
Originally Posted by tyandjel94 View Post
Ok thanks. Does it matter how many milliseconds you put in for Thread.sleep(...). Could it be really small? Also would you mind posting an example of using the Command classes. Thanks.

It should be as large as acceptable, and only as small as necessary, for the specific purpose of the thread.


Reply With Quote
  #11   Spotlight this post!  
Unread 02-05-2012, 19:54
vinnie's Avatar
vinnie vinnie is offline
Registered User
FRC #3309 (Friarbots)
Team Role: College Student
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Los Angeles / Anaheim
Posts: 107
vinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond reputevinnie has a reputation beyond repute
Re: Multitasking in Java

Regarding UDP, I do not believe that it is supported on the cRIO in Java (as of last year at least). The classes exist for it, but when I tried to run my UDP code, it said that datagram sockets were not supported.
Reply With Quote
  #12   Spotlight this post!  
Unread 28-05-2012, 20:32
ItzWarty ItzWarty is offline
Registered User
FRC #1072 (Harker Robotics)
Team Role: Leadership
 
Join Date: Jan 2012
Rookie Year: 2010
Location: California
Posts: 15
ItzWarty is an unknown quantity at this point
Re: Multitasking in Java

Quote:
Originally Posted by Lalaland1125 View Post
Does anyone know exactly where someone could find the threading guarantees we have with the WPILIB library? Can we set motors from separate threads? Can we call I/O functions(both networking and storage) concurrently? Are the constructors for the PWM-like classes free from contention? Etc.

Some of the newer programmers on my team want to look into multithreading next year and I am wondering whether we need to mutex the world in order for it to work well.
From what I've seen, you can have numerous calls to the same device instance, and synchronized blocks will be placed somewhere in the invoked method.
__________________
Leader of Team 1072. Software developer of RAF Manager.
Follow me on Twitter: Twitter.com/ItzWarty/
Reply With Quote
  #13   Spotlight this post!  
Unread 08-06-2012, 16:17
daniel_dsouza daniel_dsouza is offline
does what needs to be done.
FRC #2449 (Out of Orbit Robotics)
Team Role: Alumni
 
Join Date: May 2011
Rookie Year: 2011
Location: Chandler, AZ
Posts: 231
daniel_dsouza has a spectacular aura aboutdaniel_dsouza has a spectacular aura about
Re: Multitasking in Java

The "PIDcontroller" class creates a thread to run its calculations.
It's code looks like this (extra stuff removed so that the thread code remains):
Code:
java.util.Timer controlLoop;
...
private class PIDTask extends TimerTask {
     ...
     public void run() {
           m_controller.calculate();
     }
}

public PIDController(double Kp, double Ki, double Kd, PIDSource source, PIDOutput[] output, double iperiod) {
     controlLoop = new java.util.Timer();
      ...
     controlLoop.schedule(new PIDController.PIDTask(this), 0L, (long) (period * 1000)); //starts thread
}
I haven't examined the process in depth, but this is what is written.
Reply With Quote
  #14   Spotlight this post!  
Unread 09-06-2012, 19:46
Slix Slix is offline
Registered User
AKA: Peter Kowalczyk
FRC #2115 (NightMares)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2010
Location: Mundelein, IL
Posts: 31
Slix is an unknown quantity at this point
Re: Multitasking in Java

Quote:
Originally Posted by vinnie View Post
I haven't seen it documented anywhere, but I have set motors within Commands, which is WPILib's overly simplified (to a fault I think) system of threading, which I would assume uses the default Thread class.
Commands don't use threads. They are scheduled.

When the user's code runs the Scheduler's run() method, the scheduler iterates through a list of active commands and calls execute() on each one (sequentially, not parallel).

So there's no issues with race conditions or anything complicated like that with the new command-based system, since there's no threads. I thought commands were useful because we could easily make the robot perform actions by binding button presses to Commands. It also made it easy for our robot to sequentially fire balls during autonomous just by scheduling three of our "shoot ball" Commands to run using a CommandGroup.

Last edited by Slix : 09-06-2012 at 21:01.
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:28.

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