def read(self): if self.type == 'accelerometer': data = {} data['orientation'] = grove6axis.getOrientation() data['acceleration'] = grove6axis.getAccel() data['magnitude'] = grove6axis.getMag() return data else: return None
import grovepi import grove6axis import time while True: #read from 6-Axis Accelerometer&Compass sensor on input I1 grove6axis.init6Axis() # start it up i1 = grove6axis.getOrientation() # returns orientation as yaw,pitch,roll i2 = grove6axis.getAccel() # get acceleration values i3 = grove6axis.getMag() # get magnetometer values print('Orientation', i1) print('Accel', i2) print('Mag', i3) #read from Tilt sensor on input D4 #d4= grovepi.digitalRead(4) #read from Ultrasonic sensor on input D3 d3 = grovepi.ultrasonicRead(3) #output the data print("Ultrasonic_Distance:", d3) # Read roughly 10 times a second # - n.b. analog read takes time to do also # set the time for 1s to display data time.sleep(2)
rowFormatString = ','.join(sensorVariables) waitTime = 0.1 print(header) while(True): timestamp = time.time() distance = grovepi.ultrasonicRead(inputPortDistance) loudness = grovepi.analogRead(inputPortLoudness) loudness2 = grovepi.analogRead(inputPortLoudness2) touch = grovepi.digitalRead(inputPortTouch) orientation = grove6axis.getOrientation() orientationX = orientation[0] orientationY = orientation[1] orientationZ = orientation[2] acceleration = grove6axis.getAccel() accelerationX = acceleration[0] accelerationY = acceleration[1] accelerationZ = acceleration[2] print(rowFormatString % (timestamp, distance, loudness, loudness2, touch, orientationX, orientationY, orientationZ, accelerationX, accelerationY, accelerationZ)) # print(rowFormatString % (timestamp, distance, loudness, orientationX, orientationY, orientationZ, accelerationX, accelerationY, accelerationZ)) # if distance < nearDistanceThreshold: # print('%d: near!', distance) # else: # print('%d: far...', distance) time.sleep(waitTime)
pressed = False while grovepi.digitalRead(2) == 0 | pressed == False: if grovepi.digitalRead(2) == 1: pressed = True logging = True while logging: time.sleep(2) try: swapLight(3, 4) grove6axis.init6Axis() # start it up print("x, y, z, time") firstVals = list(grove6axis.getAccel()) startTime = time.time() print("%f, %f, %f, %f" % (firstVals[0], firstVals[1], firstVals[2], 0.0)) index = 1 while True: # End logging if grovepi.digitalRead(2) == 1: # Turn red light on and green off swapLight(4, 3) logging = False break # Get the acceleration Values nextVals = list(grove6axis.getAccel()) # Print values - with piping it'll go to csv
ultra_last = 0 constant = 0.1 # get a timestamp so we know when it happened timestamp = time.clock() historyBuffer=collections.deque(maxlen=21) print ('time,yaw,motion,ultra,ultra_low,ultra_high,ultra_median') while True: grove6axis.init6Axis() # returns orientation as yaw,pitch,roll # ori_list = list(ori) yaw = ori_list[3] * 180.0 / math.pi # pitch = ori_list[2] * 180.0 / math.pi # roll = ori_list[3] * 180.0 / math.pi grove6axis.getAccel() motion = grovepi.digitalRead(4) # read from Ultrasonic sensor on input D3 ultra = grovepi.ultrasonicRead(3) # Filter1 De-trending / high pass filter: # it removes long term bias and drift & show short term variations ultra_high = constant * (ultra_high + ultra - ultra_last) ultra_last = ultra # Filter2 Smoothing / low pass filter: # it removes random noise & shows longer term variations ultra_low = ultra_low * (1.0 - constant) + ultra * constant # Filter3 Median / Non linear filter historyBuffer.append(ultra) orderedHistory = sorted(historyBuffer) ultra_median = orderedHistory[int(len(orderedHistory) / 2)]
timestamp = time.clock() historyBuffer = collections.deque(maxlen=21) print('time,yaw,yaw_accel,motion,ultra,ultra_low,ultra_high,ultra_median') while True: grove6axis.init6Axis() ori = grove6axis.getOrientation() ori_list = list(ori) yaw = ori_list[3] * 180.0 / math.pi # returns orientation as yaw,pitch,roll # ori_list = list(ori) # pitch = ori_list[2] * 180.0 / math.pi # roll = ori_list[3] * 180.0 / math.pi accel = grove6axis.getAccel() accel_list = list(accel) rol pitch_accel = accel_list[2] yaw_accel = accel_list[2] motion = grovepi.digitalRead(4) # read from Ultrasonic sensor on input D3 ultra = grovepi.ultrasonicRead(3) # Filter1 De-trending / high pass filter: # it removes long term bias and drift & show short term variations ultra_high = constant * (ultra_high + ultra - ultra_last) ultra_last = ultra # Filter2 Smoothing / low pass filter: # it removes random noise & shows longer term variations ultra_low = ultra_low * (1.0 - constant) + ultra * constant
def getXAccel(): # Read the X value from the Six Axis Accel/Magnetometer return grove6axis.getAccel()[1]