using Phidgets across multiple methods

Supporting Java SE version 7 and up
Post Reply
astrokeith
Phidgetsian
Posts: 10
Joined: Tue Sep 14, 2021 11:55 am
Contact:

using Phidgets across multiple methods

Post by astrokeith »

I am updating an old program that used to drive Phidget steppers, gps and a 2/2/2 interface kit, using Phidget21. Its a sophisticated telescope control program that has been working well for years.

I'm using the new VINT modules and Phidget22.

After a lot of learning (I'm also new to Java!) I have modified the code (1700 lines) and it compiles OK. But it dosent work.

So I wrote two short programs to separately exercise the stepper, and the 2/2/2 (which has a joystick). These work nicely as stand alone programs. They were based the excellent examples on the website.

When I try to combine them, the joystick change events are being detected OK, but the steppers cant be controlled. I get no errors flagged - just nothing happens.

I've attached the java code. Its full of print statements as I try to understand what's going on (or not!) It does the print inside the altChange event method, but not the rest of that try block. I guess its all about where I am declaring or opening Phidgets?

Any pointers greatly fully accepted, but remember I'm a bit of a Java newbie!
Many thanks, Keith
ps:is there a better way to post code examples?

Code: Select all

import com.phidget22.*;

public class Java_Example {
	public static Stepper altStepper;
	public static DigitalOutput LED1;

	public static void main(String[] args) throws Exception {
		
		try {
			VoltageRatioInput altJoy = new VoltageRatioInput();
			VoltageRatioInput azJoy = new VoltageRatioInput();
			DigitalInput button = new DigitalInput();
			DigitalInput toggle = new DigitalInput();
			DigitalOutput LED1 = new DigitalOutput();
			DigitalOutput LED2 = new DigitalOutput();
			Stepper altStepper = new Stepper();
			altJoy.setChannel(0);
			azJoy.setChannel(1);
			button.setChannel(0);
			toggle.setChannel(1);
			LED1.setChannel(0);
			LED2.setChannel(1);

			altJoy.addVoltageRatioChangeListener(new VoltageRatioInputVoltageRatioChangeListener() {
				public void onVoltageRatioChange(VoltageRatioInputVoltageRatioChangeEvent altJ) {
					altChange(altJ);
				}
			});

			azJoy.addVoltageRatioChangeListener(new VoltageRatioInputVoltageRatioChangeListener() {
				public void onVoltageRatioChange(VoltageRatioInputVoltageRatioChangeEvent azJ) {
					azChange(azJ);
				}
			});

			button.addStateChangeListener(new DigitalInputStateChangeListener() {
				public void onStateChange(DigitalInputStateChangeEvent butJ) {
					try {
						if (button.getState())
						{
							butChange(butJ);
						}
					}
					catch (PhidgetException ex) {
						System.out.println("");
					}
				}
			});

			toggle.addStateChangeListener(new DigitalInputStateChangeListener() {
				public void onStateChange(DigitalInputStateChangeEvent togJ) {
					togChange(togJ);
				}
			});
			altStepper.open(5000);
			System.out.println(altStepper.getPosition());
			altStepper.setTargetPosition(100);
			altStepper.setEngaged(true);
			Thread.sleep(1);
			System.out.println(altStepper.getPosition());
			altJoy.open(5000);
			altJoy.setVoltageRatioChangeTrigger(0.2);
			azJoy.open(5000);
			azJoy.setVoltageRatioChangeTrigger(0.2);
			button.open(5000);
			toggle.open(5000);
			LED1.open(5000);
			LED2.open(5000);
			LED1.setDutyCycle(0);
			LED2.setDutyCycle(0);
			//Thread.sleep(1000);

		}
		catch (PhidgetException ex) {
			System.out.println("PhidgetException " + ex.getErrorCode() + " (" + ex.getDescription() + "): " + ex.getDetail());
		}
		//Wait until Enter has been pressed before exiting
		

		
	}
	public static void azChange(VoltageRatioInputVoltageRatioChangeEvent az){
		System.out.println("azimuth change: " + az.getVoltageRatio());
	}
	public static void altChange(VoltageRatioInputVoltageRatioChangeEvent alt){
		System.out.println("altitude change: " + alt.getVoltageRatio());
		
		try {
			System.out.println("trying motor");
			System.out.println(altStepper.getPosition());
			LED1.setDutyCycle(1);
			altStepper.setTargetPosition(100000);
			altStepper.setEngaged(true);
			System.out.println(altStepper.getPosition());
		} catch (PhidgetException ex) {
			System.out.println("PhidgetException " + ex.getErrorCode() + " (" + ex.getDescription() + "): " + ex.getDetail());
		}
	}
	public static void butChange(DigitalInputStateChangeEvent but){
		System.out.println("button change: ");
	}
	public static void togChange(DigitalInputStateChangeEvent tog){
		System.out.println("toggle change: ");
	}
}
/code]
astrokeith
Phidgetsian
Posts: 10
Joined: Tue Sep 14, 2021 11:55 am
Contact:

Re: using Phidgets across multiple methods

Post by astrokeith »

By trial and (many) errors I have found that Phidgets need to be explicitly transferred when calling a method, even if they have been declared public in the main class.

This seems to be new? I wasnt doing it with Phidgets21.

Is there a way of declaring them truly public
astrokeith
Phidgetsian
Posts: 10
Joined: Tue Sep 14, 2021 11:55 am
Contact:

Re: using Phidgets across multiple methods

Post by astrokeith »

Follow up. Solved with help from Brian at Phidgets tech support.

The problem was I was creating the Phidget channels inside the main method,
ie
Stepper stepper0 = new Stepper

This is how it is shown in the code examples.
If you declare the phidgets in the public class
eg
public static Stepper stepper0;
then create your channel inside the main method with the shorter syntax
stepper0 = new Stepper
it is accessible across all methods.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest