Log in

View Full Version : How in the world do I code and wire this wacky sensor(a.k.a. Photoswitch)


cogbrained3244
10-02-2014, 23:25
So, we're tossing a telescoping blocker on our robot, and the mentors want a way to control it so it doesn't try and go past it's safe spot. So they handed me this thingy and told me "Figure out how to wire this. Then figure out how to program it." The Javadocs don't have anything I could see about using these. So how they work is a little beyond me.

I know that this is a photoswitch/photometer and that I want to get a true false value out of the thing's feedback. But where do these four wires connect to, is it analog or digital, and how do I write code for it?

Note that other members of my team might write replies to this topic consorting with me, so check our team number before you get confused by someone's post. Also, apologies for tossing the pictures of the thing in a word document. It was the only way to get the file size down.

Mark McLeod
10-02-2014, 23:39
The wiring diagram is on the back of the sensor.
Blue = ground
Brown = 12v
White or Black = signal (pick one, they just give opposite results)
It gets wired to a Digital Input and in code it'll act like a switch.

Something to be aware of with these sensors is they nominally require at least 11v, so if your robot power dips too low you may see bogus inputs.
It works best if you can feed it with a regulated 12v of power.

Allen-Bradley RightSight Specs

Pault
10-02-2014, 23:49
One thing to beware of is that unlike most sensors, you are going to want to wire this guy to your power distribution board for 12v and ground. There should only be a signal wire going to the digital side car; whether you choose the white or black only changes whether light = true or light = false.

Treat it like a normal digital input in code. I can't remember how you do that in Java, but it should be fairly simple.

wolfspell12
11-02-2014, 21:42
It is actually 24 volts according to the manufacture's manual that we printed- don't mean to be a know-it-all.

Anyways, we can't even get the part to move without the sensor. The Jag code all looks normal to me.

We tested and the jag will operate with BDC-Comm.

Anyone have a clue what's wrong with our 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.templates;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DriverStationLCD;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.IterativeRobot;

/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class RobotTemplate extends IterativeRobot {

Joystick driver = new Joystick(1);
Joystick operator = new Joystick(2);
Jaguar claw = new Jaguar(6);
Jaguar frontLeft = new Jaguar(2);
Jaguar rearLeft = new Jaguar(3);
Jaguar frontRight = new Jaguar(4);
Jaguar rearRight = new Jaguar(5);
Jaguar defender = new Jaguar(10);
Compressor comp = new Compressor(1, 1);//Constructor Order: Digitial I/O, Relay
Solenoid boomup = new Solenoid(7);
Solenoid boomdown = new Solenoid(1);
RobotDrive drive = new RobotDrive(frontLeft, rearLeft, frontRight, rearRight);
Gyro gyro = new Gyro(1);
//Timer time = new Timer();
DigitalInput photoswitch = new DigitalInput(2);
DriverStationLCD dataPrinter = DriverStationLCD.getInstance();
boolean didAuto = false;

/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
public void robotInit() {
gyro.reset();
comp.start();
drive.setSafetyEnabled(false);
/* drive.setInvertedMotor(RobotDrive.MotorType.kFront Left, false);
drive.setInvertedMotor(RobotDrive.MotorType.kRearL eft, false);
drive.setInvertedMotor(RobotDrive.MotorType.kFront Right, false);
drive.setInvertedMotor(RobotDrive.MotorType.kRearR ight, false);*/

}

/**
* This function is called periodically during autonomous
*/
public void autonomousPeriodic() {
log("didAuto= " + didAuto);

if (didAuto) {
return;//after value is set to true, the code will stop executing.
}
final double autoSpeed = 1.0;
final long autoTime = 5000; //mili seconds

long start = System.currentTimeMillis();
while (true) {
drive.tankDrive(autoSpeed, autoSpeed);
//frontLeft.set(autoSpeed);//invert these if it drives backwards
//rearLeft.set(autoSpeed);
//frontRight.set(-autoSpeed);//Experimental
//rearRight.set(-autoSpeed);//inversion of input will drive forward (we hope!)
if ((System.currentTimeMillis() - start) > autoTime) {
break;
}
}//moves robot straight forward for five seconds
// log("Time start " + System.currentTimeMillis());
// Timer.delay(autoTime);
// log("Timer stop " + System.currentTimeMillis());
claw.set(-.7);//drops claw
didAuto = true;//prevents autonomous from running again.
}

/**
* This function is called periodically during operator control
*/
public void teleopPeriodic() {
//final double axisOutput = driver.getRawAxis(4);
drive.mecanumDrive_Cartesian(driver.getRawAxis(1), driver.getRawAxis(3), -driver.getRawAxis(2), gyro.getAngle());
/*if (driver.getRawAxis(4) < -0.1) {
//to strafe left
log("I'm going left " + axisOutput);
frontLeft.set(-driver.getRawAxis(4));
rearLeft.set(driver.getRawAxis(4));
frontRight.set(driver.getRawAxis(4));
rearRight.set(-driver.getRawAxis(4));
} else if (driver.getRawAxis(4) > 0.1) {
//to strafe right
log("I'm going right " + axisOutput);
frontLeft.set(driver.getRawAxis(4));
rearLeft.set(-driver.getRawAxis(4));
frontRight.set(-driver.getRawAxis(4));
rearRight.set(driver.getRawAxis(4));
} else if(driver.getRawAxis(5) > 0){
frontLeft.set(driver.getRawAxis(5));
rearLeft.set(driver.getRawAxis(5));
frontRight.set(driver.getRawAxis(5));
rearRight.set(driver.getRawAxis(5));
}
else if(driver.getRawAxis(5) < 0){
frontLeft.set(driver.getRawAxis(5));
rearLeft.set(driver.getRawAxis(5));
frontRight.set(driver.getRawAxis(5));
rearRight.set(driver.getRawAxis(5));
} else {
drive.mecanumDrive_Cartesian(driver.getRawAxis(1), driver.getRawAxis(2), -driver.getRawAxis(3), gyro.getAngle());
log("I'm reaching the else. Yay!");
}*/

if (operator.getRawButton(4)) {
//Y button
claw.set(1);
} else if (operator.getRawButton(1)) {
claw.set(-0.7);
} else {
claw.set(0);
}

if (operator.getRawButton(8)) {
//Start button
comp.start();
} else if (operator.getRawButton(7)) {
//Back
comp.stop();
}

if (driver.getRawButton(8)) {
comp.start();
} else if (driver.getRawButton(7)) {
comp.stop();
}

if (operator.getRawButton(5)) {
boomup.set(true);
boomdown.set(false);
} else if (operator.getRawButton(6)) {
boomdown.set(true);
boomup.set(false);
}
/* if (photoswitch.get() == true) {
if (operator.getRawButton(2)) {
defender.set(1.0);
}
}
else if (photoswitch.get() == false) {
if (operator.getRawButton(3)) {
defender.set(-1.0);
}
}*/
if (operator.getRawButton(2)) {
defender.set(1.0);
} else if (operator.getRawButton(3)) {
defender.set(-1.0);
}
}



/**
* This function is called periodically during test mode
*/
public void testPeriodic() {
didAuto = false;
}

public double centerXbox(double axisOutput) {
double reducedSensitivity = axisOutput;
if (axisOutput > -0.2 || axisOutput < 0.2) {
reducedSensitivity = 0;
}
return reducedSensitivity;
}

private void log(String msg) {
dataPrinter.clear();
dataPrinter.println(DriverStationLCD.Line.kUser1, 1, msg);
dataPrinter.updateLCD();
}

}

Joe Ross
12-02-2014, 01:09
Anyways, we can't even get the part to move without the sensor. The Jag code all looks normal to me.

We tested and the jag will operate with BDC-Comm.

Since you mention BDC-Comm, does that mean you are trying to use CAN? The code you posted has defender jaguar on PWM 10.