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)
示例#2
0
"""
Name: Ori Shadmon 
description: code for Spatial sensors (Acceleration, Gyroscope, Magnometer)
URL: https://www.phidgets.com/?tier=3&catid=10&pcid=8&prodid=975
"""

import time
from Phidget22.Devices.CurrentInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

ch = CurrentInput()
ch.setDeviceSerialNumber(561266)
ch.setHubPort(3)
ch.setIsHubPortDevice(False)
ch.setChannel(0)
ch.openWaitForAttachment(5000)
try:
    ch.getCurrent()
except Exception as e:
    pass
time.sleep(2)
print(ch.getCurrent())