mparadis not sure I understand what adding the 2 second sleep does but it works. the position reached handler doesn't work like I thought it would. it gets triggered almost as soon as the motor starts moving even though the motor may move for several seconds after that. Also when I add the 2 second sleep the motor will keep moving after the program ends if it takes the motor longer than 2 seconds to move. If I move the motor form position 44.0 to 144.0 it can take more that 9 seconds. I'll have to try to find some info a bout pulse width etc. here's what I have now and it does what I want it to. I added in the currentinput device because I might going to monitor the current an use that as an indicator that I've reached desired position. ie current = 0.0
Code: Select all
import sys
import time
from Phidget22.Devices.RCServo import *
from Phidget22.Devices.CurrentInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *
from time import sleep
class PokeyMtr:
def __init__(self, channelId, serial_number):
self.mtr = RCServo()
self.driver_board = CurrentInput()
self.driver_board.setDeviceSerialNumber(serial_number)
self.driver_board.setChannel(0)
self.driver_board.openWaitForAttachment(1000)
self.mtr.setDeviceSerialNumber(serial_number)
self.mtr.setChannel(channelId)
self.mtr.openWaitForAttachment(1000)
self.mtr.setMinPosition(0)
self.mtr.setOnTargetPositionReachedHandler(self.targetReachedHandler())
self.isDone = False
def go_mtr_go(self, target_position):
try:
self.mtr.setTargetPosition(target_position)
except PhidgetException as e:
print(e.details)
self.mtr.setEngaged(True)
sleep(1)
def close_mtr(self):
self.mtr.close()
pokey = PokeyMtr(0, 305396)
pokey.go_mtr_go(44)
pokey.close_mtr()
I did this to prevent the go_mtr_go() from returning before the mtr is done moving:
Code: Select all
def go_mtr_go(self, target_position):
try:
self.mtr.setTargetPosition(target_position)
except PhidgetException as e:
print(e.details)
self.mtr.setEngaged(True)
sleep(2)
while True:
current = self.driver_board.getCurrent()
print(current)
if current == 0:
print("motor off")
return