Go to Post Just that the robot battery wont catch fire if you drop it the wrong way. Those LiPos pack quite a punch. - dellagd [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 01-27-2016, 08:42 PM
ljw9801055 ljw9801055 is offline
Registered User
FRC #1635
 
Join Date: Jan 2016
Location: China
Posts: 2
ljw9801055 is an unknown quantity at this point
FRC Sonar Reading Delays

Hi all,

I'm the programmer from Team 1635. Recently I've been programming our sonar sensor to measure the distance to a target. However, when an initially moving robot stops, the sonar always has a 1 second delay before it returns the correct distance. Are there any ways to compensate for the delay? Any help will be much appreciated.
  #2   Spotlight this post!  
Unread 01-28-2016, 12:06 AM
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy (Most people call me Palardy)
VRC #3333
Team Role: College Student
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Auburn Hills, MI
Posts: 1,347
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: FRC Sonar Reading Delays

Post some code?

What type of sonar sensor is it, and how it it configured in software?

Some output an analog voltage. These should be pretty clear and easy to use with an AnalogInput.

Some have two digital lines, one pulsed to transmit and one pulsed when the sonar receives a ping back. You have to pulse the TX pin and measure the time before you get a response on the RX pin. This can take time, and if your timeout is very long (e.g. a second) and the response pulse is either missed or lost then your code would wait for a very long time. My guess is this particular condition causes a pulse to be lost, but the RoboRIO is waiting for a pulse for a second before giving up on it.

Another option is that something else in the same thread is blocking, and preventing the sonar code from executing.
__________________
Kettering University - Computer Engineering
Kettering Motorsports
Williams International - Commercial Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack
  #3   Spotlight this post!  
Unread 01-28-2016, 10:23 PM
ljw9801055 ljw9801055 is offline
Registered User
FRC #1635
 
Join Date: Jan 2016
Location: China
Posts: 2
ljw9801055 is an unknown quantity at this point
Re: FRC Sonar Reading Delays

here's our code.
We're using the analog sonar from MaxBotix

package org.usfirst.frc.team1635.robot.subsystems;

import org.usfirst.frc.team1635.robot.RobotMap;
import org.usfirst.frc.team1635.robot.commands.DriveWithJ oystick;

import NavxMXP.AHRS;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DigitalOutput;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.RobotDrive.MotorType;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboar d;
import edu.wpi.first.wpilibj.Ultrasonic;

/**
* @author Xxx_MrProLi_xxX & _Pussyslayer_Miguel & Simranj69t
*
*
*/
public class DriveTrain extends Subsystem {

RobotDrive robotDrive;
Joystick stick;
// int frontLeft, frontRight, rearLeft, rearRight;
private SpeedController frontLeft, backLeft, frontRight, backRight;
boolean onTarget;

SerialPort serial_port;

// IMU imu; // This class can be used w/nav6 and navX MXP.
// IMUAdvanced imu; // This class can be used w/nav6 and navX MXP.
AHRS imu; // This class can only be used w/the navX MXP.
boolean first_iteration;

// Ultrasonic sonar;

AnalogInput sonar;
double DistanceToStop;
double Degrees;

// analog

// Put methods for controlling this subsystem
// here. Call these from Commands.
public DriveTrain() {
super();

// DigitalInput echoChannel;
// DigitalInput pingChannel;

// sonar = new Ultrasonic(0, 1);
sonar = new AnalogInput(0);

// frontLeft = RobotMap.frontLeftChannel;
// frontRight = RobotMap.frontRightChannel;
// rearLeft = RobotMap.rearLeftChannel;
// rearRight = RobotMap.rearRightChannel;

frontLeft = new Victor(RobotMap.frontLeftChannel);
backLeft = new Victor(RobotMap.rearLeftChannel);
frontRight = new Victor(RobotMap.frontRightChannel);
backRight = new Victor(RobotMap.rearRightChannel);

robotDrive = new RobotDrive(frontLeft, backLeft, frontRight, backRight);
robotDrive.setExpiration(0.1);

robotDrive.setInvertedMotor(MotorType.kFrontRight, true); // invert the
// left side
// motors
robotDrive.setInvertedMotor(MotorType.kRearRight, true);
// robotDrive.setInvertedMotor(MotorType.kRearLeft, true);

try {
serial_port = new SerialPort(57600, SerialPort.Port.kMXP);

byte update_rate_hz = 50;
// imu = new IMU(serial_port,update_rate_hz);
// imu = new IMUAdvanced(serial_port,update_rate_hz);
imu = new AHRS(serial_port, update_rate_hz);
} catch (Exception ex) {
ex.printStackTrace();
}

if (imu != null) {
LiveWindow.addSensor("IMU", "Gyro", imu);
}
first_iteration = true;

// When calibration has completed, zero the yaw
// Calibration is complete approaximately 20 seconds
// after the robot is powered on. During calibration,
// the robot should be still

boolean is_calibrating = imu.isCalibrating();
if (first_iteration && !is_calibrating) {
Timer.delay(0.3);
imu.zeroYaw();
first_iteration = false;
}

}

/*
* to obtain the gyro value in degrees
*/
public double obtainYaw() {
return imu.getYaw();

}

public float convertToFarenheit() {
float celcius = imu.getTempC();
float farenheit = (float) (celcius * 1.8 + 32);
return farenheit;

}

public void log() {
SmartDashboard.putNumber("Gyro", obtainYaw());
SmartDashboard.putNumber("Temperature", convertToFarenheit());
SmartDashboard.putNumber("FrontLeftMotorSpd", frontLeft.get());
SmartDashboard.putNumber("FrontRightMotorSpd", frontRight.get());
SmartDashboard.putNumber("RearLeftMotorSpd", backLeft.get());
SmartDashboard.putNumber("RearRightMotorSpd", backRight.get());
SmartDashboard.putNumber("DistanceSonar", getDistanceSonar());
// SmartDashboard.putNumber("", getDistanceSonar());

}

// codes for the mecanum drive
public void mecanumDrive(Joystick joy) {
//robotDrive.mecanumDrive_Cartesian(-joy.getX() * 0.6, -joy.getY() * 0.6, -joy.getRawAxis(3) * 0.6, 0);
robotDrive.mecanumDrive_Cartesian(-joy.getX()*3 , -joy.getY()*3, -joy.getRawAxis(3)*3, 0);
// robotDrive.mecanumDrive_Cartesian(-joy.getX() * 0.6, -joy.getY() *
// 0.6,
// 0, 0);
SmartDashboard.putNumber("Xdirection_values", joy.getX());
SmartDashboard.putNumber("rotation_xxx_value", joy.getRawAxis(3));

}

public void mecanumFixed(Joystick joy) {
if (joy.getX() != 0 && joy.getY() == 0 && joy.getRawAxis(3) == 0) {
if (joy.getX() < 0) {
robotDrive.mecanumDrive_Cartesian(-joy.getX() * 0.6, 0, 0, 0);
backLeft.set(frontLeft.get() * 2);
frontRight.set(frontLeft.get() * 2);

// frontRight.set(speed);
// frontLeft.set(speed);
// backLeft.set(speed);
// backleft.set
} else if (joy.getX() > 0) {
robotDrive.mecanumDrive_Cartesian(0, -joy.getY() * 0.6, -joy.getRawAxis(3) * 0.6, 0);
backLeft.set(frontLeft.get() * 2);
frontRight.set(frontLeft.get() * 2);

}

} else if (joy.getX() == 0) {
robotDrive.mecanumDrive_Cartesian(-joy.getX() * 0.6, -joy.getY() * 0.6, -joy.getRawAxis(3) * 0.6, 0);

}

}

/*
*
* this code is designed to obtain the distance from the
* xxx_MLG_NEXus_LI_XXX_#360n0SC()P3
*/
public double getDistanceSonar() {// sonar is weird: it has a deadzone of 12
// inches; distance will always be 0
// within the deadzone
// double distance = sonar.getRangeInches();
// double distance = sonar.getValue();

//double valueToInches = 0.125;
double valueToInches_2 = 1 / 20.5;// 14.45
double distanceX = sonar.getAverageValue();
double distance = (distanceX - 237) * valueToInches_2 + 12; // convert
// voltage
// into
// inches
int distanceInt=(int) distance;
return distanceInt;

}

public void setDistToStop(double dist_) {
this.DistanceToStop = dist_;
}

public void stopRobotAtDistance() {

double distanceObtained;

// getDistanceSonar()= 0 ;
distanceObtained = getDistanceSonar();

if (distanceObtained <= DistanceToStop) {
robotDrive.mecanumDrive_Cartesian(0, 0, 0, 0);
onTarget = true;
System.out.println(" is robot finished " + true);
} else {
// Timer.delay(0.05);// compensate for sonar reading delays
robotDrive.mecanumDrive_Cartesian(0, 0.2, 0, 0);
// xxx_delayMecanum(0, 0.2, 0, 0);
System.out.println(" is robot finished " + false);
}
}

public void mecanumWithParameters(double x_axis, double y_axis, double rotaion) {
robotDrive.mecanumDrive_Cartesian(-x_axis * 0.6, -y_axis * 0.6, -rotaion * 0.6, 0);

}

public void initDefaultCommand() {
this.setDefaultCommand(new DriveWithJoystick());

// Set the default command for a subsystem here.
// setDefaultCommand(new MySpecialCommand());
}

public boolean isOnTarget() {
return onTarget;
}

public void reset() {
imu.zeroYaw();
//sonar.resetAccumulator();
onTarget = false;
// getDistanceSonar()=0;

}

public void stop() {
robotDrive.mecanumDrive_Cartesian(0, 0, 0, 0);
reset();
}
  #4   Spotlight this post!  
Unread 01-28-2016, 11:10 PM
jajabinx124's Avatar
jajabinx124 jajabinx124 is offline
Team 2052 Alumni
AKA: Kshitij Wavre
no team
Team Role: College Student
 
Join Date: Apr 2014
Rookie Year: 2013
Location: Madison, WI
Posts: 534
jajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond repute
Re: FRC Sonar Reading Delays

This may not be it, but I notice you guys are using getAverageValue() which generates the average values the analog sensor is detecting at that very moment.. maybe you guys should try and change that to getValue() (which gets the current value) My hunch is maybe there is a 1 second delay because the sensor is computing the average value instead of just spitting out the current value. (the current value will oscillate, but there might not be a 1 second delay)
__________________
FRC Volunteer CSA (MRI off-season event, 2017 Lake Superior Regional, 2017 10,000 Lakes Regional) 2016 - Present
FRC 2052 KnightKrawler (Team Captain, Strategist, Scouting, Programming) 2013 - 2016

1 Division Win & Einstein Appearance
3 Division Quarter-Finalists
1 Regional Chairman's Award
5 Regional Wins, 3 Regional Finalists
3 MN State Championship Wins, 1 MN State Championship Finalist
Thanks to all our alliance partners who krawled with us: 41, 70, 225, 525, 1595, 2054, 2062, 2122, 2175, 2227, 2472, 2526, 2883, 2990, 3018, 3244, 3276, 3310, 3313, 3360, 3538, 3692, 4011, 4198, 4536, 4607, 4778, 5172, 5690
  #5   Spotlight this post!  
Unread 01-29-2016, 08:14 PM
Sky Captain's Avatar
Sky Captain Sky Captain is offline
Lead Software Mentor
AKA: Will
FRC #0386 (Voltage)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2006
Location: Virginia
Posts: 18
Sky Captain is an unknown quantity at this point
Send a message via AIM to Sky Captain
Re: FRC Sonar Reading Delays

Two things, if you're posting on CD, maybe have a little class and change your username in the comments in the top area of the code. Second, post your code in between code tags. Code tags are [ CODE ] [/ CODE], except don't put spaces between the brackets and the word code. This formats it properly and makes it much easier to read.
__________________
When in doubt, check the documentation.

Last edited by Sky Captain : 01-29-2016 at 08:16 PM.
  #6   Spotlight this post!  
Unread 01-29-2016, 08:47 PM
kevin.li.rit's Avatar
kevin.li.rit kevin.li.rit is offline
Imaginary Friend
AKA: Kevin Li
FRC #0596 (SciClones)
Team Role: Student
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Hopkinton, Massachusetts
Posts: 936
kevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond reputekevin.li.rit has a reputation beyond repute
Send a message via Yahoo to kevin.li.rit
Re: FRC Sonar Reading Delays

Quote:
Originally Posted by Sky Captain View Post
Two things, if you're posting on CD, maybe have a little class and change your username in the comments in the top area of the code. Second, post your code in between code tags. Code tags are [ CODE ] [/ CODE], except don't put spaces between the brackets and the word code. This formats it properly and makes it much easier to read.
You should remove the comments from the code to save weight on the robot anyways.
__________________
Kevin Li

596 - Sciclones
1405 - Finney Falcons
2262 - Holliston Panthers
  #7   Spotlight this post!  
Unread 01-29-2016, 08:54 PM
jajabinx124's Avatar
jajabinx124 jajabinx124 is offline
Team 2052 Alumni
AKA: Kshitij Wavre
no team
Team Role: College Student
 
Join Date: Apr 2014
Rookie Year: 2013
Location: Madison, WI
Posts: 534
jajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond repute
Re: FRC Sonar Reading Delays

Quote:
Originally Posted by kevin.li.rit View Post
You should remove the comments from the code to save weight on the robot anyways.
^
Yep. Best way to save weight.
__________________
FRC Volunteer CSA (MRI off-season event, 2017 Lake Superior Regional, 2017 10,000 Lakes Regional) 2016 - Present
FRC 2052 KnightKrawler (Team Captain, Strategist, Scouting, Programming) 2013 - 2016

1 Division Win & Einstein Appearance
3 Division Quarter-Finalists
1 Regional Chairman's Award
5 Regional Wins, 3 Regional Finalists
3 MN State Championship Wins, 1 MN State Championship Finalist
Thanks to all our alliance partners who krawled with us: 41, 70, 225, 525, 1595, 2054, 2062, 2122, 2175, 2227, 2472, 2526, 2883, 2990, 3018, 3244, 3276, 3310, 3313, 3360, 3538, 3692, 4011, 4198, 4536, 4607, 4778, 5172, 5690
  #8   Spotlight this post!  
Unread 01-29-2016, 09:04 PM
jajabinx124's Avatar
jajabinx124 jajabinx124 is offline
Team 2052 Alumni
AKA: Kshitij Wavre
no team
Team Role: College Student
 
Join Date: Apr 2014
Rookie Year: 2013
Location: Madison, WI
Posts: 534
jajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond reputejajabinx124 has a reputation beyond repute
Re: FRC Sonar Reading Delays

Also to readdress the question let me define the method getAverageValue() as it is stated in the latest FRC api.

public int getAverageValue():

Gets a sample from the output of the oversample and average engine for this channel. The sample is 12-bit + the bits configured in SetOversampleBits(). The value configured in setAverageBits() will cause this value to be averaged 2^bits number of samples. This is not a sliding window. The sample will not change until 2^(OversampleBits + AverageBits) samples have been acquired from this channel. Use getAverageVoltage() to get the analog value in calibrated units.

Method Returns:
A sample from the oversample and average engine for this channel.

So in this case, the output is having a 1 second delay because the sample will not change until 2^(OversampleBits + AverageBits) have been acquired.

Hope this answers your question!
__________________
FRC Volunteer CSA (MRI off-season event, 2017 Lake Superior Regional, 2017 10,000 Lakes Regional) 2016 - Present
FRC 2052 KnightKrawler (Team Captain, Strategist, Scouting, Programming) 2013 - 2016

1 Division Win & Einstein Appearance
3 Division Quarter-Finalists
1 Regional Chairman's Award
5 Regional Wins, 3 Regional Finalists
3 MN State Championship Wins, 1 MN State Championship Finalist
Thanks to all our alliance partners who krawled with us: 41, 70, 225, 525, 1595, 2054, 2062, 2122, 2175, 2227, 2472, 2526, 2883, 2990, 3018, 3244, 3276, 3310, 3313, 3360, 3538, 3692, 4011, 4198, 4536, 4607, 4778, 5172, 5690
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 06:58 PM.

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