Encoder 1047

Supporting 2.7 and 3.2+
Post Reply
Vince
Fresh meat
Posts: 3
Joined: Thu Jan 03, 2019 8:47 pm
Contact:

Encoder 1047

Post by Vince »

Ok I am new. I am trying to make an interface that uses all four (4) encoders and has reset buttons for each one after I manually home each encoder to set it at zero. I have read through the documents and the code examples. I am at a loss.
1) how do I open and print all the channels at once
2) how do I get the position (not the change that the code example has but like the phidgets interface that I downloaded and used to make sure the board was good) so it reads 100 then 101,102 etc it stead of 5,0,9,0 etc (change of state since last check)
3) reset to zero once homed... no clue.

I have the interface designed in QT5 and I know how to get stuff to it and have the buttons on there to reset zero when pushed what I am lacking is the knowledge of the encoder board calls to do it.

Help please.
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Encoder 1047

Post by mparadis »

1) how do I open and print all the channels at once
In order to open all four encoder channels, you need to create four separate encoder handles:

Code: Select all

ch0 = Encoder()
ch1 = Encoder()
ch2 = Encoder()
ch3 = Encoder()
Then you need to set parameters for each channel. If the 1047 is the only encoder Phidget connected to your computer or network, you can just set channel:

Code: Select all

ch0.setChannel(0)
ch1.setChannel(1)
ch2.setChannel(2)
ch3.setChannel(3)
Then set position change handlers for each one:

Code: Select all

ch0.setOnPositionChangeHandler(onPositionChangeHandler)
ch1.setOnPositionChangeHandler(onPositionChangeHandler)
ch2.setOnPositionChangeHandler(onPositionChangeHandler)
ch3.setOnPositionChangeHandler(onPositionChangeHandler)
Then you open each channel:

Code: Select all

ch0.openWaitForAttachment(5000)
ch1.openWaitForAttachment(5000)
ch2.openWaitForAttachment(5000)
ch3.openWaitForAttachment(5000)
If you're using the digital input channels, you can do the same process but with DigitalInput and setOnStateChangeHandler.

In order to print position data from all four channels, you need to create the "onPositionChangeHandler" function, which will trigger any time one of the encoders moves. Take a look at this function in our python encoder example to see what to put in it. Since all four channels trigger the same event function, you'll want to use the line

Code: Select all

self.getChannel()
inside the event to determine which of the four channels this data belongs do. Then you can print the channel at the same time as the data. If this output looks too messy, you can instead store the data in one of four variables (one for each channel) and then use loop in your main program that prints these variables perodically.
2) how do I get the position (not the change that the code example has but like the phidgets interface that I downloaded and used to make sure the board was good) so it reads 100 then 101,102 etc it stead of 5,0,9,0 etc (change of state since last check)
The best you can do is keep track of the positions using variables that start at zero, and then add the position change to the appropriate variable each time an event comes in.

3) reset to zero once homed... no clue.
If you keep track of the total position using variables as I just described, then zeroing is as simple as setting that variable equal to zero.

EDIT: Oops, I forgot that there's a variable for total position already in the API. See James' response below.
jdecoux
Labview Developer
Posts: 180
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Encoder 1047

Post by jdecoux »

(2) You can use the getPosition() function to get the accumulated encoder position from the Phidget22 library.

Code: Select all

ch1.getPosition()
(3) You can use ch.setPosition(0) to zero the encoder positions.

Code: Select all

ch1.setPosition(0)
You can find these and other functions under the API tab on your product page. Just remember to select Python from the drop-down menu.
Vince
Fresh meat
Posts: 3
Joined: Thu Jan 03, 2019 8:47 pm
Contact:

Re: Encoder 1047

Post by Vince »

Thank you Both for the help I am going to try this now.
Vince
Fresh meat
Posts: 3
Joined: Thu Jan 03, 2019 8:47 pm
Contact:

Re: Encoder 1047

Post by Vince »

So I a back...... I am dying here. I have studied up on Python and still can't figure this out. I read the primer, took the example and modified it to try and get it to do what I need it to do and it just is not happening for me. I can not figure out how to
1)store each channel info in a separate variable that I can use.
2)hell I can't even get it to do anything other than print the "onPositionChangeHandler" handler from the example.
3)I need to be able to get the data out of the

def onPositionChangeHandler(self, positionChange, getPostion, indexTriggered):

print("\n[Position Event] -> Position change: " + str(positionChange))

so that I can track the position of the encoder and display it on a GUI but I need to be absolutely sure its the correct one that I am displaying.

Any help would be greatly appreciated or any tutorial or example that you can point me at. I have had no luck in finding anything.
jdecoux
Labview Developer
Posts: 180
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Encoder 1047

Post by jdecoux »

Since you're using Python, you can append one or more variables to the Phidget object you are using, to keep them linked regardless of where the object is used.

This should help you get the information you need into and out of the event handlers.

Here's a short example:

Code: Select all

def onPositionChangeHandler(self, positionChange, timeChange, indexTriggered):
    #We can now access and even change "myVariable" from the event 
    print(self.myVariable)
    self.myVariable += 1


ch = Encoder()

#Here we create an attribute of ch called "myVariable"
#and assign it the information to store
#It's an integer here for simplicity, but it could be anything
ch.myVariable = 0
ch.setOnStateChangeHandler(onStateChangeHandler)

ch.openWaitForAttachment(5000)

# The rest of your code here....
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests