Two motors simultaneous with the 1064 board

C, C++, and Visual C++
Post Reply
BarnyardProgrammer
Phidgetsian
Posts: 6
Joined: Tue Jan 09, 2018 9:26 pm
Contact:

Two motors simultaneous with the 1064 board

Post 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);
 	
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Two motors simultaneous with the 1064 board

Post 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);
	}
BarnyardProgrammer
Phidgetsian
Posts: 6
Joined: Tue Jan 09, 2018 9:26 pm
Contact:

Re: Two motors simultaneous with the 1064 board

Post 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!
BarnyardProgrammer
Phidgetsian
Posts: 6
Joined: Tue Jan 09, 2018 9:26 pm
Contact:

Re: Two motors simultaneous with the 1064 board

Post 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);
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Two motors simultaneous with the 1064 board

Post 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?
BarnyardProgrammer
Phidgetsian
Posts: 6
Joined: Tue Jan 09, 2018 9:26 pm
Contact:

Re: Two motors simultaneous with the 1064 board

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests