Phidget SBC3 Digital Inputs and Encoder QME-01

General PhidgetSBC Discussion.
Post Reply
ShadowOne333
Fresh meat
Posts: 3
Joined: Thu Sep 15, 2016 10:51 am
Contact:

Phidget SBC3 Digital Inputs and Encoder QME-01

Post by ShadowOne333 »

Good day, Phidget Community.

Last week I was trying to connect this particular model of encoder:
http://www.robotshop.com/en/lynxmotion- ... ifications

Alongside the Phidgets SBC3.
I managed to get some sort of output by using an example I found online, the output is as follows:

Code: Select all

Digital Input: 0 > State: 1
Digital Input: 1 > State: 0
Digital Input: 2 > State: 0
Digital Input: 3 > State: 0
Digital Input: 4 > State: 0
Digital Input: 5 > State: 0
Digital Input: 6 > State: 0
Digital Input: 7 > State: 0
Digital Input: 0 > State: 0
Digital Input: 0 > State: 1
Digital Input: 0 > State: 0
Digital Input: 0 > State: 1
(and so on)
However, the problem is that the Digital input print is not on pair when the DC motor starts picking up speed.
I can only get the previously written output when I move the wheel by hand and quite slowly, but as soon as I start rotating it slightly faster, I stop getting any kind of output.

Now my question is, what could be the cause of this issue?
I took the example online as-is, and reduced it to only print the Digital inputs, and I am still getting the same problem.

Here's the main code I am using (reduced):
(NOTE: the kservo.hpp is a library used for the movement of the DC motor, is completely separate and I only use it to enable/disable motor motion)

Code: Select all

//
//  sbcexample.c
//
// This simple example creates an InterfaceKit handle, hooks the event
// handlers and opens it.
// It reads the status of the inputs and outputs then waits for an InterfaceKit
// to be attached and waits for events to be fired.
// Digital output 0 will trigger on when any sensor value is greater than 500.
//
// Modified by Kat Dornian on 2014-03-26.
//
// Copyright 2014 Phidgets Inc.
// This work is licensed under the Creative Commons Attribution 2.5 Canada License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/

// To compile:
// g++ -o test encoder.c -lphidget21 -ldl -lpthread -lm

#include <stdio.h>
#include <unistd.h>
#include <phidget21.h>
#include "kservo.hpp"
#define phi0 0

//Callback that will run if an input changes.
//Index - Index of the input that generated the event, State - boolean (0 or 1) representing the input state (on or off)
int CCONV InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
{
		fprintf(stdout, "Digital Input: %d > State: %d\n", Index, State);
	    fflush(stdout);

	return 0;
}

int interfacekit_simple()
{
	int result;
	const char *err;

	//Declare an InterfaceKit handle
	CPhidgetInterfaceKitHandle ifKit = 0;

	//create the InterfaceKit object
	CPhidgetInterfaceKit_create(&ifKit);

	//open the interfacekit for device connections
	CPhidget_open((CPhidgetHandle)ifKit, -1);

	//get the program to wait for an interface kit device to be attached
	printf("Waiting for interface kit to be attached....");

	if((result = CPhidget_waitForAttachment((CPhidgetHandle)ifKit, 10000))) {
		CPhidget_getErrorDescription(result, &err);
		printf("Problem waiting for attachment: %s\n", err);

		return 0;
	}

	//Registers a callback that will run if an input changes.
	//Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).

	CPhidgetInterfaceKit_set_OnInputChange_Handler (ifKit, InputChangeHandler, NULL);

	//keep displaying interface kit until program is ended
    while(1) {
        usleep(1000);
    }

	CPhidget_close((CPhidgetHandle)ifKit);
	CPhidget_delete((CPhidgetHandle)ifKit);

	//all done, exit
	return 0;
}

int main(int argc, char* argv[])
{
	interfacekit_simple();

	// int enc = CPhidgetRFID_getOutputState(IFK, 0);
	//printf("%d \n",&enc);

	return 0;
}
Any kind of help would be greatly appreciated!
User avatar
mparadis
Site Admin
Posts: 658
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Phidget SBC3 Digital Inputs and Encoder QME-01

Post by mparadis »

The problem is that the digital inputs only update every 8 milliseconds (125 pulses per second). They're not designed to work with a quadrature signal which has very short pulses. As soon as you reach a sufficient speed, you start missing pulses and it becomes impossible to know the position or direction of the encoder.

I would recommend getting a 1057, which is specifically designed to read encoders at up to 200000 pulses per second and connects to the USB port of your SBC. It comes with an encoder cable that you can snip in half and solder to your encoder.
ShadowOne333
Fresh meat
Posts: 3
Joined: Thu Sep 15, 2016 10:51 am
Contact:

Re: Phidget SBC3 Digital Inputs and Encoder QME-01

Post by ShadowOne333 »

Oh I see, so the issue is related to the pulses/count per second of the input.
Is there any other way to read the encoder which doesn't involve additional hardware?
Maybe with interruptions or something similar.

I ask because I am using currently four DC motors, each one with their respective encoders, and seeing how the 1057 is used for individual encoders only, the expense would be quite high. Also, the product specifications also mention the Update Rate as 125 samples/s.
User avatar
mparadis
Site Admin
Posts: 658
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Phidget SBC3 Digital Inputs and Encoder QME-01

Post by mparadis »

To clarify, the 1057 is able to capture pulses at 200000 pulses per second, but will only send that data to your computer every 8 milliseconds. So even if you get more than one pulse within an 8 millisecond window, the 1057 will, at the end of those 8ms, tell your computer the number of pulses that occurred and the count will be updated in your program.

Unless your motors are running at extremely low speeds, there's no good way to get around this requirement. If you plan on using many motors and encoders, the 1047 will be much more cost effective than the 1057.
ShadowOne333
Fresh meat
Posts: 3
Joined: Thu Sep 15, 2016 10:51 am
Contact:

Re: Phidget SBC3 Digital Inputs and Encoder QME-01

Post by ShadowOne333 »

Thank you for the clarification.
I am already in talks with the people involved in the project for getting the 1047 you mentioned, but before that I want to know:

Are there are any examples in C/C++ code to use the 1047 with a Phidgets SBC3 using Debian?
Or rather C/C++ for use with Linux.
User avatar
mparadis
Site Admin
Posts: 658
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Phidget SBC3 Digital Inputs and Encoder QME-01

Post by mparadis »

This guide briefly describes how to use Phidgets with the SBC. You can find encoder-specific code samples in the C/C++ examples.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest