Go to Post Who wants to see your robot get their butt handed to them? - Joe_Widen [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-01-2016, 13:16
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
How to detect missing CAN devices from Java?

We are heavily using CAN this year, but our test chassis does not have any CAN Talons or a CAN PDB on it (yet).

If we deploy our CAN-Talon-using code to a RoboRIO that does not have CAN Talons present, the robot gets "stuttery"; the PWM-based drive system keeps cutting out. We commented out all the code to get data out of the PDB and Talons (we're doing a lot of data logging while we get things tuned), and things smoothed out. It *appears* the CAN Recieve timeouts are causing problems.

Is there a good way to determine in software if a CAN Talon or PDB is absent so that we can avoid the method calls that cause issues with missing hardware?

I know we can put in a compiled-in boolean, jumper detect, or dashboard toggle to avoid the code, but would really like to just have the software self-configure....
  #2   Spotlight this post!  
Unread 23-01-2016, 13:22
ozrien's Avatar
ozrien ozrien is offline
Omar Zrien
AKA: Omar
no team
Team Role: Mentor
 
Join Date: Sep 2006
Rookie Year: 2003
Location: Sterling Heights, MI
Posts: 549
ozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond repute
Re: How to detect missing CAN devices from Java?

Hey Doug,
Most of the getters do not block regardless of CAN device presence, so this should not be happening. If you want, send me the code and I can take a look.
support@crosstheroadelectronics.com
  #3   Spotlight this post!  
Unread 23-01-2016, 14:13
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: How to detect missing CAN devices from Java?

Getting

Robot Drive... Output not updated often enough.

when it stutters.

Figuring out way to send code; zip file of project is too large to be a CD attachment.

I suspect the problem may be that all the console messages are swamping the network pipe, so the RIO is missing some DS packets, periodic() doesn't get called for a while, the drive object doesn't get poked for a while, and the safety watchdog fires off. Just conjecture, but will turn off the safeties when I get the chassis back (students are driving....).

We can turn off the logging, but checking to see if we can turn it on and off based on whether the devices are present.

I think a getVersion for the CAN Talon will work to detect it's presence or absence (if I see 0.0, I know it's not there). No documented way to check for presence or version on the PDB.
  #4   Spotlight this post!  
Unread 23-01-2016, 14:56
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: How to detect missing CAN devices from Java?

took the safeties off; they were the cause of the stutter. Will try to figure out how keep them from getting activated....
  #5   Spotlight this post!  
Unread 23-01-2016, 15:17
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 544
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: How to detect missing CAN devices from Java?

If the robot drive is stuttering your robot, what cycle rate are you running your code at? If its below 60hz robot drive is probably just getting angry for you updating too slowly. Go up to 60hz (0.016sec delay minimum) and see if that helps with safety enabled. If not, dump the system time at the beginning and end of teleopPeriodic to see how long it takes to process all of your motor commands. Motor safeties aren't a bad thing.
__________________
FRC Team 1684 - Head Programmer (2013-2016)
FRC Team 5460 - Programming Mentor (2015-2016)

FIRST in Michigan - Technical Crew (2015-continuing)
  #6   Spotlight this post!  
Unread 23-01-2016, 15:24
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: How to detect missing CAN devices from Java?

how do I change my rate? I thought periodic fired off whenever we got a packet from the DS?
  #7   Spotlight this post!  
Unread 23-01-2016, 15:28
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: How to detect missing CAN devices from Java?

...and getting back to the original question: the problem goes away if I can quiet down the console....
  #8   Spotlight this post!  
Unread 23-01-2016, 15:42
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 544
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: How to detect missing CAN devices from Java?

If you're running IterativeRobot than the time between teleopPeriodic() calls should be fine. I don't have any access to your code so it is a slight possibility that you have some stalling code... Are you doing any camera processing iteratively by any chance?
__________________
FRC Team 1684 - Head Programmer (2013-2016)
FRC Team 5460 - Programming Mentor (2015-2016)

FIRST in Michigan - Technical Crew (2015-continuing)

Last edited by Arhowk : 23-01-2016 at 15:45.
  #9   Spotlight this post!  
Unread 23-01-2016, 17:13
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: How to detect missing CAN devices from Java?

yes, I have stalling code; I'm trying to avoid it. Getting data from missing CAN devices seems to chew up time, which is why I was looking for a way to detect missing CAN devices so I can avoid trying to access them if they are missing.

Attached is a demo project that shows the issue. The code to fetch data from the CANTalon can take upwards of 100ms if the CANTalon is missing, which will definitely cause the watchdog to get grumpy.

Omar, while the code to fetch is non-blocking, is it possible that the error handling and reporting code does block (or is slow)?
Attached Files
File Type: java Robot.java (1.8 KB, 10 views)

Last edited by fovea1959 : 23-01-2016 at 17:52.
  #10   Spotlight this post!  
Unread 24-01-2016, 16:33
ozrien's Avatar
ozrien ozrien is offline
Omar Zrien
AKA: Omar
no team
Team Role: Mentor
 
Join Date: Sep 2006
Rookie Year: 2003
Location: Sterling Heights, MI
Posts: 549
ozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond reputeozrien has a reputation beyond repute
Re: How to detect missing CAN devices from Java?

I can reproduce the issue by simply calling lots of DriverStation.ReportError (no CAN devices whatsoever).

The cause of the delays appear to be the large amount of strings passed into standard error stream (stderr).

A similar issue can be reproduced in C++ due to the large amount of strings pass into standard output stream (cout).

After killing these two in WPILIB, I have reran your example and had good-rapid-control of the robot, even though the DS racked up lots of error messages.

I wrote up a tracker for this issue, but my recommendation is to not design your robot to produce tons of error information in its typical use. So if disconnecting a camera, i2c device, spi device, CAN device, etc. has the result of creating frequent DS messages (and therefore stderr/stdout streams) turn off the object in your code.

Doug I put together a quick class that will find any CTRE CAN devices on the bus so you can better switch between your test bed and robot. Create an object of this class type and call Find() at the beginning of your code. Use it's output to determine if you should even bother polling out data from you CAN devices.

You could try using the getters you have now, but typically if a device "disappears" the getters will return the last good value and report an error to the DS, so they may not be best to serve your purposes.

Code:
package org.usfirst.frc.team3620.robot;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;

import edu.wpi.first.wpilibj.can.CANJNI;

public class DeviceFinder {

	private ByteBuffer targetID = ByteBuffer.allocateDirect(4);
	private ByteBuffer timeStamp = ByteBuffer.allocateDirect(4);

	/** helper routine to get last received message for a given ID */
	private long checkMessage(int fullId, int deviceID) {
		try {
			targetID.clear();
			targetID.order(ByteOrder.LITTLE_ENDIAN);
			targetID.asIntBuffer().put(0,fullId|deviceID);

			timeStamp.clear();
			timeStamp.order(ByteOrder.LITTLE_ENDIAN);
			timeStamp.asIntBuffer().put(0,0x00000000);
			
			CANJNI.FRCNetworkCommunicationCANSessionMuxReceiveMessage(targetID.asIntBuffer(), 0x1fffffff, timeStamp);
		
			long retval = timeStamp.getInt();
			retval &= 0xFFFFFFFF; /* undo sign-extension */ 
			return retval;
		} catch (Exception e) {
			return -1;
		}
	}
	/** polls for received framing to determine if a device is present.
	 *   This is meant to be used once initially (and not periodically) since 
	 *   this steals cached messages from the robot API.
	 * @return ArrayList of strings holding the names of devices we've found.
	 */
	public ArrayList<String> Find() {
		ArrayList<String> retval = new ArrayList<String>();

		/* get timestamp0 for each device */
		long pdp0_timeStamp0; // only look for PDP at '0'
		long []pcm_timeStamp0 = new long[63];
		long []srx_timeStamp0 = new long[63];
		
		pdp0_timeStamp0 = checkMessage(0x08041400,0);
		for(int i=0;i<63;++i) {
			pcm_timeStamp0[i] = checkMessage(0x09041400, i);
			srx_timeStamp0[i] = checkMessage(0x02041400, i);
		}

		/* wait 200ms */
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		/* get timestamp1 for each device */
		long pdp0_timeStamp1; // only look for PDP at '0'
		long []pcm_timeStamp1 = new long[63];
		long []srx_timeStamp1 = new long[63];
		
		pdp0_timeStamp1 = checkMessage(0x08041400,0);
		for(int i=0;i<63;++i) {
			pcm_timeStamp1[i] = checkMessage(0x09041400, i);
			srx_timeStamp1[i] = checkMessage(0x02041400, i);
		}

		/* compare, if timestamp0 is good and timestamp1 is good, and they are different, device is healthy */
		if( pdp0_timeStamp0>=0 && pdp0_timeStamp1>=0 && pdp0_timeStamp0!=pdp0_timeStamp1)
			retval.add("PDP 0");

		for(int i=0;i<63;++i) {
			if( pcm_timeStamp0[i]>=0 && pcm_timeStamp1[i]>=0 && pcm_timeStamp0[i]!=pcm_timeStamp1[i])
				retval.add("PCM " + i);
			if( srx_timeStamp0[i]>=0 && srx_timeStamp1[i]>=0 && srx_timeStamp0[i]!=srx_timeStamp1[i])
				retval.add("SRX " + i);
		}
		return retval;
	}
}
  #11   Spotlight this post!  
Unread 24-01-2016, 20:02
fovea1959's Avatar
fovea1959 fovea1959 is offline
Herder of programmers
AKA: Doug Wegscheid
FRC #3620 (The Average Joes)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2011
Location: St Joseph
Posts: 336
fovea1959 will become famous soon enough
Re: How to detect missing CAN devices from Java?

thanks for digging into this and doing the analysis (and providing the device-detection code). We'll use this to avoid polling non-existent CAN hardware; we still need to dig into non-existent cameras...

This is our first experience with moving our closed-loop control out to a controller. CTRE's documentation and online support are making this a positive learning experience for our students....
  #12   Spotlight this post!  
Unread 28-01-2016, 12:53
Crossle86's Avatar
Crossle86 Crossle86 is offline
Software Engineer
AKA: Richard Corn
FRC #4450 (Olympia Robotics Federation)
Team Role: Mentor
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Olympia, WA.
Posts: 93
Crossle86 is an unknown quantity at this point
Re: How to detect missing CAN devices from Java?

Quote:
Originally Posted by fovea1959 View Post
thanks for digging into this and doing the analysis (and providing the device-detection code). We'll use this to avoid polling non-existent CAN hardware; we still need to dig into non-existent cameras...

This is our first experience with moving our closed-loop control out to a controller. CTRE's documentation and online support are making this a positive learning experience for our students....
Same here on programmatically getting a list of the cameras recognized by the RoboRio.
Closed Thread


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 03:13.

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