Go to Post If the left side of your brain controls the right side of your body then left handers are the only ones in "their right mind". - Al Skierkiewicz [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 25-01-2017, 08:02
MarlyM64 MarlyM64 is offline
Registered User
FRC #1660 (Harlem Knights)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2016
Location: New York City
Posts: 3
MarlyM64 is an unknown quantity at this point
Need NetworkTables to update??(Grip)

How do I get the NetworkTables to update when the camera is plugged into the roboRio? (Grip related issue)

When the camera USB is plugged into the computer our networkTables update fine, and the GripPipeline code that Grip generated works fine, but we cannot get grip to publish networkTables that update when the camera is connected to the robot.
Reply With Quote
  #2   Spotlight this post!  
Unread 25-01-2017, 10:32
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 161
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Need NetworkTables to update??(Grip)

GRIP only generates the vision processing code. You have to write the NetworkTables part.

If you take a look at this screensteps page, you can publish to NetworkTables inside the callback in the second code block. Something like

Code:
visionThread = new VisionThread(camera, new Pipeline(), pipeline -> {
  ...
  centerX = ...
  centerY = ...
  NetworkTables.getTable("vision").putNumber("centerX", centerX);
  NetworkTables.getTable("vision").putNumber("centerY", centerY);
});
We should also have an example for sending results to the SmartDashboard up in the next few days.
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #3   Spotlight this post!  
Unread 25-01-2017, 14:14
MarlyM64 MarlyM64 is offline
Registered User
FRC #1660 (Harlem Knights)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2016
Location: New York City
Posts: 3
MarlyM64 is an unknown quantity at this point
Re: Need NetworkTables to update??(Grip)

yes i have code similar to i, but it still won't work ._.



visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
if (!pipeline.filterContoursOutput().isEmpty()) {

System.out.println(pipeline.filterContoursOutput() .get(0));
//SmartDashboard.putNumber("opencv",pipeline.filterC ontoursOutput().get(0));
table = NetworkTable.getTable("GRIP/marly");

double[] def = new double[0];
double[] areas = table.getNumberArray("width", def);
System.out.println(areas[0]);
}
});
visionThread.start();
Reply With Quote
  #4   Spotlight this post!  
Unread 25-01-2017, 14:24
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 161
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Need NetworkTables to update??(Grip)

You're doing vision processing, but not using the results. You need to be putting values into the table.
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #5   Spotlight this post!  
Unread 25-01-2017, 16:44
wlogeais wlogeais is offline
Registered User
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2016
Rookie Year: 2011
Location: Minnesota
Posts: 18
wlogeais is an unknown quantity at this point
Re: Need NetworkTables to update??(Grip)

Quote:
Originally Posted by MarlyM64 View Post
yes i have code similar to i, but it still won't work ._.


if (!pipeline.filterContoursOutput().isEmpty()) {

System.out.println(pipeline.filterContoursOutput() .get(0));
//SmartDashboard.putNumber("opencv",pipeline.filterC ontoursOutput().get(0));
Seems to me your goal is really how to 'get' and display?

if (!pipeline.filterContoursOutput().isEmpty()) {
Rect r = Imgproc.boundingRect(pipeline.filterContoursOutput ().get(0));
SmartDashboard.putString("opencv", "at (" + r.x + ',' + r.y + ')' );
Reply With Quote
  #6   Spotlight this post!  
Unread 08-02-2017, 22:02
MarlyM64 MarlyM64 is offline
Registered User
FRC #1660 (Harlem Knights)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2016
Location: New York City
Posts: 3
MarlyM64 is an unknown quantity at this point
Re: Need NetworkTables to update??(Grip)

Quote:
Originally Posted by wlogeais View Post
Seems to me your goal is really how to 'get' and display?

if (!pipeline.filterContoursOutput().isEmpty()) {
Rect r = Imgproc.boundingRect(pipeline.filterContoursOutput ().get(0));
SmartDashboard.putString("opencv", "at (" + r.x + ',' + r.y + ')' );
Thanks for answering my previous questions. We decided not to use NetworkTables because we were able to see some values directly based on the previous code.

We have been able to get the Rect values to display on the SmartDashboard, but they don't seem to be updating.

We noticed that the process() method in the pipeline is what actually updates the values, but the example code doesn't seem to use it. (We are able to see new values each time we restart the SmartDashboard!, but not updating).

We tried calling the process() method inside of the while loop of our init method. We attempted to grab a picture and pass in as the Mat parameter, but there were no rectangles being seen (even though we have been consistently seeing rectangles in the Grip software).

I'm stuck again on trying to figure out how to get the rectangles to update in real time.

Any help would be greatly appreciated!

This is our camInit() method:
Code:
public void camInit() {

		// NetworkTable.setIPAddress("10.16.60.63");
		// table = NetworkTable.getTable("marly");

		/* Creates UsbCamera and MjpegServer [1] and connects them */
		UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();

		/* Creates the CvSource and MjpegServer [2] and connects them */
		CvSource outputStream = CameraServer.getInstance().putVideo("steamVideo", 640, 480);

		/* Creates the CvSink and connects it to the UsbCamera */
		CvSink cvSink = CameraServer.getInstance().getVideo();
       Mat mat = new Mat();
       Mat outputMat = new Mat();
	//   camera.

		VisionThread visionThread = new VisionThread(camera, new GripPipeline(), pipeline -> {
			while (camRunning) {
				ArrayList<MatOfPoint> amat = pipeline.filterContoursOutput();
				int tempNumRectangles = amat.size();
				
				Rect tempR0 = r0; //keep old value if you don't see it?
				System.out.println(" tempNumRectangles "+ tempNumRectangles);
				
				if (tempNumRectangles > 0){		//!pipeline.filterContoursOutput().isEmpty()) {

				    tempR0 = Imgproc.boundingRect(amat.get(0));
					Rect tempR1 = r1;		//keep old value if you don't see it?

					if (tempNumRectangles > 1) {
						tempR1 = Imgproc.boundingRect(amat.get(1));
					}
					synchronized(camLock){
						System.out.println(" here");
						r0 = tempR0;
						r1 = tempR1;
						numRectangles = tempNumRectangles;
					}
				}
				
//				System.out.print ("r0.x: " +r0.x + " r0.y: " +r0.y+ " r0.height: " + r0.height + " r0.width: "+r0.width);
				SmartDashboard.putNumber("rect0.x", r0.x);
				SmartDashboard.putNumber("rect0.y", r0.y);
				SmartDashboard.putNumber("rect0.height", r0.height);
				SmartDashboard.putNumber("rect0.width", r0.width);
				
	//			System.out.print ("r1.x: " +r1.x + " r1.y: " +r1.y+ " r1.height: " + r1.height + " r1.width: "+r1.width);
				SmartDashboard.putNumber("rect1.x", r1.x);
				SmartDashboard.putNumber("rect1.y", r1.y);
				SmartDashboard.putNumber("rect1.height", r1.height);
				SmartDashboard.putNumber("rect1.width", r1.width);

		//		System.out.println("\t rect count = " + numRectangles);
				SmartDashboard.putNumber("rect count", tempNumRectangles);
			
				try {
				    Thread.sleep(100);
				} catch(InterruptedException e){
					camRunning = false;
					System.out.println("Thread sleep exception");
				}
				
				if (cvSink.grabFrame(mat) == 0)
				{
					System.out.println("not working****");
					outputStream.notifyError(cvSink.getError());
					continue;
				}
				
				//System.out.println("working!!");
				
				pipeline.process(mat);
				//cvSink.grabFrame(mat);
                //Imgproc.cvtColor(mat, outputMat, Imgproc.COLOR_BGR2GRAY);
                //outputStream.putFrame(outputMat);
				//System.out.println(mat.toString());
				
			}
		});

		visionThread.start();

	}
Reply With Quote
  #7   Spotlight this post!  
Unread 09-02-2017, 00:00
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 161
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Need NetworkTables to update??(Grip)

Code:
while (camRunning) {
This is something I've seen quite a lot of and I'm not really sure why people are doing it. The callback is called after the pipeline runs. It's intended to copy the outputs to variables the rest of the robot program can use, and maybe do a little processing on the outputs (like the bounding rects). Putting in a loop will make the vision thread run the pipeline exactly once and then enter the loop. In your case, the loop only exits if the vision thread was interrupted, which will very likely never happen. So the pipeline will never run again after that first time.

So why did you decide to add a loop? I'd like to improve the documentation so that it's clearer that this is not the right way to do this.

The Javadoc for VisionRunner.Listener, which is what that lambda is
__________________
WPILib
GRIP, RobotBuilder
Reply With Quote
  #8   Spotlight this post!  
Unread 09-02-2017, 16:45
JABianchi's Avatar
JABianchi JABianchi is offline
Registered User
FRC #1660 (Harlem Knights)
Team Role: Coach
 
Join Date: Mar 2006
Rookie Year: 2005
Location: Harlem, NY
Posts: 21
JABianchi is on a distinguished road
Re: Need NetworkTables to update??(Grip)

Sam,
I think what has been throwing us for a loop hasn't really been so much a GRIP problem (generating java code from GRIP has made this part very elegant), but a steep learning curve for more advanced Java syntax.

There is a fundamental need for the vision code to loop, and really understanding how this is done can feel like hiking through a jungle:
  • What is a thread, and how does Java implement them? (See more on the Runnable interface)
  • What is a lambda expression (->), and how can you implement an interface directly with one? (particularly as used in the constructor for a VisionThread)
  • What does it mean for a method be called as a parameter using method references ( :: ) in java8? (as related to the 1st parameter of the first constructor for VisionThread)

The while loop that we, and perhaps others, needed was really in the runForever() method of the VisionRunner class, but that's not a simple find unless you are already familiar with the previous concepts.

Throw on top of that:
  • Understanding scope issues in regards to how FIRST executes the methods of our code through the FMS,
  • Getting familiar with openCV,
  • and good ol'-fashioned hardware issues
and it can be hard while learning to feel confident about the levels of abstracted code we are using.

Our school recently began offering APCS A for students, which has been very helpful for robotics team students who program our robot, but a lot of these topics are not in the scope of the AP exam and are things we'll need to figure out how to integrate into our learning pipeline.

The GRIP/vision screensteps have been much improved this year, and very helpful, and we are especially appreciative of the time you have put into ChiefDelphi forums to help us and others out with GRIP-related questions. I, personally, am very excited about how GRIP and the openCV-based libraries can help make vision-processing a reality for a larger number of FRC teams going forward.

Perhaps in the future, there is an opportunity to add some additional screensteps (or point to good tutorial resources) that address some of the topics I mentioned above.
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 13:18.

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