View Single Post
  #9   Spotlight this post!  
Unread 17-02-2014, 11:38
shmet shmet is offline
Registered User
FRC #4178
 
Join Date: Feb 2014
Location: Declo, ID
Posts: 4
shmet is an unknown quantity at this point
Re: motor encoder java

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:
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;
    }
}
Reply With Quote