Go to Post Robotics is about people. It always was, it always will be. What more guidelines do we need? - Ken Leung [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 14-02-2016, 18:50
kmodos kmodos is offline
Registered User
AKA: Alex
FRC #1126 (SparX)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2013
Location: New York
Posts: 57
kmodos is a splendid one to beholdkmodos is a splendid one to beholdkmodos is a splendid one to beholdkmodos is a splendid one to beholdkmodos is a splendid one to beholdkmodos is a splendid one to beholdkmodos is a splendid one to beholdkmodos is a splendid one to behold
[Snippet] Using 2+ USBCameras with minimal bandwidth usage

Hey CD,

I updated my old code that worked with any type of camera to be a bit more extendable, but it only works with USBCameras now.

Imports:
Code:
import java.util.ArrayList;

import com.ni.vision.NIVision;
import com.ni.vision.NIVision.Image;

import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.vision.USBCamera;
Variables:
Code:
/**
 * The image to push to the CameraServer
 */
private Image frame;

/**
 * The list of all attached cameras
 */
private ArrayList<USBCamera> cams;

/**
 * The index of the current camera we are looking at
 */
private int currCam;

/**
 * The current camera we are viewing.
 */
private USBCamera cam;

/**
 * The maximum fps for all of the cameras
 */
private final int MAX_FPS = 15;

/**
 * The quality of image to push back to the driver station. Lower numbers save more bandwidth (0-100)
 */
private final int QUALITY = 10;
	
/**
 * Time to sleep after changing camera views. This is to prevent errors as USBCamera.startCapture() returns before it is ready to be seen 
 */
private final long SLEEP_TIME = 100;
init Code:
Code:
cams = new ArrayList<USBCamera>();
frame = NIVision.imaqCreateImage(NIVision.ImageType.IMAGE_RGB, 0);
currCam = 0;
CameraServer.getInstance().setQuality(QUALITY);
for(String s: /*Array of camera names*/){
	addCamera(s);
}
cam = cams.get(currCam);
cam.openCamera();
cam.startCapture();
addCamera method:
Code:
/**
 * Adds the camera to our list to switch between and sets the FPS max
 * @param camName The name of the camera
 */
private void addCamera(String camName){
	USBCamera temp = new USBCamera(camName);
	temp.setFPS(MAX_FPS);
	cams.add(temp);
	temp = null;
}
Loop code:
Code:
cams.get(currCam).getImage(frame);
CameraServer.getInstance().setImage(frame);
Switching Method:
Code:
/**
 * Switch to the next camera in our ArrayList
 */
public void switchCamera(){
	try{
		cam.stopCapture();
		cam.closeCamera();
		currCam++;
		currCam %= cams.size();
		cam = cams.get(currCam);
		cam.openCamera();
		cam.startCapture();
		Thread.sleep(SLEEP_TIME);
	}catch(Exception e){
		e.printStackTrace();
	}
}
As always, feel free to post below or send me a PM if you need help!
Reply With Quote
  #2   Spotlight this post!  
Unread 20-03-2016, 15:45
zeldathegood zeldathegood is offline
Registered User
FRC #4277
 
Join Date: Jan 2013
Location: United States
Posts: 26
zeldathegood is an unknown quantity at this point
Re: [Snippet] Using 2+ USBCameras with minimal bandwidth usage

Thanks for posting the code. We gave this a try yesterday but kept getting no robot code after we deployed the code. We didn't have time to debug. We will try again on Monday.

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 10:17.

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