Ultrasonic analog input?

We have ultrasonic analog input, wired based on a youtube video.

I however don’t understand the parameters of Ultrasonic namehere=new Ultrasonic(a, b);

a is input, what does that mean?
b is output, what does that mean?

I tried 8, 7 and tried with the ultrasound in both analog 8 and analog 7, with the following code:


if(namehere.getRangeMM<10)
{
drive.tankDrive(1, 1);
}

I know drive.tankDrive(1, 1); works fine, as that works based on joysticks. What am I doing wrong?

Also are their good java tutorials for cameras/encoders/solenoid magnetic sensor thingies anyone know them? (By “good” I mean that it explains the api and the parameters. for example: this parameter means you have to wire the output to <analog/DIO/pwm/other> and it will reference that slot.)

We have had more luck with an AnalogChannel object instead of Ultrasonic. Then the getAverageVoltage method returns a value between 0-5v which you can use a scale factor to translate into a distance. We found the scale factor on the stats sheet that came with the rangefinder.

Thanks, tried that. It didn’t work. I tried distance.getAverageVoltage()<=1 and distance.getAverageVoltage()*9.8<=6 but neither of those worked.

Am I doing something wrong? Using the slot 1 because it won’t let me use slot 8. AnalogChannel distance=new AnalogChannel(1);

Could you post the code to see what else there might be? Also, have you tried printing out the value from the distance.getAverageVoltage() method to make sure the hardware is working properly?
something like:

System.out.println(distance.getAverageVoltage());

It should print out the value in the IDE, or you could use NetConsole to read the values.

It’s always giving a value of 2.99 or 3 (approximately.), I think it is a hardware problem, but will post rest of code tonight if I remember a memory stick.

Also having trouble stopping a piston partway through, tried setting both solenoids to true, and both to false.

here is code:


/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.                             */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package edu.wpi.first.wpilibj.templates;


import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.Watchdog;
import edu.wpi.first.wpilibj.AnalogChannel;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the SimpleRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class team4692robot extends SimpleRobot 
{
    RobotDrive drive=new RobotDrive(1, 2);
    Joystick left=new Joystick(1);
    Joystick right=new Joystick(2);
    Compressor airCompressor=new Compressor(1, 1);
    Solenoid launchopen=new Solenoid(1);
    Solenoid launchclose=new Solenoid(2);
    Joystick gamepad=new Joystick(3);
    Talon winch=new Talon(3);
    DigitalInput winchstopper=new DigitalInput(2);
    DigitalInput winchstarter=new DigitalInput(3);
    AnalogChannel distance=new AnalogChannel(1);
    Solenoid extendoropen=new Solenoid(3);
    Solenoid extendorclose=new Solenoid(4);
    
    /**
     * This function is called once each time the robot enters autonomous mode.
     */
    public void autonomous() 
    {
        int autotimer=0;
        
        airCompressor.start();
        
        while(true && isAutonomous() && isEnabled())
        {
            if(autotimer<100)
            {
                drive.tankDrive(.5, -.5);
            }
            else
            {
                drive.tankDrive(0, 0);
            }
            
            autotimer++;
            Timer.delay(.005);
        }
    }

    /**
     * This function is called once each time the robot enters operator control.
     */
    public void operatorControl() 
    {
        boolean winching=false;
        
        airCompressor.start();
        
        while(true && isOperatorControl() && isEnabled())
        {
            double leftpow=left.getY(); double rightpow=right.getY();
            double powermod=.7;
            if(left.getTrigger())
            {
                powermod+=.1;
            }
            if(right.getTrigger())
            {
                powermod+=.2;
            }
            leftpow*=powermod; rightpow*=powermod;
            
            drive.tankDrive(leftpow, rightpow); 
            
            if(gamepad.getRawButton(1) && winching==false && winchstarter.get())
            {
                winching=true;
            }
            else if(winching==true)
            {
                winch.set(1);
                if(winchstopper.get())
                {
                    winching=false;
                    winch.set(0);
                }
            }
            
            winch.set(gamepad.getRawAxis(1));
            
            launchopen.set(!gamepad.getRawButton(2));
            launchclose.set(gamepad.getRawButton(2));
            
            if(gamepad.getRawButton(3))
            {
                extendoropen.set(true);
                extendorclose.set(false);
            }
            if(gamepad.getRawButton(4))
            {
                extendoropen.set(false);
                extendorclose.set(true);
            }
            
            if(!gamepad.getRawButton(3) && !gamepad.getRawButton(4))
            {
                extendoropen.set(true);
                extendorclose.set(true);
            }
                    
            System.out.println(distance.getAverageVoltage());
            if(distance.getAverageVoltage()<=1)
            {
                drive.tankDrive(.5, .5);
            }
            
            Timer.delay(.005);
        }
    }
    
    /**
     * This function is called once each time the robot enters test mode.
     */
    public void test() 
    {
    
    }
    
    public void robotInit()
    {
        drive.setSafetyEnabled(false);
        Watchdog.getInstance().kill();
    }
}

/*
double left=y; double right=y;
if(x>0)
{
    left+=x;
}
if(x<0)
{
    right+=x;
}
drive.tankDrive(left, right);
*/

With an Ultrasonic sensor, you need to either tell it to ping, or set it to automatic ping mode. Otherwise it will read maximum distance.

Add this to your robotInit():

distance.setAutomaticMode(true);

Hello all,

1st year mentoring, and boy did I underestimate the time it would take for me to come up to speed on Java. :eek:

Anyway, I had the team order a MaxBotix MB7366 http://www.maxbotix.com/Ultrasonic_Sensors/MB7366.htm I speced this one as it has the Free Reading mode which will ignore nearly all other ultrasonic sensors that may be in use on the field. The sensor offers analog voltage, PWM & serial outputs. For better signal/ratio ration I want to use the PWM output, but is there an input in this year’s cRIO that will take that PWM input? If not, is there a card that will work with it (preferable a mixed I/O type card so as to not buy 1 card just for 1 PWN input)?

Thanks ::ouch::

Doesn’t work, no function setAutomaticMode with parameter boolean found in analog class.
EDIT: not exact words, but general meaning.

Blah. I had it wired in backwards. for anyone in the future: Black wires go towards the main part of the crio.

The setAutomaticMode is a function of the Ultrasonic class, not the AnalogChannel class. The Ultrasonic class makes using the sensors much simpler.
Glad you got yours going though!