Go to Post We could not make weight so we got rid of the cRIO. - Mike Copioli [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, 22:29
MRT45's Avatar
MRT45 MRT45 is offline
Registered User
FRC #0253 (Mills Robotics Team)
Team Role: Programmer
 
Join Date: Feb 2014
Rookie Year: 2013
Location: CA
Posts: 17
MRT45 is an unknown quantity at this point
Cannot program two ultrasonic sensors. Please help us!

Hey everyone,

So my team followed the documentation WPI to code a single ultrasonic to the smartdashboard.

We did that with success, but now find ourselves with a problem.

We want two to show, but for some reason... The console throws an "Illegal exception" error every time we now push code. (Which means no robot code on the driverstation)

It appears that the "error" conflicts on the lines where our naming conventions are the same, but we clearly put "ultra1" and "ultra2" to be different variables.

Furthermore, we looked into another solution that said it may be the setAutomaticMode being used twice and causing the problem...

We removed that in our second sensor class, but that did not help either.

Any suggestions help! We need this issue fixed asap.

Here is the code:
Code:
package org.usfirst.frc253.Code2016.commands;

import com.ni.vision.NIVision.Image;
import edu.wpi.first.wpilibj.Ultrasonic;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
i

/**
 *
 */
public class Ultrasonic1 extends Command {
	
	Ultrasonic ultra1;
    int session;
    Image frame;
    AxisCamera camera;
    public Ultrasonic1() {
    	
    	ultra1 = new Ultrasonic(0,1);
    	ultra1.setAutomaticMode(true);
    	// turns on automatic mode
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
    	
     		//accelerationX = accel.getX();
     		//accelerationY = accel.getY();
     		//accelerationZ = accel.getZ();
     		 double range1 = ultra1.getRangeInches();
     		//String stringTEST = ultra.getSmartDashboardType();
     		
         //System.out.println(accelerationX + " " + accelerationY + " " + accelerationZ + ".");
 		//SmartDashboard.putNumber("Accelometer X", accel.getX());
 		//SmartDashboard.putNumber("Accelometer Y", accel.getY());
 		//SmartDashboard.putNumber("Accelometer Z", accel.getZ());
 		SmartDashboard.putNumber("Ultrasonic", range1);
 		  
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return false;
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }
}

Ultrasonic Sensor 2:
Code:
package org.usfirst.frc253.Code2016.commands;

import com.ni.vision.NIVision.Image;
import edu.wpi.first.wpilibj.Ultrasonic;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.vision.AxisCamera;

/**
 *
 */
public class Ultrasonic2 extends Command {
	
	Ultrasonic ultra2;
    int session;
    Image frame;
    AxisCamera camera;
    public Ultrasonic2() {
    	
    	ultra2 = new Ultrasonic(2,3);

    	// turns on automatic mode
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
    	
     		//accelerationX = accel.getX();
     		//accelerationY = accel.getY();
     		//accelerationZ = accel.getZ();
     		 double range2 = ultra2.getRangeInches();
     		//String stringTEST = ultra.getSmartDashboardType();
     		
         //System.out.println(accelerationX + " " + accelerationY + " " + accelerationZ + ".");
 		//SmartDashboard.putNumber("Accelometer X", accel.getX());
 		//SmartDashboard.putNumber("Accelometer Y", accel.getY());
 		//SmartDashboard.putNumber("Accelometer Z", accel.getZ());
 		SmartDashboard.putNumber("Ultrasonic 2", range2);
 		  
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return false;
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }
}
Reply With Quote
  #2   Spotlight this post!  
Unread 26-01-2017, 04:49
Auxiliatrix Auxiliatrix is offline
Registered User
FRC #0604
 
Join Date: Jan 2017
Location: San Jose, California
Posts: 2
Auxiliatrix is an unknown quantity at this point
Re: Cannot program two ultrasonic sensors. Please help us!

Hi, I'm programming something similar for our team's robot. For your code, I'm not sure if this was intentional or not, but in the Ultrasonic2 class default constructor, you don't seem to turn on automatic mode? However, I don't think that should be an issue.

Here are some possible ideas:

Automatic mode means that the sensor is continuously reading, correct? It's possible that having two ultrasonic sensors would cause interference with each other, if that's what's causing the error. If this were the issue, you should try implementing them without automatic mode (getting them to ping only when prompted).

However, if you cannot find a solution to this specific code, might I suggest implementing your sensors as analog inputs? The Ultrasonic class is, after all, an extension of the AnalogInput class, and what our team has been doing is treating the sensors as basic analog inputs, taking the readings, and then multiplying the number by some constant to retrieve the data in inches.
In this case, the implementation would be nearly identical to that of encoders, in terms of reading data and putting it onto the dashboard.

I hope some of this helps.
Reply With Quote
  #3   Spotlight this post!  
Unread 26-01-2017, 06:34
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,687
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: Cannot program two ultrasonic sensors. Please help us!

The only difference other than the variable names and the arguments to the constructor that I see is the import of AxisCamera in the second case. I suggest commenting out the import and the camera and frame declarations from both classes, as you aren't using them.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #4   Spotlight this post!  
Unread 26-01-2017, 08:17
SamCarlberg's Avatar
SamCarlberg SamCarlberg is online now
GRIP, WPILib. 2084 alum
FRC #2084
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 136
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: Cannot program two ultrasonic sensors. Please help us!

You're creating new sensors every time you create new Ultra1 or Ultra 2 commands. That's what's causing the problem.

With WPILib, it's assumed that your sensors don't change during a match so whenever you create a new sensor, it will check if another sensor object has already used those channels. Your sensor variables should be in the appropriate subsystems. Look at the GearsBot example program in Eclipse to see how you should structure your code
__________________
WPILib
GRIP, RobotBuilder
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:16.

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