Page 1 of 1

Two motors simultaneous with the 1064 board

Posted: Mon Jan 15, 2018 9:41 pm
by BarnyardProgrammer
Control of one motor on a 1064 works. Use of one motor or the other seems to work; however, I am having trouble figuring out how to use both motors at once. Can anyone help me with the issue in this code?

Thanks!
Jerry

Code: Select all

	PhidgetLog_enable(PHIDGET_LOG_INFO, NULL);

	PhidgetDCMotorHandle dcmotor1;
	PhidgetDCMotorHandle dcmotor2;	
	
	PhidgetDCMotor_create(&dcmotor1);
	PhidgetDCMotor_create(&dcmotor2);

	Phidget_setDeviceSerialNumber((PhidgetHandle)dcmotor1, STEERING_AND_THROTTLE_PHIDGET_SERIAL_NUMBER);
	Phidget_setDeviceSerialNumber((PhidgetHandle)dcmotor2, STEERING_AND_THROTTLE_PHIDGET_SERIAL_NUMBER);

	Phidget_setChannel((PhidgetHandle)dcmotor1, 0);
	Phidget_setChannel((PhidgetHandle)dcmotor2, 1);

	Phidget_openWaitForAttachment((PhidgetHandle)dcmotor1, 5000);
	Phidget_openWaitForAttachment((PhidgetHandle)dcmotor2, 5000);

	PhidgetDCMotor_setTargetVelocity(dcmotor1, 1);
  	PhidgetDCMotor_setTargetVelocity(dcmotor2, 1);


	sleep(2.5);

	PhidgetDCMotor_setTargetVelocity(dcmotor1, -1);
 	PhidgetDCMotor_setTargetVelocity(dcmotor2, -1);

	sleep(2.5);

	PhidgetDCMotor_setTargetVelocity(dcmotor1, 0);
 	PhidgetDCMotor_setTargetVelocity(dcmotor2, 0);

	PhidgetDCMotor_delete(&dcmotor1);
 	PhidgetDCMotor_delete(&dcmotor2);
 	

Re: Two motors simultaneous with the 1064 board

Posted: Tue Jan 16, 2018 11:16 am
by mparadis
The code looks correct to me. What exactly happens when you try to run it? Does only the first motor work, or do neither work?

Also, you can add error checking to the code by checking the return values for functions like open or setTargetVelocity:

Code: Select all

PhidgetReturnCode res;
const char *errs;

res = Phidget_openWaitForAttachment((PhidgetHandle)dcmotor1, 5000);
if (res != EPHIDGET_OK) {
		Phidget_getErrorDescription(res, &errs);
		fprintf(stderr, "failed to open: %s\n", errs);
	}

Re: Two motors simultaneous with the 1064 board

Posted: Fri Jan 19, 2018 7:56 am
by BarnyardProgrammer
The additional error handling code was tried and it did not output an error. (I'm running the recent version of Ubuntu Linux and compiling in eclipse).

The first motor runs but the second one does nothing.

It is possible to switch which motor runs by changing between

Code: Select all

Phidget_setChannel((PhidgetHandle)dcmotor1, 0);
and

Code: Select all

Phidget_setChannel((PhidgetHandle)dcmotor1, 1);
then recompiling the code.

This comment in the C example code is confusing.

Code: Select all

	/*
	 * Specifies which channel to attach to.  It is important that the channel of
	 * the device is the same class as the channel that is being opened.
	 *
	 * The default is any channel.
	 */
	// Phidget_setChannel(ch, 0);

C has no classes and the library will not compile to c++. Overloading is also not possible in C. Does this mean anything?

This warning will pop up sometimes. I think it is related to not properly closing the phidget.
WARN [_phidget22usb][2018-01-18T23:59:29]:libusb_claim_interface() failed with BUSY - the device may already be open

-Thanks!

Re: Two motors simultaneous with the 1064 board

Posted: Fri Jan 19, 2018 9:27 pm
by BarnyardProgrammer
Both motors are working.

Changed channel 1 to -1. Could the channel number have been changed on channel 1 on the 1064 Phidget?

Code: Select all

   PhidgetLog_enable(PHIDGET_LOG_INFO, NULL);

   PhidgetDCMotorHandle dcmotor1;
   PhidgetDCMotorHandle dcmotor2;   
   
   PhidgetDCMotor_create(&dcmotor1);
   PhidgetDCMotor_create(&dcmotor2);

   Phidget_setDeviceSerialNumber((PhidgetHandle)dcmotor1, STEERING_AND_THROTTLE_PHIDGET_SERIAL_NUMBER);
   Phidget_setDeviceSerialNumber((PhidgetHandle)dcmotor2, STEERING_AND_THROTTLE_PHIDGET_SERIAL_NUMBER);

   Phidget_setChannel((PhidgetHandle)dcmotor1, 0);
   Phidget_setChannel((PhidgetHandle)dcmotor2, -1);

   Phidget_openWaitForAttachment((PhidgetHandle)dcmotor1, 5000);
   Phidget_openWaitForAttachment((PhidgetHandle)dcmotor2, 5000);

   PhidgetDCMotor_setTargetVelocity(dcmotor1, 1);
     PhidgetDCMotor_setTargetVelocity(dcmotor2, 1);


   sleep(2.5);

   PhidgetDCMotor_setTargetVelocity(dcmotor1, -1);
    PhidgetDCMotor_setTargetVelocity(dcmotor2, -1);

   sleep(2.5);

   PhidgetDCMotor_setTargetVelocity(dcmotor1, 0);
    PhidgetDCMotor_setTargetVelocity(dcmotor2, 0);
    
   Phidget_close((PhidgetHandle)dcmotor1)
   Phidget_close((PhidgetHandle)dcmotor2)

   PhidgetDCMotor_delete(&dcmotor1);
    PhidgetDCMotor_delete(&dcmotor2);

Re: Two motors simultaneous with the 1064 board

Posted: Mon Jan 22, 2018 8:26 am
by mparadis
It's bizarre that it works with -1. Using -1 (or PHIDGET_CHANNEL_ANY) will match the first available channel of the type you're looking for. The only way this would be a solution is if the channel index was not 1, as you said. But I've never heard of such behaviour on any Phidget.

When you call getChannel on dcmotor2 after opening it with -1, what number does it give you?

Re: Two motors simultaneous with the 1064 board

Posted: Sun Feb 11, 2018 10:08 pm
by BarnyardProgrammer
I think I figured this out.

get_channel returns 1.

It seems that if I initially try to turn both motors on at full voltage at the same time one will not come on. If I stagger them 0.5 ms this issue does not occur.