Created
February 15, 2023 11:42
-
-
Save chamellion/4e19d0680518525dceead382a9f518d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.servo; | |
import com.phidget22.*; | |
import java.io.IOException; | |
public class CSCM79 { | |
private static final int SERVO_SERIAL_NUMBER = 19946; | |
private static final int LCD_SERIAL_NUMBER = 39834; | |
/*Variable for updating slider values, variable was | |
made global to only update using getters and setters | |
because slider keeps updating even on the same values every | |
sec. Changes are only made if it's a new value. See getter | |
and setter below | |
*/ | |
private static double voltageRatioNum = 0; | |
public static void main(String[] args) { | |
try { | |
//Create the channels | |
RCServo rcServo = new RCServo(); | |
LCD lcdScreen = new LCD(); | |
VoltageRatioInput slider = new VoltageRatioInput(); | |
//Setting address on channels | |
rcServo.setDeviceSerialNumber(SERVO_SERIAL_NUMBER); | |
lcdScreen.setDeviceSerialNumber(LCD_SERIAL_NUMBER); | |
slider.setChannel(0); | |
slider.setDeviceSerialNumber(LCD_SERIAL_NUMBER); | |
//Attaching callbacks listeners to phidget objects when phidgets are connected | |
rcServo.addAttachListener(attachEvent -> { | |
try { | |
System.out.println("Server motor Device has been attached and device number is " | |
+ attachEvent.getSource().getDeviceSerialNumber()); | |
} catch (PhidgetException e) { | |
e.printStackTrace(); | |
System.out.println("Server motor Attach listener just threw an exception"); | |
} | |
}); | |
lcdScreen.addAttachListener(attachEvent -> { | |
try { | |
System.out.println("LCD Device has been attached and device number is " | |
+ attachEvent.getSource().getDeviceSerialNumber()); | |
} catch (PhidgetException e) { | |
e.printStackTrace(); | |
System.out.println("LCD Attach listener just threw an exception"); | |
} | |
}); | |
slider.addAttachListener(attachEvent -> { | |
try { | |
System.out.println("Slider Device has been attached and device number is " | |
+ attachEvent.getSource().getDeviceSerialNumber()); | |
} catch (PhidgetException e) { | |
e.printStackTrace(); | |
System.out.println("Slider Attach listener just threw an exception"); | |
} | |
}); | |
//Opening the channel | |
rcServo.open(5000); | |
lcdScreen.open(5000); | |
slider.open(5000); | |
//Prepping LCD screen | |
lcdScreen.setContrast(0.5); | |
lcdScreen.setBacklight(0.5); | |
lcdScreen.writeText(LCDFont.DIMENSIONS_5X8, 0, 0, "Starting Device"); | |
lcdScreen.flush(); | |
//Callback listeners for handling changes in event on phidgets | |
rcServo.addPositionChangeListener(rcServoPositionChangeEvent -> { | |
try { | |
lcdScreen.writeText(LCDFont.DIMENSIONS_6X12, 0, 0, | |
"" + rcServoPositionChangeEvent.getPosition()); | |
lcdScreen.flush(); | |
System.out.println("LCD triggered"); | |
} catch (PhidgetException e) { | |
e.printStackTrace(); | |
System.out.println("RCServo threw an exception"); | |
} | |
}); | |
slider.addVoltageRatioChangeListener(voltageRatioInputVoltageRatioChangeEvent -> { | |
setVoltageRatioNum(voltageRatioInputVoltageRatioChangeEvent.getVoltageRatio()); | |
try { | |
/*RC servo default max rotation is 180 and slider is 1 | |
scaling slider measurement to 180 by multiplying by 180 | |
*/ | |
rcServo.setTargetPosition(getVoltageRatioNum() * 180); | |
rcServo.setEngaged(true); | |
System.out.println("RCServo position reported is " + getVoltageRatioNum() * 180); | |
} catch (PhidgetException e) { | |
e.printStackTrace(); | |
System.out.println("Slider threw an exception"); | |
} | |
}); | |
//Open input stream to keep program running | |
System.in.read(); | |
//Closing all Phidget connection to release resource | |
rcServo.close(); | |
lcdScreen.close(); | |
slider.close(); | |
} catch (PhidgetException | IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void setVoltageRatioNum(double regulator) { | |
//Only set the value if input changes(reason stated above on variable declaration) | |
if (voltageRatioNum != regulator) { | |
voltageRatioNum = regulator; | |
} | |
} | |
private static double getVoltageRatioNum() { | |
return voltageRatioNum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment