Author: Masato R. Nakamura, Eng.Sc.D.
Final version of the python code
import sys
import time
import traceback
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.Devices.DCMotor import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *
try:
from PhidgetHelperFunctions import *
except ImportError:
sys.stderr.write(“\nCould not find PhidgetHelperFunctions. Either add PhdiegtHelperFunctions.py to your project folder ”
“or remove the import from your project.”)
sys.stderr.write(“\nPress ENTER to end program.”)
readin = sys.stdin.readline()
sys.exit()
“””
* Configures the device’s DataInterval and ChangeTrigger.
* Displays info about the attached Phidget channel.
* Fired when a Phidget channel with onAttachHandler registered attaches
*
* @param self The Phidget channel that fired the attach event
“””
#====voltage====
try:
ch1 = VoltageRatioInput()
ch2 = VoltageRatioInput()
ch3 = DCMotor()
ch4 = DCMotor()
ch1.setDeviceSerialNumber(539133)
ch1.setIsHubPortDevice(True)
ch1.setIsRemote(False)
ch1.setHubPort(3)
ch1.setChannel(0)
ch2.setDeviceSerialNumber(539133)
ch2.setIsHubPortDevice(True)
ch2.setIsRemote(False)
ch2.setHubPort(4)
ch2.setChannel(0)
ch3.setDeviceSerialNumber(539133)
ch3.setIsRemote(False)
ch3.setHubPort(0)
ch3.setChannel(0)
ch4.setDeviceSerialNumber(539133)
ch4.setIsRemote(False)
ch4.setHubPort(1)
ch4.setChannel(0)
except RuntimeError as e:
print(“Runtime Exception %s” % e.details)
print(“Press Enter to Exit…\n”)
readin = sys.stdin.read(1)
exit(1)
def onAttachHandler(self):
ph = self
try:
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print(“\nAttach Event:”)
“””
* Get device information and display it.
“””
channelClassName = ph.getChannelClassName()
serialNumber = ph.getDeviceSerialNumber()
channel = ph.getChannel()
if(ph.getDeviceClass() == DeviceClass.PHIDCLASS_VINT):
hubPort = ph.getHubPort()
print(“\n\t-> Channel Class: ” + channelClassName + “\n\t-> Serial Number: ” + str(serialNumber) +
“\n\t-> Hub Port: ” + str(hubPort) + “\n\t-> Channel: ” + str(channel) + “\n”)
else:
print(“\n\t-> Channel Class: ” + channelClassName + “\n\t-> Serial Number: ” + str(serialNumber) +
“\n\t-> Channel: ” + str(channel) + “\n”)
“””
* Set the DataInterval inside of the attach handler to initialize the device with this value.
* DataInterval defines the minimum time between VoltageRatioChange events.
* DataInterval can be set to any value from MinDataInterval to MaxDataInterval.
“””
print(“\n\tSetting DataInterval to 100ms”)
ph.setDataInterval(100)
“””
* Set the VoltageRatioChangeTrigger inside of the attach handler to initialize the device with this value.
* VoltageRatioChangeTrigger will affect the frequency of VoltageRatioChange events, by limiting them to only occur when
* the voltage ratio changes by at least the value set.
“””
print(“\tSetting Voltage Ratio ChangeTrigger to 0.0”)
ph.setVoltageRatioChangeTrigger(0.0)
“””
* Set the SensorType inside of the attach handler to initialize the device with this value.
* You can find the appropriate SensorType for your sensor in its User Guide and the VoltageRatioInput API
* SensorType will apply the appropriate calculations to the voltage ratio reported by the device
* to convert it to the sensor’s units.
* SensorType can only be set for Sensor Port voltage ratio inputs (VINT Ports and Analog Input Ports)
“””
if(ph.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_SENSOR_PORT):
print(“\tSetting VoltageRatio SensorType”)
ph.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_VOLTAGERATIO)
except PhidgetException as e:
print(“\nError in Attach Event:”)
DisplayError(e)
traceback.print_exc()
return
“””
* Displays info about the detached Phidget channel.
* Fired when a Phidget channel with onDetachHandler registered detaches
*
* @param self The Phidget channel that fired the attach event
“””
def onDetachHandler(self):
ph = self
try:
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print(“\nDetach Event:”)
“””
* Get device information and display it.
“””
channelClassName = ph.getChannelClassName()
serialNumber = ph.getDeviceSerialNumber()
channel = ph.getChannel()
if(ph.getDeviceClass() == DeviceClass.PHIDCLASS_VINT):
hubPort = ph.getHubPort()
print(“\n\t-> Channel Class: ” + channelClassName + “\n\t-> Serial Number: ” + str(serialNumber) +
“\n\t-> Hub Port: ” + str(hubPort) + “\n\t-> Channel: ” + str(channel) + “\n”)
else:
print(“\n\t-> Channel Class: ” + channelClassName + “\n\t-> Serial Number: ” + str(serialNumber) +
“\n\t-> Channel: ” + str(channel) + “\n”)
except PhidgetException as e:
print(“\nError in Detach Event:”)
DisplayError(e)
traceback.print_exc()
return
“””
* Writes Phidget error info to stderr.
* Fired when a Phidget channel with onErrorHandler registered encounters an error in the library
*
* @param self The Phidget channel that fired the attach event
* @param errorCode the code associated with the error of enum type ph.ErrorEventCode
* @param errorString string containing the description of the error fired
“””
def onErrorHandler(self, errorCode, errorString):
sys.stderr.write(“[Phidget Error Event] -> ” + errorString + ” (” + str(errorCode) + “)\n”)
“””
* Outputs the VoltageRatioInput’s most recently reported voltage ratio.
* Fired when a VoltageRatioInput channel with onVoltageRatioChangeHandler registered meets DataInterval and ChangeTrigger criteria
*
* @param self The VoltageRatioInput channel that fired the VoltageRatioChange event
* @param voltageRatio The reported voltage ratio from the VoltageRatioInput channel
“””
def onVelocityUpdateHandler(self, velocity):
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print(“[Velocity Event] -> Velocity: ” + str(velocity))
def onVoltageRatioChangeHandler(self, voltageRatio):
ch1.setDataInterval(100)
ch2.setDataInterval(100)
ch3.setDataInterval(100)
ch4.setDataInterval(100)
voltageRatio1 = ch1.getVoltageRatio()
voltageRatio2 = ch2.getVoltageRatio()
if voltageRatio1 > 0.07 and voltageRatio2 > 0.07:
# if voltageRatio < 0.30:
print(“too far away: ” + str(voltageRatio))
ch3.setTargetVelocity(1.0)
ch4.setTargetVelocity(1.0)
elif voltageRatio1 > 0.07 and voltageRatio2 < 0.07:
ch3.setTargetVelocity(0.5)
ch4.setTargetVelocity(0.0)
elif voltageRatio1 < 0.07 and voltageRatio2 > 0.07:
ch3.setTargetVelocity(0.0)
ch4.setTargetVelocity(0.5)
else:
print(“too close: ” + str(voltageRatio))
ch3.setTargetVelocity(0.0)
ch4.setTargetVelocity(0.0)
“””
if voltageRatio2 > 0.07:
print(“too far away: ” + str(voltageRatio))
ch4.setTargetVelocity(1.0)
else:
print(“too close: ” + str(voltageRatio))
ch4.setTargetVelocity(0.0)
“””
def onVelocityUpdateHandler(self, velocity):
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print(“[Velocity Event] -> Velocity: ” + str(velocity))
“””
* Outputs the VoltageRatioInput’s most recently reported sensor value.
* Fired when a VoltageRatioInput channel with onSensorChangeHandler registered meets DataInterval and ChangeTrigger criteria
*
* @param self The VoltageRatioInput channel that fired the SensorChange event
* @param sensorValue The reported sensor value from the VoltageRatioInput channel
“””
def onSensorChangeHandler(self, sensorValue, sensorUnit):
#If you are unsure how to use more than one Phidget channel with this event, we recommend going to
#www.phidgets.com/docs/Using_Multiple_Phidgets for information
print(“[Sensor Event] -> Sensor Value: ” + str(sensorValue) + sensorUnit.symbol)
“””
* Prints descriptions of how events related to this class work
“””
def PrintEventDescriptions():
print(“\n——————–\n”
“\n | Voltage Ratio change events will call their associated function every time new voltage ratio data is received from the device.\n”
” | The rate of these events can be set by adjusting the DataInterval for the channel.\n”)
print(
“\n | Sensor change events contain the most recent sensor value received from the device.\n”
” | Sensor change events will occur instead of Voltage Ratio change events if the SensorType is changed from the default.\n”
” | Press ENTER once you have read this message.”)
readin = sys.stdin.readline(1)
print(“\n——————–“)
“””
* Creates, configures, and opens a VoltageRatioInput channel.
* Displays Voltage Ratio events for 10 seconds
* Closes out VoltageRatioInput channel
*
* @return 0 if the program exits successfully, 1 if it exits with errors.
“””
try:
“””
* Add event handlers before calling open so that no events are missed.
“””
print(“\n————————————–“)
print(“\nSetting OnAttachHandler…”)
ch1.setOnAttachHandler(onAttachHandler)
ch2.setOnAttachHandler(onAttachHandler)
ch3.setOnAttachHandler(onAttachHandler)
ch4.setOnAttachHandler(onAttachHandler)
print(“Setting OnDetachHandler…”)
ch1.setOnDetachHandler(onDetachHandler)
ch2.setOnDetachHandler(onDetachHandler)
ch3.setOnDetachHandler(onDetachHandler)
ch4.setOnDetachHandler(onDetachHandler)
print(“Setting OnErrorHandler…”)
ch1.setOnErrorHandler(onErrorHandler)
ch2.setOnErrorHandler(onErrorHandler)
ch3.setOnErrorHandler(onErrorHandler)
ch4.setOnErrorHandler(onErrorHandler)
print(“\nSetting OnVoltageRatioChangeHandler…”)
ch1.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler)
ch2.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler)
print(“\nSetting OnSensorChangeHandler…”)
ch1.setOnSensorChangeHandler(onSensorChangeHandler)
ch2.setOnSensorChangeHandler(onSensorChangeHandler)
print(“\nSetting OnVelocityUpdateHandler…”)
ch3.setOnVelocityUpdateHandler(onVelocityUpdateHandler)
ch4.setOnVelocityUpdateHandler(onVelocityUpdateHandler)
“””
* Open the channel with a timeout
“””
print(“\nOpening and Waiting for Attachment…”)
try:
ch1.openWaitForAttachment(5000)
ch2.openWaitForAttachment(5000)
ch3.openWaitForAttachment(5000)
ch4.openWaitForAttachment(5000)
except PhidgetException as e:
PrintOpenErrorMessage(e, ch)
raise EndProgramSignal(“Program Terminated: Open Failed”)
print(“Sampling data for 3 seconds…”)
print(“You can do stuff with your Phidgets here and/or in the event handlers.”)
time.sleep(20)
“””
* Perform clean up and exit
“””
print(“\nDone Sampling…”)
print(“Cleaning up…”)
ch1.close()
ch2.close()
ch3.close()
ch4.close()
print(“\nExiting…”)
except PhidgetException as e:
sys.stderr.write(“\nExiting with error(s)…”)
DisplayError(e)
traceback.print_exc()
print(“Cleaning up…”)
ch1.close()
ch2.close()
ch3.close()
ch4.close()
except EndProgramSignal as e:
print(e)
print(“Cleaning up…”)
ch1.close()
ch2.close()
ch3.close()
ch4.close()
except RuntimeError as e:
sys.stderr.write(“Runtime Error: \n\t” + e)
traceback.print_exc()
finally:
print(“Press ENTER to end program.”)
readin = sys.stdin.readline()
Chain sprokets and chain
Hi team, could you select the best parts for our project?
Day13 5/9/2019
How are you doing today?
Day12 5/2/2019
How was the spring break? and also how did you enjoy the2019 Microsoft Tech Transfer Days conference? Any thought? Any feedback from the audience? Please put your comments.
We presented our project in 2019 Microsoft Tech Transfer Days Conference
Day10 4/11/2019
How are you today?
Tech Transfer Days Conference
Project folder
Day09 4/4/2019
How was the midterm exam? Is it complicated or straight forward for you?