Guest User

code balise ffvl

a guest
Sep 19th, 2013
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import RPi.GPIO as GPIO
  5. import time
  6. import urllib,urllib2,base64
  7. import math
  8.  
  9. P_ANEMO    = 18
  10. P_CLK  = 23
  11. P_MOSI = 19
  12. P_MISO = 21
  13. P_CS   = 24
  14.  
  15.  
  16. GPIO.setmode(GPIO.BOARD)
  17.  
  18. GPIO.setup(P_ANEMO, GPIO.IN)
  19. GPIO.setup(P_CLK, GPIO.OUT)
  20. GPIO.setup(P_MOSI, GPIO.OUT)
  21. GPIO.setup(P_MISO, GPIO.IN)
  22. GPIO.setup(P_CS, GPIO.OUT)
  23.  
  24.  
  25.  
  26. def anemo_pulse(channel):  
  27.   global anemo_count
  28.   anemo_count += 1
  29.  
  30.  
  31. def read_sensor(ch_no = 0):
  32.   if (ch_no == 0):
  33.     cmd = 0x6   # CH0
  34.   else:
  35.     cmd = 0x7   # CH1
  36.  
  37.   # init pins low
  38.   GPIO.output(P_CLK, False)
  39.   GPIO.output(P_CS, False)
  40.  
  41.   # write first 3 bits to MOSI
  42.   cmd <<= 5
  43.   for bno in range(3):
  44.     if (cmd & 0x80):
  45.       GPIO.output(P_MOSI, True)
  46.     else:
  47.       GPIO.output(P_MOSI, False)
  48.     cmd <<= 1
  49.     GPIO.output(P_CLK, True)
  50.     GPIO.output(P_CLK, False)
  51.  
  52.   # read 2(empty and null bit) + 10 ADC bits from MISO
  53.   adc_result = 0
  54.   for bit_no in range(12):
  55.     GPIO.output(P_CLK, True)
  56.     GPIO.output(P_CLK, False)
  57.     adc_result <<= 1
  58.     if (GPIO.input(P_MISO)):
  59.       adc_result |= 0x1
  60.  
  61.   GPIO.output(P_CS, True)
  62.  
  63.   adc_result /= 2    # first bit is 'null' so drop it
  64.   return adc_result
  65.  
  66.  
  67. anemo_count = 0.
  68. GPIO.add_event_detect(P_ANEMO, GPIO.FALLING, callback=anemo_pulse)
  69.  
  70.  
  71. try:
  72.   while True:
  73.    
  74.     vitesseVentMoy = 0.
  75.     vitesseVentMin = 999999.
  76.     vitesseVentMax = 0.
  77.    
  78.     xVector = 0.
  79.     yVector = 0.
  80.    
  81.     for i in range(0,30):
  82.      
  83.       anemo_count=0.
  84.       time.sleep(10)
  85.    
  86.       vitesse  = anemo_count
  87.       vitesse *= 0.1006 * 3.6 # 1.006m/s sur 10 secondes
  88.      
  89.       dir_sensor = read_sensor(0)/1023.
  90.      
  91.       direction = dir_sensor*360.
  92.       direction_rad = dir_sensor*2*math.pi
  93.      
  94.       xVector += vitesse * math.cos(direction_rad)
  95.       yVector += vitesse * math.sin(direction_rad)
  96.      
  97.       if vitesse < vitesseVentMin:
  98.         vitesseVentMin = vitesse
  99.  
  100.       if vitesse > vitesseVentMax:
  101.         vitesseVentMax = vitesse
  102.  
  103.       vitesseVentMoy += vitesse
  104.      
  105.       #print "#", i, " : ", vitesse, "km/h  ", direction, "°"
  106.  
  107.     vitesseVentMoy /= 30.;
  108.    
  109.     dirVentMoy = math.atan2( yVector , xVector) / math.pi * 180.
  110.      
  111.     #print "vitesse inst:", vitesse, "km/h"
  112.     #print "vitesse moy :", vitesseVentMoy, "km/h"
  113.     #print "vitesse max :", vitesseVentMax, "km/h"
  114.     #print "vitesse min :", vitesseVentMin, "km/h"
  115.     #print "dir inst    :", direction, "°"
  116.     #print "dir moy     :", dirVentMoy, "°"
  117.        
  118.     params = urllib.urlencode({
  119.       'idBalise': 6000,
  120.       'vitesseVentMoy': vitesseVentMoy,
  121.       'vitesseVentMin': vitesseVentMin,
  122.       'vitesseVentMax': vitesseVentMax,
  123.       'directVentMoy': dirVentMoy,
  124.       'directVentInst': direction
  125.     })
  126.    
  127.     req = urllib2.Request('http://URL-UPLOAD-FFVL', params)
  128.     try:
  129.       response = urllib2.urlopen(req)
  130.       data=response.read()
  131.       print data
  132.     except urllib2.URLError, e:
  133.       print "error", e
  134.     del req
  135.    
  136.    
  137. except KeyboardInterrupt:  
  138.   GPIO.cleanup()
  139.  
  140. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment