Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Network Tables (http://www.chiefdelphi.com/forums/showthread.php?t=128328)

JamesTerm 31-03-2014 23:17

Re: Network Tables
 
Quote:

Originally Posted by lucas.alvarez96 (Post 1367616)
James, that would be awesome! Please post the link as soon as you can. Thanks man!

Here
https://www.youtube.com/watch?v=nLmviNrMers

Shows a demo of everything... and in here is a link to the first forge... I'll include here for convenience:
http://firstforge.wpi.edu/sf/projects/smartcppdashboard

Now... this code is due for an update, but we are just about ready to head out to Lonestar regionals... so after that I may get the code all updated probably in the next two weeks, but in its current state it should get you going pretty well.

Here is a sneak peak of what the newer stuff can do (not yet checked in there): https://www.youtube.com/watch?v=fccxxlvMqY0

virtuald 31-03-2014 23:49

Re: Network Tables
 
I think he's talking about this: http://firstforge.wpi.edu/sf/sfmain/...rtcppdashboard

The opencv files should be structured something like so:

Code:

C:\Python27\opencv_ffmpeg2xx.dll
C:\Python27\lib\site-packages\cv2.pyd

I custom compiled my version of ffmpeg, but at this point I don't recall if I had to do something special to get MJPG support. I'll have to go find the source tree if I still have it...

sparkytwd 01-04-2014 00:17

Re: Network Tables
 
We had to install ffdshow as well to get loading from a file working on windows.

yash101 01-04-2014 00:41

Re: Network Tables
 
So it really seems as though you were in the same boat as me three weeks ago. There is one very easy fix to the problem you are fixing, but it requires kinda out of the box thinking ;)
Through my VC++ opencv adventure, I learned that many processor intensive routines may have a such a low performance that the processing rate will become less than the capture rate. I believe I properly understand your setup. You are using an MJPEG stream with VideoCapture. I have two solutions available for you that should be relatively easy to use.
If you must stay with the network MJPEG stream approach,
--Decrease the capture rate of the camera to the MAX rate that your vision software will run at. This will cause you to economize on your lag and if for some reason the software runs a bit faster, it will just wait for the next frame.
--THREAD YOUR GRABBER
The second option is what saved me. I was before getting 30 seconds of lag even though I was running at 5fps. What this will do is run the grabber parallelly. I believe you are pretty decent at C++ so you should be able to figure it out. Try <thread> and <mutex>.
So what you want to do is wait for the next frame to be available from the camera and download it immediately. Do not do this in the main Mat because it will make the entire program wait and brick up your effort. After the frame grab, send the data to the actual processing Mat. In your processing loop, copy over that global Mat into a local Mat so the system can free the resource as quick as possible! Then, perform the operations you want to do. This way, the old frames are constantly deleted as new frames are available
Try to use both the following together. Using just the first option will make no difference. Using both options is when you get both efficient and get rid of the lag.

I have been paying attention to your posts lately and I believe you have a PandaBoard. Snag an inexpensive USB webcam and that should eliminate most of the lag you are experiencing!

I just gave you my two cents, so good luck!


Also, you might find it easier to implement a UDP bidirectional socket instead of NetworkTables. There are libraries for this in C++, Java and even LabView!

JamesTerm 01-04-2014 11:31

Re: Network Tables
 
Quote:

Originally Posted by yash101 (Post 1367694)
Also, you might find it easier to implement a UDP bidirectional socket instead of NetworkTables. There are libraries for this in C++, Java and even LabView!

I am not sure of yash101's setup, but if UDP is used from robot to driver-station beware. It costs more bandwidth by nature... which shouldn't matter if it is a direct connection, but if the robot is receiving packets it will need a dedicate thread to listen to packets on startup, due to the VxWorks issue. This thread elaborates on that and also talks about a bug fix with the Network Tables.

apalrd 01-04-2014 12:55

Re: Network Tables
 
Quote:

Originally Posted by JamesTerm (Post 1367811)
I am not sure of yash101's setup, but if UDP is used from robot to driver-station beware. It costs more bandwidth by nature... which shouldn't matter if it is a direct connection, but if the robot is receiving packets it will need a dedicate thread to listen to packets on startup, due to the VxWorks issue. This thread elaborates on that and also talks about a bug fix with the Network Tables.

I don't see how this can be true.

Assuming the data to send is the same size (which depends on implementation), UDP would in the real world send less data, as it never resends packets that failed. You are also sending images on the same ethernet link, so a few extra bytes or packets here or there really makes no difference.

In either case, you need a listener somewhere to read all of the packets from the buffer. Network Tables already created a thread to do this, with UDP you are doing it on your own.

In many ways, I prefer UDP sockets as it is very simple to implement (there are thousands of tutorials on the internet describing basic sockets), you can use a simple struct to organize the data, and a checksum to discard packets that are garbled in transit. No library required. In LV, you can do it super easily by flattening a cluster to a string and then unflattering it on the other side.

JamesTerm 01-04-2014 13:05

Re: Network Tables
 
Quote:

Originally Posted by apalrd (Post 1367866)
I don't see how this can be true.

Assuming the data to send is the same size (which depends on implementation), UDP would in the real world send less data, as it never resends packets that failed. You are also sending images on the same ethernet link, so a few extra bytes or packets here or there really makes no difference.

I should post some benchmarks... I think I got the high band width from UDP because I used the DO_NOT_WAIT flag when creating the socket. So perhaps it would be less, but I have yet to see confirmation of that.

Quote:

Originally Posted by apalrd (Post 1367866)
I don't see how this can be true.
In either case, you need a listener somewhere to read all of the packets from the buffer. Network Tables already created a thread to do this, with UDP you are doing it on your own.

Needing a listener somewhere is not the same as needing a listener that is on its own dedicated thread listening even at startup. When I first dinked around with UDP I used WinSock2 for everything, and could set the options as such where I didn't need to make a new thread... this was great and kept the code simple. I then transfer this same code to VxWorks and saw the fireworks... the driver station would repeatedly lose connection and get it back. The UDP buffer overflowed and corrupted the TCPIP packets.

So ... you don't need to have a dedicated thread if you are using WinSock2, but for VxWorks, and the original WinSock... you do.

Alan Anderson 01-04-2014 13:48

Re: Network Tables
 
Quote:

Originally Posted by JamesTerm (Post 1367873)
I then transfer this same code to VxWorks and saw the fireworks... the driver station would repeatedly lose connection and get it back. The UDP buffer overflowed and corrupted the TCPIP packets.

So ... you don't need to have a dedicated thread if you are using WinSock2, but for VxWorks, and the original WinSock... you do.

I believe you are ascribing the wrong cause to what you saw. VxWorks running on the cRIO has a single shared buffer for all incoming network communication, and that's what made your flood of UDP traffic get in the way of the TCP packets from the Driver Station. Under Windows, your UDP buffer was possibly overflowing and (correctly) discarding packets, but under Windows that wouldn't mess up network communication on any other ports.

You do need to read the incoming network data at least as fast as it is arriving, but there is no requirement for you to do it in its own thread.

JamesTerm 01-04-2014 14:23

Re: Network Tables
 
Quote:

Originally Posted by Alan Anderson (Post 1367908)
I believe you are ascribing the wrong cause to what you saw. VxWorks running on the cRIO has a single shared buffer for all incoming network communication, and that's what made your flood of UDP traffic get in the way of the TCP packets from the Driver Station. Under Windows, your UDP buffer was possibly overflowing and (correctly) discarding packets, but under Windows that wouldn't mess up network communication on any other ports.

You do need to read the incoming network data at least as fast as it is arriving, but there is no requirement for you to do it in its own thread.


What you say here is what I was trying to say more or less... WinSock2 handles the stress of neglecting to process the packets. It was indeed the neglect of processing the incoming packets that caused this symptom which I confirmed with Greg McKaskle, and Brian from team 118 also ran into this issue back in 2012. It may indeed be possible that you can make it where it doesn't have its own thread... you could implement some kind of hand shake solution or defer sending packets until Auton or Telop functions are being called. But these alternatives are messy implementation IMHO. NetworkTables on the other hand does not have these issues at all, and probably one of the main reasons I switched... that and the ease of use, where adding more variables manually in UDP is a real pain. I've included these UDP_Listener.h UDP_Listener.cpp for reference where I could macro switch from UDP to Network Tables... it was so much easier working with network tables that I dropped this whole interfacing design.

mhaeberli 01-04-2014 23:09

Re: Network Tables
 
So, maybe I didn't follow your analysis and notes well enough, but FYI I had similar problems using direct python opencv VideoCapture bindings, until I had the Windows PATH variables set correctly to the OpenCV install. I'm seeing latencies like a second, but nothing like 30.

apalrd 02-04-2014 14:08

Re: Network Tables
 
Why wait until Auton or Teleop functions are called?

Why don't you just run your code all the time, and then additionally run auton or teleop code based on the driver station data?

JamesTerm 02-04-2014 15:40

Re: Network Tables
 
Quote:

Originally Posted by apalrd (Post 1368432)
Why wait until Auton or Teleop functions are called?

Why don't you just run your code all the time, and then additionally run auton or teleop code based on the driver station data?

I take it you are talking about the idea of waiting for auton and telop to start listening. Network tables does like what you say "run your code all the time" so that any c++ programmer can use it without having to know anything about writing a new thread (i.e. task). I agree that this is the best way because it can still work while robot is disabled, and handles 2 way communication.

But let's go back for a moment... when I first did UDP and waited for Auton and Teleop before I knew about this bug. Why did I do it then? Because writing threads is a messy business, and should be avoided if at all possible. I mean look at the trouble that happened with the lockup bug in Network Tables... Using winsock2 I was able to listen to packets on the same thread and it was nice and clean code to work out. So given that... all of our code runs on a single thread... we don't use the PID that comes with WPI... it does not need to be on a separate thread. Instead we introduce a time-slice delta in seconds to the computations... this way the PID can work in 10ms (ish) iterations on the same thead rather than the default 50 on a separate thread... thus being free from any mess of critical sections!

lucas.alvarez96 02-04-2014 18:53

Re: Network Tables
 
Thanks for all the help :D
So does anybody have the Network Tables library for C++?
I haven't got Wind River and nobody on my team know where the CD is...

apalrd 02-04-2014 19:37

Re: Network Tables
 
We run everything in a single task (including a UDP listener and RS232 listener). We just don't stop the task when auton or teleop is disabled. We clear the integrators and reset to a safe state when the disabled signal goes from low to high, and run everything in a single 10ms high priority task. The UDP listener reads in a While loop with a 0ms timeout and breaks when the read returns a timeout. The RS232 listener does similar.

yash101 02-04-2014 21:52

Re: Network Tables
 
I don't have the UDP the setup down, but I would like to get the code down sometime soon.
I was wondering if anyone knows how to write a socket server with javax.microedition.io.*;
I am currently shot in the dark and have no idea to start


All times are GMT -5. The time now is 22:01.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi