def signalInterruptUSR1(signum, stack): # ------------------------- # signalInterruptUSR1 # SIGUSR1 was send to this process (from setDefaults) # # read Defaults again # ------------------------- global adjustX, adjustY, adjustZ, toleranceX, toleranceY, approximationX, approximationY, distRight, distFront, distAxis print(signum, ' received: read defaults again') (adjustX, adjustY, adjustZ, toleranceX, toleranceY, approximationX, approximationY, distRight, distFront, distAxis) = CaravanPiFiles.readAdjustment()
def main(): # ------------------------- # main # ------------------------- # lesen der Werte aus der HTML form cgitb.enable(display=0, logdir="/var/log/apache2") form = cgi.FieldStorage() cgi_adjustX = form.getvalue('adjustX') cgi_adjustY = form.getvalue('adjustY') cgi_adjustZ = form.getvalue('adjustZ') #print(cgi_adjustX, cgi_adjustY, cgi_adjustZ) (adjustX, adjustY, adjustZ, toleranceX, toleranceY, approximationX, approximationY, distRight, distFront, distAxis) = CaravanPiFiles.readAdjustment() if (cgi_adjustX != None and cgi_adjustY != None and cgi_adjustZ != None): adjustX = float(cgi_adjustX) adjustY = float(cgi_adjustY) adjustZ = float(cgi_adjustZ) CaravanPiFiles.writeAdjustment(0, 0, adjustX, adjustY, adjustZ, toleranceX, toleranceY, approximationX, approximationY, distRight, distFront, distAxis) # Ergebnis Website schreiben print("Content-Type: text/html; charset=utf-8\n\n") print("<html>") print("<head>") print("<title>CaravanPi Konfiguration</title>") print("<meta http-equiv='refresh' content='2; URL=position.php'>") print("<link rel='stylesheet' type='text/css' href='css/main.css'>") print("<link rel='stylesheet' type='text/css' href='css/custom.css'>") print("</head>") print("<body>") print('<header class="header">CaravanPi Konfiguration - manuelle Einstellungen Lage-Sensor</header>') if (cgi_adjustX != None and cgi_adjustY != None and cgi_adjustZ != None): print("Die eingegebenen Werte wurden erfolgreich gespeichert") else: print("ES KONNTEN KEINE WERTE AUS DEM FORMULAR GELESEN WERDEN!") print("<br/><br/>Sie werden zur Eingabeseite weitergeleitet") print("</body>") print("</html>")
def main(): # ------------------------- # main # ------------------------- global adjustX, adjustY, adjustZ, toleranceX, toleranceY, approximationX, approximationY, distRight, distFront, distAxis global LED_HR, LED_HL, LED_ZR, LED_ZL, LED_VR, LED_VL, LED_Vo global globY, globAdjustY, globAdjustSwitchY global lengthOverAll, width, lengthBody global pinSwitchNowHorizontal, pinSwitchLive, pinLEDLive global liveMode # ------------------------- # tactile switches # ------------------------- GPIO.setmode(GPIO.BCM) GPIO.setup(pinSwitchNowHorizontal, GPIO.IN) GPIO.add_event_detect(pinSwitchNowHorizontal, GPIO.RISING, callback=switchInterruptNowHorizontal, bouncetime=400) GPIO.setup(pinSwitchLive, GPIO.IN) GPIO.add_event_detect(pinSwitchLive, GPIO.RISING, callback=switchInterruptLive, bouncetime=400) GPIO.setup(pinLEDLive, GPIO.OUT) GPIO.output(pinLEDLive, False) # ------------------------- # process call parameters # ------------------------- opts = [] args = [] writeFile = 0 displayScreen = 0 liveMode = 0 try: opts, args = getopt.getopt(sys.argv[1:], shortOptions, longOptions) except getopt.GetoptError: print(datetime.datetime.now().strftime("%Y%m%d%H%M%S "), "ERROR: options not correct") usage() sys.exit() for o, a in opts: if o == "--help" or o == "-h": print("HELP") usage() sys.exit() elif o == "--file" or o == "-f": print("output also to file ", filePosition) writeFile = 1 elif o == "--screen" or o == "-s": print("output also to this screen") displayScreen = 1 elif o == "--live" or o == "-l": print("start in live mode") liveMode = 1 GPIO.output(pinLEDLive, True) for a in args: print("further argument: ", a) # ------------------------- # avoid outliers - init values # ------------------------- lastX = None lastY = None lastZ = None secondLastX = None # read defaults # The 3-axis sensor may not be installed exactly horizontally. The values to compensate for this installation difference are read from a file. # --> adjustX, adjustY, adjustZ # In addition, the LEDs should already indicate "horizontal" as soon as the deviation from the horizontal is within a tolerance. # --> approximationX, approximationY (adjustX, adjustY, adjustZ, toleranceX, toleranceY, approximationX, approximationY, distRight, distFront, distAxis) = CaravanPiFiles.readAdjustment() # dimensions of the caravan (lengthOverAll, width, lengthBody) = CaravanPiFiles.readDimensions() # ------------------------- # listen to SIGUSR1 for renew the defaults # ------------------------- signal.signal(signal.SIGUSR1, signalInterruptUSR1) signal.signal(signal.SIGUSR2, signalInterruptUSR2) # ------------------------- # initialize LEDs # ------------------------- LED_HR = setupHR() LED_HL = setupHL() LED_ZR = setupZR() LED_ZL = setupZL() LED_VR = setupVR() LED_VL = setupVL() LED_Vo = setupVo() # ------------------------- # leds off if non-live-mode # ------------------------- if liveMode == 0: ledOff() # read sensor and adjust LEDs while True: print(datetime.datetime.now().strftime("%Y%m%d%H%M%S "), "live Modus:", liveMode) try: i = 0 arrayX = [] arrayY = [] arrayZ = [] # read sensor 200 times and put values in a list while i < 200: (x, y, z) = accelerometer.acceleration arrayX.append(x) arrayY.append(y) arrayZ.append(z) i += 1 # no sleep here, because the accuracy of Python/Raspberry Sleep is not sufficient anyway # instead a high number of passes over the loop variable # normalize values over the calculation of the median (extreme values/outliers are not considered) # Adjustment of the values by subtraction of the initial value x = statistics.median(arrayX) # determine the index of the first occurrence of x in the list try: i = arrayX.index(x) # set the counterparts for y and z belonging to the value x y = arrayY[i] z = arrayZ[i] except (ValueError, IndexError): # the median did not result in a value that appears in the list (i.e. an actually calculated value). # Then set the other median values as well y = statistics.median(arrayY) z = statistics.median(arrayZ) # save values for tactile switch adjustment globY = y globAdjustY = adjustY tolX = checkTolerance(x, adjustX, toleranceX) tolY = checkTolerance(y, adjustY, toleranceY) # calculate the horizontal distance # Attention: If the 3-axis sensor is mounted behind the axis (in driving direction), # the distance to the axis is positive. Otherwise this constant is negative. distLeft = width - distRight distBack = lengthBody - distFront # ADXL345 data sheet: # resolution typical 3.9 mg = 0.0039g # arcsin(0.0039) = 0.223454 degree # # diff = dist * sin(angle) # # x is the gravity in longitudinal direction # arcsin (gravity) = angle # sin(angle) = sin(arcsin(gravity)) = gravity # difference negative = position is "too high" diffHL = round((distLeft * tolY / 10) + (distBack * tolX / 10) * -1) diffHR = round((distRight * tolY / 10) + (distBack * tolX / 10) * -1) diffZL = round((distLeft * tolY / 10) + (distAxis * tolX / 10)) diffZR = round((distRight * tolY / 10) + (distAxis * tolX / 10)) diffVL = round((distLeft * tolY / 10) + (distFront * tolX / 10)) diffVR = round((distRight * tolY / 10) + (distFront * tolX / 10)) # where is the sensor left or right of the middle? if distRight <= (width / 2): # sensor is on the right side in driving direction of the caravan diffVo = round((((width / 2) - distRight) * tolY / 10) + ((distFront + (lengthOverAll - lengthBody)) * tolX / 10)) else: diffVo = round((((width / 2) - distLeft) * tolY / 10) + ((distFront + (lengthOverAll - lengthBody)) * tolX / 10)) # one of the Z-values should be zero - normalise the others if diffZL >= diffZR: diffNormal = diffZL else: diffNormal = diffZR diffHL = diffHL - diffNormal diffHR = diffHR - diffNormal diffZL = diffZL - diffNormal diffZR = diffZR - diffNormal diffVL = diffVL - diffNormal diffVR = diffVR - diffNormal diffVo = diffVo - diffNormal # write values to file and or screen write2file(writeFile, displayScreen, x, y, z, tolX, tolY, z - adjustZ, lastX, secondLastX, diffHL, diffHR, diffVL, diffVR, diffZL, diffZR, diffVo) if liveMode == 1: ledX = x ledY = y ledZ = z # avoid outliers # if lastX and secondLastX have already been assigned values once # and the current value is not equal to lastX and is also not equal to secondLastX, # but the two values lastX and secondLastX are equal, it may be an outlier. # Then the current values are not used for LED control, but the previous values. #Only the comparison with the X values is necessary, because y and z are dependent on x. if (lastX != None and secondLastX != None): if (x != lastX and x != secondLastX and lastX == secondLastX): ledX = lastX ledY = lastY ledZ = lastZ LED(ledX, ledY, ledZ, adjustX, adjustY, globAdjustSwitchY, adjustZ, toleranceX, toleranceY, approximationX, approximationY) # avoid outliers - swap vars secondLastX = lastX lastX = x lastY = y lastZ = z sleep(.1) else: j = 0 while j < 120 and liveMode == 0: # 120/.5 = 60 Sekunden j = j + 1 sleep(.5) # falls inzwsichen LEDtest gestartet wurde, alle LEDs aus ledOff() except KeyboardInterrupt: ledOff() GPIO.cleanup() break except: print("unprocessed Error:", sys.exc_info()[0]) ledOff() GPIO.cleanup() raise GPIO.cleanup()