Log in

View Full Version : motor encoder java


Maxchrist
12-02-2014, 17:15
we are trying to get a magnetic motor encoder (AS5145B from ams) to give values and it only ever gives a value of zero. can someone help us out, we do not know why it isn't reading any pulses/rotations. A sample code would be much appreciated. Thanks

Ether
12-02-2014, 17:18
we are trying to get a magnetic motor encoder (AS5145B from ams) to give values and it only ever gives a value of zero. can someone help us out, we do not know why it isn't reading any pulses/rotations. A sample code would be much appreciated. Thanks

What are you trying to sense (position? speed?) and how do you have it wired?

Joe Ross
12-02-2014, 17:20
Have you followed the directions here: http://wpilib.screenstepslive.com/s/3120/m/8559/l/91404-using-the-as5145b-magnetic-encoder-with-the-frc-control-system

Maxchrist
12-02-2014, 17:25
we are trying to sense position. we have it on an a and b channel plugged into the digital i/0 on port 1 and 2.

Maxchrist
12-02-2014, 17:28
@joe yeah
but we are not 100 percent sure how to program it.

irvingc
12-02-2014, 17:48
we are trying to sense position. we have it on an a and b channel plugged into the digital i/0 on port 1 and 2.

This is not the proper wiring to use the encoder as an absolute sensor (position). Refer to the last section of Joe Ross's link.


[ edit ]
Provided you do want to use it as a quadrature, though, you would first create a new Encoder object:
Encoder encoder = new Encoder(aChannel, bChannel);

Then enable it:
encoder.start();

Then, you would read it like so:
encoder.getDistance();

Depending on the application, you may also want to calibrate the distance per pulse:
encoder.setDistancePerPulse(someConstant);

Maxchrist
12-02-2014, 18:34
@irving that didn't work, I'm not sure whats wrong

notmattlythgoe
13-02-2014, 07:58
@irving that didn't work, I'm not sure whats wrong

You'll need to post code so we can see what you have done.

Ether
13-02-2014, 08:31
You'll need to post code so we can see what you have done.

Also post some pictures showing:

- how you have the encoder mounted

- how you have it wired

(please use camera that can focus close-up)

shmet
17-02-2014, 11:38
I have also been having an issue with the same symptoms. I am using the AS5145B magnetic encoder. We one PWM wire soldered in to the board as well as the signal part of another PWM wire. PWM wire one is soldered to ground, 5v, and a; PWM wire two is soldered to b. PWM wire one is connected to Digital I/O 3 and PWM wire two is connected to Digital I/O 4.
Here is my 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.defaultCode;


import edu.wpi.first.wpilibj.CounterBase.EncodingType;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Watchdog;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Jaguar;
import java.lang.Math;

public class DefaultRobot extends IterativeRobot {
// Declare variable for the robot drive system

private int m_dsPacketsReceivedInCurrentSecond; // keep track of the ds packets received in the current second
private Encoder catapult;
private XboxPaddle xboxController;
// Local variables to count the number of periodic loops performed
private int m_autoPeriodicLoops;
private int m_disabledPeriodicLoops;
private int m_telePeriodicLoops;

private Victor frontLeftDrive;
private Victor frontRightDrive;
private Victor backLeftDrive;
private Victor backRightDrive;
private Jaguar wench;
private Jaguar latch;
private Jaguar intake;
private Gyro gyro;
private double magnitude;
private double direction;
private Vector vector;
private double rotation;

private double axis1;
private double axis2;
private double axis4;
private double axis5;
private double deadZone;
/**
* Constructor for this "BuiltinDefaultCode" Class.
*
* The constructor creates all of the objects used for the different inputs and outputs of
* the robot. Essentially, the constructor defines the input/output mapping for the robot,
* providing named objects for each of the robot interfaces.
*/
public DefaultRobot() {
System.out.println("BuiltinDefaultCode Constructor Started\n");
m_dsPacketsReceivedInCurrentSecond = 0;
}

/********************************** Init Routines *************************************/

public void robotInit() {
// Actions which would be performed once (and only once) upon initialization of the
// robot would be put here.
frontLeftDrive = new Victor (1);
frontRightDrive = new Victor (2);
backLeftDrive = new Victor (3);
backRightDrive = new Victor (4);
wench = new Jaguar(5);
latch = new Jaguar(6);
intake = new Jaguar(7);
xboxController = new XboxPaddle(1);
catapult = new Encoder(3, 4, true, EncodingType.k4X);
System.out.println("RobotInit() completed.\n");
//robotDrive.setSafetyEnabled(false);
Watchdog.getInstance().kill();
gyro = new Gyro(2);
deadZone = .15;
catapult.setMaxPeriod(.1);
catapult.setMinRate(10);
catapult.setDistancePerPulse(1024);
catapult.setReverseDirection(true);
catapult.setSamplesToAverage(7);
catapult.start();
}

private void drive(double multiplier){
if (multiplier>Math.abs(1)) return; //make sure a value between -1 and 1 will be received by motor controllers
//create deadzones for the xbox controller
if (Math.abs(xboxController.getRawAxis(1)) < deadZone) axis1 = 0;
else axis1 = xboxController.getRawAxis(1);
if (Math.abs(xboxController.getRawAxis(2)) < deadZone) axis2 = 0;
else axis2 = xboxController.getRawAxis(2);
if (Math.abs(xboxController.getRawAxis(4)) < deadZone) axis4 = 0;
else axis4 = xboxController.getRawAxis(4);
if (Math.abs(xboxController.getRawAxis(5)) < deadZone) axis5 = 0;
else axis5 = xboxController.getRawAxis(5);
//set the drive motors
frontRightDrive.set((axis2 + axis1)*multiplier);
backRightDrive.set((axis2 - axis1)*multiplier);
frontLeftDrive.set((axis5 - axis4)*multiplier);
backLeftDrive.set((axis5 + axis4)*multiplier);
}

public void disabledInit() {
m_disabledPeriodicLoops = 0; // Reset the loop counter for disabled mode
startSec = (int)(Timer.getUsClock() / 1000000.0);
printSec = startSec + 1;
}

public void autonomousInit() {
m_autoPeriodicLoops = 0; // Reset the loop counter for autonomous mode
}

public void teleopInit() {
m_telePeriodicLoops = 0; // Reset the loop counter for teleop mode
m_dsPacketsReceivedInCurrentSecond = 0; // Reset the number of dsPackets in current second
//catapult.start();
}

/********************************** Periodic Routines *************************************/
static int printSec;
static int startSec;

public void disabledPeriodic() {
// feed the user watchdog at every period when disabled
Watchdog.getInstance().feed();

// increment the number of disabled periodic loops completed
m_disabledPeriodicLoops++;

// while disabled, printout the duration of current disabled mode in seconds
if ((Timer.getUsClock() / 1000000.0) > printSec) {
System.out.println("Disabled seconds: " + (printSec - startSec));
printSec++;
}
}

public void autonomousPeriodic() {
Watchdog.getInstance().feed();

m_autoPeriodicLoops++;

}

public void teleopPeriodic() {
Watchdog.getInstance().feed();

// increment the number of teleop periodic loops completed
m_telePeriodicLoops++;

//Drive////////////////////////////////////////////////
m_dsPacketsReceivedInCurrentSecond++;// increment DS packets received
drive(1);
System.out.println(catapult.get());
}

int GetLoopsPerSec() {
return 20;
}
}

Ether
17-02-2014, 11:43
PWM wire one is soldered to ground, 5v, and a;

You are soldering the same wire to ground and 5V?

shmet
17-02-2014, 12:20
No.

shmet
17-02-2014, 12:22
I meant to say that the cluster of the three PWM wires are connected to those pins.

shmet
17-02-2014, 13:03
We got it working. The problem was that we didn't have csn grounded.

Ether
17-02-2014, 13:07
Just a thought: We might have been able to save you 4 days of troubleshooting had you posted pictures of the wiring.