View Single Post
  #3   Spotlight this post!  
Unread 10-02-2016, 23:18
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 102
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: New to FRC Programming

The PWM splitter is a great way to solve this problem.

If you don't have a PWM splitter and it will take you awhile to get one, you can use a software approach by adding a new SpeedControllerArray class to your project (shown below). This class controls an array of motors as if they were one. It is a simple implementation that expects that all of the motors turn the same direction (for example, if you want to set a power of .5 on one, you will be setting them all to .5).

To use the SpeedControllerArray class in your Robot.java, you will need to create a SpeedControllerArray.java file with the code shown below in the same package as your Robot.java file and then change your RobotDrive construction from:

Code:
    RobotDrive driveRobot = new RobotDrive(frontLeft, backLeft, frontRight, backRight);
To:

Code:
    SpeedController[] leftSideMotors = { frontLeft, middleLeft, backLeft };
    SpeedController[] rightSideMotors = { frontRight, middleRight, backRight };
    
    SpeedController leftSide = new SpeedControllerArray(leftSideMotors);
    SpeedController rightSide = new SpeedControllerArray(rightSideMotors);
    
    RobotDrive driveRobot = new RobotDrive(leftSide, rightSide);
Here is the source for the SpeedControllerArray class. NOTE: Please review (I think it is OK, but it has not been verified it on a live robot).

Code:
package org.usfirst.frc.team3756.robot;

import edu.wpi.first.wpilibj.SpeedController;

/**
 * A SpeedController implementation that wraps an array of one or more
 * SpeedController objects so they look like a single SpeedController (a
 * software version of a PWM splitter cable).
 */
public class SpeedControllerArray implements SpeedController {
	
	// Array of motors to control like one
	private SpeedController[] motors;

	public SpeedControllerArray(SpeedController[] motors) {
		if (motors.length < 1) {
			throw new IllegalArgumentException("You must pass an array of at least one SpeedController");
		}
		this.motors = motors;
	}

	@Override
	public void pidWrite(double output) {
		set(output);
	}

	@Override
	public double get() {
		// Since all motors have same value, return value from first one
		return motors[0].get();
	}

	@Override
	public void set(double speed, byte syncGroup) {
		for (SpeedController motor : motors) {
			motor.set(speed, syncGroup);
		}
	}

	@Override
	public void set(double speed) {
		for (SpeedController motor : motors) {
			motor.set(speed);
		}
	}

	@Override
	public void setInverted(boolean isInverted) {
		for (SpeedController motor : motors) {
			motor.setInverted(isInverted);
		}
	}

	@Override
	public boolean getInverted() {
		// Since all motors have same value, return value from first one
		return motors[0].getInverted();
	}

	@Override
	public void disable() {
		for (SpeedController motor : motors) {
			motor.disable();
		}
	}

}
Good luck, and I hope you found the PWM splitter cable as that is a simpler hardware solution to the problem.
Reply With Quote