FRC_Hall sensor question for bosch motor (about java program)


#1

Hi,I have some question here.

i want to use the hall sensor in bosch motor.(FRC,robotRIO)


(it only have one channel)

I saw “hall effect is only giving counts” from my other topic.
but when i try to use them(Counter’s command from “ edu.wpi.first.wpilibj.Counter ”,the the program will have some bug.
they made the robot stop working.

if i use the wrong command?
or I should use other command to made it works? Where can i find them?


Encoder sourceA sourceB inputs?
#2

https://wpilib.screenstepslive.com/s/3120/m/7912/l/85635-using-counters

Using normal mode setup, I suspect.

And then getting the counts.


#3

i still cannot use it
this is my program

//============================================
package frc.robot;

import java.sql.Time;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import com.ctre.phoenix.motorcontrol.can.VictorSPX;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Joystick.AxisType;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
import edu.wpi.first.wpilibj.SendableBase;
import edu.wpi.first.wpilibj.Counter;
import edu.wpi.first.wpilibj.Solenoid;

public class Robot extends TimedRobot
{
private Timer timer;
public static Subsystem m_subsystem;
private WPI_VictorSPX LMotor;
private WPI_VictorSPX RMotor;
private DifferentialDrive myRobot;
private Joystick stick;
private Compressor compressor;
private ADXRS450_Gyro gyro;
private Counter counter= new Counter(1);
private WPI_VictorSPX hand=new WPI_VictorSPX(4);
private Solenoid sol0=new Solenoid(0);
private Solenoid sol1=new Solenoid(1);
int handtype;
double lmadd;
double rmadd;
double p=0.001;
@Override
public void robotInit()
{
LMotor = new WPI_VictorSPX(6);
RMotor = new WPI_VictorSPX(7);
stick = new Joystick(0);
compressor = new Compressor(1);
LMotor.setInverted(false);
RMotor.setInverted(false);
timer =new Timer();
gyro = new ADXRS450_Gyro();
myRobot = new DifferentialDrive(LMotor, RMotor);
counter.setUpSource(1);
counter.setUpDownCounterMode();

}
@Override
public void autonomousInit()
{
}

@Override
public void autonomousPeriodic()
{
}
@Override
public void teleopInit()
{
gyro.reset();
timer.reset();
counter.reset();
}
@Override
public void teleopPeriodic()
{

//compressor
if(compressor.getCompressorCurrent() <=115)
{
compressor.setClosedLoopControl(true);
compressor.start();
}
else
{
compressor.setClosedLoopControl(false);
compressor.close();
}

lmadd=gyro.getAngle()p;
rmadd=lmadd
-1;
myRobot.arcadeDrive(stick.getAxis(AxisType.kY)+lmadd,stick.getAxis(AxisType.kZ)*0.9+rmadd);

if(stick.getRawButton(6))
{
if(handtype==1)
{
hand.set(ControlMode.PercentOutput,1);
timer.delay(1);
handtype=0;
}
else
{
hand.set(ControlMode.PercentOutput,-1);
timer.delay(1);
handtype=1;
}
}
else
{
hand.set(ControlMode.PercentOutput,0);
}
System.out.println(counter.get());
if(stick.getRawButton(1))
{
sol0.set(true);
sol1.set(true);
}
else
{
sol0.set(false);
sol1.set(false);
}
}

@Override
public void testPeriodic()
{

}
}

//===================================

do you know what’s the problem?


#4

Can you describe what the behavior of the robot you are currently seeing is? Or, if you are getting error messages in the driver station Console (or rioLog in VSCode), what are the errors?

One additional experiment to try: comment out all lines of code related to counter. Does the behavior persist? If so, the addition of counter was not the source of your issues.


#5

I have also been struggling with implementing the Bosch motor encoder. when I add the counter to the program it builds and deploys, but crashes upon startup. the error statement is the “robots should not quit, but yours did” when I comment out the counter it starts right up, and I can see the digital input in the livewindow when the motor is running. Ive tried several different ways of invoking the counter from the examples. Hopefully someone with more experience can weight in.


#6

my problem is as same as bstites82’s problem

after deploy it and click Enable in driverstation.

about two second later,it stop working (maybe the program is been crashed)
and it show up “robots should not quit, but yours did

although l hadn’t tried to see the digital input in the livewindow before.

but if i delete all of command about counter,robot can works normaly
so I think it must be the counter’s problem


#7

Correct, that message indicates software execution encountered an error at runtime.

There are other messages before the Robots should not quit line which indicate the type of error and the lines of code on which it occurred. Can you post those?

What I’m looking for in particular is the Stack Trace associated with the crash.


#8

I was able to make some progress tonight, I started un-commenting the counter code one piece at a time and redeploying until I recreated the crash. looking at the error messages it was crashing while initializing the digital inputs. I am using robot builder and command based robot, so I had initially created as a digital input in robot builder, then added the counter in VScode. what happened was the allocations were fighting each other,
Code: -1029. HAL: Resource already allocated"
because my code was:
hall = new DigitalInput(5); <=from robot builder
addChild(“hall”,hall); <= from robot builder
tick = new Counter(5);
so by commenting out the first 2 lines it seemed to work.

@medwin, try commenting out :
counter.setUpSource(1);

since it is the same as :
private Counter counter= new Counter(1);
and see if referencing the same channel twice is causing your issue.


#9

thanks,it works
my problem has been fix.