def __init__(self, master, initial_name, move_pos_text, move_neg_text, serial_number, port, color, amp, vel): frame = Frame(master, borderwidth=2, relief=SUNKEN, bg=color) frame.pack() self.master = master self.frame_name = StringVar() self.frame_name.set(initial_name) self.fontType = "Comic Sans" #Set the parameters for the axis self.current_limit = amp #2 to 25A self.max_velocity = vel #0 to 1 self.acceleration = 1 #0.1 to 100 self.invert = FALSE #TRUE or FALSE self.axis_name = initial_name self.move_pos_text = move_pos_text self.move_neg_text = move_neg_text self.current_text = StringVar() self.jog_pos_btn = Button(frame, text=self.move_pos_text) self.jog_neg_btn = Button(frame, text=self.move_neg_text) self.configure_btn = Button(frame, text="CONFIGURE", font=(self.fontType,6), command=lambda: self.configure()) self.current = Entry(frame, width=5, state=DISABLED, textvariable=self.current_text) self.speed = Scale(frame, orient=HORIZONTAL, from_=0.01, to=1, resolution=.01, bg=color, label=" Axis Speed", highlightthickness=0) self.custom_label = Label(frame, textvariable=self.frame_name, font=(self.fontType, 14), bg=color) self.label = Label(frame, text=initial_name, bg=color) self.speed.set(0.25) frame.rowconfigure(0, minsize=30) self.custom_label.grid(row=0, column=0, columnspan=2, sticky=S) self.jog_pos_btn.grid(column=0, row=1, pady=10) self.jog_neg_btn.grid(column=0, row=2, pady=10) self.current.grid(column=0, row=3,pady=5) self.speed.grid(column=0, row=4, padx=20) self.configure_btn.grid(column=0, row=5, pady=5) self.label.grid(column=0, row=6) #Connect to Phidget Motor Driver self.axis = DCMotor() self.axis.setDeviceSerialNumber(serial_number) self.axis.setIsHubPortDevice(False) self.axis.setHubPort(port) self.axis.setChannel(0) self.axis.openWaitForAttachment(5000) self.axis.setAcceleration(self.acceleration) self.axis_current = CurrentInput() self.axis_current.setDeviceSerialNumber(serial_number) self.axis_current.setIsHubPortDevice(False) self.axis_current.setHubPort(port) self.axis_current.setChannel(0) self.axis_current.openWaitForAttachment(5000) self.axis_current.setDataInterval(200) self.axis_current.setCurrentChangeTrigger(0.0) self.update_current() #Bind user button press of jog button to movement method self.jog_pos_btn.bind('<ButtonPress-1>', lambda event: self.jog("+")) self.jog_pos_btn.bind('<ButtonRelease-1>', lambda event: self.jog("0")) self.jog_neg_btn.bind('<ButtonPress-1>', lambda event: self.jog("-")) self.jog_neg_btn.bind('<ButtonRelease-1>', lambda event: self.jog("0"))
def main(): try: """ * Allocate a new Phidget Channel object """ try: ch = DCMotor() except PhidgetException as e: sys.stderr.write("Runtime Error -> Creating DCMotor: \n\t") DisplayError(e) raise except RuntimeError as e: sys.stderr.write("Runtime Error -> Creating DCMotor: \n\t" + e) raise """ * Set matching parameters to specify which channel to open """ #You may remove this line and hard-code the addressing parameters to fit your application #channelInfo = AskForDeviceParameters(ch) ch.setDeviceSerialNumber(473647) ch.setHubPort(-1) ch.setIsHubPortDevice(False) ch.setChannel(0) """ * Add event handlers before calling open so that no events are missed. """ print("\n--------------------------------------") print("\nSetting OnAttachHandler...") ch.setOnAttachHandler(onAttachHandler) print("Setting OnDetachHandler...") ch.setOnDetachHandler(onDetachHandler) print("Setting OnErrorHandler...") ch.setOnErrorHandler(onErrorHandler) print("\nSetting OnVelocityUpdateHandler...") ch.setOnVelocityUpdateHandler(onVelocityUpdateHandler) """ * Open the channel with a timeout """ print("\nOpening and Waiting for Attachment...") try: ch.openWaitForAttachment(5000) except PhidgetException as e: PrintOpenErrorMessage(e, ch) raise EndProgramSignal("Program Terminated: Open Failed") print( "--------------------\n" "\nInput a desired velocity/acceleration value and press ENTER\n" 'Velocity Syntax: "v100" will set the motor speed to 100% in the positive direction \n' 'and calling "v-52" will set the motor speed to 52% in the negative direction \n' '\nAcceleration Syntax: "a25" will set the motor acceleration to 25dty/s^2' "\nThe motor cannot accelerate any faster than 62.5 dty/s^2 or slower than 0.28 dty/s^2\n" '\nTo enter cyclic mode, press "c" or "C" and the program will run with stored parameters' '\nTo modify the cyclic mode parameters, edit this python file and edit the variables at' '\nthe top of the file.\n' '\nNote: Due to some limitations within Python, once cyclic mode is engaged the only' '\nway to quit cyclic mode is to use Control + C (Command + C on a Mac). This ends the' '\nentire program, and will stop the motor instantly. For this reason, if please consider' '\nusing a small number of cycles so that you do not have to wait for them all to finish\n' '\nInput "Q" and press ENTER to come to a hard stop\n' '\nInput "S" and press ENTER to come to a soft stop\n') end = False while (end != True): buf = sys.stdin.readline(100) if not buf: continue if (buf[0] == 'Q' or buf[0] == 'q'): end = True continue if (buf[0] == 'v' or buf[0] == 'V'): #set velocity of the motor try: velocity = float(buf[1:]) except ValueError as e: print("Input must be a number, or Q to quit.") continue if (velocity > 100.0): velocity = 100.0 print("WARNING: MAXIMUM velocity is +/- 100%") if (velocity < -100.0): velocity = -100.0 print("WARNING: MAXIMUM velocity is +/- 100%") print("Setting DCMotor TargetVelocity to " + str(velocity) + "%") ch.setTargetVelocity(velocity / 100) elif (buf[0] == 'a' or buf[0] == 'A'): #set acceleration of the motor try: acceleration = float(buf[1:]) except ValueError as e: print( "Input must be a number, V for velocity, A for acceleration or Q to quit." ) continue if (acceleration > 62.5): acceleration = 62.5 print("WARNING: Maximum acceleration is 62.5 dty/s^2") if (acceleration < 0.28): acceleration = 0.28 print("WARNING: Minimum acceleration is 0.28 dty/s^2") print("Setting DCMotor Acceleration to " + str(acceleration) + " dty/s^2") ch.setAcceleration(acceleration) elif (buf[0] == 'c' or buf[0] == 'C'): print("Entering Cycle Mode") for i in range(n): print("Beginning cycle " + str(i + 1)) print("Setting Velocity to " + str(omega_1) + "%") ch.setTargetVelocity(omega_1 / 100) time.sleep(t_half) #calculate acceleration ch.setAcceleration( abs(((omega_1 / 100) - (omega_2 / 100)) / t_0)) print("Setting Velocity to " + str(omega_2) + "%") ch.setTargetVelocity(omega_2 / 100) time.sleep(t_0) time.sleep(t_1) print("Setting Velocity to " + str(omega_1) + "%") ch.setTargetVelocity(omega_1 / 100) time.sleep(t_0) time.sleep(t_half) print("Exiting Cycle Mode") print("Velocity currently set to " + str(ch.getVelocity() * 100) + "%") elif (buf[0] == 's' or buf[0] == 'S'): print("Initiating Soft Stop") ch.setAcceleration(ch.getMinAcceleration()) ch.setTargetVelocity(0) while (ch.getVelocity() > 0): time.sleep(0.1) print("Motor Stopped") end = True continue else: print("WARNING: Invalid Command") ''' * Perform clean up and exit ''' #clear the VelocityUpdate event handler ch.setOnVelocityUpdateHandler(None) print("Cleaning up...") ch.close() print("\nExiting...") return 0 except PhidgetException as e: sys.stderr.write("\nExiting with error(s)...") DisplayError(e) traceback.print_exc() print("Cleaning up...") #clear the VelocityUpdate event handler ch.setOnVelocityUpdateHandler(None) ch.close() return 1 except EndProgramSignal as e: print(e) print("Cleaning up...") #clear the VelocityUpdate event handler ch.setOnVelocityUpdateHandler(None) ch.close() return 1 finally: pass
class AxisFrame: def __init__(self, master, initial_name, move_pos_text, move_neg_text, serial_number, port, color, amp, vel): frame = Frame(master, borderwidth=2, relief=SUNKEN, bg=color) frame.pack() self.master = master self.frame_name = StringVar() self.frame_name.set(initial_name) self.fontType = "Comic Sans" #Set the parameters for the axis self.current_limit = amp #2 to 25A self.max_velocity = vel #0 to 1 self.acceleration = 1 #0.1 to 100 self.invert = FALSE #TRUE or FALSE self.axis_name = initial_name self.move_pos_text = move_pos_text self.move_neg_text = move_neg_text self.current_text = StringVar() self.jog_pos_btn = Button(frame, text=self.move_pos_text) self.jog_neg_btn = Button(frame, text=self.move_neg_text) self.configure_btn = Button(frame, text="CONFIGURE", font=(self.fontType, 6), command=lambda: self.configure()) self.current = Entry(frame, width=5, state=DISABLED, textvariable=self.current_text) self.speed = Scale(frame, orient=HORIZONTAL, from_=0.01, to=1, resolution=.01, bg=color, label=" Axis Speed", highlightthickness=0) self.custom_label = Label(frame, textvariable=self.frame_name, font=(self.fontType, 14), bg=color) self.label = Label(frame, text=initial_name, bg=color) self.speed.set(0.25) frame.rowconfigure(0, minsize=30) self.custom_label.grid(row=0, column=0, columnspan=2, sticky=S) self.jog_pos_btn.grid(column=0, row=1, pady=10) self.jog_neg_btn.grid(column=0, row=2, pady=10) self.current.grid(column=0, row=3, pady=5) self.speed.grid(column=0, row=4, padx=20) self.configure_btn.grid(column=0, row=5, pady=5) self.label.grid(column=0, row=6) #Connect to Phidget Motor Driver self.axis = DCMotor() self.axis.setDeviceSerialNumber(serial_number) self.axis.setIsHubPortDevice(False) self.axis.setHubPort(port) self.axis.setChannel(0) self.axis.openWaitForAttachment(5000) self.axis.setAcceleration(self.acceleration) self.axis_current = CurrentInput() self.axis_current.setDeviceSerialNumber(serial_number) self.axis_current.setIsHubPortDevice(False) self.axis_current.setHubPort(port) self.axis_current.setChannel(0) self.axis_current.openWaitForAttachment(5000) self.axis_current.setDataInterval(200) self.axis_current.setCurrentChangeTrigger(0.0) self.update_current() #Bind user button press of jog button to movement method self.jog_pos_btn.bind('<ButtonPress-1>', lambda event: self.jog("+")) self.jog_pos_btn.bind('<ButtonRelease-1>', lambda event: self.jog("0")) self.jog_neg_btn.bind('<ButtonPress-1>', lambda event: self.jog("-")) self.jog_neg_btn.bind('<ButtonRelease-1>', lambda event: self.jog("0")) def jog(self, direction): #Calculate the speed as a percentage of the maximum velocity velocity = float(self.speed.get()) * self.max_velocity #Apply invert if necessary if self.invert == TRUE: velocity *= -1 #Command Movement if direction == "+": self.axis.setTargetVelocity(velocity) elif direction == "-": self.axis.setTargetVelocity(-1 * velocity) elif direction == "0": self.axis.setTargetVelocity(0) def update_current(self): self.current_text.set(abs(round(self.axis_current.getCurrent(), 3))) root.after(200, self.update_current) def configure(self): #current_limit, max_velocity, acceleration, invert self.window = popupWindow(self.master, self.current_limit, self.max_velocity, self.acceleration, self.invert) self.configure_btn.config(state=DISABLED) self.master.wait_window(self.window.top) self.configure_btn.config(state=NORMAL) #Set the new parameters from the configuration window self.current_limit = self.window.current_limit self.max_velocity = self.window.max_velocity self.acceleration = self.window.acceleration self.invert = self.window.invert.get() #Update Phidget parameters self.axis.setCurrentLimit(self.current_limit) self.axis.setAcceleration(self.acceleration)
if platform.system() == "Darwin": SWITCH_MODE_BTN = 10 STOP_BTN = 12 STEER_AXIS = 0 THROTTLE_AXIS = 5 GEAR_CHANGE_AXIS = 3 elif platform.system() == "Linux": SWITCH_MODE_BTN = 8 STOP_BTN = 1 STEER_AXIS = 0 THROTTLE_AXIS = 5 GEAR_CHANGE_AXIS = 4 try: servo_ch = RCServo() motor_ch = DCMotor() except RuntimeError as e: print("Runtime Exception, ", e) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) def RCServoAttached(self): try: attached = self print("\nAttach Event Detected (Information Below)") print("===========================================") print("Library Version: %s" % attached.getLibraryVersion()) print("Serial Number: %d" % attached.getDeviceSerialNumber()) print("Channel: %d" % attached.getChannel()) print("Channel Class: %s" % attached.getChannelClass())
mtrA.setTargetVelocity(-1 * float(value)) #Set which controllers are connected A = 1 B = 0 C = 0 ACCEL = 2 #Line required to look for Phidget devices on the network Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE) if A: # Motor A: Setup Motor Control and Motor Current mtrA = DCMotor() mtrA.setDeviceSerialNumber(534830) mtrA.openWaitForAttachment(1000) mtrA.setAcceleration(ACCEL) aCur = CurrentInput() aCur.setDeviceSerialNumber(534830) aCur.openWaitForAttachment(1000) if B: # Motor B: Setup Motor Control and Motor Current mtrB = DCMotor() mtrB.setDeviceSerialNumber(465371) mtrB.openWaitForAttachment(1000) mtrB.setAcceleration(ACCEL)
def main(): try: """ * Allocate a new Phidget Channel object """ try: ch = DCMotor() except PhidgetException as e: sys.stderr.write("Runtime Error -> Creating DCMotor: \n\t") DisplayError(e) raise except RuntimeError as e: sys.stderr.write("Runtime Error -> Creating DCMotor: \n\t" + e) raise """ * Set matching parameters to specify which channel to open """ #You may remove this line and hard-code the addressing parameters to fit your application channelInfo = AskForDeviceParameters(ch) ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber) ch.setHubPort(channelInfo.hubPort) ch.setIsHubPortDevice(channelInfo.isHubPortDevice) ch.setChannel(channelInfo.channel) if(channelInfo.netInfo.isRemote): ch.setIsRemote(channelInfo.netInfo.isRemote) if(channelInfo.netInfo.serverDiscovery): try: Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE) except PhidgetException as e: PrintEnableServerDiscoveryErrorMessage(e) raise EndProgramSignal("Program Terminated: EnableServerDiscovery Failed") else: Net.addServer("Server", channelInfo.netInfo.hostname, channelInfo.netInfo.port, channelInfo.netInfo.password, 0) """ * Add event handlers before calling open so that no events are missed. """ print("\n--------------------------------------") print("\nSetting OnAttachHandler...") ch.setOnAttachHandler(onAttachHandler) print("Setting OnDetachHandler...") ch.setOnDetachHandler(onDetachHandler) print("Setting OnErrorHandler...") ch.setOnErrorHandler(onErrorHandler) #This call may be harmlessly removed PrintEventDescriptions() print("\nSetting OnVelocityUpdateHandler...") ch.setOnVelocityUpdateHandler(onVelocityUpdateHandler) """ * Open the channel with a timeout """ print("\nOpening and Waiting for Attachment...") try: ch.openWaitForAttachment(5000) except PhidgetException as e: PrintOpenErrorMessage(e, ch) raise EndProgramSignal("Program Terminated: Open Failed") print("--------------------\n" "\n | DC motor speed can be controlled by setting its Target Velocity (ie its duty cycle).\n" " | The target velocity can be a number from -1.0 to +1.0, where sign indicates direction of rotation.\n" " | For this example, acceleration has been fixed to 1.0Hz, but can be changed in custom code.\n" "\nInput a desired velocity between -1.0 and 1.0 and press ENTER\n" "Input Q and press ENTER to quit\n") end = False while (end != True): buf = sys.stdin.readline(100) if not buf: continue if (buf[0] == 'Q' or buf[0] == 'q'): end = True continue try: velocity = float(buf) except ValueError as e: print("Input must be a number, or Q to quit.") continue if (velocity > 1.0): velocity = 1.0 print("MAXIMUM velocity is +/-1.0") if (velocity < -1.0): velocity = -1.0 print("MAXIMUM velocity is +/-1.0") print("Setting DCMotor TargetVelocity to " + str(velocity)) ch.setTargetVelocity(velocity) ''' * Perform clean up and exit ''' #clear the VelocityUpdate event handler ch.setOnVelocityUpdateHandler(None) print("Cleaning up...") ch.close() print("\nExiting...") return 0 except PhidgetException as e: sys.stderr.write("\nExiting with error(s)...") DisplayError(e) traceback.print_exc() print("Cleaning up...") #clear the VelocityUpdate event handler ch.setOnVelocityUpdateHandler(None) ch.close() return 1 except EndProgramSignal as e: print(e) print("Cleaning up...") #clear the VelocityUpdate event handler ch.setOnVelocityUpdateHandler(None) ch.close() return 1 finally: print("Press ENTER to end program.") readin = sys.stdin.readline()
def __init__(self, DC_MOTOR_HUB, DC_MOTOR_PORT): motor_ch = DCMotor() motor_ch.setDeviceSerialNumber(DC_MOTOR_HUB) motor_ch.setHubPort(DC_MOTOR_PORT) super(DCMotorDevice, self).__init__(motor_ch)
zoom_select = DigitalOutput() zoom_select.setDeviceSerialNumber(540047) zoom_select.setIsHubPortDevice(False) zoom_select.setHubPort(HUB_ZOOM_SELECT) zoom_select.setChannel(CH_ZOOM_SELECT) zoom_select.openWaitForAttachment(5000) manual_select = DigitalOutput() manual_select.setDeviceSerialNumber(540047) manual_select.setIsHubPortDevice(False) manual_select.setHubPort(HUB_MAN_SELECT) manual_select.setChannel(CH_MAN_SELECT) manual_select.openWaitForAttachment(5000) #Setup Lights left_light = DCMotor() left_light.setDeviceSerialNumber(540047) left_light.setIsHubPortDevice(False) left_light.setHubPort(0) left_light.setChannel(0) left_light.openWaitForAttachment(5000) left_light.setAcceleration(20) right_light = DCMotor() right_light.setDeviceSerialNumber(540047) right_light.setIsHubPortDevice(False) right_light.setHubPort(5) right_light.setChannel(0) right_light.openWaitForAttachment(5000) right_light.setAcceleration(20)
import sys import time from Phidget22.Devices.DCMotor import * from Phidget22.Devices.Encoder import * from Phidget22.PhidgetException import * from Phidget22.Phidget import * from Phidget22.Net import * rightWheels = 0 leftWheels = 1 try: motorControl = DCMotor() enc = Encoder() except RuntimeError as e: print("Runtime Exception %s" % e.details) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) def ObjectAttached(self): try: attached = self print("\nAttach Event Detected (Information Below)") print("===========================================") print("Library Version: %s" % attached.getLibraryVersion()) print("Serial Number: %d" % attached.getDeviceSerialNumber()) print("Channel: %d" % attached.getChannel()) print("Channel Class: %s" % attached.getChannelClass())