def __init__(self): ############################ # lets introduce and init the main variables #self.com = None self.isInitialized = False #Create an accelerometer object try: self = Spatial() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: self.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: self.waitForAttach(4000) print "is attached = ", self.isAttached() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: self.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: self.setDataRate(1000)
def StartPhidget(self): # Initialise the Phidgets sensor self.phidget_attached = False self.time_start = None try: if self.accelerometer: self.accelerometer.closePhidget() self.accelerometer = Spatial() self.accelerometer.setOnAttachHandler(self.AccelerometerAttached) self.accelerometer.setOnDetachHandler(self.AccelerometerDetached) self.accelerometer.setOnErrorhandler(self.AccelerometerError) self.accelerometer.setOnSpatialDataHandler(self.SpatialData) #self.accelerometer.enableLogging(Phidget.PhidgetLogLevel.PHIDGET_LOG_WARNING, None) #self.accelerometer.enableLogging(Phidget.PhidgetLogLevel.PHIDGET_LOG_VERBOSE, None) self.accelerometer.openPhidget() self.accelerometer.waitForAttach(10000) # set data rate in milliseconds (we will decimate from 4ms to 20ms later) # we now do this in the Attach handler #self.accelerometer.setDataRate(PHIDGETS_NOMINAL_DATA_INTERVAL_MS) except RuntimeError as e: logging.error("Runtime Exception: %s", e.details) return except PhidgetException as e: logging.error("Phidget Exception: %s. Is the Phidget not connected?", e.details) return self.phidget_attached = True
def __init__(self): """ """ # update from the IMU. self.spatial = Spatial() imu_handlers = Controller.IMU_Handlers() self.prev = 0 # attach the event handlers. try: self.spatial.setOnAttachHandler(imu_handlers.on_attach) self.spatial.setOnDetachHandler(imu_handlers.on_detach) self.spatial.setOnErrorhandler(imu_handlers.on_error) self.spatial.setOnSpatialDataHandler(imu_handlers.on_data) self.spatial.openPhidget() self.spatial.waitForAttach(10000) self.spatial.setDataRate(4) except: print("Error connecting to IMU, I cannot handle this. " + \ "I will just go die now!") exit(1) samplePeriod = 1 / 256 self.ahrs = MadgwickAHRS(samplePeriod) #t = Thread(target=self.update_head) #t.daemon = True #t.start() self.x = 0 self.y = 0 self.z = 0 self.update_head()
def __init__(self): self.spatial = Spatial() self.spatial.setOnSpatialDataHandler(self.getDataHandler) self.spatial.setOnAttachHandler(self.attachDeviceHandler) self.spatial.openPhidget() self.spatial.waitForAttach(10000) self.spatial.setDataRate(16)
def create_spatial(self): try: spatial = Spatial() self.status = "connected" return spatial except RuntimeError as e: self.status = "not connected" print("Runtime Exception: %s" % e.details)
def __init__(self): """ """ # update from the IMU. self.spatial = Spatial() imu_handlers = Controller.IMU_Handlers() self.prev = 0 # attach the event handlers. try: self.spatial.setOnAttachHandler(imu_handlers.on_attach) self.spatial.setOnDetachHandler(imu_handlers.on_detach) self.spatial.setOnErrorhandler(imu_handlers.on_error) self.spatial.setOnSpatialDataHandler(imu_handlers.on_data) self.spatial.openPhidget() self.spatial.waitForAttach(10000) self.spatial.setDataRate(4) except: print("Error connecting to IMU, I cannot handle this. " + \ "I will just go die now!") exit(1) #t = Thread(target=self.update_head) #t.daemon = True #t.start() self.x = 0 self.y = 0 self.z = 0 data = copy.copy(Controller.imu_measurements) while (len(data[2]) != 3): data = copy.copy(Controller.imu_measurements) print("here") mag_v = np.array(data[2]) norm = np.linalg.norm(mag_v) mag_v = mag_v / norm x = mag_v[0] y = mag_v[1] z = mag_v[2] # intial reference frame self.state = [x, y, z] self.update_head()
__author__ = 'Adam Stelmack' __version__ = '2.1.8' __date__ = 'May 17 2010' #Basic imports from ctypes import * import sys #Phidget specific imports from Phidgets.Phidget import Phidget from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import SpatialDataEventArgs, AttachEventArgs, DetachEventArgs, ErrorEventArgs from Phidgets.Devices.Spatial import Spatial, SpatialEventData, TimeSpan #Create an accelerometer object try: spatial = Spatial() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) #Information Display Function def DisplayDeviceInfo(): print( "|------------|----------------------------------|--------------|------------|" ) print( "|- Attached -|- Type -|- Serial No. -|- Version -|" ) print(
def startAccel(trialId, graph): global accelFilename global outPutTxt_a global orientation global fA global totalTimeElapsed global totalSamplesTaken_a global totalAbsZAccel global totalAVec global totalAvgAVec totalAvgAVec = 0 totalAVec = 0 totalAbsZAccel = 0 totalSamplesTaken_a = 0 totalTimeElapsed = 0 # Setutp graphing ability outPutTxt_a = graph orientation = True # True corresponds to "right-side up" if graph: accelFilename = str(trialId) + "_accelOutput.txt" fA = open(accelFilename, 'w') #WRITES OVER PREVIOUS DATA header = "#Trial: " + str( trialId) + " (x[g], y[g], z[g], 3D A_Vec, samples) \n" fA.write(header) #Create an accelerometer object try: global spatial spatial = Spatial() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: spatial.setOnAttachHandler(SpatialAttached) spatial.setOnDetachHandler(SpatialDetached) spatial.setOnErrorhandler(SpatialError) spatial.setOnSpatialDataHandler(SpatialData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: spatial.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: spatial.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: spatial.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: spatial.setDataRate(24) ############### Sample rate is 24ms averaged
def AttachSpatial(databasepath, serialNumber): def onAttachHandler(event): logString = "Spatial Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Spatial Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Spatial Error " + str( event.device.getSerialNum()) + ", Error: " + event.description #print(logString) DisplayErrorDeviceInfo(event.device) def onServerConnectHandler(event): logString = "Spatial Server Connect " + str( event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Spatial Server Disconnect " + str( event.device.getSerialNum()) #print(logString) def spatialDataHandler(event): logString = "Spatial Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) index = 0 for spatialData in enumerate(event.spatialData): accelX = 0 accelY = 0 accelZ = 0 if len(spatialData[1].Acceleration) > 0: accelX = spatialData[1].Acceleration[0] accelY = spatialData[1].Acceleration[1] if len(spatialData[1].Acceleration) > 2: accelZ = spatialData[1].Acceleration[2] angularX = 0 angularY = 0 angularZ = 0 if len(spatialData[1].AngularRate) > 0: angularX = spatialData[1].AngularRate[0] angularY = spatialData[1].AngularRate[1] if len(spatialData[1].AngularRate) > 2: angularZ = spatialData[1].AngularRate[2] magneticX = 0 magneticY = 0 magneticZ = 0 if len(spatialData[1].MagneticField) > 0: magneticX = spatialData[1].MagneticField[0] magneticY = spatialData[1].MagneticField[1] if len(spatialData[1].AngularRate) > 2: magneticZ = spatialData[1].MagneticField[2] conn.execute( "INSERT INTO SPATIAL_DATACHANGE VALUES(NULL, DateTime('now'), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (event.device.getSerialNum(), index, accelX, accelY, accelZ, angularX, angularY, angularZ, magneticX, magneticY, magneticZ)) index += 1 conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = Spatial() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnSpatialDataHandler(spatialDataHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
#!/usr/bin/env python import serial import time import math #Basic imports from ctypes import * import sys #Phidget specific imports from Phidgets.Phidget import Phidget from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import SpatialDataEventArgs, AttachEventArgs, DetachEventArgs, ErrorEventArgs from Phidgets.Devices.Spatial import Spatial, SpatialEventData, TimeSpan sp = Spatial() print("Opening phidget object....") try: sp.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: sp.waitForAttach(6000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: